I have a local testnet (represented by this Docker image) and I am trying to send some ERC20 tokens from one address to another.
I use the following HardHat script for this:
npx hardhat run --network localTestnet scripts/send-usdt-to-exchange.js
which executes this file:
async function main() {
const USDT = await ethers.getContractFactory("USDT");
const usdt = await USDT.attach(
"0xB816192c15160a2C1B4D032CDd7B1009583b21AF"
);
const amount = 1;
const gas = 1000000;
const exchange = "0x190FD61ED8fE0067f0f09EA992C1BF96209bab66";
const usdtSender = "0xDd1e8cC92AF9748193459ADDF910E1b96E88154D";
console.log("Approving the transfer...");
await usdt.approve(usdtSender, amount + gas);
console.log("Done");
console.log("Sending USDT...");
const result = await usdt.transferFrom(usdtSender, exchange, amount, { gasLimit: gas });
console.log("Done, result=", result);
}
main()
.then(() => process.exit(0))
.catch((error) => {
console.error(error);
process.exit(1);
});
A couple of minutes after I execute this script I can see in Blockscout that the transaction failed with Error: execution reverted
error.
I see some output in Blockscout under Raw Trace
.
[
{
"action": {
"callType": "call",
"from": "0x411167fefecad12da17f9063143706c39528aa28",
"gas": "0xEEC98",
"input": "0x23b872dd000000000000000000000000dd1e8cc92af9748193459addf910e1b96e88154d000000000000000000000000190fd61ed8fe0067f0f09ea992c1bf96209bab660000000000000000000000000000000000000000000000000000000000000001",
"to": "0xb816192c15160a2c1b4d032cdd7b1009583b21af",
"value": "0x0"
},
"error": "execution reverted",
"subtraces": 0,
"traceAddress": [],
"type": "call"
}
]
The USDT contract is deployed on the testnet using this script:
async function main() {
// We get the contract to deploy
const USDT = await ethers.getContractFactory("USDT");
const usdt = await USDT.deploy("0x190FD61ED8fE0067f0f09EA992C1BF96209bab66", "0xDd1e8cC92AF9748193459ADDF910E1b96E88154D");
console.log("USDT contract deployed to:", usdt.address);
}
main()
.then(() => process.exit(0))
.catch((error) => {
console.error(error);
process.exit(1);
});
The contract itself looks like this:
pragma solidity ^0.8.0;
import "./tokens/ERC20/ERC20.sol";
contract USDT is ERC20 {
constructor(address exchange, address usdtSender) ERC20("Test Tether", "tUSDT") {
_mint(exchange, 1000000000000000000000);
_mint(usdtSender, 1000000000000000000000);
}
}
Given that all of this happens on a local testnet, what can I do to diagnose execution reverted
error and how to fix it?
Update 1: I noticed a strange behavior.
Before executing send-usdt-to-exchange.js
, I add ETH to the usdtSender
account (0xDd1e8cC92AF9748193459ADDF910E1b96E88154D
) by opening the geth shell and running the following commands:
geth attach http://localhost:8178
web3.personal.unlockAccount('0x411167FeFecAD12Da17F9063143706C39528aa28',
'carsdrivefasterbecausetheyhavebrakes', 600);
eth.sendTransaction({
from: '0x411167FeFecAD12Da17F9063143706C39528aa28',
to: '0xDd1e8cC92AF9748193459ADDF910E1b96E88154D',
value: web3.toWei(1000, 'ether')});
This returns the transaction ID 0x76794f48a21518a3b3acfe820c2a57f88c188949054573be1805894bb9627471
and in BlockScout this transaction is marked as successful:
"Raw trace" in Blockscout looks like this:
[
{
"action": {
"callType": "call",
"from": "0x411167fefecad12da17f9063143706c39528aa28",
"gas": "0x0",
"input": "0x",
"to": "0xdd1e8cc92af9748193459addf910e1b96e88154d",
"value": "0x3635C9ADC5DEA00000"
},
"result": {
"gasUsed": "0x0",
"output": "0x"
},
"subtraces": 0,
"traceAddress": [],
"type": "call"
}
]
However, if I call
console.log("Determining the ETH balance of USDT sender...");
const ethBalance = await usdt.provider.getBalance(usdtAddress);
console.log("ETH balance:", ethBalance);
console.log("Done");
in send-usdt-to-exchange.js
(after the transaction is marked as successful in Blockscout) I get the output
Determining the ETH balance of USDT sender...
ETH balance: BigNumber { _hex: '0x00', _isBigNumber: true }
Done
For some reason, the ETH balance of the USDT sender is zero even though the transaction was successful.
Update 2: Inside the geth shell, eth.getBalance("0xDd1e8cC92AF9748193459ADDF910E1b96E88154D");
(ETH balance of the USDT sender account) returns 2e+21
.
Update 3: Looks like "execution reverted" error is triggered in instructions.go file in Go-Ethereum:
func opRevert(pc *uint64, interpreter *EVMInterpreter, scope *ScopeContext) ([]byte, error) {
offset, size := scope.Stack.pop(), scope.Stack.pop()
ret := scope.Memory.GetPtr(int64(offset.Uint64()), int64(size.Uint64()))
interpreter.returnData = ret
return ret, ErrExecutionReverted
}
This is a dirty hack that seems to work.
First, add a method to the contract which transfers funds without changing the allowance:
pragma solidity ^0.8.0;
import "./tokens/ERC20/ERC20.sol";
contract USDT is ERC20 {
constructor(address exchange, address usdtSender) ERC20("Test Tether", "tUSDT") {
_mint(exchange, 1000000000000000000000);
_mint(usdtSender, 1000000000000000000000);
}
function transferFromWithoutChangingAllowance(
address sender,
address recipient,
uint256 amount
) public returns (bool) {
_transfer(sender, recipient, amount);
return true;
}
}
Then, change the script:
async function main() {
const USDT = await ethers.getContractFactory("USDT");
const usdtAddress = "0xa682a5972D1A8175E2431B26586F486bBa161A11";
const usdt = await USDT.attach(usdtAddress);
const amount = 1;
const exchange = "0x190FD61ED8fE0067f0f09EA992C1BF96209bab66";
const usdtSender = "0xDd1e8cC92AF9748193459ADDF910E1b96E88154D";
console.log("Determining the ETH balance of USDT sender...");
const ethBalance = await usdt.provider.getBalance(usdtSender);
console.log("ETH balance:", ethBalance);
console.log("Done");
console.log("Approving the transfer...");
const approveResult = await usdt.approve(usdtSender, amount+1);
console.log("Done, result: ", approveResult);
console.log("Sending USDT...");
const result = await usdt.transferFromWithoutChangingAllowance(usdtSender, exchange, amount, { gasLimit: 1000000 });
console.log("Done, result=", result);
}
main()
.then(() => process.exit(0))
.catch((error) => {
console.error(error);
process.exit(1);
});