From 1a928879bc239ab8933be2e86e123d9ac57dfcd3 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Ph=E1=BA=A1m=20V=C4=83n=20Nam?= Date: Thu, 27 Oct 2022 14:59:46 +0700 Subject: [PATCH] feat: add stargateClient feature for Wasm --- .cspell.json | 3 ++ src/lib/wallet/index.ts | 18 +++++-- src/lib/wasm/index.ts | 102 ++++++++++++++++++++++++++++++++++++---- 3 files changed, 111 insertions(+), 12 deletions(-) diff --git a/.cspell.json b/.cspell.json index 54387f3..b475729 100644 --- a/.cspell.json +++ b/.cspell.json @@ -12,6 +12,7 @@ "codecov", "commitlint", "cosmjs", + "Cosmoshub", "cosmwasm", "dependabot", "editorconfig", @@ -24,11 +25,13 @@ "prettierignore", "queryclient", "sandboxed", + "Secp", "stargate", "tendermint", "tendermintbatchclient", "transpiled", "typedoc", + "Undelegate", "untracked" ], "flagWords": [], diff --git a/src/lib/wallet/index.ts b/src/lib/wallet/index.ts index e621c7b..2f55260 100644 --- a/src/lib/wallet/index.ts +++ b/src/lib/wallet/index.ts @@ -3,6 +3,7 @@ import { SigningCosmWasmClientOptions, } from '@cosmjs/cosmwasm-stargate'; import { + Coin, DirectSecp256k1HdWallet, makeCosmoshubPath, OfflineSigner, @@ -37,7 +38,8 @@ export class Wallet { hdPaths: [makeCosmoshubPath(0)], prefix: provider.bech32Prefix, }); - let cosmWasmClient, stargateClient; + let cosmWasmClient: SigningCosmWasmClient; + let stargateClient: SigningStargateClient; if (options != undefined) { cosmWasmClient = await SigningCosmWasmClient.connectWithSigner( provider.rpcUrl, @@ -84,7 +86,8 @@ export class Wallet { const accounts = await wallets.getAccounts(); const results = []; for (let i = 0; i < accounts.length; i++) { - let cosmWasmClient, stargateClient; + let cosmWasmClient: SigningCosmWasmClient; + let stargateClient: SigningStargateClient; if (options != undefined) { cosmWasmClient = await SigningCosmWasmClient.connectWithSigner( provider.rpcUrl, @@ -122,7 +125,8 @@ export class Wallet { const results = []; const accounts = await signer.getAccounts(); for (let i = 0; i < accounts.length; i++) { - let cosmWasmClient, stargateClient; + let cosmWasmClient: SigningCosmWasmClient; + let stargateClient: SigningStargateClient; if (options != undefined) { cosmWasmClient = await SigningCosmWasmClient.connectWithSigner( provider.rpcUrl, @@ -188,6 +192,14 @@ export class Wallet { get stargateSigner(): SigningStargateClient { return this._stargateSigner; } + + public coin(denom: string, amount: string): Coin { + const coin: Coin = { + denom: denom, + amount: amount + } + return coin; + } } const checkProvider = (provider: provider) => { diff --git a/src/lib/wasm/index.ts b/src/lib/wasm/index.ts index ac93146..7279976 100644 --- a/src/lib/wasm/index.ts +++ b/src/lib/wasm/index.ts @@ -7,11 +7,13 @@ import { MigrateResult, UploadResult, } from '@cosmjs/cosmwasm-stargate'; +import { Coin, OfflineSigner } from '@cosmjs/proto-signing'; import { - Coin, - OfflineSigner, -} from '@cosmjs/proto-signing'; -import { calculateFee, GasPrice, StdFee } from '@cosmjs/stargate'; + calculateFee, + DeliverTxResponse, + GasPrice, + StdFee, +} from '@cosmjs/stargate'; import { Wallet } from '../wallet'; @@ -20,11 +22,7 @@ const defaultInitGas = 1000000; const defaultExecGas = 500000; const defaultGasPrice = 0.25; -export { - Coin, - OfflineSigner, - InstantiateOptions -} +export { Coin, OfflineSigner, InstantiateOptions }; export interface InstantiateMessage { readonly codeId: number; readonly instantiateMsg: JsonObject; @@ -48,6 +46,21 @@ export interface QueryMessage { readonly queryMsg: JsonObject; } +export interface SendMessage { + readonly recipient: string; + readonly coins: readonly Coin[]; +} + +export interface DelegateMessage { + readonly validator: string; + readonly coin: Coin; +} + +export interface UndelegateMessage { + readonly validator: string; + readonly coin: Coin; +} + export class Wasm { private wallet: Wallet; @@ -160,6 +173,77 @@ export class Wasm { ); } + public async send( + sendMessage: SendMessage, + memo?: string, + sendFee?: StdFee + ): Promise { + sendFee = + sendFee == null ? this.getFee(defaultExecGas, defaultGasPrice) : sendFee; + return await this.wallet.stargateSigner.sendTokens( + this.wallet.address, + sendMessage.recipient, + sendMessage.coins, + sendFee, + memo + ); + } + + public async delegate( + delegateMessage: DelegateMessage, + memo?: string, + delegateFee?: StdFee + ): Promise { + delegateFee = + delegateFee == null + ? this.getFee(defaultExecGas, defaultGasPrice) + : delegateFee; + + return await this.wallet.stargateSigner.delegateTokens( + this.wallet.address, + delegateMessage.validator, + delegateMessage.coin, + delegateFee, + memo + ); + } + + public async undelegate( + undelegateMessage: UndelegateMessage, + memo?: string, + undelegateFee?: StdFee + ): Promise { + undelegateFee = + undelegateFee == null + ? this.getFee(defaultExecGas, defaultGasPrice) + : undelegateFee; + + return await this.wallet.stargateSigner.delegateTokens( + this.wallet.address, + undelegateMessage.validator, + undelegateMessage.coin, + undelegateFee, + memo + ); + } + + public async withdrawRewards( + validatorAddress: string, + memo?: string, + withdrawFee?: StdFee + ) { + withdrawFee = + withdrawFee == null + ? this.getFee(defaultExecGas, defaultGasPrice) + : withdrawFee; + return await this.wallet.stargateSigner.withdrawRewards( + this.wallet.address, + validatorAddress, + withdrawFee, + memo + ); + } + public getFee(gas: number, gasPrice: number): StdFee { return calculateFee( gas,