fix(hdnode):add default gasLimit and gasPrice

dev
neeboo 5 years ago
parent 1a0a5300c7
commit d9fa1c5875
  1. 41
      packages/harmony-account/src/hdnode.ts
  2. 17
      packages/harmony-core/src/truffleProvider.ts

@ -58,6 +58,8 @@ export class HDNode {
private addressCount: number; private addressCount: number;
private addresses: string[]; private addresses: string[];
private wallets: WalletsInterfaces; private wallets: WalletsInterfaces;
private gasLimit: string;
private gasPrice: string;
constructor( constructor(
provider: string | HttpProvider | WSProvider = 'http://localhost:9500', provider: string | HttpProvider | WSProvider = 'http://localhost:9500',
@ -66,6 +68,8 @@ export class HDNode {
addressCount: number = 1, addressCount: number = 1,
chainType: ChainType = ChainType.Harmony, chainType: ChainType = ChainType.Harmony,
chainId: ChainID = ChainID.Default, chainId: ChainID = ChainID.Default,
gasLimit = '1000000',
gasPrice = '2000000000',
) { ) {
this.provider = this.setProvider(provider); this.provider = this.setProvider(provider);
this.messenger = new Messenger(this.provider, chainType, chainId); this.messenger = new Messenger(this.provider, chainType, chainId);
@ -76,6 +80,8 @@ export class HDNode {
this.index = index; this.index = index;
this.addressCount = addressCount; this.addressCount = addressCount;
this.getHdWallet(menmonic || HDNode.generateMnemonic()); this.getHdWallet(menmonic || HDNode.generateMnemonic());
this.gasLimit = gasLimit;
this.gasPrice = gasPrice;
} }
normalizePrivateKeys(mnemonic: string | string[]) { normalizePrivateKeys(mnemonic: string | string[]) {
@ -155,16 +161,31 @@ export class HDNode {
); );
const to: string = txParams.to ? getAddress(txParams.to).checksum : '0x'; const to: string = txParams.to ? getAddress(txParams.to).checksum : '0x';
const gasLimit =
txParams.gas !== undefined && isHex(txParams.gas) let gasLimit = new Unit('0').asWei().toWei();
? // ? new Unit(hexToNumber(txParams.gas)).asWei().toWei()
new Unit('1000000').asWei().toWei() if (txParams.gas !== undefined && isHex(txParams.gas)) {
: new Unit('0').asWei().toWei(); gasLimit = new BN(hexToNumber(txParams.gas)).lt(new BN(this.gasLimit))
const gasPrice = ? new Unit(hexToNumber(txParams.gas)).asWei().toWei()
txParams.gasPrice !== undefined && isHex(txParams.gasPrice) : new Unit(this.gasLimit).asWei().toWei();
? // ? new Unit(txParams.gasPrice).asWei().toWei() }
new Unit('2').asGwei().toWei() if (txParams.gasLimit !== undefined && isHex(txParams.gasLimit)) {
: new Unit('0').asWei().toWei(); gasLimit = new BN(hexToNumber(txParams.gasLimit)).lt(
new BN(this.gasLimit),
)
? new Unit(hexToNumber(txParams.gasLimit)).asWei().toWei()
: new Unit(this.gasLimit).asWei().toWei();
}
let gasPrice = new Unit('0').asWei().toWei();
if (txParams.gasPrice !== undefined && isHex(txParams.gasPrice)) {
gasPrice = new BN(hexToNumber(txParams.gasPrice)).lt(
new BN(this.gasPrice),
)
? new Unit(hexToNumber(txParams.gasPrice)).asWei().toWei()
: new Unit(this.gasPrice).asWei().toWei();
}
const value = const value =
txParams.value !== undefined && isHex(txParams.value) txParams.value !== undefined && isHex(txParams.value)
? txParams.value ? txParams.value

@ -20,8 +20,23 @@ export class TruffleProvider extends HDNode {
constructor( constructor(
provider: string | HttpProvider | WSProvider = 'http://localhost:9500', provider: string | HttpProvider | WSProvider = 'http://localhost:9500',
menmonic?: string, menmonic?: string,
index: number = 0,
addressCount: number = 1,
chainType: ChainType = ChainType.Harmony,
chainId: ChainID = ChainID.Default,
gasLimit = '1000000',
gasPrice = '2000000000',
) { ) {
super(provider, menmonic, 0, 1, ChainType.Harmony, ChainID.Default); super(
provider,
menmonic,
index,
addressCount,
chainType,
chainId,
gasLimit,
gasPrice,
);
} }
async send(...args: [RPCRequestPayload<any>, any]) { async send(...args: [RPCRequestPayload<any>, any]) {
const {newArgs, id, params, newMethod, callback} = this.resolveArgs( const {newArgs, id, params, newMethod, callback} = this.resolveArgs(

Loading…
Cancel
Save