Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add send and sendFrom to cw20-base helpers.ts #415

Merged
merged 2 commits into from
Sep 13, 2021
Merged
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
21 changes: 18 additions & 3 deletions contracts/cw20-base/helpers.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import { SigningCosmWasmClient } from "@cosmjs/cosmwasm-stargate";
import { GasPrice, calculateFee, StdFee } from "@cosmjs/stargate";
import { DirectSecp256k1HdWallet, makeCosmoshubPath } from "@cosmjs/proto-signing";
import { Slip10RawIndex } from "@cosmjs/crypto";
import { toUtf8 } from "@cosmjs/encoding";
import path from "path";
/*
* This is a set of helpers meant for use with @cosmjs/cli
Expand All @@ -16,13 +17,13 @@ import path from "path";
*
* Get the mnemonic:
* await useOptions(pebblenetOptions).recoverMnemonic(password);
*
*
* Create contract:
* const contract = CW20(client, pebblenetOptions.fees);
*
*
* Upload contract:
* const codeId = await contract.upload(addr);
*
*
* Instantiate contract example:
* const initMsg = {
* name: "Potato Coin",
Expand Down Expand Up @@ -185,10 +186,12 @@ interface CW20Instance {
// actions
mint: (txSigner: string, recipient: string, amount: string) => Promise<string>
transfer: (txSigner: string, recipient: string, amount: string) => Promise<string>
send: (txSigner: string, recipient: string, amount: string, msg: Record<string, unknown>) => Promise<string>
burn: (txSigner: string, amount: string) => Promise<string>
increaseAllowance: (txSigner: string, recipient: string, amount: string) => Promise<string>
decreaseAllowance: (txSigner: string, recipient: string, amount: string) => Promise<string>
transferFrom: (txSigner: string, owner: string, recipient: string, amount: string) => Promise<string>
sendFrom: (txSigner: string, owner: string, recipient: string, amount: string, msg: Record<string, unknown>) => Promise<string>
}

interface CW20Contract {
Expand Down Expand Up @@ -265,6 +268,16 @@ export const CW20 = (client: SigningCosmWasmClient, fees: Options["fees"]): CW20
return result.transactionHash;
}

const send = async (senderAddress: string, recipient: string, amount: string, msg: Record<string, unknown>): Promise<string> => {
const result = await client.execute(senderAddress, contractAddress, {send: {recipient, amount, msg: toUtf8(JSON.stringify(msg))}}, fees.exec);
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

One more step needed here (maybe you can pull this out to a function jsonToBinary

msg: toBase64(toUtf8(JSON.stringify(msg)))

We want to explicitly make it a base64-encoded string. I think the default JSON serialisation in JS land for Uint8Array is [43, 91, 81, ...], which is not what we want

return result.transactionHash;
}

const sendFrom = async (senderAddress: string, owner: string, recipient: string, amount: string, msg: Record<string, unknown>): Promise<string> => {
const result = await client.execute(senderAddress, contractAddress, {send_from: {owner, recipient, amount, msg: toUtf8(JSON.stringify(msg))}}, fees.exec);
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Same here with the msg encoding

return result.transactionHash;
}

return {
contractAddress,
balance,
Expand All @@ -279,6 +292,8 @@ export const CW20 = (client: SigningCosmWasmClient, fees: Options["fees"]): CW20
increaseAllowance,
decreaseAllowance,
transferFrom,
send,
sendFrom
};
}

Expand Down