Fix empty to undefined conversion in AminoMsgInstantiateContract

This commit is contained in:
Simon Warta 2021-10-12 11:04:40 +02:00
parent 37aa102e86
commit 22e0d32aa3
2 changed files with 103 additions and 54 deletions

View File

@ -44,36 +44,61 @@ describe("AminoTypes", () => {
});
it("works for MsgInstantiateContract", () => {
const msg: MsgInstantiateContract = {
sender: "cosmos1pkptre7fdkl6gfrzlesjjvhxhlc3r4gmmk8rs6",
codeId: Long.fromString("12345"),
label: "sticky",
msg: toUtf8(
JSON.stringify({
foo: "bar",
}),
),
funds: coins(1234, "ucosm"),
admin: "cosmos10dyr9899g6t0pelew4nvf4j5c3jcgv0r73qga5",
};
const aminoMsg = new AminoTypes({ additions: cosmWasmTypes }).toAmino({
typeUrl: "/cosmwasm.wasm.v1.MsgInstantiateContract",
value: msg,
});
const expected: AminoMsgInstantiateContract = {
type: "wasm/MsgInstantiateContract",
value: {
// With admin
{
const msg: MsgInstantiateContract = {
sender: "cosmos1pkptre7fdkl6gfrzlesjjvhxhlc3r4gmmk8rs6",
code_id: "12345",
codeId: Long.fromString("12345"),
label: "sticky",
msg: {
foo: "bar",
},
msg: toUtf8(`{"foo":"bar"}`),
funds: coins(1234, "ucosm"),
admin: "cosmos10dyr9899g6t0pelew4nvf4j5c3jcgv0r73qga5",
},
};
expect(aminoMsg).toEqual(expected);
};
const aminoMsg = new AminoTypes({ additions: cosmWasmTypes }).toAmino({
typeUrl: "/cosmwasm.wasm.v1.MsgInstantiateContract",
value: msg,
});
const expected: AminoMsgInstantiateContract = {
type: "wasm/MsgInstantiateContract",
value: {
sender: "cosmos1pkptre7fdkl6gfrzlesjjvhxhlc3r4gmmk8rs6",
code_id: "12345",
label: "sticky",
msg: { foo: "bar" },
funds: coins(1234, "ucosm"),
admin: "cosmos10dyr9899g6t0pelew4nvf4j5c3jcgv0r73qga5",
},
};
expect(aminoMsg).toEqual(expected);
}
// Without admin
{
const msg: MsgInstantiateContract = {
sender: "cosmos1pkptre7fdkl6gfrzlesjjvhxhlc3r4gmmk8rs6",
codeId: Long.fromString("12345"),
label: "sticky",
msg: toUtf8(`{"foo":"bar"}`),
funds: coins(1234, "ucosm"),
admin: "",
};
const aminoMsg = new AminoTypes({ additions: cosmWasmTypes }).toAmino({
typeUrl: "/cosmwasm.wasm.v1.MsgInstantiateContract",
value: msg,
});
const expected: AminoMsgInstantiateContract = {
type: "wasm/MsgInstantiateContract",
value: {
sender: "cosmos1pkptre7fdkl6gfrzlesjjvhxhlc3r4gmmk8rs6",
code_id: "12345",
label: "sticky",
msg: { foo: "bar" },
funds: coins(1234, "ucosm"),
admin: undefined,
},
};
expect(aminoMsg).toEqual(expected);
}
});
it("works for MsgUpdateAdmin", () => {
@ -197,36 +222,60 @@ describe("AminoTypes", () => {
});
it("works for MsgInstantiateContract", () => {
const aminoMsg: AminoMsgInstantiateContract = {
type: "wasm/MsgInstantiateContract",
value: {
sender: "cosmos1pkptre7fdkl6gfrzlesjjvhxhlc3r4gmmk8rs6",
code_id: "12345",
label: "sticky",
msg: {
foo: "bar",
// With admin
{
const aminoMsg: AminoMsgInstantiateContract = {
type: "wasm/MsgInstantiateContract",
value: {
sender: "cosmos1pkptre7fdkl6gfrzlesjjvhxhlc3r4gmmk8rs6",
code_id: "12345",
label: "sticky",
msg: { foo: "bar" },
funds: coins(1234, "ucosm"),
admin: "cosmos10dyr9899g6t0pelew4nvf4j5c3jcgv0r73qga5",
},
};
const msg = new AminoTypes({ additions: cosmWasmTypes }).fromAmino(aminoMsg);
const expectedValue: MsgInstantiateContract = {
sender: "cosmos1pkptre7fdkl6gfrzlesjjvhxhlc3r4gmmk8rs6",
codeId: Long.fromString("12345"),
label: "sticky",
msg: toUtf8(`{"foo":"bar"}`),
funds: coins(1234, "ucosm"),
admin: "cosmos10dyr9899g6t0pelew4nvf4j5c3jcgv0r73qga5",
},
};
const msg = new AminoTypes({ additions: cosmWasmTypes }).fromAmino(aminoMsg);
const expectedValue: MsgInstantiateContract = {
sender: "cosmos1pkptre7fdkl6gfrzlesjjvhxhlc3r4gmmk8rs6",
codeId: Long.fromString("12345"),
label: "sticky",
msg: toUtf8(
JSON.stringify({
foo: "bar",
}),
),
funds: coins(1234, "ucosm"),
admin: "cosmos10dyr9899g6t0pelew4nvf4j5c3jcgv0r73qga5",
};
expect(msg).toEqual({
typeUrl: "/cosmwasm.wasm.v1.MsgInstantiateContract",
value: expectedValue,
});
};
expect(msg).toEqual({
typeUrl: "/cosmwasm.wasm.v1.MsgInstantiateContract",
value: expectedValue,
});
}
// Without admin
{
const aminoMsg: AminoMsgInstantiateContract = {
type: "wasm/MsgInstantiateContract",
value: {
sender: "cosmos1pkptre7fdkl6gfrzlesjjvhxhlc3r4gmmk8rs6",
code_id: "12345",
label: "sticky",
msg: { foo: "bar" },
funds: coins(1234, "ucosm"),
},
};
const msg = new AminoTypes({ additions: cosmWasmTypes }).fromAmino(aminoMsg);
const expectedValue: MsgInstantiateContract = {
sender: "cosmos1pkptre7fdkl6gfrzlesjjvhxhlc3r4gmmk8rs6",
codeId: Long.fromString("12345"),
label: "sticky",
msg: toUtf8(`{"foo":"bar"}`),
funds: coins(1234, "ucosm"),
admin: "",
};
expect(msg).toEqual({
typeUrl: "/cosmwasm.wasm.v1.MsgInstantiateContract",
value: expectedValue,
});
}
});
it("works for MsgUpdateAdmin", () => {

View File

@ -152,7 +152,7 @@ export const cosmWasmTypes: Record<string, AminoConverter> = {
label: label,
msg: JSON.parse(fromUtf8(msg)),
funds: funds,
admin: admin ?? undefined,
admin: admin || undefined,
}),
fromAmino: ({
sender,