contract TestCall {
...
function call(address payable _to, uint256 _value, bytes calldata _data) external onlyOwner payable returns (bytes memory) {
require(_to != address(0));
(bool _success, bytes memory _result) = _to.call{value: _value}(_data);
require(_success);
return _result;
}
...
}
我在goerli测试网中部署了上述合约。并使用以下js代码通过调用TestCall.call
函数将token2交换为token1。
import { BigNumber, Contract, providers, Wallet, utils } from "ethers";
const provider = new providers.StaticJsonRpcProvider(ETHEREUM_RPC_URL);
const wallet = new Wallet(PRIVATE_KEY, provider); // The account which deployed the contract
const signingAddr = await wallet.getAddress();
const uniswapInterface = new Contract(UNISWAP_ROUTERV2_ADDRESS, UNISWAP_ROUTERV2_ABI, wallet);
const testCallContract = new Contract(TESTCALL_CONTRACT_ADDRESS, TESTCALL_ABI, wallet);
const nonce = await wallet.getTransactionCount();
const gasPriceGwei = "100"; // in GWEI
const gasPriceWei = utils.parseUnits(gasPriceGwei, "gwei");
const amountIn = BigNumber.from(1000000000000);
let amountOut = await uniswapInterface.getAmountsOut(amountIn, [token2Addr, token1Addr]);
amountOut = amountOut[1];
const amountOutMin = amountOut.mul(0.998 * 1000).div(1000);
const params = {
"amountIn": amountIn,
"amountOutMin": amountOutMin,
"path": [token2Addr, token1Addr],
"to": signingAddr,
"deadline": Date.now() + 1000*60*10,
};
console.log("params", params)
/// This can run successfully.
// const tx = await uniswapInterface.swapExactTokensForTokens(...Object.values(params), {
// "value": BigNumber.from(0),
// "gasLimit": utils.hexlify(1000000),
// "gasPrice": gasPriceWei,
// "nonce": nonce
// });
// const receipt = await tx.wait()
// console.log(receipt)
const tx = await uniswapInterface.populateTransaction.swapExactTokensForTokens(...Object.values(params));
console.log(tx);
if (tx === undefined || tx.data === undefined) throw new Error("ERROR")
const transaction = await testCallContract.call(UNISWAP_ROUTERV2_ADDRESS, BigNumber.from(0), tx.data, {
"value": BigNumber.from(0),
"gasLimit": utils.hexlify(1000000),
"gasPrice": gasPriceWei,
"nonce": nonce,
});
console.log("transaction", JSON.stringify(transaction))
const receipt = await transaction.wait()
console.log(receipt)
在错误信息中找不到有用的信息:
Error encountered during contract execution [Reverted]
使用swapExactETHForTokens
运行成功:
const amountInEther = '0.01';
const amountIn = utils.parseEther(amountInEther);
let amountOut = await uniswapInterface.getAmountsOut(amountIn, [WETH_ADDRESS, token2Addr]);
console.log(amountOut);
amountOut = amountOut[1];
const amountOutMin = amountOut.mul(0.998 * 1000).div(1000);
const params = {
"amountOutMin": amountOutMin,
"path": [WETH_ADDRESS, token2Addr],
"to": signingAddr,
"deadline": Date.now() + 1000*60*10,
};
const tx1 = await uniswapInterface.populateTransaction.swapExactETHForTokens(...Object.values(params), {"value": amountIn});
console.log(tx1);
if (tx1 === undefined || tx1.data === undefined) throw new Error("ERROR");
const transaction = await bundleExecutorContract.call(UNISWAP_ROUTERV2_ADDRESS, BigNumber.from(amountIn), tx1.data, {
"value": amountIn,
"gasLimit": utils.hexlify(1000000),
"gasPrice": gasPriceWei,
"nonce": nonce,
});
console.log("transaction", JSON.stringify(transaction))
const receipt = await transaction.wait()
console.log(receipt)
是否有某种方法来调试部署在测试网络中的稳定性代码?我搜索并找到了hardhat,但不知道如何在这种情况下使用它。
我找到了修复:
-
在合同代码中,为UNISWAP_ROUTERV2_ADDRESS批准足够的token2。并发送足够的令牌TESTCALL_CONTRACT_ADDRESS .
-
由于swapExactTokensForTokens是不可支付的,在js代码中,将value设置为0。
"value": BigNumber.from(0),