Skip to content

Commit

Permalink
launchpad: Update GasPrice to hold Decimal
Browse files Browse the repository at this point in the history
  • Loading branch information
willclarktech committed Aug 18, 2020
1 parent 25d9365 commit 4ad7fa4
Show file tree
Hide file tree
Showing 3 changed files with 41 additions and 8 deletions.
20 changes: 17 additions & 3 deletions packages/launchpad/src/gas.spec.ts
Original file line number Diff line number Diff line change
@@ -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");
});
});
});
23 changes: 20 additions & 3 deletions packages/launchpad/src/gas.ts
Original file line number Diff line number Diff line change
@@ -1,22 +1,39 @@
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>> = {
readonly [key in keyof T]: number;
};

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(),
Expand Down
6 changes: 4 additions & 2 deletions packages/launchpad/types/gas.d.ts
Original file line number Diff line number Diff line change
@@ -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;
Expand Down

0 comments on commit 4ad7fa4

Please sign in to comment.