-
Notifications
You must be signed in to change notification settings - Fork 1.2k
remove web3js dependency #6409
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
remove web3js dependency #6409
Changes from 69 commits
a6158ac
5a2d91c
c1609ca
c17fe4e
417e95a
d91c3a4
871ddb0
68e8274
b29df25
0f2ae2e
1b9a2eb
f20341f
d935b7d
7c73820
7ab29a2
cb58824
78caa8b
c9642a0
6c0defc
9dfab81
f3edccc
9f8e694
1b0d6cf
324d6f0
9a0eb81
6503f9a
8ff5bee
ccc58cf
cf88fdd
b2516e8
d803a97
f415e0c
fffa9fc
174f436
6958e59
2916d2e
3ac5228
73500de
2659e1b
64a547e
fb0df90
1d2e420
17e9b24
e188701
1655160
a8c8d34
a9acba3
cf32e82
e54a10c
b9a08d9
8da346d
44ee764
37ddec4
e527a9f
df298f5
6571f90
8a90002
fd91357
8469e17
a4e98ee
f4218bb
7924b00
5c761e0
39e2a95
f337f6a
89d5af2
2cc84b8
fe2f676
66e5c74
d9fefd2
f80be91
18fb733
9e88621
c53ce90
7b8c79d
ca2acf5
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -1,16 +1,9 @@ | ||
| import Web3, { | ||
| FMT_NUMBER, | ||
| type EthExecutionAPI, | ||
| type SupportedProviders, | ||
| FMT_BYTES, | ||
| type Bytes, | ||
| } from 'web3'; | ||
| import { addHexPrefix, toBytes } from '@ethereumjs/util'; | ||
| import { execution } from '@remix-project/remix-lib'; | ||
| import { toBigInt } from 'web3-utils'; | ||
| import { saveSettings } from '../actions'; | ||
| import { BrowserProvider, ethers, formatUnits, toNumber, TransactionReceipt, TransactionResponse } from 'ethers' | ||
|
|
||
| const web3 = new Web3(); | ||
| const provider = ethers.getDefaultProvider() | ||
|
|
||
| export const shortenAddress = (address: string, etherBalance?: string) => { | ||
| const len = address.length; | ||
|
|
@@ -29,12 +22,9 @@ async function pause() { | |
| }); | ||
| } | ||
|
|
||
| async function tryTillReceiptAvailable(txhash: Bytes) { | ||
| async function tryTillReceiptAvailable(txhash) { | ||
| try { | ||
| const receipt = await web3.eth.getTransactionReceipt(txhash, { | ||
| number: FMT_NUMBER.NUMBER, | ||
| bytes: FMT_BYTES.HEX, | ||
| }); | ||
| const receipt: TransactionReceipt = await provider.getTransactionReceipt(txhash); | ||
| if (receipt) { | ||
| if (!receipt.to && !receipt.contractAddress) { | ||
| // 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) { | |
| return await tryTillReceiptAvailable(txhash); | ||
| } | ||
|
|
||
| async function tryTillTxAvailable(txhash: Bytes) { | ||
| async function tryTillTxAvailable(txhash) { | ||
| try { | ||
| const tx = await web3.eth.getTransaction(txhash, { | ||
| number: FMT_NUMBER.NUMBER, | ||
| bytes: FMT_BYTES.HEX, | ||
| }); | ||
| const tx: TransactionResponse = await provider.getTransaction(txhash, ); | ||
| if (tx?.blockHash) return tx; | ||
| return tx; | ||
| } catch (e) { | ||
|
|
@@ -90,16 +77,13 @@ export class TxRunner { | |
| }, 30000); | ||
| } | ||
|
|
||
| setProvider( | ||
| provider: string | SupportedProviders<EthExecutionAPI> | undefined | ||
| ) { | ||
| web3.setProvider(provider); | ||
| setProvider(provider) { | ||
| new ethers.BrowserProvider(provider) | ||
| } | ||
|
|
||
| getAccounts() { | ||
| saveSettings({ isRequesting: true }); | ||
| void web3.eth | ||
| .getAccounts() | ||
| (provider as any).send("eth_requestAccounts", []) | ||
| .then(async (accounts) => { | ||
| const loadedAccounts: any = {}; | ||
| for (const account of accounts) { | ||
|
|
@@ -115,17 +99,18 @@ export class TxRunner { | |
| } | ||
|
|
||
| async getBalanceInEther(address: string) { | ||
| const balance = await web3.eth.getBalance(address); | ||
| return Web3.utils.fromWei(balance.toString(10), 'ether'); | ||
| const balance = await provider.getBalance(address); | ||
| return formatUnits(balance.toString(10), 'ether'); | ||
|
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. same here.
Collaborator
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. yes, formatUnits https://docs.ethers.org/v6/api/utils/#formatUnits works with that |
||
| } | ||
|
|
||
| async getGasPrice() { | ||
| return await web3.eth.getGasPrice(); | ||
| const { gasPrice } = await provider.getFeeData() | ||
| return gasPrice; | ||
| } | ||
|
|
||
| async runTx(tx: any, gasLimit: any, useCall: boolean) { | ||
| if (useCall) { | ||
| const returnValue = await web3.eth.call({ ...tx, gas: gasLimit }); | ||
| const returnValue = await provider.call({ ...tx, gasLimit }); | ||
|
|
||
| return toBytes(addHexPrefix(returnValue)); | ||
| } | ||
|
|
@@ -147,8 +132,8 @@ export class TxRunner { | |
| txCopy.maxFeePerGas = Math.ceil( | ||
| Number( | ||
| ( | ||
| toBigInt(network.lastBlock.baseFeePerGas) + | ||
| toBigInt(network.lastBlock.baseFeePerGas) / BigInt(3) | ||
| BigInt(network.lastBlock.baseFeePerGas) + | ||
| BigInt(network.lastBlock.baseFeePerGas) / BigInt(3) | ||
| ).toString() | ||
| ) | ||
| ); | ||
|
|
@@ -159,8 +144,8 @@ export class TxRunner { | |
| } | ||
|
|
||
| try { | ||
| const gasEstimation = await web3.eth.estimateGas(txCopy); | ||
| tx.gas = !gasEstimation ? gasLimit : gasEstimation; | ||
| const gasEstimation = await provider.estimateGas(txCopy); | ||
| tx.gasLimit = !gasEstimation ? gasLimit : gasEstimation; | ||
| return await this._executeTx(tx, network); | ||
| } catch (error) { | ||
| console.log(error); | ||
|
|
@@ -169,7 +154,8 @@ export class TxRunner { | |
| } | ||
|
|
||
| async detectNetwork() { | ||
| const id = Number(await web3.eth.net.getId()); | ||
| const { chainId } = await provider.getNetwork() | ||
| const id = Number(chainId) | ||
| let name = ''; | ||
| if (id === 1) name = 'Main'; | ||
| else if (id === 3) name = 'Ropsten'; | ||
|
|
@@ -180,7 +166,7 @@ export class TxRunner { | |
| else name = 'Custom'; | ||
|
|
||
| if (id === 1) { | ||
| const block = await web3.eth.getBlock(0); | ||
| const block = await provider.getBlock(0); | ||
| if (block && block.hash !== this.mainNetGenesisHash) name = 'Custom'; | ||
| return { | ||
| id, | ||
|
|
@@ -205,18 +191,18 @@ export class TxRunner { | |
|
|
||
| async _updateChainContext() { | ||
| try { | ||
| const block = await web3.eth.getBlock('latest'); | ||
| const block = await provider.getBlock('latest'); | ||
| // we can't use the blockGasLimit cause the next blocks could have a lower limit : https://github.com/ethereum/remix/issues/506 | ||
| this.blockGasLimit = block?.gasLimit | ||
| ? Math.floor( | ||
| Number(web3.utils.toNumber(block.gasLimit)) - | ||
| (5 * Number(web3.utils.toNumber(block.gasLimit))) / 1024 | ||
| Number(toNumber(block.gasLimit)) - | ||
| (5 * Number(toNumber(block.gasLimit))) / 1024 | ||
| ) | ||
| : web3.utils.toNumber(this.blockGasLimitDefault); | ||
| : toNumber(this.blockGasLimitDefault); | ||
| this.lastBlock = block; | ||
| try { | ||
| this.currentFork = execution.forkAt( | ||
| await web3.eth.net.getId(), | ||
| (await provider.getNetwork()).chainId, | ||
| block.number | ||
| ); | ||
| } catch (e) { | ||
|
|
@@ -247,19 +233,16 @@ export class TxRunner { | |
|
|
||
| let currentDateTime = new Date(); | ||
| try { | ||
| const { transactionHash } = await web3.eth.sendTransaction( | ||
| tx, | ||
| undefined, | ||
| { checkRevertBeforeSending: false, ignoreGasPricing: true } | ||
| ); | ||
| const receipt = await tryTillReceiptAvailable(transactionHash); | ||
| tx = await tryTillTxAvailable(transactionHash); | ||
| const signer = await (provider as BrowserProvider).getSigner(); | ||
|
||
| const { hash } = await signer.sendTransaction(tx); | ||
| const receipt = await tryTillReceiptAvailable(hash); | ||
| tx = await tryTillTxAvailable(hash); | ||
|
|
||
| currentDateTime = new Date(); | ||
| return { | ||
| receipt, | ||
| tx, | ||
| transactionHash: receipt ? receipt.transactionHash : null, | ||
| transactionHash: receipt ? receipt.hash : null, | ||
| }; | ||
| } catch (error: any) { | ||
| console.log( | ||
|
|
||
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
did you make sure the input types ar ethe same?