From 3b8f0f3c377070e9fc54dd9b738318e6f993bf7a Mon Sep 17 00:00:00 2001 From: willclarktech Date: Tue, 2 Feb 2021 18:02:48 +0000 Subject: [PATCH] stargate: Adjust custom registry example for ts-proto --- .../src/signingstargateclient.spec.ts | 88 ++++++++++++++----- 1 file changed, 68 insertions(+), 20 deletions(-) diff --git a/packages/stargate/src/signingstargateclient.spec.ts b/packages/stargate/src/signingstargateclient.spec.ts index abf3714f..9db79896 100644 --- a/packages/stargate/src/signingstargateclient.spec.ts +++ b/packages/stargate/src/signingstargateclient.spec.ts @@ -1,4 +1,4 @@ -/* eslint-disable @typescript-eslint/naming-convention */ +/* eslint-disable @typescript-eslint/naming-convention,no-bitwise */ import { coin, coins, @@ -8,11 +8,12 @@ import { } from "@cosmjs/launchpad"; import { DirectSecp256k1HdWallet, Registry } from "@cosmjs/proto-signing"; import { assert, sleep } from "@cosmjs/utils"; +import protobuf from "protobufjs/minimal"; import { AminoTypes } from "./aminotypes"; import { MsgSend } from "./codec/cosmos/bank/v1beta1/tx"; import { Coin } from "./codec/cosmos/base/v1beta1/coin"; -import { MsgDelegate } from "./codec/cosmos/staking/v1beta1/tx"; +import { DeepPartial, MsgDelegate } from "./codec/cosmos/staking/v1beta1/tx"; import { Tx } from "./codec/cosmos/tx/v1beta1/tx"; import { PrivateSigningStargateClient, SigningStargateClient } from "./signingstargateclient"; import { assertIsBroadcastTxSuccess } from "./stargateclient"; @@ -235,18 +236,6 @@ describe("SigningStargateClient", () => { }); 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 () => { pendingWithoutSimapp(); const wallet = await Secp256k1HdWallet.fromMnemonic(faucet.mnemonic); @@ -296,6 +285,65 @@ describe("SigningStargateClient", () => { it("works with a custom registry and custom message", async () => { pendingWithoutSimapp(); const wallet = await Secp256k1HdWallet.fromMnemonic(faucet.mnemonic); + + 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 { + 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({ additions: { "/cosmos.staking.v1beta1.MsgDelegate": { @@ -334,10 +382,10 @@ describe("SigningStargateClient", () => { const options = { registry: customRegistry, aminoTypes: customAminoTypes }; const client = await SigningStargateClient.connectWithSigner(simapp.tendermintUrl, wallet, options); - const msg = { - custom_delegator_address: faucet.address0, - custom_validator_address: validator.validatorAddress, - custom_amount: coin(1234, "ustake"), + const msg: CustomMsgDelegate = { + customDelegatorAddress: faucet.address0, + customValidatorAddress: validator.validatorAddress, + customAmount: coin(1234, "ustake"), }; const msgAny = { typeUrl: "/cosmos.staking.v1beta1.MsgDelegate", @@ -357,13 +405,13 @@ describe("SigningStargateClient", () => { const wallet = await ModifyingSecp256k1HdWallet.fromMnemonic(faucet.mnemonic); const client = await SigningStargateClient.connectWithSigner(simapp.tendermintUrl, wallet); - const msg = { + const msg: MsgDelegate = { delegatorAddress: faucet.address0, validatorAddress: validator.validatorAddress, amount: coin(1234, "ustake"), }; const msgAny = { - typeUrl: msgDelegateTypeUrl, + typeUrl: "/cosmos.staking.v1beta1.MsgDelegate", value: msg, }; const fee = {