我最近在主网上发布了一个 ERC-20 代币,一切运行良好。现在我正在尝试编写一个水龙头智能合约,以便将硬币分发给任何需要它们的人。请注意,这不是直接从 Token 合约铸造新代币的水龙头,而是我可以预加载代币的水龙头(通过从我的钱包发送到水龙头)。
我已经为各种来源的水龙头整理了一份智能合约,除了“取款”功能外,一切正常。我收到以下错误:
truffle(development)> FaucetDeployed.withdraw()
Uncaught Error: Returned error: VM Exception while processing transaction: revert
at evalmachine.<anonymous>:0:16
at sigintHandlersWrap (vm.js:273:12)
at Script.runInContext (vm.js:142:14)
at runScript (C:\Users\Jonathan\AppData\Roaming\npm\node_modules\truffle\build\webpack:\packages\core\lib\console.js:364:1)
at Console.interpret (C:\Users\Jonathan\AppData\Roaming\npm\node_modules\truffle\build\webpack:\packages\core\lib\console.js:379:1)
at bound (domain.js:421:15)
at REPLServer.runBound [as eval] (domain.js:432:12)
at REPLServer.onLine (repl.js:909:10)
at REPLServer.emit (events.js:400:28)
at REPLServer.emit (domain.js:475:12)
at REPLServer.Interface._onLine (readline.js:434:10)
at REPLServer.Interface._normalWrite (readline.js:588:12)
at Socket.ondata (readline.js:246:10)
at Socket.emit (events.js:400:28) {
data: {
'0xf08ce8522dce0fb4c19ac791e5a7960055e1ad0e106761efa8f411c1cc9e23c9': { error: 'revert', program_counter: 677, return: '0x' },
stack: 'c: VM Exception while processing transaction: revert\n' +
' at Function.c.fromResults (C:\\Users\\Jonathan\\AppData\\Roaming\\npm\\node_modules\\ganache-cli\\build\\ganache-core.node.cli.js:4:192416)\n' +
' at w.processBlock (C:\\Users\\Jonathan\\AppData\\Roaming\\npm\\node_modules\\ganache-cli\\build\\ganache-core.node.cli.js:42:50915)\n' +
' at runMicrotasks (<anonymous>)\n' +
' at processTicksAndRejections (internal/process/task_queues.js:95:5)',
name: 'c'
这是我的水龙头智能合约代码:
// SPDX-License-Identifier: MIT
pragma solidity ^0.8.0;
import "@openzeppelin/contracts/token/ERC20/IERC20.sol";
contract Faucet {
address payable owner;
IERC20 private _token;
uint256 public withdrawalAmount = 50 * (10 ** 18);
event Withdrawal(address indexed to, uint amount);
event Deposit(address indexed from, uint amount);
constructor (IERC20 token) {
_token = token;
owner = payable(msg.sender);
}
// Accept any incoming amount
receive() external payable {
emit Deposit(msg.sender, msg.value);
}
// Give out ether to anyone who asks
function withdraw() public {
// Limit withdrawal amount to 1,000 tokens
require(
withdrawalAmount <= 1000 * (10 ** 18),
"Request exceeds maximum withdrawal amount of 1000 ICHC"
);
require(
_token.balanceOf(address(this)) >= withdrawalAmount,
"Insufficient balance in faucet for withdrawal request"
);
require(
msg.sender != address(0),
"Request must not originate from a zero account"
);
// Send the amount to the address that requested it
_token.transfer(msg.sender, withdrawalAmount);
}
// setter for withdrawl amount
function setWithdrawalAmount(uint256 amount) public onlyOwner {
// Limit max withdrawal amount to 10,000 tokens
require(amount <= 10000 * (10 ** 18));
withdrawalAmount = amount * (10 ** 18);
}
// Contract destructor
function destroy() public onlyOwner {
selfdestruct(owner);
}
// Access control modifier
modifier onlyOwner {
require(msg.sender == owner, "Only the contract owner can call this function");
_;
}
}
我正在通过“truffle migrate”将它以及 ERC20 令牌部署到本地 ganache 区块链。然后我在 truffle 控制台中使用以下命令对其进行测试:
设置:
Faucet.deployed().then(i=>{FaucetDeployed = i});
MyToken.deployed().then(s=>{ token = s });
使用令牌加载水龙头:
token.transfer("0x55b9bCF39F78ef22E452d54957366cCBFffaF85E","300000000000000000000")
检查水龙头平衡:
token.balanceOf("0x55b9bCF39F78ef22E452d54957366cCBFffaF85E").then((b)=> { balf = b })
balf.toString() // shows "300000000000000000000"
尝试退出水龙头:
FaucetDeployed.withdraw()
这导致了这篇文章顶部的错误。我尝试删除所有要求语句,但结果相同。我确定我忽略了一些非常愚蠢的东西。谁能发现我做错了什么?我很感激任何建议-谢谢!
- 乔纳森