Merge pull request #976 from cosmos/gasprice-tostring

Add GasPrice.toString
This commit is contained in:
Simon Warta 2022-01-10 15:41:18 +01:00 committed by GitHub
commit 6ad4135bb7
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
3 changed files with 22 additions and 0 deletions

View File

@ -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

View File

@ -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", () => {

View File

@ -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 {