我试图通过元掩码支付合约地址并让它调用一个 buyTokens 函数,但交易一直失败。合约有足够的代币余额,因为我通过元掩码成功地将 ERC20 代币发送到了合约地址。只有当我将以太币发送到合约地址时,它才会失败。我尝试了默认的气体限制,并尝试在 metamask 中将气体设置为 200000。请帮我。
我在本地主机 genache 上运行
pragma solidity ^0.5.0;
import "./Token.sol";
// rate is 1 token = 0.01 ether
contract Crowdsale {
constructor() public {
rate = 100;
on = true;
}
Token public token;
address payable wallet;
uint256 public rate;
bool on;
event TokenPurchase(address recipient, uint256 numPaid, uint256 numTokensPurchased);
function buyTokens() public payable {
require(msg.value > 0 && on == true);
uint256 tokenAmount = _getTokenAmount(msg.value);
require(token.balanceOf(address(this)) >= tokenAmount);
token.transfer(msg.sender, tokenAmount);
emit TokenPurchase( msg.sender, msg.value, tokenAmount);
}
function () external payable{
buyTokens();
}