Skip to content

Commit 0d7f9af

Browse files
committed
feat (bridge): estimate gasfee
1 parent 488c898 commit 0d7f9af

File tree

3 files changed

+64
-0
lines changed

3 files changed

+64
-0
lines changed

src/localServer/define.d.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -263,6 +263,7 @@ declare type WorkerCommand =
263263
| "redeemAirdrop"
264264
| "redeemSilentPassPassport"
265265
| "bridge"
266+
| "estimateGasForBridge"
266267

267268
type SINodesSortby = 'CUSTOMER_REVIEW'|'TOTAL_ONLINE_TIME'|
268269
'STORAGE_PRICE_LOW'|'STORAGE_PRICE_HIGH'|'OUTBOUND_PRICE_HIGH'|'OUTBOUND_PRICE_LOW'

src/localServer/workers/encrypt.ts

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -852,6 +852,10 @@ const processCmd = async (cmd: worker_command) => {
852852
return bridge(cmd);
853853
}
854854

855+
case "estimateGasForBridge": {
856+
return estimateGasForBridge(cmd);
857+
}
858+
855859
default: {
856860
cmd.err = "INVALID_COMMAND";
857861
responseChannel.postMessage(JSON.stringify(cmd));

src/localServer/workers/utilities/web3Util.ts

Lines changed: 59 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2675,6 +2675,7 @@ const CONET_guardian_purchase_Receiving_Address = (networkName) => {
26752675

26762676
const parseEther = (_ether, tokenName) => {
26772677
const ether = typeof (_ether) === 'number' ? _ether.toFixed(8) : _ether
2678+
26782679
switch (tokenName) {
26792680
case 'arb_usdt':
26802681
case 'usdt': {
@@ -3059,6 +3060,7 @@ const transferAssetToCONET_wallet = (privateKey, token, transferNumber, toAddr)
30593060
const provide = new ethers.JsonRpcProvider(getNetwork(token.name))
30603061
const wallet = new ethers.Wallet(privateKey, provide)
30613062
const smartContractAddr = getAssetERC20Address(token.name)
3063+
30623064
if (smartContractAddr) {
30633065
const transferObj = new ethers.Contract(smartContractAddr, blast_CNTPAbi, wallet)
30643066
const amount = parseEther(transferNumber, token.name)
@@ -4018,6 +4020,63 @@ const bridge = async (cmd: any) => {
40184020
cmd.data.push(error?.reason);
40194021
return returnUUIDChannel(cmd);
40204022
}
4023+
}
4024+
4025+
const estimateGasForBridge = async (cmd: any) => {
4026+
const walletAddress = cmd.data[0];
4027+
const tokenName = cmd.data[1];
4028+
const amount = cmd.data[2];
4029+
4030+
if (!CoNET_Data || !walletAddress || !tokenName || !amount) {
4031+
cmd.err = "FAILURE";
4032+
return returnUUIDChannel(cmd);
4033+
}
4034+
4035+
const profile = CoNET_Data.profiles?.find((profile) => profile.keyID === walletAddress);
4036+
4037+
if (!profile) {
4038+
cmd.err = "FAILURE";
4039+
return returnUUIDChannel(cmd);
4040+
}
4041+
4042+
if(!profile.tokens){
4043+
cmd.err = "FAILURE";
4044+
return returnUUIDChannel(cmd);
4045+
}
40214046

4047+
const provider = new ethers.JsonRpcProvider(getNetwork(tokenName));
4048+
const wallet = new ethers.Wallet(profile.privateKeyArmor, provider);
40224049

4050+
const tx = {
4051+
to: ethTreasuryContractAddress,
4052+
value: parseEther(amount, tokenName),
4053+
};
4054+
4055+
let _fee;
4056+
try {
4057+
try {
4058+
_fee = await wallet.estimateGas(tx)
4059+
} catch (error) {
4060+
throw new Error('Getting gas failed. Check your ETH balance and try again!');
4061+
}
4062+
4063+
const Fee = await provider.getFeeData();
4064+
const gasPrice = ethers.formatUnits(Fee.gasPrice, "gwei");
4065+
const fee = parseFloat(ethers.formatEther(_fee * Fee.gasPrice)).toFixed(8);
4066+
4067+
cmd.data = [fee, gasPrice];
4068+
4069+
return returnUUIDChannel(cmd);
4070+
} catch (error: any) {
4071+
cmd.err = "FAILURE";
4072+
4073+
if (!error?.reason) {
4074+
error.reason =
4075+
"Getting gas failed. Check your ETH balance and try again!";
4076+
}
4077+
4078+
cmd.data = []
4079+
cmd.data.push(error?.reason);
4080+
return returnUUIDChannel(cmd);
4081+
}
40234082
}

0 commit comments

Comments
 (0)