stargate: Remove snakifyForAmino encoding helper

This commit is contained in:
willclarktech 2020-10-27 16:45:22 +01:00
parent 36d4c9d97b
commit e08ee36297
No known key found for this signature in database
GPG Key ID: 551A86E2E398ADF7
4 changed files with 3 additions and 73 deletions

View File

@ -1,50 +1,7 @@
/* eslint-disable @typescript-eslint/naming-convention */
import { coin, coins, makeSignDoc as makeSignDocAmino } from "@cosmjs/launchpad";
import { cosmos } from "./codec";
import { getMsgType, snakifyForAmino } from "./encoding";
import { faucet, validator } from "./testutils.spec";
import { getMsgType } from "./encoding";
describe("encoding", () => {
describe("snakifyForAmino", () => {
it("works", () => {
const msg = cosmos.staking.v1beta1.MsgDelegate.create({
delegatorAddress: faucet.address0,
validatorAddress: validator.validatorAddress,
amount: coin(1234, "ustake"),
});
const msgAny = {
type: "cosmos-sdk/MsgDelegate",
value: msg,
};
const fee = {
amount: coins(2000, "ucosm"),
gas: "200000",
};
const chainId = "testing";
const memo = "testing testing";
const accountNumber = 1;
const sequence = 16;
const signDoc = makeSignDocAmino([msgAny], fee, chainId, memo, accountNumber, sequence);
expect(snakifyForAmino(signDoc)).toEqual({
...signDoc,
msgs: [
{
type: "cosmos-sdk/MsgDelegate",
value: {
delegator_address: faucet.address0,
validator_address: validator.validatorAddress,
amount: {
amount: "1234",
denom: "ustake",
},
},
},
],
});
});
});
describe("getMsgType", () => {
it("works for known type url", () => {
const msgType = getMsgType("/cosmos.staking.v1beta1.MsgDelegate");

View File

@ -1,28 +1,3 @@
import { Msg, StdSignDoc } from "@cosmjs/launchpad";
function snakifyMsgValue(obj: Msg): Msg {
return {
...obj,
value: Object.entries(obj.value).reduce(
(snakified, [key, value]) => ({
...snakified,
[key
.split(/(?=[A-Z])/)
.join("_")
.toLowerCase()]: value,
}),
{},
),
};
}
export function snakifyForAmino(signDoc: StdSignDoc): StdSignDoc {
return {
...signDoc,
msgs: signDoc.msgs.map(snakifyMsgValue),
};
}
export function getMsgType(typeUrl: string): string {
const typeRegister: Record<string, string> = {
"/cosmos.bank.v1beta1.MsgSend": "cosmos-sdk/MsgSend",

View File

@ -24,7 +24,7 @@ import {
import { Client as TendermintClient } from "@cosmjs/tendermint-rpc";
import { cosmos } from "./codec";
import { getMsgType, snakifyForAmino } from "./encoding";
import { getMsgType } from "./encoding";
import { BroadcastTxResponse, StargateClient } from "./stargateclient";
const { TxRaw } = cosmos.tx.v1beta1;
@ -141,7 +141,7 @@ export class SigningStargateClient extends StargateClient {
value: msg.value,
}));
const signDoc = makeSignDocAmino(msgs, fee, chainId, memo, accountNumber, sequence);
const signResponse = await this.signer.signAmino(address, snakifyForAmino(signDoc));
const signResponse = await this.signer.signAmino(address, signDoc);
const txRaw = TxRaw.create({
bodyBytes: txBodyBytes,
authInfoBytes: authInfoBytes,

View File

@ -1,3 +1 @@
import { StdSignDoc } from "@cosmjs/launchpad";
export declare function snakifyForAmino(signDoc: StdSignDoc): StdSignDoc;
export declare function getMsgType(typeUrl: string): string;