diff --git a/CHANGELOG.md b/CHANGELOG.md index 50f66c4d..b18aaf15 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -20,6 +20,7 @@ and this project adheres to - @cosmjs/amino: Added `StdTx`, `isStdTx` and `makeStdTx` and removed them from @cosmjs/launchpad. They are re-exported in @cosmjs/launchpad for backwards compatibility. +- @cosmjs/stargate: Add `GasPrice.toString`. [#938]: https://github.com/cosmos/cosmjs/issues/938 [#932]: https://github.com/cosmos/cosmjs/issues/932 diff --git a/packages/stargate/src/fee.spec.ts b/packages/stargate/src/fee.spec.ts index 43897041..03a9fca7 100644 --- a/packages/stargate/src/fee.spec.ts +++ b/packages/stargate/src/fee.spec.ts @@ -58,6 +58,19 @@ describe("GasPrice", () => { expect(() => GasPrice.fromString("..utkn")).toThrowError(/More than one separator found/i); }); }); + + describe("toString", () => { + it("works", () => { + const price1 = new GasPrice(Decimal.fromUserInput("3.14", 18), "utest"); + expect(price1.toString()).toEqual("3.14utest"); + const price2 = new GasPrice(Decimal.fromUserInput("0.14", 18), "utest"); + expect(price2.toString()).toEqual("0.14utest"); + + // is normalized just like other Decimals + const price3 = new GasPrice(Decimal.fromUserInput("003.000", 18), "utest"); + expect(price3.toString()).toEqual("3utest"); + }); + }); }); describe("calculateFee", () => { diff --git a/packages/stargate/src/fee.ts b/packages/stargate/src/fee.ts index f8b40590..d531e2a7 100644 --- a/packages/stargate/src/fee.ts +++ b/packages/stargate/src/fee.ts @@ -49,6 +49,14 @@ export class GasPrice { const decimalAmount = Decimal.fromUserInput(amount, fractionalDigits); return new GasPrice(decimalAmount, denom); } + + /** + * Returns a string representation of this gas price, e.g. "0.025uatom". + * This can be used as an input to `GasPrice.fromString`. + */ + public toString(): string { + return this.amount.toString() + this.denom; + } } export function calculateFee(gasLimit: number, gasPrice: GasPrice | string): StdFee {