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

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
这是因为
tokencontractAddress上的功能
是由finalToken
(用户调用)调用。如果您想从用户作为功能的一部分中提取令牌,则用户需要发送2次单独的交易。
第一次交易到
tokencontractAddress
直接调用其and code> anderve()
函数,传递 finalToken 地址为spender。以及您的
finalToken
合同的第二个。您还需要进行更改以调用
safetransfrom()
在代码中 - 不是safetransfer()
- 将用户地址作为令牌发送者传递。It's because the safeTransfer() function on
tokenContractAddress
is called byFinalToken
(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.
First transaction to the
tokenContractAddress
contract directly, invoking itsapprove()
function, passingFinalToken
address as the spender.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 - notsafeTransfer()
- passing the user address as the token sender.