From 6d3865a27e048e56553a37b14e03e7b4cce0edee Mon Sep 17 00:00:00 2001 From: Ethan Frey Date: Sun, 16 Feb 2020 19:55:07 +0100 Subject: [PATCH] Add sendToken() and test --- packages/sdk/src/cosmwasmclient.spec.ts | 31 ++++++++++++++++++++++ packages/sdk/src/cosmwasmclient.ts | 34 +++++++++++++++++++++++++ packages/sdk/types/cosmwasmclient.d.ts | 1 + 3 files changed, 66 insertions(+) diff --git a/packages/sdk/src/cosmwasmclient.spec.ts b/packages/sdk/src/cosmwasmclient.spec.ts index 319610a8..4742790f 100644 --- a/packages/sdk/src/cosmwasmclient.spec.ts +++ b/packages/sdk/src/cosmwasmclient.spec.ts @@ -446,6 +446,37 @@ describe("CosmWasmClient", () => { }); }); + describe("sendToken", () => { + it("works", async () => { + pendingWithoutCosmos(); + const pen = await Secp256k1Pen.fromMnemonic(faucet.mnemonic); + const client = CosmWasmClient.makeWritable(httpUrl, faucet.address, signBytes => pen.sign(signBytes)); + + // instantiate + const transferAmount: readonly Coin[] = [ + { + amount: "7890", + denom: "ucosm", + }, + ]; + const beneficiaryAddress = makeRandomAddress(); + + // no tokens here + const before = await client.getAccount(beneficiaryAddress); + expect(before).toBeUndefined(); + + // send + const result = await client.sendToken(beneficiaryAddress, transferAmount, "for dinner"); + const [firstLog] = result.logs; + expect(firstLog).toBeTruthy(); + + // got tokens + const after = await client.getAccount(beneficiaryAddress); + assert(after); + expect(after.coins).toEqual(transferAmount); + }); + }); + describe("queryContractRaw", () => { const configKey = toAscii("config"); const otherKey = toAscii("this_does_not_exist"); diff --git a/packages/sdk/src/cosmwasmclient.ts b/packages/sdk/src/cosmwasmclient.ts index e611eb04..b7dd7c0f 100644 --- a/packages/sdk/src/cosmwasmclient.ts +++ b/packages/sdk/src/cosmwasmclient.ts @@ -10,6 +10,7 @@ import { CosmosSdkTx, MsgExecuteContract, MsgInstantiateContract, + MsgSend, MsgStoreCode, StdFee, StdSignature, @@ -329,6 +330,39 @@ export class CosmWasmClient { }; } + public async sendToken( + recipientAddress: string, + transferAmount: readonly Coin[], + memo = "", + ): Promise { + const sendMsg: MsgSend = { + type: "cosmos-sdk/MsgSend", + value: { + // eslint-disable-next-line @typescript-eslint/camelcase + from_address: this.senderAddress, + // eslint-disable-next-line @typescript-eslint/camelcase + to_address: recipientAddress, + amount: transferAmount, + }, + }; + const fee = this.feeTable.send; + const { accountNumber, sequence } = await this.getNonce(); + const chainId = await this.chainId(); + const signBytes = makeSignBytes([sendMsg], fee, chainId, memo, accountNumber, sequence); + const signature = await this.signCallback(signBytes); + const signedTx = { + msg: [sendMsg], + fee: fee, + memo: memo, + signatures: [signature], + }; + + const result = await this.postTx(marshalTx(signedTx)); + return { + logs: result.logs, + }; + } + /** * Returns the data at the key if present (raw contract dependent storage data) * or null if no data at this key. diff --git a/packages/sdk/types/cosmwasmclient.d.ts b/packages/sdk/types/cosmwasmclient.d.ts index d4464a80..f3fba702 100644 --- a/packages/sdk/types/cosmwasmclient.d.ts +++ b/packages/sdk/types/cosmwasmclient.d.ts @@ -83,6 +83,7 @@ export declare class CosmWasmClient { memo?: string, transferAmount?: readonly Coin[], ): Promise; + sendToken(recipientAddress: string, transferAmount: readonly Coin[], memo?: string): Promise; /** * Returns the data at the key if present (raw contract dependent storage data) * or null if no data at this key.