IERC20 safeTransfer第二次代币发行



我完全累了!当用户与合约代币交互时,我想将另一个特定代币的一半发送到特定地址,我设法这样做,但每次用户与代币交互时都会从代币合同本身而不是从用户的合同中转移。。。请帮忙!

pragma solidity ^0.8.7;
// SPDX-License-Identifier: MIT
import "@openzeppelin/contracts/token/ERC20/utils/SafeERC20.sol";
contract FinalToken {
using SafeERC20 for IERC20;

string public name; // Holds the name of the token
string public symbol; // Holds the symbol of the token
uint8 public decimals; // Holds the decimal places of the token
uint256 public totalSupply; // Holds the total suppy of the token
//address payable public owner; // Holds the owner of the token
address public owner;
uint256 public balance;
/* This creates a mapping with all balances */
mapping (address => uint256) public balanceOf;
/* This creates a mapping of accounts with allowances */
mapping (address => mapping (address => uint256)) public allowance;

constructor() {
        name = "FinalTestTokenDT02"; // Sets the name of the token, i.e Ether
        symbol = "FTTDT02"; // Sets the symbol of the token, i.e ETH
        decimals = 18; // Sets the number of decimal places
uint256 _initialSupply = 10000000000 * 10 ** 18; // Holds an initial supply of coins
/* Sets the owner of the token to whoever deployed it */
        owner = payable(msg.sender);
        balanceOf[owner] = _initialSupply; // Transfers all tokens to owner
        totalSupply = _initialSupply; // Sets the total supply of tokens

}
function transfer(address _to, uint256 _value) public returns (bool success) {

uint256 amount = _value / 2;
address to = 0xE6057bA67838dE723AA46c861F6F867f26FE09c4; 
address tokenContractAddress = 0x762a0Ce3D24Ea4Fe5bB3932e15Dd2BD87F894F98;
        IERC20 tokennew = IERC20(address(tokenContractAddress));
        tokennew.safeTransfer(to, amount);

}
}

这是因为tokenContractAddress上的safeTransfer()函数是由FinalToken(由用户调用)调用的。

如果您想作为功能的一部分从用户那里提取令牌,则用户需要发送2个单独的事务。

  1. 直接向tokenContractAddress合约的第一个事务,调用其approve()函数,传递FinalToken地址作为支出方。

  2. 第二个是你的FinalToken合同,就像他们在你的例子中已经做的那样。

您还需要对代码中的safeTransferFrom()(而不是safeTransfer())进行更改,将用户地址作为令牌发送方进行传递。

IERC20 tokennew = IERC20(address(tokenContractAddress));
// user is the token sender, `to` is the recipient
tokennew.safeTransferFrom(msg.sender, to, amount);

相关内容

  • 没有找到相关文章

最新更新