我希望接受稳定的资金转移,目前有独立的业务来处理支付,但我担心这会导致错误的交互、资金损失和不正确的执行:
function depositDAI() external payable {
require(msg.value >= amount, "Insufficient funds");
transferFunds(DAI, 10**18);
}
function depositUSDC() external payable {
require(msg.value >= amount, "Insufficient funds");
transferFunds(USDC, 10**6);
}
function depositUSDT() external payable {
require(msg.value >= amount, "Insufficient funds");
transferFunds(USDT, 10**6);
}是否可以在单个函数中处理这些操作?我想查查:
amount of ERC20是充分的发布于 2021-10-27 08:04:42
因此,让我们从这里开始,如果您不想接收ETH,您的函数就不必是payable。
对于第1和第2点,这段代码将有助于:
interface ERC20 {
function transferFrom(address sender, address recipient, uint256 amount) public returns (bool);
}
contract Name {
mapping(address -> bool) supportedStableCoins;
mapping(address -> uint) minAmauntForSuportedStableCoin;
function addCoin(address _coinAddress, uint _minAmount) external {
require(supportedStableCoins[_coinAddress] == true, "Already added");
supportedStableCoins[_coinAddress] = true;
minAmauntForSuportedStableCoin[_coinAddress] = _minAmount;
}
function deposit(address _coinAddress, uint _amount) external {
require(supportedStableCoins[_coinAddress] == true)
require(minAmauntForSuportedStableCoin[_coinAddress] <= _amount);
require(ERC20(_coinAddress).transferFrom(msg.sender, address(this), _amount));
}注1:为了执行transferFrom,消息发送方首先必须让approved将_amount传递给契约。
注2:您必须有constructor和addCoin函数的一些修饰符。你不希望每个人都给你的合同加硬币。
关于第3点,如果您提供了前端功能来进行事务处理,那么所有的事情都会被排除在外。
https://ethereum.stackexchange.com/questions/112308
复制相似问题