launchpad: Extract gas helpers from SigningCosmosClient file

This commit is contained in:
willclarktech 2020-08-18 14:39:05 +01:00
parent 2e1380975b
commit 4571b9cb19
No known key found for this signature in database
GPG Key ID: 551A86E2E398ADF7
7 changed files with 71 additions and 59 deletions

View File

@ -0,0 +1,42 @@
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;
constructor(amount: number, denom: string) {
this.amount = amount;
this.denom = denom;
}
}
export type GasLimits = {
readonly [key in keyof FeeTable]: number;
};
function calculateFee(gasLimit: number, denom: string, price: number): StdFee {
const amount = Math.ceil(price * 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);
}

View File

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

View File

@ -3,9 +3,10 @@ import { assert } from "@cosmjs/utils";
import { Coin, coin, coins } from "./coins";
import { assertIsBroadcastTxSuccess, PrivateCosmosClient } from "./cosmosclient";
import { GasPrice } from "./gas";
import { MsgDelegate } from "./msgs";
import { Secp256k1Wallet } from "./secp256k1wallet";
import { GasPrice, SigningCosmosClient } from "./signingcosmosclient";
import { SigningCosmosClient } from "./signingcosmosclient";
import { makeRandomAddress, pendingWithoutWasmd, validatorAddress } from "./testutils.spec";
const httpUrl = "http://localhost:1317";

View File

@ -1,54 +1,14 @@
/* eslint-disable @typescript-eslint/naming-convention */
import { Coin, coins } from "./coins";
import { Coin } from "./coins";
import { Account, BroadcastTxResult, CosmosClient, GetSequenceResult } from "./cosmosclient";
import { makeSignBytes } from "./encoding";
import { buildFeeTable, FeeTable, 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 {
readonly send: StdFee;
}
export class GasPrice {
readonly amount: number;
readonly denom: string;
constructor(amount: number, denom: string) {
this.amount = amount;
this.denom = denom;
}
}
export type GasLimits = {
readonly [key in keyof FeeTable]: number;
};
function calculateFee(gasLimit: number, denom: string, price: number): StdFee {
const amount = Math.ceil(price * gasLimit);
return {
amount: coins(amount, denom),
gas: gasLimit.toString(),
};
}
function buildFeeTable({ denom, amount }: GasPrice, gasLimits: GasLimits): FeeTable {
return Object.entries(gasLimits).reduce((feeTable, [type, gasLimit]) => {
if (gasLimit === undefined) {
return feeTable;
}
return {
...feeTable,
[type]: calculateFee(gasLimit, denom, amount),
};
}, {} as FeeTable);
}
const defaultGasPrice: GasPrice = new GasPrice(0.025, "ucosm");
const defaultGasPrice = new GasPrice(0.025, "ucosm");
const defaultGasLimits: GasLimits = { send: 80000 };
export class SigningCosmosClient extends CosmosClient {

16
packages/launchpad/types/gas.d.ts vendored Normal file
View File

@ -0,0 +1,16 @@
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 function buildFeeTable({ denom, amount }: GasPrice, gasLimits: GasLimits): FeeTable;

View File

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

View File

@ -1,23 +1,14 @@
import { Coin } from "./coins";
import { Account, BroadcastTxResult, CosmosClient, GetSequenceResult } from "./cosmosclient";
import { FeeTable, 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 {
readonly send: StdFee;
/** Use for testing only */
export interface PrivateSigningCosmosClient {
readonly fees: FeeTable;
}
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 class SigningCosmosClient extends CosmosClient {
readonly senderAddress: string;
private readonly signer;