相关图片如下,有描述详情:
合约代码如下:
// 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);
}
}
approve 是用户调用ERC20 合约, 授权给合约。 不是在合约内调用。
有两个方法: