我正在尝试使用Web3在Ropsten网络上增加Uniswap的流动性。
我的JavaScript代码如下:(async () => {
console.log("account: ", this.state.account);
const deployedContract = await new web3.eth.Contract(
UniswapRouter02Contract.abi,
"0x7a250d5630B4cF539739dF2C5dAcb4c659F2488D"
);
console.log(web3.currentProvider);
console.log("pair address ", this.state.pairAddress);
console.log(this.state);
const liq = await deployedContract.methods
.addLiquidity(
this.state.firstAddress,
this.state.secondAddress,
1000,
1000,
0,
0,
this.state.pairAddress,
200
)
.send(
{
from: this.state.account,
gas: "2000000"
},
function(error, transactionHash) {}
)
.on("error", error => {
console.log("my error", error.message);
});
//console.log("Events", pairCreated.events);
})();
My error:
Transaction has been reverted by the EVM: { "blockHash": "0xc4bcbfe7c4e6045d20b466f7eab2a7af1693cb3e11be7a197722855876554eaa", "blockNumber": 9707061, "contractAddress": null,
"cumulativeGasUsed": 4110545, "from": "0xe3a6752cf416bd9fb766b046782a21b8722bcc3c", "gasUsed": 23341,
"logsBloom": "0x00000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000", "status": false, "to": "0x7a250d5630b4cf539739df2c5dacb4c659f2488d", "transactionHash": "0xf23421d4a6cba30515e01e288893e8ecda482fccc1e3b969966187855298120c", "transactionIndex": 8, "events": {} }
我还有一个问题:我必须在参数"to"中输入的地址和截止日期是什么?在Uniswap addLiquidity功能?
功能:
function addLiquidity(
address tokenA,
address tokenB,
uint amountADesired,
uint amountBDesired,
uint amountAMin,
uint amountBMin,
address to,
uint deadline ) external returns (uint amountA, uint amountB, uint liquidity);
您在ropstenn .etherscan.io中的错误显示Expired,这可能意味着事务执行时间超过20分钟(假设您使用默认截止日期),因此失败。
参见下面的https://ethereum.stackexchange.com/questions/98678/fail-with-error-uniswapv2router-expired。
似乎是时间戳问题。
同样,对于v2,你要确保
- 你先调用approve方法
- 提供UTC时间戳,比当前时间早20分钟。在您的示例中,时间戳是200,这肯定会导致问题。将"200"替换为当前时间戳+ 20分钟
将Date.now()作为截止日期的参数而不是200。这解决了我的问题。实际上它需要一个日期,而你传递的是一个整数。