0

Hoping someone can assist me. I have a contract I deployed and verified on the Goerli testnet. Its a simple ERC20 contract (contract myToken is ERC20) in which I minted all the coins to the deployer address upon deployment. Here is the contract on etherscan…. https://goerli.etherscan.io/address/0xA56AfB65Df413F36f9c753c2e8E56249a44AB812#code

Now I would like to deploy a 2nd contract in which i create a few functions that call approve and transferFrom (since my previous token contract "is ERC20") so I can transfer those tokens into this contract pasted here below and ultimately call this drop function.... https://goerli.etherscan.io/address/0x873289a1aD6Cf024B927bd13bd183B264d274c68#code

In other words I would like to create a 2nd contract to interact with my previous token contract and basically use this new contract to transfer tokens to this other contract. I am using the same account and private key that deployed my previous token contract and this new contract and I do have the ERC20 tokens in personal account balance (not the token contract). Before I show my deploy scripts (using hardhat) can someone tell me if my solidity file contract looks set up correctly?

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

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

interface Bucket {
    function drop(address erc20, uint256 amount) external;
}

contract EmitWinner {
    address hoetokenAddress = 0xA56AfB65Df413F36f9c753c2e8E56249a44AB812;
    address bucketAddress = 0x873289a1aD6Cf024B927bd13bd183B264d274c68;

    ERC20 erc20 = ERC20(hoetokenAddress);

    function approve(uint256 _amount) public {
        erc20.approve(bucketAddress, _amount);
    }

    function drop(uint _amount) external payable {
        bool success = ERC20(erc20).transferFrom(msg.sender, bucketAddress, _amount);
        require(success, "Transaction Failed");
        Bucket(bucketAddress).drop(msg.sender, _amount);
    }
}

To be clear i am calling approve and transferFrom functions from the deployer/msg.sender account and giving the bucketAddress the permission to approve and transfer my tokens. I am not giving this contract here the approval to call TransferFrom. Is that the correct way to do this? Don't I want to give the bucketAddress the approval and the ability to call TransferFrom?

2
  • It won't work. You need to call the approve() function specifically from the token contract only, or the approval will be given from the contract rather the user. Feb 21, 2023 at 13:02
  • Thanks for the response. I had a feeling this was the case. This is what i believe my options are... <br> 1.)Create a new token contract instead of using the previously deployed contract which i can deploy with this new EmitWinner Contract. This gives me access to call approve from the new contract using the deployer account (assuming tokens are transferred to him upon deployment) ' <br> '. 2.)Go back into my previously deployed contract & write scripts to approve? But wont' i need to redeploy the contract if i do this? ' <br> '. Am i missing any other ways of doing this?
    – Blockpain
    Feb 21, 2023 at 14:01

1 Answer 1

0

I see the drop function is doing transferFrom msg.sender so the approve function has no use in this context.

Now as an admin and you will be calling the drop function you need to first approve hoetoken to EmitWinner address from write section of goerliscan (need to verify the contract first). Then only you can call drop function of EmitWinner otherwise it will through error not enough allowance to spend the token

7
  • Okay so I can remove the transferFrom line and just call drop but first i need to approve hoetoken via write section of etherscan goerliscan? So you are saying i don't need to redeploy a new hoetoken here? I can simply use the existing hoetoken contract deployed (it is verified), call approve on etherscan with EmitWinner address. Then call drop function from the EmitWinner contract? But won't i need to recompile and redeploy EmitWinner again after i already deployed and approved EmitWinner? Won't redeploying negate the approval i just did?
    – Blockpain
    Feb 21, 2023 at 14:21
  • Yes, you can use the old one. You should not remove the transferFrom line this is necessary. bool success = ERC20(erc20).transferFrom(msg.sender, bucketAddress, _amount)
    – Safi
    Feb 21, 2023 at 15:54
  • Okay one last question. I greatly appreciate the help. When I call the drop function in my Emit Winner function I was going to do this from inside the Emit Winner contract in my deploy scripts but if I do this I will end up redeploying the contact again won’t I? And if I do this it will give me a new Emit Winner address so the approval i just did on etherscan wouldnt work anymore cause now I have a new Emit Winner contract address. Is there a way around this?
    – Blockpain
    Feb 21, 2023 at 16:25
  • Yes you are correct. If you redeploy the EmitWinner then you need to re approve for that new EmitWinner address.
    – Safi
    Feb 21, 2023 at 16:27
  • If you are new to smart contract field. I suggest you to use remix ide for the deployment of the contract so that you better understand it. You do not need to redeploy your EmitWinner always. Check this remix.ethereum.org
    – Safi
    Feb 21, 2023 at 16:31

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.