0

I'm creating an NFT contract which can be bought with my token, when calling the approve function of the token to be able to buy the nft using the tokens, the msg.sender in this case is the nft contract itself and not the user who approves the transaction. How could I solve this?

NFT function:

function createRandomGoblin () public payable {

            uint256 price = getCreateGoblinPrice() * 10 ** 18; // Wei price 
            require(blueGem.balanceOf(msg.sender) >= price);
            require(blueGem.approve(address(this), price), "Failed to approve token transfer");
            //address a = blueGem.returnaddress();
            require(blueGem.transferFrom(msg.sender, address(this), price));
            _createGoblin(msg.sender);

    }

Token function:

function approve(address spender, uint256 amount) public notPaused override returns (bool) {
        _approve(msg.sender, spender, amount);
        return true;
    }

I'm using the IBEP20 standard for the token.

I have thought of creating a function that receives the user's address as a parameter and uses the msg.sender (nft contract) as sender. But this opens a code sploit to anyone being able to create their approve.

function approveNFTContract(address owner, uint amount) public notPaused returns (bool){
    _approve(owner, msg.sender, amount);
    return true;
}

1 Answer 1

2

You are correct, when you call other smart contract from a contract, caller in other contract will be the contract from which the request is being initiated.

While for the approval of tokens its best practice to call the approve function of any standard directly. you can make some changes in your contracts to implement the functionality you want but you should call the approve functions of both IBEP20 and ERC721 offchain(client side) before user calls the createRandomGoblin() function, retaining the integrity and originality of tokens standards.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.