我已经编写了ERC20代币预售智能合约。 我定义了代币转移函数如下:
function transferTokensToPresale(uint256 presaleSupplyAmount_) public onlyOwner returns (bool) {
require(presaleSupplyAmount_ > 0, "Amount must be greater than zero");
require(
token.balanceOf(msg.sender) >= presaleSupplyAmount_,
"Insufficient balance"
);
require(block.timestamp < startTime, "Presale has already started");
//Send the tokens to the presale contract
SafeERC20.safeTransferFrom(
token,
msg.sender,
address(this),
presaleSupplyAmount_
);
return true;
}
但是当我用 Hardhat 和 Chai 测试该功能时,传输并没有发生。没有交易失败或错误。
我写的测试脚本如下:
console.log("token amount before transferring token to presale contract--->", presale.tokensAvailable());
const tx = await presale.connect(owner).transferTokensToPresale(presaleSupply);
console.log("token amount before transferring token to presale contract--->", presale.tokensAvailable());
交易前后预售合约中的代币数量相同。
原因是什么?
transferTokensToPresale
功能正常工作。
该错误发生在您的测试脚本中。
您需要添加await tx.wait();
正确的代码库是这样的:
console.log("token amount before transferring token to presale contract--->", presale.tokensAvailable());
const tx = await presale.connect(owner).transferTokensToPresale(presaleSupply);
await tx.wait();
console.log("token amount before transferring token to presale contract--->", presale.tokensAvailable());
这将修复您的错误。