Creation of CustomToken errored: VM error: revert



我是新手,所以你们能帮帮我吗,谢谢?我试图部署合约将ETH价值,但总是得到错误&;交易已恢复到初始状态。如果你发送价值,并且你发送的价值应该小于你当前的余额,那么调用函数应该是支付的。有办法解决这个问题吗?

// SPDX-License-Identifier: GPL-3.0
pragma solidity ^0.8.0;
import "@openzeppelin/contracts/token/ERC20/ERC20.sol";
contract CustomToken is ERC20 {
constructor(string memory name, string memory symbol) ERC20(name, symbol){
_mint(msg.sender, 1000000 * 10 ** 18);
}
}
contract Uniswap {
// Custom tokens to be initialiazed
string[] public tokens = ["CoinA", "CoinB", "CoinC"];
// map to maintain the tokens and its instances
mapping(string => ERC20) public tokenInstanceMap;
// 1 CoinA/CoinB/COinC = 0.0001 eth
uint256 ethValue = 100000000000000;
// 0.0001 eth = 1 CoinA/CoinB/CoinC
// 1 CoinA/CoinB/CoinC = 1 CoinA/CoinB/CoinC
constructor() {
for (uint i=0; i<tokens.length; i++) {
CustomToken token = new CustomToken(tokens[i], tokens[i]);
tokenInstanceMap[tokens[i]] = token;
}
}
function getBalance(string memory tokenName, address _address) public view returns (uint256) {
return tokenInstanceMap[tokenName].balanceOf(_address);
}
function getTotalSupply(string memory tokenName) public view returns (uint256) {
return tokenInstanceMap[tokenName].totalSupply();
}
function getName(string memory tokenName) public view returns (string memory) {
return tokenInstanceMap[tokenName].name();
}
function getTokenAddress(string memory tokenName) public view returns (address) {
return address(tokenInstanceMap[tokenName]);
}
function getEthBalance() public view returns (uint256) {
return address(this).balance;
}
function swapEthToToken(string memory tokenName) public payable returns (uint256) {
uint256 inputValue = msg.value;
uint256 outputValue = (inputValue / ethValue) * 10 ** 18; // Convert to 18 decimal places
require(tokenInstanceMap[tokenName].transfer(msg.sender, outputValue));
return outputValue;
}


}

我尝试了0 ETH,它工作得很好!(https://i.stack.imgur.com/UtLNt.png)然而,有了1个ETH,它得到了错误消息(https://i.stack.imgur.com/MsjE0.png)

函数需要使用payable修饰符才能接受ETH。

当你部署一个合约时,它会执行constructor

constructor(string memory name, string memory symbol) ERC20(name, symbol) payable {
_mint(msg.sender, 1000000 * 10 ** 18);
}

相关内容

  • 没有找到相关文章

最新更新