launchpad: Update GasPrice to hold Decimal
This commit is contained in:
parent
b94825f806
commit
86e7afbb1e
@ -1,9 +1,23 @@
|
||||
import { Decimal } from "@cosmjs/math";
|
||||
|
||||
import { GasPrice } from "./gas";
|
||||
|
||||
describe("GasPrice", () => {
|
||||
it("can be constructed", () => {
|
||||
const gasPrice = new GasPrice(3.14, "utest");
|
||||
expect(gasPrice.amount).toEqual(3.14);
|
||||
expect(gasPrice.denom).toEqual("utest");
|
||||
const inputs = ["3.14", "3", "0.14"];
|
||||
inputs.forEach((input) => {
|
||||
const gasPrice = new GasPrice(Decimal.fromUserInput(input, 18), "utest");
|
||||
expect(gasPrice.amount.toString()).toEqual(input);
|
||||
expect(gasPrice.denom).toEqual("utest");
|
||||
});
|
||||
});
|
||||
|
||||
it("can be constructed from a config string", () => {
|
||||
const inputs = ["3.14", "3", "0.14"];
|
||||
inputs.forEach((input) => {
|
||||
const gasPrice = GasPrice.fromString(`${input}utest`);
|
||||
expect(gasPrice.amount.toString()).toEqual(input);
|
||||
expect(gasPrice.denom).toEqual("utest");
|
||||
});
|
||||
});
|
||||
});
|
||||
|
||||
@ -1,14 +1,30 @@
|
||||
import { Decimal } from "@cosmjs/math";
|
||||
|
||||
import { coins } from "./coins";
|
||||
import { StdFee } from "./types";
|
||||
|
||||
export class GasPrice {
|
||||
public readonly amount: number;
|
||||
public readonly amount: Decimal;
|
||||
public readonly denom: string;
|
||||
|
||||
constructor(amount: number, denom: string) {
|
||||
constructor(amount: Decimal, denom: string) {
|
||||
this.amount = amount;
|
||||
this.denom = denom;
|
||||
}
|
||||
|
||||
public static fromString(gasPrice: string): GasPrice {
|
||||
const matchResult = gasPrice.match(/^(?<amount>.+?)(?<denom>[a-z]+)$/);
|
||||
if (!matchResult) {
|
||||
throw new Error("Invalid gas price string");
|
||||
}
|
||||
const { amount, denom } = matchResult.groups as { readonly amount: string; readonly denom: string };
|
||||
if (denom.length < 3 || denom.length > 127) {
|
||||
throw new Error("Gas price denomination must be between 3 and 127 characters");
|
||||
}
|
||||
const fractionalDigits = 18;
|
||||
const decimalAmount = Decimal.fromUserInput(amount, fractionalDigits);
|
||||
return new GasPrice(decimalAmount, denom);
|
||||
}
|
||||
}
|
||||
|
||||
export type GasLimits<T extends Record<string, StdFee>> = {
|
||||
@ -16,7 +32,8 @@ export type GasLimits<T extends Record<string, StdFee>> = {
|
||||
};
|
||||
|
||||
function calculateFee(gasLimit: number, { denom, amount: gasPriceAmount }: GasPrice): StdFee {
|
||||
const amount = Math.ceil(gasPriceAmount * gasLimit);
|
||||
const gasLimitDecimal = Decimal.fromUserInput(gasLimit.toString(), gasPriceAmount.fractionalDigits);
|
||||
const amount = Math.ceil(gasPriceAmount.multiply(gasLimitDecimal).toFloatApproximation());
|
||||
return {
|
||||
amount: coins(amount, denom),
|
||||
gas: gasLimit.toString(),
|
||||
|
||||
6
packages/launchpad/types/gas.d.ts
vendored
6
packages/launchpad/types/gas.d.ts
vendored
@ -1,8 +1,10 @@
|
||||
import { Decimal } from "@cosmjs/math";
|
||||
import { StdFee } from "./types";
|
||||
export declare class GasPrice {
|
||||
readonly amount: number;
|
||||
readonly amount: Decimal;
|
||||
readonly denom: string;
|
||||
constructor(amount: number, denom: string);
|
||||
constructor(amount: Decimal, denom: string);
|
||||
static fromString(gasPrice: string): GasPrice;
|
||||
}
|
||||
export declare type GasLimits<T extends Record<string, StdFee>> = {
|
||||
readonly [key in keyof T]: number;
|
||||
|
||||
Loading…
Reference in New Issue
Block a user