Adapt AMINO JSON representation of messages sent to contracts

This commit is contained in:
Simon Warta 2021-11-24 11:09:10 +01:00
parent b3b0ed1070
commit 4d2479f68f
2 changed files with 22 additions and 22 deletions

View File

@ -1,5 +1,5 @@
/* eslint-disable @typescript-eslint/naming-convention */
import { fromBase64, toBase64, toUtf8 } from "@cosmjs/encoding";
import { fromBase64, toUtf8 } from "@cosmjs/encoding";
import { AminoTypes, coins } from "@cosmjs/stargate";
import {
MsgClearAdmin,
@ -64,7 +64,7 @@ describe("AminoTypes", () => {
sender: "cosmos1pkptre7fdkl6gfrzlesjjvhxhlc3r4gmmk8rs6",
code_id: "12345",
label: "sticky",
msg: toBase64(toUtf8(`{"foo":"bar"}`)),
msg: { foo: "bar" },
funds: coins(1234, "ucosm"),
admin: "cosmos10dyr9899g6t0pelew4nvf4j5c3jcgv0r73qga5",
},
@ -92,7 +92,7 @@ describe("AminoTypes", () => {
sender: "cosmos1pkptre7fdkl6gfrzlesjjvhxhlc3r4gmmk8rs6",
code_id: "12345",
label: "sticky",
msg: toBase64(toUtf8(`{"foo":"bar"}`)),
msg: { foo: "bar" },
funds: coins(1234, "ucosm"),
admin: undefined,
},
@ -157,7 +157,7 @@ describe("AminoTypes", () => {
value: {
sender: "cosmos1pkptre7fdkl6gfrzlesjjvhxhlc3r4gmmk8rs6",
contract: "cosmos1xy4yqngt0nlkdcenxymg8tenrghmek4nmqm28k",
msg: toBase64(toUtf8(`{"foo":"bar"}`)),
msg: { foo: "bar" },
funds: coins(1234, "ucosm"),
},
};
@ -181,7 +181,7 @@ describe("AminoTypes", () => {
sender: "cosmos1pkptre7fdkl6gfrzlesjjvhxhlc3r4gmmk8rs6",
contract: "cosmos1xy4yqngt0nlkdcenxymg8tenrghmek4nmqm28k",
code_id: "98765",
msg: toBase64(toUtf8(`{"foo":"bar"}`)),
msg: { foo: "bar" },
},
};
expect(aminoMsg).toEqual(expected);
@ -218,7 +218,7 @@ describe("AminoTypes", () => {
sender: "cosmos1pkptre7fdkl6gfrzlesjjvhxhlc3r4gmmk8rs6",
code_id: "12345",
label: "sticky",
msg: toBase64(toUtf8(`{"foo":"bar"}`)),
msg: { foo: "bar" },
funds: coins(1234, "ucosm"),
admin: "cosmos10dyr9899g6t0pelew4nvf4j5c3jcgv0r73qga5",
},
@ -246,7 +246,7 @@ describe("AminoTypes", () => {
sender: "cosmos1pkptre7fdkl6gfrzlesjjvhxhlc3r4gmmk8rs6",
code_id: "12345",
label: "sticky",
msg: toBase64(toUtf8(`{"foo":"bar"}`)),
msg: { foo: "bar" },
funds: coins(1234, "ucosm"),
},
};
@ -312,7 +312,7 @@ describe("AminoTypes", () => {
value: {
sender: "cosmos1pkptre7fdkl6gfrzlesjjvhxhlc3r4gmmk8rs6",
contract: "cosmos1xy4yqngt0nlkdcenxymg8tenrghmek4nmqm28k",
msg: toBase64(toUtf8(`{"foo":"bar"}`)),
msg: { foo: "bar" },
funds: coins(1234, "ucosm"),
},
};
@ -336,7 +336,7 @@ describe("AminoTypes", () => {
sender: "cosmos1pkptre7fdkl6gfrzlesjjvhxhlc3r4gmmk8rs6",
contract: "cosmos1xy4yqngt0nlkdcenxymg8tenrghmek4nmqm28k",
code_id: "98765",
msg: toBase64(toUtf8(`{"foo":"bar"}`)),
msg: { foo: "bar" },
},
};
const msg = new AminoTypes({ additions: cosmWasmTypes }).fromAmino(aminoMsg);

View File

@ -1,5 +1,5 @@
/* eslint-disable @typescript-eslint/naming-convention */
import { fromBase64, toBase64 } from "@cosmjs/encoding";
import { fromBase64, fromUtf8, toBase64, toUtf8 } from "@cosmjs/encoding";
import { AminoConverter, Coin } from "@cosmjs/stargate";
import {
MsgClearAdmin,
@ -45,8 +45,8 @@ export interface AminoMsgExecuteContract {
readonly sender: string;
/** Bech32 account address */
readonly contract: string;
/** Execute message as base64 encoded JSON */
readonly msg: string;
/** Execute message as JavaScript object */
readonly msg: any;
readonly funds: readonly Coin[];
};
}
@ -65,8 +65,8 @@ export interface AminoMsgInstantiateContract {
readonly code_id: string;
/** Human-readable label for this contract */
readonly label: string;
/** Instantiate message as base64 encoded JSON */
readonly msg: string;
/** Instantiate message as JavaScript object */
readonly msg: any;
readonly funds: readonly Coin[];
/** Bech32-encoded admin address */
readonly admin?: string;
@ -87,8 +87,8 @@ export interface AminoMsgMigrateContract {
readonly contract: string;
/** The new code */
readonly code_id: string;
/** Migrate message as base64 encoded JSON */
readonly msg: string;
/** Migrate message as JavaScript object */
readonly msg: any;
};
}
@ -150,7 +150,7 @@ export const cosmWasmTypes: Record<string, AminoConverter> = {
sender: sender,
code_id: codeId.toString(),
label: label,
msg: toBase64(msg),
msg: JSON.parse(fromUtf8(msg)),
funds: funds,
admin: admin || undefined,
}),
@ -165,7 +165,7 @@ export const cosmWasmTypes: Record<string, AminoConverter> = {
sender: sender,
codeId: Long.fromString(code_id),
label: label,
msg: fromBase64(msg),
msg: toUtf8(JSON.stringify(msg)),
funds: [...funds],
admin: admin ?? "",
}),
@ -199,13 +199,13 @@ export const cosmWasmTypes: Record<string, AminoConverter> = {
toAmino: ({ sender, contract, msg, funds }: MsgExecuteContract): AminoMsgExecuteContract["value"] => ({
sender: sender,
contract: contract,
msg: toBase64(msg),
msg: JSON.parse(fromUtf8(msg)),
funds: funds,
}),
fromAmino: ({ sender, contract, msg, funds }: AminoMsgExecuteContract["value"]): MsgExecuteContract => ({
sender: sender,
contract: contract,
msg: fromBase64(msg),
msg: toUtf8(JSON.stringify(msg)),
funds: [...funds],
}),
},
@ -215,7 +215,7 @@ export const cosmWasmTypes: Record<string, AminoConverter> = {
sender: sender,
contract: contract,
code_id: codeId.toString(),
msg: toBase64(msg),
msg: JSON.parse(fromUtf8(msg)),
}),
fromAmino: ({
sender,
@ -226,7 +226,7 @@ export const cosmWasmTypes: Record<string, AminoConverter> = {
sender: sender,
contract: contract,
codeId: Long.fromString(code_id),
msg: fromBase64(msg),
msg: toUtf8(JSON.stringify(msg)),
}),
},
};