Add helper methods to parse gas, fees and gasPrice #29
@ -588,3 +588,4 @@ export { LaconicClient };
|
|||||||
export * from './constants';
|
export * from './constants';
|
||||||
export * from './types/cerc/bond/message';
|
export * from './types/cerc/bond/message';
|
||||||
export * from './types/cerc/onboarding/message';
|
export * from './types/cerc/onboarding/message';
|
||||||
|
export * from './util';
|
||||||
|
25
src/util.ts
25
src/util.ts
@ -1,6 +1,8 @@
|
|||||||
import * as Block from 'multiformats/block';
|
import * as Block from 'multiformats/block';
|
||||||
import { sha256 as hasher } from 'multiformats/hashes/sha2';
|
import { sha256 as hasher } from 'multiformats/hashes/sha2';
|
||||||
|
import assert from 'assert';
|
||||||
|
|
||||||
|
import { StdFee, parseCoins } from '@cosmjs/stargate';
|
||||||
import * as dagCBOR from '@ipld/dag-cbor';
|
import * as dagCBOR from '@ipld/dag-cbor';
|
||||||
import * as dagJSON from '@ipld/dag-json';
|
import * as dagJSON from '@ipld/dag-json';
|
||||||
|
|
||||||
@ -118,4 +120,27 @@ export class Util {
|
|||||||
|
|
||||||
return block.cid.toString();
|
return block.cid.toString();
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Get gas in proper format
|
||||||
|
*/
|
||||||
|
static parseGasAndFees (gas?: string, fees?: string): StdFee | undefined | number {
|
||||||
|
// If fees is not given or a number, treat it as a gas estimation multiplier
|
||||||
|
if (fees === null || fees === undefined) {
|
||||||
|
return undefined;
|
||||||
|
}
|
||||||
|
|
||||||
|
const isFeesANumber = !isNaN(Number(fees));
|
||||||
|
if (isFeesANumber) {
|
||||||
|
return Number(fees);
|
||||||
|
}
|
||||||
|
|
||||||
|
// If fees is not a gas estimation multiplier, gas is required
|
||||||
|
assert(gas, 'Invalid gas.');
|
||||||
|
|
||||||
|
return {
|
||||||
|
amount: parseCoins(String(fees)),
|
||||||
|
gas: String(gas)
|
||||||
|
};
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
Loading…
Reference in New Issue
Block a user