import {createProtobufRpcClient, QueryClient, SequenceResponse, SigningStargateClient} from "@cosmjs/stargate"; import {getRegistry} from "./registry"; import {SigningStargateClientOptions} from "@cosmjs/stargate"; import {QueryClientImpl as StorageQueryClientImpl} from "../curium/lib/generated/storage/query"; import {QueryClientImpl as AuthQueryClientImpl} from "../curium/lib/generated/cosmos/auth/v1beta1/query"; import {QueryClientImpl as BankQueryClientImpl} from "../curium/lib/generated/cosmos/bank/v1beta1/query"; import {QueryClientImpl as FaucetQueryClientImpl} from '../curium/lib/generated/faucet/query' import {BluzelleWallet} from "../wallets/BluzelleWallet"; import {QueryClientImpl as TaxQueryClientImpl} from '../curium/lib/generated/tax/query'; import {QueryClientImpl as StakingQueryClientImpl} from "../curium/lib/generated/cosmos/staking/v1beta1/query"; import {QueryClientImpl as DistributionQueryClientImpl} from "../curium/lib/generated/cosmos/distribution/v1beta1/query"; import {QueryClientImpl as NftQueryClientImpl} from "../curium/lib/generated/nft/query"; import {QueryClientImpl as AuthzQueryClientImpl} from "../curium/lib/generated/cosmos/authz/v1beta1/query"; import {MsgClientImpl as VestingClientImpl} from "../curium/lib/generated/cosmos/vesting/v1beta1/tx"; import {QueryClientImpl as GovQueryClientImpl} from "../curium/lib/generated/cosmos/gov/v1beta1/query"; import {QueryClientImpl as UpgradeQueryClientImpl} from "../curium/lib/generated/cosmos/upgrade/v1beta1/query"; import {QueryClientImpl as ParamsQueryClientImpl} from "../curium/lib/generated/cosmos/params/v1beta1/query"; import {ServiceClientImpl} from "../curium/lib/generated/cosmos/tx/v1beta1/service"; import {Tendermint37Client} from "@cosmjs/tendermint-rpc"; // import {Tendermint34Client} from "@cosmjs/tendermint-rpc"; type QueryClientImpl = { auth: AuthQueryClientImpl; storage: StorageQueryClientImpl; bank: BankQueryClientImpl; faucet: FaucetQueryClientImpl; tax: TaxQueryClientImpl; staking: StakingQueryClientImpl; distribution: DistributionQueryClientImpl; tx: ServiceClientImpl; nft: NftQueryClientImpl; authz: AuthzQueryClientImpl; vesting: VestingClientImpl; gov: GovQueryClientImpl; upgrade: UpgradeQueryClientImpl; params: ParamsQueryClientImpl; } export interface BluzelleClient { url: string; address: string; sgClient: SigningStargateClient; queryClient: QueryClientImpl; tmClient: Tendermint37Client; } export const newBluzelleClient = (config: { wallet: () => Promise; url: string }): Promise => config.wallet() .then(wallet => SigningBluzelleClient.connectWithSigner(config.url, wallet, {prefix: 'bluzelle', registry: getRegistry()}) .then(sgClient => Promise.all([ getRpcClient(config.url), sgClient, wallet.getAccounts().then(acc => acc[0].address), Tendermint37Client.connect(config.url) ]))) .then(([queryClient, sgClient, address, tmClient]) => ({ url: config.url, queryClient, sgClient, address, tmClient })); const getRpcClient = (url: string): Promise => Tendermint37Client.connect(url) .then(tendermintClient => new QueryClient(tendermintClient)) .then(createProtobufRpcClient) .then(rpcClient => Promise.resolve({ auth: new AuthQueryClientImpl(rpcClient), storage: new StorageQueryClientImpl(rpcClient), bank: new BankQueryClientImpl(rpcClient), faucet: new FaucetQueryClientImpl(rpcClient), tax: new TaxQueryClientImpl(rpcClient), staking: new StakingQueryClientImpl(rpcClient), distribution: new DistributionQueryClientImpl(rpcClient), tx: new ServiceClientImpl(rpcClient), nft: new NftQueryClientImpl(rpcClient), authz: new AuthzQueryClientImpl(rpcClient), vesting: new VestingClientImpl(rpcClient), gov: new GovQueryClientImpl(rpcClient), upgrade: new UpgradeQueryClientImpl(rpcClient), params: new ParamsQueryClientImpl(rpcClient), })); export class SigningBluzelleClient extends SigningStargateClient { private wallet: BluzelleWallet protected constructor(tmClient: Tendermint37Client | undefined, signer: BluzelleWallet, options: SigningStargateClientOptions) { super(tmClient, signer, options); this.wallet = signer } getSequenceFromNetwork(address: string): Promise { return super.getSequence(address) } getSequence(address: string): Promise { return this.wallet.getSequence(this, address) } static async connectWithSigner(endpoint: string, signer: BluzelleWallet, options = {}) { return Tendermint37Client.connect(endpoint) .then(tmClient => new SigningBluzelleClient(tmClient, signer, options)) } }