今天在学习uni的flashswap,代码如下:
contract Flash_swap_arb is Ownable,IUniswapV2Callee{
using SafeMath for uint256;
address public my_to_address = 0x194A025C90D384539D05094eC73F21EC5b81b277;
address public factory;
address payable public uni_router_add = 0x7a250d5630B4cF539739dF2C5dAcb4c659F2488D;
IUniswapV2Router02 public uni_router;
//bytes public data;
address public lastTokenBorrow;
uint public lastAmount;
IUniswapV2Router02 public sushiRouter;
constructor() public{
factory = 0x5C69bEe701ef814a2B6a3EDD4B1652CB9cc5aA6f;
uni_router = IUniswapV2Router02(uni_router_add);
sushiRouter = IUniswapV2Router02(0x1b02dA8Cb0d097eB8D57A175b88c7D8b47997506);
}
function flashswap(uint amount0,uint amount1) public {
address token0 = 0xc778417E063141139Fce010982780140Aa0cD5Ab;//weth address on Rinkeby
address token1 = 0x1f9840a85d5aF5bf1D1762F925BDADdC4201F984;//uni address on Rinkeby
// address token1 = 0xc7AD46e0b8a400Bb3C915120d284AafbA8fc4735;//dai
address pairAddress = IUniswapV2Factory(factory).getPair(token0,token1);
require(pairAddress != address(0),'There is no such pool');
IUniswapV2Pair(pairAddress).swap(
amount0,amount1,address(this),
bytes('not empty')
);
}
function uniswapV2Call(address sender,uint amount0,uint amount1, bytes calldata _data)external override{
address[] memory path = new address[](2);
uint amountToken = amount0 == 0? amount1:amount0;
address token0 = IUniswapV2Pair(msg.sender).token0();
address token1 = IUniswapV2Pair(msg.sender).token1();
address pairAddress = IUniswapV2Factory(factory).getPair(token0,token1);
require(msg.sender == UniswapV2Library.pairFor(factory,token0,token1),'Unauthorized');
// require(sender == address(this),'only this contract can arb');
path[0] = amount0 == 0? token1:token0;
path[1] = amount0 == 0? token0:token1;
IERC20 token = IERC20(amount0 == 0 ? token1 : token0);
token.approve(address(sushiRouter), amountToken);
lastTokenBorrow = token0;
lastAmount = amount0;
uint deadline = block.timestamp + 300;
uint amountRequired = UniswapV2Library.getAmountsIn(factory,amount1,path)[0];
uint amountReceived = sushiRouter.swapExactTokensForTokens(
amountToken,
amountRequired/10,
path,
msg.sender,
deadline
)[1];
require(amountReceived > amountRequired,'No profit to earn');
IERC20(token0).transfer(pairAddress,amountRequired);
IERC20(token0).transfer(tx.origin, amountReceived - amountRequired);
// IERC20(token1).transfer(pairAddress,amountToken);
}
然后我调用flashswap函数,传递的参数是 amount0=0 ,amount1=10000000000000000
,
执行,就报错:
Gas estimation errored with the following message (see below). The
transaction execution will likely fail. Do you want to force sending?
execution
reverted: Uni::_transferTokens: transfer amount exceeds balance {
"originalError": { "code": 3, "data":
"0x08c379a000000000000000000000000000000000000000000000000000000000000000200000000000000000000000000000000000000000000000000000000000000035556e693a3a5f7472616e73666572546f6b656e733a207472616e7366657220616d6f756e7420657863656564732062616c616e63650000000000000000000000",
"message": "execution reverted: Uni::_transferTokens: transfer amount
exceeds balance" }
这是咋回事? 根据这个提示,转账数量超过余额,这个余额指的是哪的余额?