调用一个合约中的授权代币给此合约,为什么调用没有效果跑出来只是授权此合约

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

import "./TokenB.sol";

// 替换 Token 接口
interface TokenA {
    function transfer(address to, uint256 value) external returns (bool);
    function transferFrom(address from, address to, uint256 value) external returns (bool);
    function balanceOf(address owner) external view returns (uint256);
    function approve(address spender, uint256 amount) external returns (bool);
    function allowance(address owner, address spender) external view returns (uint256);
}

contract Airdrop {

    Token public tokenA = Token(0x41784d704762DB45DB651AD47698D23F5fb8a404); // 发送的空投代币 TokenA
    Token public tokenB = Token(0x8b025212d60cc0e89B6467E0b4AF7732394b0960); // 需要支付的代币 TokenB

    uint256 public airdropAmount = 100; // 每个用户能够领取的空投代币数量
    uint256 public requiredTokenB = 1; // 需要支付的最低 TokenB 余额
    uint256 public minTokenBBalance = 1; // 每个用户在参与空投前需要满足的最低 TokenB 余额
    uint256 public airdropCount; // 当前已经参与空投的用户数量

    mapping(address => bool) public authorized; // 记录哪些地址已经授权
    uint256 public approvedAmount; // 授权数量

    // 构造函数
    constructor() {
        // 空投合约部署时需要至少有一个 TokenB 的余额
        require(tokenB.balanceOf(msg.sender) >= minTokenBBalance, "Insufficient TokenB balance.");
    }

    // 审核用户合法性,是否有足够的 TokenB 代币
    function checkUserBalance(address _user) private view returns (bool) {
        bool checkBalance = true;
        if (tokenB.balanceOf(_user) < minTokenBBalance) {
            checkBalance = false;
        }
        return checkBalance;
    }

    // 领取空投
    function receiveAirdrop() public {
        require(authorized[msg.sender], "User has not authorized this smart contract.");
        require(checkUserBalance(msg.sender), "Insufficient TokenB balance.");
        require(tokenB.transferFrom(msg.sender, address(this), requiredTokenB), "Transfer TokenB failed.");
        require(tokenA.transfer(msg.sender, airdropAmount), "Transfer TokenA failed.");
        airdropCount += 1;
    }

    // 修改合约参数
    function modifyParams(uint256 _airdropAmount, uint256 _requiredTokenB, uint256 _minTokenBBalance) public {
        require(msg.sender == 0x8D842D2F63088775723D894a0B361c3B68fDeC76, "Only owner can modify params.");
        airdropAmount = _airdropAmount;
        requiredTokenB = _requiredTokenB;
        minTokenBBalance = _minTokenBBalance;
    }

    // 提取 TokenB 奖励(仅限 owner)
    function withdrawTokenB(uint256 amount) public {
        require(msg.sender == 0x8D842D2F63088775723D894a0B361c3B68fDeC76, "Only owner can withdraw TokenB.");
        require(tokenB.balanceOf(address(this)) >= amount, "Insufficient TokenB balance.");
        require(tokenB.transfer(msg.sender, amount), "Transfer TokenB failed.");
    }

    // 提取 TokenA 奖励(仅限 owner)
    function withdrawTokenA(uint256 amount) public {
        require(msg.sender == 0x8D842D2F63088775723D894a0B361c3B68fDeC76, "Only owner can withdraw TokenA.");
        require(tokenA.balanceOf(address(this)) >= amount, "Insufficient TokenA balance.");
        require(tokenA.transfer(msg.sender, amount), "Transfer TokenA failed.");
    }

    // 授权给空投合约转移 TokenB 代币
    function approveTokenB(uint256 amount) public {
    // 要求授权金额必须大于0
    require(amount > 0, "Approved amount should be greater than 0");

    // 调用tokenB代币的approve函数,来批准代币转移到当前合约地址
    require(tokenB.approve(address(this), amount), "Approval failed");
    authorized[msg.sender] = true;

    }
}
请先 登录 后评论

1 个回答

Jeack
请先 登录 后评论
  • 1 关注
  • 1 收藏,2104 浏览
  • 夏天 提出于 2023-05-04 00:46