[英]Problem with sending ERC20 tokens from one smart contract to another one
I have an ERC20 token (Cryptos) and want to send some tokens to VulnerableTokenSale smart contract, but it shows zero balance for the token balance (get_balanceToken function).我有一个 ERC20 代币(加密货币)并想向 VulnerableTokenSale 智能合约发送一些代币,但它显示代币余额为零(get_balanceToken 函数)。 Is it possible to do that?
有可能这样做吗? I am using Remix on Ethereum website.
我在以太坊网站上使用 Remix。
contract VulnerableTokenSale {
address public wallet;
uint256 rate;
ERC20 public token;
address public owner;
mapping(address => uint256) public balances;
address [] beneficiaries;
modifier onlyOwner() {
require(msg.sender == owner, "not owner");
_;
}
constructor(address _wallet, uint256 _rate, ERC20 _token)
public {
wallet = _wallet;
rate = _rate;
token = _token;
owner = msg.sender;
}
function sendTokensWithRatio(uint256 _numerator, uint256 _denominator) external onlyOwner {
require(_numerator <= _denominator);
for(uint256 i = 0; i < beneficiaries.length; i++){
address beneficiary = beneficiaries[i];
uint256 balance = balances[beneficiary];
if(balance > 0) {
uint256 amount = balance * _numerator;
amount = amount / _denominator;
balances[beneficiary] = balance - amount;
token.transfer(beneficiary, amount);
}
}
}
function purchaseTokens() public payable{
uint256 weiAmount = msg.value;
uint256 _tokenAmount = weiAmount * rate;
beneficiaries.push(msg.sender);
balances[msg.sender] = balances[msg.sender] + _tokenAmount;
}
}
Think your constructor is slightly wrong, I have never seen anyone take in an ERC token directly.认为您的构造函数略有错误,我从未见过有人直接接受 ERC 令牌。
I have something like this in my contracts:我的合同中有这样的内容:
constructor(address _wallet, uint256 _rate, address _tokenAddress) public {
wallet = _wallet;
rate = _rate;
token = ERC20(_tokenAddress);
owner = msg.sender;
}
Note you need the ERC20 interface for the above to work:请注意,您需要 ERC20 接口才能进行上述操作:
interface ERC20 {
function totalSupply() public view returns (uint);
function balanceOf(address tokenOwner) public view returns (uint balance);
function allowance(address tokenOwner, address spender) public view returns (uint remaining);
function transfer(address to, uint tokens) public returns (bool success);
function approve(address spender, uint tokens) public returns (bool success);
function transferFrom(address from, address to, uint tokens) public returns (bool success);
// optional
function name() external view returns (string);
function symbol() external view returns (string);
function decimals() external view returns (string);
event Transfer(address indexed from, address indexed to, uint tokens);
event Approval(address indexed tokenOwner, address indexed spender, uint tokens);
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.