从eth(测试网)购买自定义令牌



我想用我的硬币交换ETH。这是我的外汇合约:

pragma solidity >=0.8.2 <0.9.0;
import "https://github.com/OpenZeppelin/openzeppelin-contracts/blob/master/contracts/token/ERC20/ERC20.sol";
contract Exchange {
ERC20 public token;
uint public RATE = 1;
event BuyToken(address user, uint amount, uint boughtTokens, uint balance);
constructor() {
capCoin = ERC20(0x123456789);
}
function buyToken() payable public returns (bool success) {
require(msg.value > 0, "You must send some Ether to buy tokens");
uint tokensToBuy = msg.value * RATE;
require(token.balanceOf(address(this)) >= tokensToBuy, "Not enough tokens in the reserve");
token.transfer(msg.sender, capCoinTokensToBuy);
emit BuyToken(msg.sender, msg.value, tokensToBuy, address(this).balance);
return true;
}
}

我不知道为什么,但是当我增加值部署失败。如果我让值为0,则交易被确认。

有人知道发生了什么事吗?

问题是由于构造函数缺少payable关键字,所以构造函数应该是这样的:

constructor() payable  {
token = ERC20(0x123456789);
}

最新更新