Merge pull request #976 from cosmos/gasprice-tostring
Add GasPrice.toString
This commit is contained in:
commit
6ad4135bb7
@ -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
|
||||
|
||||
@ -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", () => {
|
||||
|
||||
@ -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 {
|
||||
|
||||
Loading…
Reference in New Issue
Block a user