launchpad: Extract gas helpers from SigningCosmosClient file
This commit is contained in:
parent
2e1380975b
commit
4571b9cb19
42
packages/launchpad/src/gas.ts
Normal file
42
packages/launchpad/src/gas.ts
Normal 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);
|
||||
}
|
||||
@ -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,
|
||||
|
||||
@ -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";
|
||||
|
||||
@ -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
16
packages/launchpad/types/gas.d.ts
vendored
Normal 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;
|
||||
3
packages/launchpad/types/index.d.ts
vendored
3
packages/launchpad/types/index.d.ts
vendored
@ -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,
|
||||
|
||||
@ -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;
|
||||
|
||||
Loading…
Reference in New Issue
Block a user