Add sendToken() and test

This commit is contained in:
Ethan Frey 2020-02-16 19:55:07 +01:00 committed by Simon Warta
parent 0a10fa57ac
commit 6d3865a27e
3 changed files with 66 additions and 0 deletions

View File

@ -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");

View File

@ -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<ExecuteResult> {
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.

View File

@ -83,6 +83,7 @@ export declare class CosmWasmClient {
memo?: string,
transferAmount?: readonly Coin[],
): Promise<ExecuteResult>;
sendToken(recipientAddress: string, transferAmount: readonly Coin[], memo?: string): Promise<ExecuteResult>;
/**
* Returns the data at the key if present (raw contract dependent storage data)
* or null if no data at this key.