1- import Web3, {
2- FMT_NUMBER,
3- type EthExecutionAPI,
4- type SupportedProviders,
5- FMT_BYTES,
6- type Bytes,
7- } from 'web3';
81import { addHexPrefix, toBytes } from '@ethereumjs/util';
92import { execution } from '@remix-project/remix-lib';
10- import { toBigInt } from 'web3-utils';
113import { saveSettings } from '../actions';
4+ import { BrowserProvider, ethers, formatUnits, toNumber, TransactionReceipt, TransactionResponse } from 'ethers'
125
13- const web3 = new Web3();
6+ const provider = ethers.getDefaultProvider()
147
158export const shortenAddress = (address: string, etherBalance?: string) => {
169 const len = address.length;
@@ -29,12 +22,9 @@ async function pause() {
2922 });
3023}
3124
32- async function tryTillReceiptAvailable(txhash: Bytes ) {
25+ async function tryTillReceiptAvailable(txhash) {
3326 try {
34- const receipt = await web3.eth.getTransactionReceipt(txhash, {
35- number: FMT_NUMBER.NUMBER,
36- bytes: FMT_BYTES.HEX,
37- });
27+ const receipt: TransactionReceipt = await provider.getTransactionReceipt(txhash);
3828 if (receipt) {
3929 if (!receipt.to && !receipt.contractAddress) {
4030 // this is a contract creation and the receipt doesn't contain a contract address. we have to keep polling...
@@ -52,12 +42,9 @@ async function tryTillReceiptAvailable(txhash: Bytes) {
5242 return await tryTillReceiptAvailable(txhash);
5343}
5444
55- async function tryTillTxAvailable(txhash: Bytes ) {
45+ async function tryTillTxAvailable(txhash) {
5646 try {
57- const tx = await web3.eth.getTransaction(txhash, {
58- number: FMT_NUMBER.NUMBER,
59- bytes: FMT_BYTES.HEX,
60- });
47+ const tx: TransactionResponse = await provider.getTransaction(txhash, );
6148 if (tx?.blockHash) return tx;
6249 return tx;
6350 } catch (e) {
@@ -90,16 +77,13 @@ export class TxRunner {
9077 }, 30000);
9178 }
9279
93- setProvider(
94- provider: string | SupportedProviders<EthExecutionAPI> | undefined
95- ) {
96- web3.setProvider(provider);
80+ setProvider(provider) {
81+ new ethers.BrowserProvider(provider)
9782 }
9883
9984 getAccounts() {
10085 saveSettings({ isRequesting: true });
101- void web3.eth
102- .getAccounts()
86+ (provider as any).send("eth_requestAccounts", [])
10387 .then(async (accounts) => {
10488 const loadedAccounts: any = {};
10589 for (const account of accounts) {
@@ -115,17 +99,18 @@ export class TxRunner {
11599 }
116100
117101 async getBalanceInEther(address: string) {
118- const balance = await web3.eth .getBalance(address);
119- return Web3.utils.fromWei (balance.toString(10), 'ether');
102+ const balance = await provider .getBalance(address);
103+ return formatUnits (balance.toString(10), 'ether');
120104 }
121105
122106 async getGasPrice() {
123- return await web3.eth.getGasPrice();
107+ const { gasPrice } = await provider.getFeeData()
108+ return gasPrice;
124109 }
125110
126111 async runTx(tx: any, gasLimit: any, useCall: boolean) {
127112 if (useCall) {
128- const returnValue = await web3.eth. call({ ...tx, gas: gasLimit });
113+ const returnValue = await provider. call({ ...tx, gasLimit });
129114
130115 return toBytes(addHexPrefix(returnValue));
131116 }
@@ -147,8 +132,8 @@ export class TxRunner {
147132 txCopy.maxFeePerGas = Math.ceil(
148133 Number(
149134 (
150- toBigInt (network.lastBlock.baseFeePerGas) +
151- toBigInt (network.lastBlock.baseFeePerGas) / BigInt(3)
135+ BigInt (network.lastBlock.baseFeePerGas) +
136+ BigInt (network.lastBlock.baseFeePerGas) / BigInt(3)
152137 ).toString()
153138 )
154139 );
@@ -159,8 +144,8 @@ export class TxRunner {
159144 }
160145
161146 try {
162- const gasEstimation = await web3.eth .estimateGas(txCopy);
163- tx.gas = !gasEstimation ? gasLimit : gasEstimation;
147+ const gasEstimation = await provider .estimateGas(txCopy);
148+ tx.gasLimit = !gasEstimation ? gasLimit : gasEstimation;
164149 return await this._executeTx(tx, network);
165150 } catch (error) {
166151 console.log(error);
@@ -169,7 +154,8 @@ export class TxRunner {
169154 }
170155
171156 async detectNetwork() {
172- const id = Number(await web3.eth.net.getId());
157+ const { chainId } = await provider.getNetwork()
158+ const id = Number(chainId)
173159 let name = '';
174160 if (id === 1) name = 'Main';
175161 else if (id === 3) name = 'Ropsten';
@@ -180,7 +166,7 @@ export class TxRunner {
180166 else name = 'Custom';
181167
182168 if (id === 1) {
183- const block = await web3.eth .getBlock(0);
169+ const block = await provider .getBlock(0);
184170 if (block && block.hash !== this.mainNetGenesisHash) name = 'Custom';
185171 return {
186172 id,
@@ -205,18 +191,18 @@ export class TxRunner {
205191
206192 async _updateChainContext() {
207193 try {
208- const block = await web3.eth .getBlock('latest');
194+ const block = await provider .getBlock('latest');
209195 // we can't use the blockGasLimit cause the next blocks could have a lower limit : https://github.com/ethereum/remix/issues/506
210196 this.blockGasLimit = block?.gasLimit
211197 ? Math.floor(
212- Number(web3.utils. toNumber(block.gasLimit)) -
213- (5 * Number(web3.utils. toNumber(block.gasLimit))) / 1024
198+ Number(toNumber(block.gasLimit)) -
199+ (5 * Number(toNumber(block.gasLimit))) / 1024
214200 )
215- : web3.utils. toNumber(this.blockGasLimitDefault);
201+ : toNumber(this.blockGasLimitDefault);
216202 this.lastBlock = block;
217203 try {
218204 this.currentFork = execution.forkAt(
219- await web3.eth.net.getId() ,
205+ ( await provider.getNetwork()).chainId ,
220206 block.number
221207 );
222208 } catch (e) {
@@ -247,19 +233,16 @@ export class TxRunner {
247233
248234 let currentDateTime = new Date();
249235 try {
250- const { transactionHash } = await web3.eth.sendTransaction(
251- tx,
252- undefined,
253- { checkRevertBeforeSending: false, ignoreGasPricing: true }
254- );
255- const receipt = await tryTillReceiptAvailable(transactionHash);
256- tx = await tryTillTxAvailable(transactionHash);
236+ const signer = await (provider as BrowserProvider).getSigner(tx.from || 0);
237+ const { hash } = await signer.sendTransaction(tx);
238+ const receipt = await tryTillReceiptAvailable(hash);
239+ tx = await tryTillTxAvailable(hash);
257240
258241 currentDateTime = new Date();
259242 return {
260243 receipt,
261244 tx,
262- transactionHash: receipt ? receipt.transactionHash : null,
245+ transactionHash: receipt ? receipt.hash : null,
263246 };
264247 } catch (error: any) {
265248 console.log(
0 commit comments