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 all commits
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
23 changes: 20 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, toBase64 } 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,18 @@ export const CW20 = (client: SigningCosmWasmClient, fees: Options["fees"]): CW20
return result.transactionHash;
}

const jsonToBinary = (json: Record<string, unknown>): string => { return toBase64(toUtf8(JSON.stringify(json)))}

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: jsonToBinary(msg)}}, fees.exec);
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: jsonToBinary(msg)}}, fees.exec);
return result.transactionHash;
}

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

Expand Down