IERC20 SAFETRANSFER第二令牌问题

发布于 2025-02-13 21:26:42 字数 1975 浏览 0 评论 0 原文

我完全被堆叠了!当用户与合同令牌进行交互时,我想将另一个特定令牌的一半发送到一个特定地址,我设法这样做,但是每次用户与令牌进行交互时,都不是由令牌合同本身进行转移用户的合同...请帮忙!

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);
 
 }

}

I'm totally stacked! When a user interacts with contract token I want to send half of the amount of another specific token to an specific address, I manage to do it this way but everytime the user interacts with the token the transfer is made from the token contract itself not from the user's contract...please help!

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);
 
 }

}

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

停顿的约定 2025-02-20 21:26:42

这是因为 tokencontractAddress上的功能是由 finalToken (用户调用)调用。

如果您想从用户作为功能的一部分中提取令牌,则用户需要发送2次单独的交易。

  1. 第一次交易到 tokencontractAddress 直接调用其 and code> anderve()函数,传递 finalToken 地址为spender。

  2. 以及您的 finalToken 合同的第二个。

您还需要进行更改以调用 safetransfrom()在代码中 - 不是 safetransfer() - 将用户地址作为令牌发送者传递。

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

It's because the safeTransfer() function on tokenContractAddress is called by FinalToken (which is called by the user).

If you want to pull tokens from the user as a part of your function, the user needs to send 2 separate transactions.

  1. First transaction to the tokenContractAddress contract directly, invoking its approve() function, passing FinalToken address as the spender.

  2. And the second to your FinalToken contract as they are doing in your example already.

You also need to make a change to call safeTransferFrom() in your code - not safeTransfer() - passing the user address as the token sender.

IERC20 tokennew = IERC20(address(tokenContractAddress));
// user is the token sender, `to` is the recipient
tokennew.safeTransferFrom(msg.sender, to, amount);
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文