Skip to content

Commit

Permalink
feat: add stargateClient feature for Wasm
Browse files Browse the repository at this point in the history
  • Loading branch information
phamnam1805 committed Oct 27, 2022
1 parent 9421a05 commit 1a92887
Show file tree
Hide file tree
Showing 3 changed files with 111 additions and 12 deletions.
3 changes: 3 additions & 0 deletions .cspell.json
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@
"codecov",
"commitlint",
"cosmjs",
"Cosmoshub",
"cosmwasm",
"dependabot",
"editorconfig",
Expand All @@ -24,11 +25,13 @@
"prettierignore",
"queryclient",
"sandboxed",
"Secp",
"stargate",
"tendermint",
"tendermintbatchclient",
"transpiled",
"typedoc",
"Undelegate",
"untracked"
],
"flagWords": [],
Expand Down
18 changes: 15 additions & 3 deletions src/lib/wallet/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ import {
SigningCosmWasmClientOptions,
} from '@cosmjs/cosmwasm-stargate';
import {
Coin,
DirectSecp256k1HdWallet,
makeCosmoshubPath,
OfflineSigner,
Expand Down Expand Up @@ -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,
Expand Down Expand Up @@ -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,
Expand Down Expand Up @@ -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,
Expand Down Expand Up @@ -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) => {
Expand Down
102 changes: 93 additions & 9 deletions src/lib/wasm/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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';

Expand All @@ -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;
Expand All @@ -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;

Expand Down Expand Up @@ -160,6 +173,77 @@ export class Wasm {
);
}

public async send(
sendMessage: SendMessage,
memo?: string,
sendFee?: StdFee
): Promise<DeliverTxResponse> {
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<DeliverTxResponse> {
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<DeliverTxResponse> {
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,
Expand Down

0 comments on commit 1a92887

Please sign in to comment.