0

I approve the contract address for the tokens of some wallet address to send the tokens. Now, how can i call the transfer function using contract address. Thanks in advance.

function approve(address to, uint256 tokenId) public virtual override {
        address owner = ERC721.ownerOf(tokenId);
        require(to != owner, "ERC721: approval to current owner");

        require(
            _msgSender() == owner || isApprovedForAll(owner, _msgSender()),
            "ERC721: approve caller is not owner nor approved for all"
        );
        allowed[msg.sender][to] = tokenId;

        _approve(to, tokenId);
    }



 function _transferFrom(
        address _from,
        address _to,
        uint256 _amount
    )   public  returns  (bool ) {
         // balances[_from] -= _amount;
            allowed[_from][msg.sender] -= _amount;
          _transfer(_from, _to, _amount);
            return true;
    }

how can i call the transfer function using contract address?

6
  • Do you want to call the transfer function from another contract?
    – jeissoni22
    Feb 15, 2022 at 14:55
  • no i want to call it in within this contract Feb 15, 2022 at 15:24
  • I honestly don't know what your question is
    – jeissoni22
    Feb 15, 2022 at 15:50
  • Hi i explain one second Feb 15, 2022 at 15:51
  • Actually i want to make a contract in which i have one master wallet which has many Nfts. So i want the contract address to send the tokens on the master wallet behalf Feb 15, 2022 at 15:53

2 Answers 2

0

Your contract that owns the NFTs must have the code to interact with the NFT contract. The NFT contract is where the records of who is the owner of which ID are, also in that contract must be the functions to approve and transfer.

So as a pseudocode it would be something like this.

pragma solidity ^0.8.0;

//ERC721 interface
//documentation 
//https://eips.ethereum.org/EIPS/eip-721
import "./IER721.sol";

contract OwnerNFT{
    IERC721 public NFT;

    function tranferNFT(
             uint256 idNFT,
             address _to
    )public returns(bool){
      ...    
      NFT.Transfer(address(this),_to, idNFT);    
     ...
    }

    //send address smartcontract NFT 
    constructor(address NFT) {
          NFT = IERC721 (NFT);
     }
}
3
  • HI jeissoni Thanks for the answer actually i have only one contract in which i have all the functions related approve etc. but i want to know how can i call transferFrom function using contract address instead of my wallet address such as msg.sender Feb 15, 2022 at 16:40
  • I give up hahahahaha I don't understand what you need
    – jeissoni22
    Feb 15, 2022 at 17:10
  • hilarious! hehehe ok i will try to resolve then i will come back but Thanks for your answer Feb 15, 2022 at 17:43
0

Actually your question is confusing, where is this function? are you interacting with ERC20 token contract or you trying to call functions of contract in your contract using interface?

If you trying to call Using interface in your contract, at first you need to define ERC20 token using token address in your contract this way:

IERC20 public token = IERC20(tokenAddress);

Then call approve an transfer function in your contract like that:

function _approve (address _spender, uint256 _amount)public {
   token.approve(_spender, _amount);

function _transferFrom (address _from, address _to, uint256 _amount)public{
   token.transferFrom (_from, _to, _amount);

Note that to calling these functions, at first you need to import ERC20 Interface (IERC20) above your contract.

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.