stargate: Remove default fee table

This commit is contained in:
willclarktech 2021-06-10 12:46:41 +02:00
parent 97e3d56136
commit 48f93b5063
No known key found for this signature in database
GPG Key ID: 551A86E2E398ADF7
5 changed files with 33 additions and 307 deletions

View File

@ -2,11 +2,6 @@ import { StdFee } from "@cosmjs/amino";
import { Decimal, Uint53 } from "@cosmjs/math";
import { coins } from "@cosmjs/proto-signing";
/**
* This is the same as FeeTable from @cosmjs/launchpad but those might diverge in the future.
*/
export type FeeTable = Record<string, StdFee>;
/**
* Denom checker for the Cosmos SDK 0.42 denom pattern
* (https://github.com/cosmos/cosmos-sdk/blob/v0.42.4/types/coin.go#L599-L601).
@ -56,37 +51,10 @@ export class GasPrice {
}
}
/**
* This is the same as GasLimits from @cosmjs/launchpad but those might diverge in the future.
*/
export type GasLimits<T extends Record<string, StdFee>> = {
readonly [key in keyof T]: number;
};
/**
* This is the same as calculateFee from @cosmjs/launchpad but those might diverge in the future.
*/
function calculateFee(gasLimit: number, { denom, amount: gasPriceAmount }: GasPrice): StdFee {
export function calculateFee(gasLimit: number, { denom, amount: gasPriceAmount }: GasPrice): StdFee {
const amount = Math.ceil(gasPriceAmount.multiply(new Uint53(gasLimit)).toFloatApproximation());
return {
amount: coins(amount, denom),
gas: gasLimit.toString(),
};
}
/**
* This is the same as buildFeeTable from @cosmjs/launchpad but those might diverge in the future.
*/
export function buildFeeTable<T extends Record<string, StdFee>>(
gasPrice: GasPrice,
defaultGasLimits: GasLimits<T>,
gasLimits: Partial<GasLimits<T>>,
): T {
return Object.entries(defaultGasLimits).reduce(
(feeTable, [type, defaultGasLimit]) => ({
...feeTable,
[type]: calculateFee(gasLimits[type] || defaultGasLimit, gasPrice),
}),
{} as T,
);
}

View File

@ -51,7 +51,7 @@ export {
MsgUndelegateEncodeObject,
MsgWithdrawDelegatorRewardEncodeObject,
} from "./encodeobjects";
export { buildFeeTable, FeeTable, GasLimits, GasPrice } from "./fee";
export { calculateFee, GasPrice } from "./fee";
export * as logs from "./logs";
export { makeMultisignedTx } from "./multisignature";
export {
@ -95,9 +95,6 @@ export {
TimeoutError,
} from "./stargateclient";
export {
CosmosFeeTable,
defaultGasLimits,
defaultGasPrice,
defaultRegistryTypes,
SignerData,
SigningStargateClient,

View File

@ -12,10 +12,10 @@ import { decodeTxRaw } from "../../proto-signing/build";
import { AminoMsgDelegate } from "./aminomsgs";
import { AminoTypes } from "./aminotypes";
import { MsgDelegateEncodeObject, MsgSendEncodeObject } from "./encodeobjects";
import { GasPrice } from "./fee";
import { PrivateSigningStargateClient, SigningStargateClient } from "./signingstargateclient";
import { assertIsBroadcastTxSuccess } from "./stargateclient";
import {
defaultSendFee,
faucet,
makeRandomAddress,
ModifyingDirectSecp256k1HdWallet,
@ -27,60 +27,6 @@ import {
describe("SigningStargateClient", () => {
describe("constructor", () => {
it("can be constructed with default fees", async () => {
pendingWithoutSimapp();
const wallet = await DirectSecp256k1HdWallet.fromMnemonic(faucet.mnemonic);
const client = await SigningStargateClient.connectWithSigner(simapp.tendermintUrl, wallet);
const openedClient = client as unknown as PrivateSigningStargateClient;
expect(openedClient.fees).toEqual({
send: {
amount: [
{
amount: "2000",
denom: "ucosm",
},
],
gas: "80000",
},
delegate: {
amount: [
{
amount: "4000",
denom: "ucosm",
},
],
gas: "160000",
},
transfer: {
amount: [
{
amount: "4000",
denom: "ucosm",
},
],
gas: "160000",
},
undelegate: {
amount: [
{
amount: "4000",
denom: "ucosm",
},
],
gas: "160000",
},
withdraw: {
amount: [
{
amount: "4000",
denom: "ucosm",
},
],
gas: "160000",
},
});
});
it("can be constructed with custom registry", async () => {
pendingWithoutSimapp();
const wallet = await DirectSecp256k1HdWallet.fromMnemonic(faucet.mnemonic);
@ -91,180 +37,6 @@ describe("SigningStargateClient", () => {
const openedClient = client as unknown as PrivateSigningStargateClient;
expect(openedClient.registry.lookupType("/custom.MsgCustom")).toEqual(MsgSend);
});
it("can be constructed with custom gas price", async () => {
pendingWithoutSimapp();
const wallet = await DirectSecp256k1HdWallet.fromMnemonic(faucet.mnemonic);
const gasPrice = GasPrice.fromString("3.14utest");
const options = { gasPrice: gasPrice };
const client = await SigningStargateClient.connectWithSigner(simapp.tendermintUrl, wallet, options);
const openedClient = client as unknown as PrivateSigningStargateClient;
expect(openedClient.fees).toEqual({
send: {
amount: [
{
amount: "251200", // 3.14 * 80_000
denom: "utest",
},
],
gas: "80000",
},
delegate: {
amount: [
{
amount: "502400", // 3.14 * 160_000
denom: "utest",
},
],
gas: "160000",
},
transfer: {
amount: [
{
amount: "502400",
denom: "utest",
},
],
gas: "160000",
},
undelegate: {
amount: [
{
amount: "502400",
denom: "utest",
},
],
gas: "160000",
},
withdraw: {
amount: [
{
amount: "502400",
denom: "utest",
},
],
gas: "160000",
},
});
});
it("can be constructed with custom gas limits", async () => {
pendingWithoutSimapp();
const wallet = await DirectSecp256k1HdWallet.fromMnemonic(faucet.mnemonic);
const gasLimits = {
send: 160000,
delegate: 120000,
};
const options = { gasLimits: gasLimits };
const client = await SigningStargateClient.connectWithSigner(simapp.tendermintUrl, wallet, options);
const openedClient = client as unknown as PrivateSigningStargateClient;
expect(openedClient.fees).toEqual({
send: {
amount: [
{
amount: "4000", // 0.025 * 160_000
denom: "ucosm",
},
],
gas: "160000",
},
delegate: {
amount: [
{
amount: "3000", // 0.025 * 120_000
denom: "ucosm",
},
],
gas: "120000",
},
transfer: {
amount: [
{
amount: "4000",
denom: "ucosm",
},
],
gas: "160000",
},
undelegate: {
amount: [
{
amount: "4000",
denom: "ucosm",
},
],
gas: "160000",
},
withdraw: {
amount: [
{
amount: "4000",
denom: "ucosm",
},
],
gas: "160000",
},
});
});
it("can be constructed with custom gas price and gas limits", async () => {
pendingWithoutSimapp();
const wallet = await DirectSecp256k1HdWallet.fromMnemonic(faucet.mnemonic);
const gasPrice = GasPrice.fromString("3.14utest");
const gasLimits = {
send: 160000,
};
const options = { gasPrice: gasPrice, gasLimits: gasLimits };
const client = await SigningStargateClient.connectWithSigner(simapp.tendermintUrl, wallet, options);
const openedClient = client as unknown as PrivateSigningStargateClient;
expect(openedClient.fees).toEqual({
send: {
amount: [
{
amount: "502400", // 3.14 * 160_000
denom: "utest",
},
],
gas: "160000",
},
delegate: {
amount: [
{
amount: "502400", // 3.14 * 160_000
denom: "utest",
},
],
gas: "160000",
},
transfer: {
amount: [
{
amount: "502400",
denom: "utest",
},
],
gas: "160000",
},
undelegate: {
amount: [
{
amount: "502400",
denom: "utest",
},
],
gas: "160000",
},
withdraw: {
amount: [
{
amount: "502400",
denom: "utest",
},
],
gas: "160000",
},
});
});
});
describe("sendTokens", () => {
@ -285,7 +57,13 @@ describe("SigningStargateClient", () => {
});
// send
const result = await client.sendTokens(faucet.address0, beneficiaryAddress, amount, memo);
const result = await client.sendTokens(
faucet.address0,
beneficiaryAddress,
amount,
defaultSendFee,
memo,
);
assertIsBroadcastTxSuccess(result);
expect(result.rawLog).toBeTruthy();
@ -311,7 +89,13 @@ describe("SigningStargateClient", () => {
});
// send
const result = await client.sendTokens(faucet.address0, beneficiaryAddress, amount, memo);
const result = await client.sendTokens(
faucet.address0,
beneficiaryAddress,
amount,
defaultSendFee,
memo,
);
assertIsBroadcastTxSuccess(result);
expect(result.rawLog).toBeTruthy();

View File

@ -67,31 +67,8 @@ import {
MsgUndelegateEncodeObject,
MsgWithdrawDelegatorRewardEncodeObject,
} from "./encodeobjects";
import { buildFeeTable, FeeTable, GasLimits, GasPrice } from "./fee";
import { BroadcastTxResponse, StargateClient } from "./stargateclient";
/**
* These fees are used by the higher level methods of SigningCosmosClient
*
* This is the same as CosmosFeeTable from @cosmjs/launchpad but those might diverge in the future.
*/
export interface CosmosFeeTable extends FeeTable {
readonly send: StdFee;
readonly delegate: StdFee;
readonly transfer: StdFee;
readonly undelegate: StdFee;
readonly withdraw: StdFee;
}
export const defaultGasPrice = GasPrice.fromString("0.025ucosm");
export const defaultGasLimits: GasLimits<CosmosFeeTable> = {
send: 80_000,
delegate: 160_000,
transfer: 160_000,
undelegate: 160_000,
withdraw: 160_000,
};
export const defaultRegistryTypes: ReadonlyArray<[string, GeneratedType]> = [
["/cosmos.bank.v1beta1.MsgMultiSend", MsgMultiSend],
["/cosmos.distribution.v1beta1.MsgFundCommunityPool", MsgFundCommunityPool],
@ -141,7 +118,6 @@ export interface SignerData {
/** Use for testing only */
export interface PrivateSigningStargateClient {
readonly fees: CosmosFeeTable;
readonly registry: Registry;
}
@ -149,14 +125,11 @@ export interface SigningStargateClientOptions {
readonly registry?: Registry;
readonly aminoTypes?: AminoTypes;
readonly prefix?: string;
readonly gasPrice?: GasPrice;
readonly gasLimits?: Partial<GasLimits<CosmosFeeTable>>;
readonly broadcastTimeoutMs?: number;
readonly broadcastPollIntervalMs?: number;
}
export class SigningStargateClient extends StargateClient {
public readonly fees: CosmosFeeTable;
public readonly registry: Registry;
public readonly broadcastTimeoutMs: number | undefined;
public readonly broadcastPollIntervalMs: number | undefined;
@ -195,13 +168,8 @@ export class SigningStargateClient extends StargateClient {
options: SigningStargateClientOptions,
) {
super(tmClient);
const {
registry = createDefaultRegistry(),
aminoTypes = new AminoTypes({ prefix: options.prefix }),
gasPrice = defaultGasPrice,
gasLimits = {},
} = options;
this.fees = buildFeeTable<CosmosFeeTable>(gasPrice, defaultGasLimits, gasLimits);
const { registry = createDefaultRegistry(), aminoTypes = new AminoTypes({ prefix: options.prefix }) } =
options;
this.registry = registry;
this.aminoTypes = aminoTypes;
this.signer = signer;
@ -213,6 +181,7 @@ export class SigningStargateClient extends StargateClient {
senderAddress: string,
recipientAddress: string,
amount: readonly Coin[],
fee: StdFee,
memo = "",
): Promise<BroadcastTxResponse> {
const sendMsg: MsgSendEncodeObject = {
@ -223,13 +192,14 @@ export class SigningStargateClient extends StargateClient {
amount: [...amount],
},
};
return this.signAndBroadcast(senderAddress, [sendMsg], this.fees.send, memo);
return this.signAndBroadcast(senderAddress, [sendMsg], fee, memo);
}
public async delegateTokens(
delegatorAddress: string,
validatorAddress: string,
amount: Coin,
fee: StdFee,
memo = "",
): Promise<BroadcastTxResponse> {
const delegateMsg: MsgDelegateEncodeObject = {
@ -240,13 +210,14 @@ export class SigningStargateClient extends StargateClient {
amount: amount,
}),
};
return this.signAndBroadcast(delegatorAddress, [delegateMsg], this.fees.delegate, memo);
return this.signAndBroadcast(delegatorAddress, [delegateMsg], fee, memo);
}
public async undelegateTokens(
delegatorAddress: string,
validatorAddress: string,
amount: Coin,
fee: StdFee,
memo = "",
): Promise<BroadcastTxResponse> {
const undelegateMsg: MsgUndelegateEncodeObject = {
@ -257,12 +228,13 @@ export class SigningStargateClient extends StargateClient {
amount: amount,
}),
};
return this.signAndBroadcast(delegatorAddress, [undelegateMsg], this.fees.undelegate, memo);
return this.signAndBroadcast(delegatorAddress, [undelegateMsg], fee, memo);
}
public async withdrawRewards(
delegatorAddress: string,
validatorAddress: string,
fee: StdFee,
memo = "",
): Promise<BroadcastTxResponse> {
const withdrawMsg: MsgWithdrawDelegatorRewardEncodeObject = {
@ -272,7 +244,7 @@ export class SigningStargateClient extends StargateClient {
validatorAddress: validatorAddress,
}),
};
return this.signAndBroadcast(delegatorAddress, [withdrawMsg], this.fees.withdraw, memo);
return this.signAndBroadcast(delegatorAddress, [withdrawMsg], fee, memo);
}
public async sendIbcTokens(
@ -284,6 +256,7 @@ export class SigningStargateClient extends StargateClient {
timeoutHeight: Height | undefined,
/** timeout in seconds */
timeoutTimestamp: number | undefined,
fee: StdFee,
memo = "",
): Promise<BroadcastTxResponse> {
const timeoutTimestampNanoseconds = timeoutTimestamp
@ -301,7 +274,7 @@ export class SigningStargateClient extends StargateClient {
timeoutTimestamp: timeoutTimestampNanoseconds,
}),
};
return this.signAndBroadcast(senderAddress, [transferMsg], this.fees.transfer, memo);
return this.signAndBroadcast(senderAddress, [transferMsg], fee, memo);
}
public async signAndBroadcast(

View File

@ -12,6 +12,8 @@ import {
import { SignMode } from "cosmjs-types/cosmos/tx/signing/v1beta1/signing";
import { AuthInfo, SignDoc, TxBody } from "cosmjs-types/cosmos/tx/v1beta1/tx";
import { calculateFee, GasPrice } from "./fee";
export function simappEnabled(): boolean {
return !!process.env.SIMAPP_ENABLED;
}
@ -46,6 +48,8 @@ export function fromOneElementArray<T>(elements: ArrayLike<T>): T {
return elements[0];
}
export const defaultSendFee = calculateFee(80_000, GasPrice.fromString("0.025ucosm"));
export const simapp = {
tendermintUrl: "localhost:26658",
tendermintUrlWs: "ws://localhost:26658",