5 如何从发起部署的地址中,转移一部分代币到已经部署的合约地址中,麻烦那位大佬看一下

相关图片如下,有描述详情:

bc63523db986c26519d02fdebe9ddbe.png

合约代码如下:

// SPDX-License-Identifier: MIT
pragma solidity 0.8.7;

import "@openzeppelin/contracts/utils/math/SafeMath.sol";

interface IToken {

    function balanceOf(address _owner) external returns (uint256 balance);

    function transfer(address _to, uint256 _value)external returns (bool success);

    function transferFrom(
        address _from,
        address _to,
        uint256 _value
    ) external returns (bool success);

    function approve(address _spender, uint256 _value)external returns (bool success);

    function allowance(address _owner, address _spender)
        external
        returns (uint256 remaining);

    event Transfer(address indexed _from, address indexed _to, uint256 _value);
    event Approval(
        address indexed _owner,
        address indexed _spender,
        uint256 _value
    );
}

contract DexContract {

    using SafeMath for uint256;

    address _tokenAddress=0x0bfFeb109277Fff2945945Ee23fc588848ce9796;

    address owner;

    struct UserInfo {
        uint256 rewardAmount;    //抽奖奖励
    }

    mapping(address => UserInfo) public userInfo;
    mapping(address => uint256) public balances;
    address[] private addressList;

    event transferLog(address from,uint256 count);
    event dexTransferLog(string payinSymbol,address from ,address to ,uint256 count);

    constructor() {
         owner = payable(msg.sender);
    }

    function deposit() external payable {
        balances[address(this)] += msg.value;
    }

    function dexTransfer(string memory _payinSymbol) external payable{
         IToken(_tokenAddress).approve(address(this),msg.value);
         IToken(_tokenAddress).transferFrom(_tokenAddress,address(this),msg.value);
         emit dexTransferLog(_payinSymbol,msg.sender,address(this),msg.value);
    }

}
请先 登录 后评论

最佳答案 2023-08-11 19:09

approve 是用户调用ERC20 合约, 授权给合约。 不是在合约内调用。

有两个方法:

  1. EOA 用户直接调用 transfer 给你的合约转。
  2. EOA用户调用 ERC20 合约 的approve, 然后在你的合约里, 调用 transferFrom

参考: https://learnblockchain.cn/question/2231

请先 登录 后评论

其它 4 个回答

btr712
请先 登录 后评论
Jeack
请先 登录 后评论
用户_14041
请先 登录 后评论
用户_16534
请先 登录 后评论
  • 4 关注
  • 0 收藏,2215 浏览
  • 醒梦无痕 提出于 2023-08-11 12:16