我有一个ERC20令牌:
// SPDX-License-Identifier: MIT
pragma solidity ^0.8.9;
import "@openzeppelin/contracts/token/ERC20/IERC20.sol";
contract Token is ERC20 {
constructor(string memory _name, string memory _symbol, uint256 _initialSupply)
ERC20(_name, _symbol)
{
_mint(msg.sender, _initialSupply);
}
}我只想将此令牌传递给另一个智能契约& ContractA &使用addToContract()函数更新其变量:
// SPDX-License-Identifier: MIT
pragma solidity ^0.8.9;
import "@openzeppelin/contracts/utils/math/SafeMath.sol";
import "@openzeppelin/contracts/token/ERC20/IERC20.sol";
contract ContractA {
using SafeMath for uint256;
IERC20 immutable private _token;
mapping(address => uint256) private _balance;
constructor(address token) {
require(token != address(0x0));
_token = IERC20(token);
}
function addToContract(uint256 amount) public {
_token.transferFrom(msg.sender, address(this), amount);
_balance[msg.sender].add(amount);
}
function withdrawFromContract(uint256 amount) public {
require(_balance[msg.sender] >= amount, "Insufficient Stake");
_token.transfer(msg.sender, amount);
_balance[msg.sender].sub(amount);
}
}我希望我想要达到的目标是清楚的。为了简单地传输令牌,用户可以调用token.transfer(),但我也希望在此之后更新变量_balances,因此使用addToContract()函数。
我的问题是,我编写的addToContract()函数是否可能?
我非常感谢你的帮助。
发布于 2022-10-18 19:12:44
这是可能的。你的逻辑是正确的:)
https://ethereum.stackexchange.com/questions/137727
复制相似问题