根据take advantage函数,它将根据权限和您所指定的数量在外部契约中使用您的令牌
但是:
在实施ERC20时,是否有必要实施批准?把它用在Dex上?
以太坊的费用高得足以做一个简单的交换。如果我不执行它会发生什么?
### approve函数允许外部地址使用来自特定地址的token来代表它。如果你不实现它,那么transferFrom函数将不会工作,因为你总是需要批准一个特定的花费者的令牌使用。因此,转移令牌的唯一方法将是调用DEX不接受的函数“transfer”。dex使用transferFrom函数来代表地址与令牌进行交互。
###Dex合同有一个赌注功能:
function stakeTokens(uint256 _amount,address _token) public{
require(_amount>0,"Amount must be more than 0");
/*call transferFrom from erc20. transfer only works if it is being called from the wallet who owns the tokens. If we dont own the token we use transferFrom. We have to have abi to call the transferFrom. so we need IERC20 interface*/
// In order to interact with a contract we need abi
// IERC20(_token) initilaizes the contract
IERC20(_token).transferFrom(msg.sender,address(this),_amount);
// ... more logic down here
}
approve用于授权DEX代表令牌所有者进行不超过批准金额的转账。