Embarking on my journey into ethereum development, I am currently engrossed in crafting a basic script that facilitates swaps using web3.js.
To begin with, my web3 is establishing a connection to the HardHat forked server.
The first step involves setting up the contract for the Uniswap v2Router02 router
const uniSwapv2Contract = new web3.eth.Contract(JSON.parse(uniswapV2Router02.result), "0x7a250d5630B4cF539739dF2C5dAcb4c659F2488D");
Subsequently, when required, I trigger the following callback
const handleSwap = async () => {
const [currencyOne, currencyTwo] = pairName.split("/");
const tokenFrom = web3.utils.toChecksumAddress("0xc02aaa39b223fe8d0a0e5c4f27ead9083c756cc2"); // WETH
const tokenTo = web3.utils.toChecksumAddress("0x6982508145454ce325ddbe47a25d4ec3d2311933"); // PEPE
const path = [tokenFrom, tokenTo];
var data = uniSwapv2Contract.methods.swapExactETHForTokens(
web3.utils.toHex(1000000000),
path,
account,
web3.utils.toHex(Math.round(Date.now()/1000)+60*20),
);
var count = await web3.eth.getTransactionCount(account);
var rawTransaction = {
from: account,
to: web3.utils.toChecksumAddress("0x7a250d5630B4cF539739dF2C5dAcb4c659F2488D"),
data:data.encodeABI(),
gasPrice: '0x09184e72a000', // Customizable by the user during MetaMask confirmation.
// gas: '0x27101', // Customizable by the user during MetaMask confirmation.
gas: web3.utils.toHex(9999999999999),
gasLimit: '0x22000', // Customizable by the user during MetaMask confirmation.
value: "0xDE0B6B3A7640000", // 1 full
// nonce:web3.utils.toHex(100)
nonce: web3.utils.toHex(count)
};
const signedTransaction = await web3.eth.accounts.signTransaction(rawTransaction, "XXXX my private key XXXX"); // private key
const txHash = await web3.utils.sha3(signedTransaction.rawTransaction);
const transactionResult = await web3.eth.sendSignedTransaction(signedTransaction.rawTransaction).catch(error=>{
console.log(error);
});
}
After execution, I encounter the following error in the browser:
TransactionRevertInstructionError: Transaction has been reverted by the EVM
Hardhat presents more detailed information as follows:
Contract call: <UnrecognizedContract>
From: 0xdd11751cdd3f6eff01b1f6151b640685bfa5db4a
To: 0x7a250d5630b4cf539739df2c5dacb4c659f2488d
Value: 1 ETH
Error: Transaction reverted without a reason string
The ordinary transactions like transfers function correctly, but those involving "Data" generated by swapExactETHForTokens
fail.
I have cross-checked the contract hashes and downloaded ABI from etherscan.
A portion of the ETH has been converted to WETH.
In an attempt to resolve the issue, I created a fresh account and cleared the history in MetaMask where I monitor the results for transfers and swaps.
Even after running the server in verbose mode, limited insights were gained.
As a precautionary measure, I intend to write a script in Python to further investigate the problem.