cosmwasm-stargate: Switch to ts-proto codec

This commit is contained in:
willclarktech 2021-02-03 12:52:16 +00:00
parent c186606612
commit 084affebc8
No known key found for this signature in database
GPG Key ID: 551A86E2E398ADF7
5 changed files with 79 additions and 29 deletions

View File

@ -30,6 +30,7 @@ describe("AminoTypes", () => {
wasmByteCode: fromBase64("WUVMTE9XIFNVQk1BUklORQ=="),
source: "Arrabiata",
builder: "Bob",
instantiatePermission: undefined,
};
const aminoMsg = new AminoTypes({ additions: cosmWasmTypes }).toAmino({
typeUrl: "/cosmwasm.wasm.v1beta1.MsgStoreCode",
@ -196,6 +197,7 @@ describe("AminoTypes", () => {
wasmByteCode: fromBase64("WUVMTE9XIFNVQk1BUklORQ=="),
source: "Arrabiata",
builder: "Bob",
instantiatePermission: undefined,
};
expect(msg).toEqual({
typeUrl: "/cosmwasm.wasm.v1beta1.MsgStoreCode",

View File

@ -46,6 +46,7 @@ export const cosmWasmTypes: Record<string, AminoConverter> = {
wasmByteCode: fromBase64(wasm_byte_code),
source: source,
builder: builder,
instantiatePermission: undefined,
}),
},
"/cosmwasm.wasm.v1beta1.MsgInstantiateContract": {

View File

@ -369,8 +369,7 @@ describe("CosmWasmClient", () => {
const client = await CosmWasmClient.connect(wasmd.endpoint);
const raw = await client.queryContractRaw(contract.address, otherKey);
assert(raw, "must get result");
expect(Uint8Array.from(raw)).toEqual(new Uint8Array());
expect(raw).toBeNull();
});
it("errors for non-existent contract", async () => {

View File

@ -197,7 +197,7 @@ describe("WasmExtension", () => {
for (const { address, contractInfo } of existingContractInfos) {
expect(address).toMatch(bech32AddressMatcher);
assertDefined(contractInfo);
expect(contractInfo.codeId!.toNumber()).toEqual(hackatomCodeId);
expect(contractInfo.codeId.toNumber()).toEqual(hackatomCodeId);
expect(contractInfo.creator).toMatch(bech32AddressMatcher);
expect(contractInfo.label).toMatch(/^.+$/);
}
@ -218,6 +218,7 @@ describe("WasmExtension", () => {
codeId: Long.fromNumber(hackatomCodeId, true),
creator: alice.address0,
label: "my escrow",
admin: "",
});
const { contractInfo } = await client.unverified.wasm.getContractInfo(myAddress);
@ -295,7 +296,7 @@ describe("WasmExtension", () => {
expect(models.length).toEqual(1);
const data = models[0];
expect(data.key).toEqual(hackatomConfigKey);
const value = JSON.parse(fromAscii(data.value!));
const value = JSON.parse(fromAscii(data.value));
expect(value.verifier).toMatch(base64Matcher);
expect(value.beneficiary).toMatch(base64Matcher);
});
@ -322,15 +323,15 @@ describe("WasmExtension", () => {
expect(model.beneficiary).toMatch(base64Matcher);
});
it("returns empty object for missing key", async () => {
it("returns undefined for missing key", async () => {
pendingWithoutWasmd();
assert(hackatomContractAddress);
const client = await makeWasmClient(wasmd.endpoint);
const response = await client.unverified.wasm.queryContractRaw(
const { data } = await client.unverified.wasm.queryContractRaw(
hackatomContractAddress,
fromHex("cafe0dad"),
);
expect({ ...response }).toEqual({ data: new Uint8Array() });
expect(data).toBeUndefined();
});
it("returns null for non-existent address", async () => {
@ -398,7 +399,7 @@ describe("WasmExtension", () => {
assertDefined(result.data);
const msgData = fromOneElementArray(result.data);
expect(msgData.msgType).toEqual("store-code");
expect(MsgStoreCodeResponse.decode(msgData.data!)).toEqual(
expect(MsgStoreCodeResponse.decode(msgData.data)).toEqual(
MsgStoreCodeResponse.fromPartial({ codeId: Long.fromNumber(codeId, true) }),
);
}
@ -418,7 +419,7 @@ describe("WasmExtension", () => {
assertDefined(result.data);
const msgData = fromOneElementArray(result.data);
expect(msgData.msgType).toEqual("instantiate");
expect(MsgInstantiateContractResponse.decode(msgData.data!)).toEqual(
expect(MsgInstantiateContractResponse.decode(msgData.data)).toEqual(
MsgInstantiateContractResponse.fromPartial({ address: contractAddress }),
);
@ -444,7 +445,7 @@ describe("WasmExtension", () => {
assertDefined(result.data);
const msgData = fromOneElementArray(result.data);
expect(msgData.msgType).toEqual("execute");
expect(MsgExecuteContractResponse.decode(msgData.data!)).toEqual(
expect(MsgExecuteContractResponse.decode(msgData.data)).toEqual(
MsgExecuteContractResponse.fromPartial({ data: fromHex("F00BAA") }),
);

View File

@ -11,14 +11,14 @@ import {
} from "@cosmjs/launchpad";
import { DirectSecp256k1HdWallet, Registry } from "@cosmjs/proto-signing";
import { AminoTypes, assertIsBroadcastTxSuccess } from "@cosmjs/stargate";
import { MsgSend } from "@cosmjs/stargate/build/codec/cosmos/bank/v1beta1/tx";
import { DeepPartial, MsgSend } from "@cosmjs/stargate/build/codec/cosmos/bank/v1beta1/tx";
import { Coin } from "@cosmjs/stargate/build/codec/cosmos/base/v1beta1/coin";
import { MsgDelegate } from "@cosmjs/stargate/build/codec/cosmos/staking/v1beta1/tx";
import { Tx } from "@cosmjs/stargate/build/codec/cosmos/tx/v1beta1/tx";
import { assert, sleep } from "@cosmjs/utils";
import Long from "long";
import pako from "pako";
import { Message } from "protobufjs";
import protobuf from "protobufjs/minimal";
import { MsgStoreCode } from "./codec/x/wasm/internal/types/tx";
import { PrivateSigningCosmWasmClient, SigningCosmWasmClient } from "./signingcosmwasmclient";
@ -572,18 +572,6 @@ describe("SigningCosmWasmClient", () => {
});
describe("legacy Amino mode", () => {
// NOTE: One custom registry shared between tests
// See https://github.com/protobufjs/protobuf.js#using-decorators
// > Decorated types reside in protobuf.roots["decorated"] using a flat structure, so no duplicate names.
const customRegistry = new Registry();
const msgDelegateTypeUrl = "/cosmos.staking.v1beta1.MsgDelegate";
interface CustomMsgDelegate {
readonly customDelegatorAddress?: string;
readonly customValidatorAddress?: string;
readonly customAmount?: Coin;
}
it("works with bank MsgSend", async () => {
pendingWithoutWasmd();
const wallet = await Secp256k1HdWallet.fromMnemonic(alice.mnemonic, undefined, wasmd.prefix);
@ -644,6 +632,7 @@ describe("SigningCosmWasmClient", () => {
wasmByteCode: pako.gzip(data),
source: source ?? "",
builder: builder ?? "",
instantiatePermission: undefined,
};
const msgAny = {
typeUrl: "/cosmwasm.wasm.v1beta1.MsgStoreCode",
@ -661,8 +650,66 @@ describe("SigningCosmWasmClient", () => {
it("works with a custom registry and custom message", async () => {
pendingWithoutWasmd();
const wallet = await Secp256k1HdWallet.fromMnemonic(alice.mnemonic, undefined, wasmd.prefix);
const customRegistry = new Registry();
const msgDelegateTypeUrl = "/cosmos.staking.v1beta1.MsgDelegate";
interface CustomMsgDelegate {
customDelegatorAddress?: string;
customValidatorAddress?: string;
customAmount?: Coin;
}
const baseCustomMsgDelegate: CustomMsgDelegate = {
customDelegatorAddress: "",
customValidatorAddress: "",
};
const CustomMsgDelegate = {
// Adapted from autogenerated MsgDelegate implementation
encode(
message: CustomMsgDelegate,
writer: protobuf.Writer = protobuf.Writer.create(),
): protobuf.Writer {
writer.uint32(10).string(message.customDelegatorAddress ?? "");
writer.uint32(18).string(message.customValidatorAddress ?? "");
if (message.customAmount !== undefined && message.customAmount !== undefined) {
Coin.encode(message.customAmount, writer.uint32(26).fork()).ldelim();
}
return writer;
},
decode(): CustomMsgDelegate {
throw new Error("decode method should not be required");
},
fromJSON(): CustomMsgDelegate {
throw new Error("fromJSON method should not be required");
},
fromPartial(object: DeepPartial<CustomMsgDelegate>): CustomMsgDelegate {
const message = { ...baseCustomMsgDelegate } as CustomMsgDelegate;
if (object.customDelegatorAddress !== undefined && object.customDelegatorAddress !== null) {
message.customDelegatorAddress = object.customDelegatorAddress;
} else {
message.customDelegatorAddress = "";
}
if (object.customValidatorAddress !== undefined && object.customValidatorAddress !== null) {
message.customValidatorAddress = object.customValidatorAddress;
} else {
message.customValidatorAddress = "";
}
if (object.customAmount !== undefined && object.customAmount !== null) {
message.customAmount = Coin.fromPartial(object.customAmount);
} else {
message.customAmount = undefined;
}
return message;
},
toJSON(): unknown {
throw new Error("toJSON method should not be required");
},
};
customRegistry.register(msgDelegateTypeUrl, CustomMsgDelegate);
const customAminoTypes = new AminoTypes({
prefix: wasmd.prefix,
additions: {
"/cosmos.staking.v1beta1.MsgDelegate": {
aminoType: "cosmos-sdk/MsgDelegate",
@ -672,7 +719,7 @@ describe("SigningCosmWasmClient", () => {
customAmount,
}: CustomMsgDelegate): LaunchpadMsgDelegate["value"] => {
assert(customDelegatorAddress, "missing customDelegatorAddress");
assert(customValidatorAddress, "missing validator_address");
assert(customValidatorAddress, "missing validatorAddress");
assert(customAmount, "missing amount");
assert(customAmount.amount, "missing amount.amount");
assert(customAmount.denom, "missing amount.denom");
@ -730,7 +777,7 @@ describe("SigningCosmWasmClient", () => {
amount: coin(1234, "ustake"),
};
const msgAny = {
typeUrl: msgDelegateTypeUrl,
typeUrl: "/cosmos.staking.v1beta1.MsgDelegate",
value: msg,
};
const fee = {
@ -748,8 +795,8 @@ describe("SigningCosmWasmClient", () => {
const tx = Tx.decode(searchResult.tx);
// From ModifyingSecp256k1HdWallet
expect(tx.body!.memo).toEqual("This was modified");
expect({ ...tx.authInfo!.fee!.amount![0] }).toEqual(coin(3000, "ucosm"));
expect(tx.authInfo!.fee!.gasLimit!.toNumber()).toEqual(333333);
expect({ ...tx.authInfo!.fee!.amount[0] }).toEqual(coin(3000, "ucosm"));
expect(tx.authInfo!.fee!.gasLimit.toNumber()).toEqual(333333);
});
});
});