Fix message encoding in AminoMsgExecuteContract

This commit is contained in:
Simon Warta 2021-10-12 11:34:43 +02:00
parent a6a5d8e2fd
commit fa9004b5ea
3 changed files with 59 additions and 25 deletions

View File

@ -145,11 +145,7 @@ describe("AminoTypes", () => {
const msg: MsgExecuteContract = {
sender: "cosmos1pkptre7fdkl6gfrzlesjjvhxhlc3r4gmmk8rs6",
contract: "cosmos1xy4yqngt0nlkdcenxymg8tenrghmek4nmqm28k",
msg: toUtf8(
JSON.stringify({
foo: "bar",
}),
),
msg: toUtf8(`{"foo":"bar"}`),
funds: coins(1234, "ucosm"),
};
const aminoMsg = new AminoTypes({ additions: cosmWasmTypes }).toAmino({
@ -161,9 +157,7 @@ describe("AminoTypes", () => {
value: {
sender: "cosmos1pkptre7fdkl6gfrzlesjjvhxhlc3r4gmmk8rs6",
contract: "cosmos1xy4yqngt0nlkdcenxymg8tenrghmek4nmqm28k",
msg: {
foo: "bar",
},
msg: toBase64(toUtf8(`{"foo":"bar"}`)),
funds: coins(1234, "ucosm"),
},
};
@ -175,11 +169,7 @@ describe("AminoTypes", () => {
sender: "cosmos1pkptre7fdkl6gfrzlesjjvhxhlc3r4gmmk8rs6",
contract: "cosmos1xy4yqngt0nlkdcenxymg8tenrghmek4nmqm28k",
codeId: Long.fromString("98765"),
msg: toUtf8(
JSON.stringify({
foo: "bar",
}),
),
msg: toUtf8(`{"foo":"bar"}`),
};
const aminoMsg = new AminoTypes({ additions: cosmWasmTypes }).toAmino({
typeUrl: "/cosmwasm.wasm.v1.MsgMigrateContract",
@ -324,9 +314,7 @@ describe("AminoTypes", () => {
value: {
sender: "cosmos1pkptre7fdkl6gfrzlesjjvhxhlc3r4gmmk8rs6",
contract: "cosmos1xy4yqngt0nlkdcenxymg8tenrghmek4nmqm28k",
msg: {
foo: "bar",
},
msg: toBase64(toUtf8(`{"foo":"bar"}`)),
funds: coins(1234, "ucosm"),
},
};
@ -334,11 +322,7 @@ describe("AminoTypes", () => {
const expectedValue: MsgExecuteContract = {
sender: "cosmos1pkptre7fdkl6gfrzlesjjvhxhlc3r4gmmk8rs6",
contract: "cosmos1xy4yqngt0nlkdcenxymg8tenrghmek4nmqm28k",
msg: toUtf8(
JSON.stringify({
foo: "bar",
}),
),
msg: toUtf8(`{"foo":"bar"}`),
funds: coins(1234, "ucosm"),
};
expect(msg).toEqual({

View File

@ -45,8 +45,8 @@ export interface AminoMsgExecuteContract {
readonly sender: string;
/** Bech32 account address */
readonly contract: string;
/** Handle message as JavaScript object */
readonly msg: any;
/** Execute message as base64 encoded JSON */
readonly msg: string;
readonly funds: readonly Coin[];
};
}
@ -199,13 +199,13 @@ export const cosmWasmTypes: Record<string, AminoConverter> = {
toAmino: ({ sender, contract, msg, funds }: MsgExecuteContract): AminoMsgExecuteContract["value"] => ({
sender: sender,
contract: contract,
msg: JSON.parse(fromUtf8(msg)),
msg: toBase64(msg),
funds: funds,
}),
fromAmino: ({ sender, contract, msg, funds }: AminoMsgExecuteContract["value"]): MsgExecuteContract => ({
sender: sender,
contract: contract,
msg: toUtf8(JSON.stringify(msg)),
msg: fromBase64(msg),
funds: [...funds],
}),
},

View File

@ -377,6 +377,56 @@ describe("SigningCosmWasmClient", () => {
client.disconnect();
});
it("works with legacy Amino signer", async () => {
pendingWithoutWasmd();
const wallet = await Secp256k1HdWallet.fromMnemonic(alice.mnemonic, { prefix: wasmd.prefix });
const options = { ...defaultSigningClientOptions, prefix: wasmd.prefix };
const client = await SigningCosmWasmClient.connectWithSigner(wasmd.endpoint, wallet, options);
const { codeId } = await client.upload(alice.address0, getHackatom().data, defaultUploadFee);
// instantiate
const funds = [coin(233444, "ucosm"), coin(5454, "ustake")];
const beneficiaryAddress = makeRandomAddress();
const { contractAddress } = await client.instantiate(
alice.address0,
codeId,
{
verifier: alice.address0,
beneficiary: beneficiaryAddress,
},
"amazing random contract",
defaultInstantiateFee,
{
funds: funds,
},
);
// execute
const result = await client.execute(
alice.address0,
contractAddress,
{ release: {} },
defaultExecuteFee,
);
const wasmEvent = result.logs[0].events.find((e) => e.type === "wasm");
assert(wasmEvent, "Event of type wasm expected");
expect(wasmEvent.attributes).toContain({ key: "action", value: "release" });
expect(wasmEvent.attributes).toContain({
key: "destination",
value: beneficiaryAddress,
});
// Verify token transfer from contract to beneficiary
const wasmClient = await makeWasmClient(wasmd.endpoint);
const beneficiaryBalanceUcosm = await wasmClient.bank.balance(beneficiaryAddress, "ucosm");
expect(beneficiaryBalanceUcosm).toEqual(funds[0]);
const beneficiaryBalanceUstake = await wasmClient.bank.balance(beneficiaryAddress, "ustake");
expect(beneficiaryBalanceUstake).toEqual(funds[1]);
const contractBalanceUcosm = await wasmClient.bank.balance(contractAddress, "ucosm");
expect(contractBalanceUcosm).toEqual(coin(0, "ucosm"));
const contractBalanceUstake = await wasmClient.bank.balance(contractAddress, "ustake");
expect(contractBalanceUstake).toEqual(coin(0, "ustake"));
client.disconnect();
});
});
describe("sendTokens", () => {