launchpad: Refactor fee table generation

This commit is contained in:
willclarktech 2020-08-18 15:15:47 +01:00
parent aee57597a2
commit c87b0643f9
No known key found for this signature in database
GPG Key ID: 551A86E2E398ADF7
6 changed files with 46 additions and 45 deletions

View File

@ -1,13 +1,6 @@
import { coins } from "./coins";
import { StdFee } from "./types";
/**
* These fees are used by the higher level methods of SigningCosmosClient
*/
export interface FeeTable {
readonly send: StdFee;
}
export class GasPrice {
public readonly amount: number;
public readonly denom: string;
@ -18,25 +11,28 @@ export class GasPrice {
}
}
export type GasLimits = {
readonly [key in keyof FeeTable]: number;
export type GasLimits<T extends Record<string, StdFee>> = {
readonly [key in keyof T]: number;
};
function calculateFee(gasLimit: number, denom: string, price: number): StdFee {
const amount = Math.ceil(price * gasLimit);
function calculateFee(gasLimit: number, { denom, amount: gasPriceAmount }: GasPrice): StdFee {
const amount = Math.ceil(gasPriceAmount * gasLimit);
return {
amount: coins(amount, denom),
gas: gasLimit.toString(),
};
}
export function buildFeeTable({ denom, amount }: GasPrice, gasLimits: GasLimits): FeeTable {
return Object.entries(gasLimits).reduce((feeTable, [type, gasLimit]) => {
return gasLimit === undefined
? feeTable
: {
...feeTable,
[type]: calculateFee(gasLimit, denom, amount),
};
}, {} as FeeTable);
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

@ -29,7 +29,7 @@ export {
isSearchByTagsQuery,
} from "./cosmosclient";
export { makeSignBytes } from "./encoding";
export { buildFeeTable, FeeTable, GasPrice } from "./gas";
export { buildFeeTable, GasPrice } from "./gas";
export {
AuthAccountsResponse,
AuthExtension,
@ -98,7 +98,7 @@ export {
} from "./pubkey";
export { findSequenceForSignedTx } from "./sequence";
export { encodeSecp256k1Signature, decodeSignature } from "./signature";
export { SigningCosmosClient } from "./signingcosmosclient";
export { FeeTable, SigningCosmosClient } from "./signingcosmosclient";
export { isStdTx, pubkeyType, CosmosSdkTx, PubKey, StdFee, StdSignature, StdTx } from "./types";
export {
AccountData,

View File

@ -2,14 +2,21 @@
import { Coin } from "./coins";
import { Account, BroadcastTxResult, CosmosClient, GetSequenceResult } from "./cosmosclient";
import { makeSignBytes } from "./encoding";
import { buildFeeTable, FeeTable, GasLimits, GasPrice } from "./gas";
import { buildFeeTable, GasLimits, GasPrice } from "./gas";
import { BroadcastMode } from "./lcdapi";
import { Msg, MsgSend } from "./msgs";
import { StdFee, StdTx } from "./types";
import { OfflineSigner } from "./wallet";
/**
* These fees are used by the higher level methods of SigningCosmosClient
*/
export interface FeeTable extends Record<string, StdFee> {
readonly send: StdFee;
}
const defaultGasPrice = new GasPrice(0.025, "ucosm");
const defaultGasLimits: GasLimits = { send: 80000 };
const defaultGasLimits: GasLimits<FeeTable> = { send: 80000 };
/** Use for testing only */
export interface PrivateSigningCosmosClient {
@ -40,20 +47,14 @@ export class SigningCosmosClient extends CosmosClient {
senderAddress: string,
signer: OfflineSigner,
gasPrice: GasPrice = defaultGasPrice,
gasLimits?: Partial<GasLimits>,
gasLimits: Partial<GasLimits<FeeTable>> = {},
broadcastMode = BroadcastMode.Block,
) {
super(apiUrl, broadcastMode);
this.anyValidAddress = senderAddress;
this.senderAddress = senderAddress;
this.signer = signer;
const mergedGasLimits = {
...defaultGasLimits,
...gasLimits,
};
this.fees = buildFeeTable(gasPrice, mergedGasLimits);
this.fees = buildFeeTable<FeeTable>(gasPrice, defaultGasLimits, gasLimits);
}
public async getSequence(address?: string): Promise<GetSequenceResult> {

View File

@ -1,16 +1,14 @@
import { StdFee } from "./types";
/**
* These fees are used by the higher level methods of SigningCosmosClient
*/
export interface FeeTable {
readonly send: StdFee;
}
export declare class GasPrice {
readonly amount: number;
readonly denom: string;
constructor(amount: number, denom: string);
}
export declare type GasLimits = {
readonly [key in keyof FeeTable]: number;
export declare type GasLimits<T extends Record<string, StdFee>> = {
readonly [key in keyof T]: number;
};
export declare function buildFeeTable({ denom, amount }: GasPrice, gasLimits: GasLimits): FeeTable;
export declare function buildFeeTable<T extends Record<string, StdFee>>(
gasPrice: GasPrice,
defaultGasLimits: GasLimits<T>,
gasLimits: Partial<GasLimits<T>>,
): T;

View File

@ -27,7 +27,7 @@ export {
isSearchByTagsQuery,
} from "./cosmosclient";
export { makeSignBytes } from "./encoding";
export { buildFeeTable, FeeTable, GasPrice } from "./gas";
export { buildFeeTable, GasPrice } from "./gas";
export {
AuthAccountsResponse,
AuthExtension,
@ -96,7 +96,7 @@ export {
} from "./pubkey";
export { findSequenceForSignedTx } from "./sequence";
export { encodeSecp256k1Signature, decodeSignature } from "./signature";
export { SigningCosmosClient } from "./signingcosmosclient";
export { FeeTable, SigningCosmosClient } from "./signingcosmosclient";
export { isStdTx, pubkeyType, CosmosSdkTx, PubKey, StdFee, StdSignature, StdTx } from "./types";
export {
AccountData,

View File

@ -1,10 +1,16 @@
import { Coin } from "./coins";
import { Account, BroadcastTxResult, CosmosClient, GetSequenceResult } from "./cosmosclient";
import { FeeTable, GasLimits, GasPrice } from "./gas";
import { GasLimits, GasPrice } from "./gas";
import { BroadcastMode } from "./lcdapi";
import { Msg } from "./msgs";
import { StdFee } from "./types";
import { OfflineSigner } from "./wallet";
/**
* These fees are used by the higher level methods of SigningCosmosClient
*/
export interface FeeTable extends Record<string, StdFee> {
readonly send: StdFee;
}
/** Use for testing only */
export interface PrivateSigningCosmosClient {
readonly fees: FeeTable;
@ -31,7 +37,7 @@ export declare class SigningCosmosClient extends CosmosClient {
senderAddress: string,
signer: OfflineSigner,
gasPrice?: GasPrice,
gasLimits?: Partial<GasLimits>,
gasLimits?: Partial<GasLimits<FeeTable>>,
broadcastMode?: BroadcastMode,
);
getSequence(address?: string): Promise<GetSequenceResult>;