Skip to content

Commit

Permalink
added support for dynamic chain id
Browse files Browse the repository at this point in the history
  • Loading branch information
pk910 committed Nov 18, 2022
1 parent 898a554 commit 1550f8b
Showing 2 changed files with 18 additions and 6 deletions.
4 changes: 2 additions & 2 deletions src/common/FaucetConfig.ts
Original file line number Diff line number Diff line change
@@ -92,7 +92,7 @@ export interface IFaucetConfig {

ethRpcHost: string; // ETH execution layer RPC host
ethWalletKey: string; // faucet wallet private key
ethChainId: number; // ETH chain id
ethChainId: number | null; // ETH chain id
ethTxGasLimit: number; // transaction gas limit (wei)
ethTxMaxFee: number; // max transaction gas fee
ethTxPrioFee: number; // max transaction priority fee
@@ -233,7 +233,7 @@ let defaultConfig: IFaucetConfig = {
denyNewSessions: false,
ethRpcHost: "http://127.0.0.1:8545/",
ethWalletKey: "fc2d0a2d823f90e0599e1e9d9202204e42a5ed388000ab565a34e7cbb566274b",
ethChainId: 1337802,
ethChainId: null,
ethTxGasLimit: 21000,
ethTxMaxFee: 1800000000,
ethTxPrioFee: 800000000,
20 changes: 16 additions & 4 deletions src/services/EthWeb3Manager.ts
Original file line number Diff line number Diff line change
@@ -102,10 +102,9 @@ export class EthWeb3Manager {

public constructor() {
this.startWeb3();
this.chainCommon = EthCom.default.forCustomChain('mainnet', {
networkId: faucetConfig.ethChainId,
chainId: faucetConfig.ethChainId,
}, 'london');
if(typeof faucetConfig.ethChainId === "number")
this.initChainCommon(faucetConfig.ethChainId);

this.walletKey = Buffer.from(faucetConfig.ethWalletKey, "hex");
this.walletAddr = EthUtil.toChecksumAddress("0x"+EthUtil.privateToAddress(this.walletKey).toString("hex"));

@@ -123,6 +122,15 @@ export class EthWeb3Manager {
});
}

private initChainCommon(chainId: number) {
if(this.chainCommon && this.chainCommon.chainIdBN().toNumber() === chainId)
return;
this.chainCommon = EthCom.default.forCustomChain('mainnet', {
networkId: chainId,
chainId: chainId,
}, 'london');
}

private startWeb3() {
let provider: any;
if(faucetConfig.ethRpcHost.match(/^wss?:\/\//))
@@ -161,19 +169,23 @@ export class EthWeb3Manager {

private loadWalletState(): Promise<void> {
this.lastWalletRefresh = Math.floor(new Date().getTime() / 1000);
let chainIdPromise = typeof faucetConfig.ethChainId === "number" ? Promise.resolve(faucetConfig.ethChainId) : this.web3.eth.getChainId();
return Promise.all([
this.web3.eth.getBalance(this.walletAddr, "pending"),
this.web3.eth.getTransactionCount(this.walletAddr, "pending"),
chainIdPromise,
]).catch((ex) => {
if(ex.toString().match(/"pending" is not yet supported/)) {
return Promise.all([
this.web3.eth.getBalance(this.walletAddr),
this.web3.eth.getTransactionCount(this.walletAddr),
chainIdPromise,
]);
}
else
throw ex;
}).then((res) => {
this.initChainCommon(res[2]);
this.walletState = {
balance: parseInt(res[0]),
nonce: res[1],

0 comments on commit 1550f8b

Please sign in to comment.