Deposit specific Token inside a smart contract

Is there a way to specify in a smart contract “A”, which token has to be supplied from the sender in order to store that token inside the same smart contract “A”?

1 Like

This is my code…

// SPDX-License-Identifier: GPL-3.0

    pragma solidity >=0.7.0 <0.8.0;

    import "https://github.com/OpenZeppelin/openzeppelin-contracts/blob/master/contracts/token/ERC20/ERC20.sol";

    contract StoreISHTokens{
        
        ERC20 private ERC20interface;
        
        address public tokenAdress; // This is the token address
        address payable public owner; // This is the client
        // uint public expenses; // The fee in eth to be stored in the smart contract (for example)


        
        constructor (){
            tokenAdress = 0x3F78e5eff771Aed5FFC5B38223c84ea1774077d4; 
            ERC20interface = ERC20(tokenAdress);
            owner = msg.sender;
            }
        
        event Transfer(address indexed _from, address indexed _to, uint256 _value);
        event Approval(address indexed _owner, address indexed _spender, uint256 _value);

        
        function contractBalance() public view returns (uint _amount){
            return ERC20interface.balanceOf(address(this));
        }
        
        function senderBalance() public view returns (uint){
            return ERC20interface.balanceOf(msg.sender);
        }
        
        function approveSpendToken(uint _amount) public returns(bool){
            return ERC20interface.approve(address(this), _amount); // We give permission to this contract to spend the sender tokens
            //emit Approval(msg.sender, address(this), _amount);
        }
        
        function allowance() public view returns (uint){
            return ERC20interface.allowance(msg.sender, address(this));
        }
        
        
        function depositISHTokens (uint256 _amount) external payable {
            address from = msg.sender;
            address to = address(this);

            ERC20interface.transferFrom(from, to, _amount);
        }
        

            function transferBack (address payable _to) public payable  {
            _to = msg.sender;
            uint balance = ERC20interface.balanceOf(address(this)); // the balance of this smart contract
            ERC20interface.transferFrom(address(this), _to, balance);
        }
        
     
    }
1 Like

This is a known limitation of the ERC20 token standard. I think your options are either to go with the approve + transferFrom pattern (two transactions are required) or if you’re in control of the token, to make it an ERC777 so you can react to transfers with the tokensReceived hook.

2 Likes

Thanks! I actually did figure out how to do it. By understanding how the erc20 tokens work.

The token is not sent to your wallet, but is your wallet address the one stored in the token contract specifying your shared of the contract. That has blown mi mind :joy:

1 Like