简体   繁体   English

收到错误“ERC 20 津贴不足”我的合同

[英]Get an error "ERC 20 insufficent allowance" my contract

i got stucked in it and i don't understand where the error is...我被卡住了,我不明白错误在哪里......

Is a Lottery Project and the problem comes in placeABid function in lottery.sol contract.是一个彩票项目,问题出现在 lottery.sol 合同中的ABid function。

Below i attached my code:下面我附上了我的代码:

LotteryToken.sol LotteryToken.sol

//SPDX-License-Identifier: MIT

import "@openzeppelin/contracts/access/Ownable.sol";
import "@openzeppelin/contracts/token/ERC20/ERC20.sol";


pragma solidity ^0.8.4;

contract LotteryToken is Ownable, ERC20{

    mapping(address=>uint)private _balances;


    constructor(string memory name, string memory symbol, uint totalSupply)ERC20(name, symbol){
        _mint(msg.sender, totalSupply);
        _balances[msg.sender] = totalSupply;

    }

    

    function increaseAllowance(address spender, uint256 addedValue) public virtual override returns (bool) {
        address owner = owner();
        _approve(owner, spender, allowance(owner, spender) + addedValue);
        return true;
    }



    function _transfer(
        address _from,
        address _to,
        uint _amount
        )internal virtual override{
            _balances[_from] -= _amount;
            _balances[_to] += _amount;
            super._transfer(_from, _to, _amount);
        }
    


    function balanceOf(address account)public view virtual override returns(uint){
        return _balances[account];
    }

}

Lottery.sol彩票.sol

//SPDX-License-Identifier: MIT

import "@openzeppelin/contracts/access/Ownable.sol";
import "./LotteryToken.sol";
import "./LotteryInfo.sol";

pragma solidity ^0.8.4;

contract Lottery is Ownable{

    LotteryInfo private lotteryInfo;
    LotteryToken private lotteryToken;

    event BidCorrect(address bidder, uint tokenId, uint defaultBid);

    constructor(){

    }


    function initLotteryTokenContract(address _lotteryTokenContract)public onlyOwner{
        lotteryToken = LotteryToken(_lotteryTokenContract);
        lotteryToken.increaseAllowance(address(this), lotteryToken.balanceOf(lotteryToken.owner()));
    }



    function initLotteryInfoContract(address _lotteryInfoContract)public onlyOwner{
        lotteryInfo = LotteryInfo(_lotteryInfoContract);
    }

    

    function setItemToWin(
        string memory _name,
        uint _defaultBid,
        uint _endingTimeLottery
        )public onlyOwner{
            require(_endingTimeLottery > block.timestamp, "Cannot set a date in the Past!");
            lotteryInfo.setItemToWin(_name, _defaultBid, _endingTimeLottery);
    }



    function buyTokens(uint _quantity)external payable{
        uint singleTokenPrice = 0.02 ether;
        uint finalPrice = singleTokenPrice * _quantity;
        require(msg.sender != owner() && msg.sender != lotteryToken.owner(), "Admin Cannot Buy Tokens!");
        require(msg.value == finalPrice, "Please set the right price!");
        payable(owner()).transfer(msg.value);
        address lotterytokenOwner = lotteryToken.owner();
        lotteryToken.transferFrom(lotterytokenOwner, msg.sender, _quantity);
    }



    function placeABid(uint _tokenIds)external{
        require(block.timestamp < lotteryInfo.getEndingTimeLottery(_tokenIds), "Lottery is closed!");
        require(msg.sender != owner() && msg.sender != lotteryToken.owner(), "Admin Cannot Place A Bid!");
        uint requiredTokenAmounts = lotteryInfo.getDefaultItemBid(_tokenIds);
        require(lotteryToken.balanceOf(msg.sender) >= requiredTokenAmounts, "No Necessary Funds To Partecipate!");
        address lotteryTokenOwner = lotteryToken.owner();
        lotteryToken.transferFrom(msg.sender, lotteryTokenOwner, requiredTokenAmounts);
        lotteryInfo.updateBid(_tokenIds, requiredTokenAmounts);
        lotteryInfo.updateAddress(_tokenIds, msg.sender);
        emit BidCorrect(msg.sender, _tokenIds, requiredTokenAmounts);
    }

There is another contract LotteryInfo but doesn't need to understand the problem.还有另一个合同 LotteryInfo 但不需要了解问题。

What i want to do is to place a bid and send back the amount of bid to the lotteryToken.owner() but i receive error revert "ERC20 insufficient allowance".我想要做的是出价并将出价金额发送回 lotteryToken.owner() 但我收到错误还原“ERC20 配额不足”。 Can someone explain me why?有人可以解释我为什么吗? Thanks a lot非常感谢

You have to call "approve" method from the caller of "placeABid" function.您必须从“placeABid”function 的调用方调用“批准”方法。 He/She has to approve your Lottery contract to spend (calling transferFrom) he/shes ERC20 tokens.他/她必须批准您的彩票合约才能使用(调用 transferFrom)他/她的 ERC20 代币。 Without increasing the allowance (approval) of ERC20, you wont be able to withdraw ERC20 tokens from msg.sender.如果不增加 ERC20 的许可(批准),您将无法从 msg.sender 提取 ERC20 代币。

You can detaily check ERC20 standart contract from OpenZeppelin library aswell.您也可以从 OpenZeppelin 库中详细检查 ERC20 标准合约。

First you have to call "approve" method from the caller of "placeABid" function.首先,您必须从“placeABid”function 的调用者调用“批准”方法。 Then person has to approve your Lottery contract to spend (calling transferFrom) the ERC20 tokens.然后人们必须批准您的彩票合约才能使用(调用 transferFrom)ERC20 代币。 Without increasing the allowance (approval) of ERC20, you wont be able to withdraw ERC20 tokens from msg.sender.如果不增加 ERC20 的许可(批准),您将无法从 msg.sender 提取 ERC20 代币。

If amount is the maximum uint256 , the allowance is not updated on * transferFrom .如果amount是最大值uint256 ,则不会在 * transferFrom上更新配额。 This is semantically equivalent to an infinite approval.这在语义上等同于无限批准。 * Requirements: - spender cannot be the zero address. * 要求: - spender者不能是零地址。

Here's a reference link这是一个参考链接

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 .allowance 方法用于将 erc20 令牌导入元掩码 - .allowance method for importing erc20 token to metamask 由于 ProviderError: HttpProviderError 错误,无法批准 ERC-20 代币合约 - cannot approve contract to ERC-20 token due to ProviderError: HttpProviderError error 当我从其他合约调用 ERC20 合约中的转账 function 时出错,但是,可以使用 mint/burn - Error when I call transfer function in ERC20 contract from other contract, But, can use mint/burn web3js 说在 ERC20 合约中找不到方法 - web3js says method is not found in ERC20 contract 如何将 ERC20 代币发送到智能合约余额? - How to send ERC20 token to smart contract balance? 我可以使用PHP从ERC20合同中转移令牌吗? - Can I transfer tokens from an ERC20 contract using PHP? Mint功能的传输事件和ERC20智能合约的传输功能有什么区别? - what is difference between transfer event of Mint function and Transfer function of ERC20 Smart Contract? 出现错误:返回错误:必须经过身份验证! ,同时使用 web3.js 发送 erc20 代码 - Getting Error: Returned error: Must be authenticated! , while sending erc20 code using web3.js 有人成功地用 Web3.js@1.0.0 获得了 ERC20 Token 的余额吗? - Someone has managed to get the balance of an ERC20 Token with Web3.js@1.0.0? 在 erc721/erc20 代币之间共享 ABI? - Sharing ABI between erc721/erc20 Tokens?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM