使用传输功能发送 ERC20 代币
Sending ERC20 tokens using the transfer function
我是 solidity 编程的新手,我目前正在尝试 运行 Remix 中的一个简单的智能合约,如下所示:
pragma solidity ^0.8.0;
import "github.com/OpenZeppelin/openzeppelin-contracts/contracts/token/ERC20/utils/SafeERC20.sol";
contract Swap {
address public owner;
uint256 public balance;
event TransferReceived(address _from, uint _amount);
event TransferSent(address _from, address _destAddr, uint _amount);
constructor() {
owner = msg.sender;
}
receive() payable external {
balance += msg.value;
emit TransferReceived(msg.sender, msg.value);
}
function withdraw(uint amount, address payable destAddr) public {
require(msg.sender == owner, "Only owner can withdraw funds");
require(amount <= balance, "Insufficient funds");
destAddr.transfer(amount);
balance -= amount;
emit TransferSent(msg.sender, destAddr, amount);
}
function transferERC20(IERC20 token, address to, uint256 amount) public {
require(msg.sender == owner, "Only owner can withdraw funds");
uint256 erc20balance = token.balanceOf(address(this));
require(amount <= erc20balance, "balance is low");
token.transfer(to, amount);
emit TransferSent(msg.sender, to, amount);
}
}
虽然我可以成功发送 BNB 并调用 withdraw
函数给出发送的值和我在 BSC 测试网中的钱包地址,但我在 运行 设置 transferERC20
功能。调用此方法时我得到的唯一输出是以下消息:
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": -32000, "message": "execution
reverted" }
我在 testnet.bscscan 网站上尝试了几个不同的 BNB 地址,同时确保合约有足够的资金用于转账,但我没有成功。
谁能告诉我 contract/setup 中可能出了什么问题?我是否正确进行了此转帐?
修复构造函数
constructor() {
// payable allows payment of ether with a call.
owner = payable(msg.sender);
}
确保那些 require
语句得到满足
require(msg.sender == owner, "Only owner can withdraw funds");
require(amount <= balance, "Insufficient funds");
检查您是否连接到正确的网络
我是 solidity 编程的新手,我目前正在尝试 运行 Remix 中的一个简单的智能合约,如下所示:
pragma solidity ^0.8.0;
import "github.com/OpenZeppelin/openzeppelin-contracts/contracts/token/ERC20/utils/SafeERC20.sol";
contract Swap {
address public owner;
uint256 public balance;
event TransferReceived(address _from, uint _amount);
event TransferSent(address _from, address _destAddr, uint _amount);
constructor() {
owner = msg.sender;
}
receive() payable external {
balance += msg.value;
emit TransferReceived(msg.sender, msg.value);
}
function withdraw(uint amount, address payable destAddr) public {
require(msg.sender == owner, "Only owner can withdraw funds");
require(amount <= balance, "Insufficient funds");
destAddr.transfer(amount);
balance -= amount;
emit TransferSent(msg.sender, destAddr, amount);
}
function transferERC20(IERC20 token, address to, uint256 amount) public {
require(msg.sender == owner, "Only owner can withdraw funds");
uint256 erc20balance = token.balanceOf(address(this));
require(amount <= erc20balance, "balance is low");
token.transfer(to, amount);
emit TransferSent(msg.sender, to, amount);
}
}
虽然我可以成功发送 BNB 并调用 withdraw
函数给出发送的值和我在 BSC 测试网中的钱包地址,但我在 运行 设置 transferERC20
功能。调用此方法时我得到的唯一输出是以下消息:
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": -32000, "message": "execution reverted" }
我在 testnet.bscscan 网站上尝试了几个不同的 BNB 地址,同时确保合约有足够的资金用于转账,但我没有成功。
谁能告诉我 contract/setup 中可能出了什么问题?我是否正确进行了此转帐?
修复构造函数
constructor() { // payable allows payment of ether with a call. owner = payable(msg.sender); }
确保那些
require
语句得到满足require(msg.sender == owner, "Only owner can withdraw funds"); require(amount <= balance, "Insufficient funds");
检查您是否连接到正确的网络