stargate: Extract encoding helpers and add tests
This commit is contained in:
parent
6030487cc9
commit
bfa2525bfa
58
packages/stargate/src/encoding.spec.ts
Normal file
58
packages/stargate/src/encoding.spec.ts
Normal file
@ -0,0 +1,58 @@
|
||||
/* 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";
|
||||
|
||||
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");
|
||||
expect(msgType).toEqual("cosmos-sdk/MsgDelegate");
|
||||
});
|
||||
|
||||
it("throws for unknown type url", () => {
|
||||
expect(() => getMsgType("/xxx.Unknown")).toThrowError(/type url not known/i);
|
||||
});
|
||||
});
|
||||
});
|
||||
35
packages/stargate/src/encoding.ts
Normal file
35
packages/stargate/src/encoding.ts
Normal file
@ -0,0 +1,35 @@
|
||||
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.staking.v1beta1.MsgDelegate": "cosmos-sdk/MsgDelegate",
|
||||
};
|
||||
const type = typeRegister[typeUrl];
|
||||
if (!type) {
|
||||
throw new Error("Type URL not known");
|
||||
}
|
||||
return type;
|
||||
}
|
||||
@ -9,9 +9,7 @@ import {
|
||||
GasLimits,
|
||||
GasPrice,
|
||||
makeSignDoc as makeSignDocAmino,
|
||||
Msg,
|
||||
StdFee,
|
||||
StdSignDoc,
|
||||
} from "@cosmjs/launchpad";
|
||||
import { Int53 } from "@cosmjs/math";
|
||||
import {
|
||||
@ -26,44 +24,11 @@ import {
|
||||
import { Client as TendermintClient } from "@cosmjs/tendermint-rpc";
|
||||
|
||||
import { cosmos } from "./codec";
|
||||
import { getMsgType, snakifyForAmino } from "./encoding";
|
||||
import { BroadcastTxResponse, StargateClient } from "./stargateclient";
|
||||
|
||||
const { TxRaw } = cosmos.tx.v1beta1;
|
||||
|
||||
function snakifyMsgValue(obj: Msg): Msg {
|
||||
return {
|
||||
...obj,
|
||||
value: Object.entries(obj.value).reduce(
|
||||
(snakified, [key, value]) => ({
|
||||
...snakified,
|
||||
[key
|
||||
.split(/(?=[A-Z])/)
|
||||
.join("_")
|
||||
.toLowerCase()]: value,
|
||||
}),
|
||||
{},
|
||||
),
|
||||
};
|
||||
}
|
||||
|
||||
function snakifyForAmino(signDoc: StdSignDoc): StdSignDoc {
|
||||
return {
|
||||
...signDoc,
|
||||
msgs: signDoc.msgs.map(snakifyMsgValue),
|
||||
};
|
||||
}
|
||||
|
||||
function getMsgType(typeUrl: string): string {
|
||||
const typeRegister: Record<string, string> = {
|
||||
"/cosmos.staking.v1beta1.MsgDelegate": "cosmos-sdk/MsgDelegate",
|
||||
};
|
||||
const type = typeRegister[typeUrl];
|
||||
if (!type) {
|
||||
throw new Error("Type URL not known");
|
||||
}
|
||||
return type;
|
||||
}
|
||||
|
||||
const defaultGasPrice = GasPrice.fromString("0.025ucosm");
|
||||
const defaultGasLimits: GasLimits<CosmosFeeTable> = { send: 80000 };
|
||||
|
||||
|
||||
3
packages/stargate/types/encoding.d.ts
vendored
Normal file
3
packages/stargate/types/encoding.d.ts
vendored
Normal file
@ -0,0 +1,3 @@
|
||||
import { StdSignDoc } from "@cosmjs/launchpad";
|
||||
export declare function snakifyForAmino(signDoc: StdSignDoc): StdSignDoc;
|
||||
export declare function getMsgType(typeUrl: string): string;
|
||||
Loading…
Reference in New Issue
Block a user