jcwallet/src/standards/ChainCommon.ts
2023-06-30 17:50:21 +08:00

29 lines
789 B
TypeScript

import Web3 from 'web3';
import { BN } from 'ethereumjs-util';
export const SAMPLE_GAS = 1000000;
export class ChainCommon {
private web3: Web3;
constructor(web3: Web3) {
this.web3 = web3;
}
public async checkEthEnough(gas: string, address?: string) {
address = address || jc.wallet.currentAccAddr;
let gasPrice = await this.web3.eth.getGasPrice();
console.log('gasPrice: ' + new BN(gasPrice));
let balance = await this.web3.eth.getBalance(address);
console.log('eth balance: ' + new BN(balance));
return new BN(balance).gte(new BN(gasPrice).mul(new BN(gas)));
}
public async getBalance(address?: string) {
address = address || jc.wallet.currentAccAddr;
let balance = await this.web3.eth.getBalance(address);
return balance;
}
}