Tbh,很难解释标题的原因,顺便说一句,我有一份预售测试合同,允许用户购买令牌,一旦硬顶达到停止卖出更多的令牌。
这是合同
contract TESTPresale is ReentrancyGuard {
using SafeMath for uint256;
using SafeBEP20 for IBEP20;
// Maps user to the number of tokens owned
mapping(address => uint256) public tokensOwned;
// The number of unclaimed tokens the user has
mapping(address => uint256) public tokensUnclaimed;
// TEST token
IBEP20 public TEST;
// USDC token
IBEP20 public USDC;
// Sale active
bool isSaleActive;
// Claim active
bool isClaimActive;
// Total TEST sold
uint256 totalTokensSold = 0;
// Price of presale TEST, 1 USDC
uint256 public testPerUsdc = 1e18/uint256(950000);
// Amount of USDC received in presale
uint256 usdcReceived = 0;
uint256 HardCap = 85000 * 10 ** 18; // 85,000
event TokenBuy(address user, uint256 tokens);
event TokenClaim(address user, uint256 tokens);
constructor(
address _TEST,
address _USDC
) public {
TEST = IBEP20(_TEST);
USDC = IBEP20(_USDC);
}
function setTestPerUsdc(uint256 _testPerUsdc) public onlyOwner{
testPerUsdc = _testPerUsdc;
}
function buy(uint256 _amount) public nonReentrant {
require(isSaleActive, "Presale has not started");
address _buyer = msg.sender;
uint256 tokens = _amount.mul(testPerUsdc);
require ( totalTokensSold + tokens <= HardCap, "presale hardcap reached");
USDC.safeTransferFrom(msg.sender, address(this), _amount);
tokensOwned[_buyer] = tokensOwned[_buyer].add(tokens);
tokensUnclaimed[_buyer] = tokensUnclaimed[_buyer].add(tokens);
totalTokensSold = totalTokensSold.add(tokens);
usdcReceived = usdcReceived.add(_amount);
emit TokenBuy(msg.sender, tokens);
}
function getTotalTokensSold() public view returns (uint256) {
return totalTokensSold;
}
function getTESTTokensLeft() external view returns (uint256) {
return TEST.balanceOf(address(this));
}
.........
}在Constructor中,我有测试令牌和USDC,我对这两个输入都使用了测试令牌
但是,每当我发送50k令牌到预售合同,然后尝试用至少1000000000000000000卫士=1测试令牌测试购买函数时,我就会得到这个错误。
Gas estimation errored with the following message (see below). The transaction execution will likely fail. Do you want to force sending?
Internal JSON-RPC error.
{
"code": 3,
"message": "execution reverted: presale hardcap reached",
"data": "0x0.......}但是当我想买30000000000魏= 0.00000003之类的东西时,它工作得很好。
有人能帮忙吗?问题在哪里?
发布于 2023-02-25 07:43:50
在您的代码中,您有以下要求条件:
require ( totalTokensSold + tokens <= HardCap, "presale hardcap reached");正如我们在您的代码中所看到的
totalTokensSold = 0
tokens = _amount.mul(testPerUsdc)-->
tokens = 50k * 1= 50000*10**18 * 1e18/uint256(950000)-->
(to make easier change 95000 to 100000) -->
tokens = 50k * 1= 50000*10**18 * 1e13
and this is greater than your HardcCap of 85*10**1e18错误应该大约出现在2e13左右,在第一个例子中,值高于这个值,这就是它失败的原因,第二个错误出现在下面,而不是失败!
希望能帮上忙!
发布于 2023-02-25 10:55:06
您必须定义testPerUsdc在每个wei基础上。
例如:
testPerUsdc = No of token in 1 wei usdc * 10**18/10**decimalUSDC
= No of token in 1 wei usdc * 10**18 / 10**9
= No of otken in 1 wei usdc * 10**9如果是No of token in 1 usdc = 1,那么在1weusdc中,带有小数的9将是令牌的= 1 * 10**9 wei。
https://ethereum.stackexchange.com/questions/145519
复制相似问题