0

Newbie here. For practice, I'm writing a smart contract where multiple users can pool funds. E.g. 3 users can contribute 100 DAI tokens to the contract. At the moment, the smart contract does nothing more than tracking the contributors.

How do I ensure the contract only accepts tokens of a specific kind (e.g. DAI)?

My code so far is rudimentary:

pragma solidity ^0.8.0;

contract Pool {
    
    struct Contributer {
        uint id;
        address wallet;
        uint contribution;
    }

    mapping(uint => Contributor) public contributors;

    address public admin;
    IERC20 public which_token;

    constructor(address _daiAddress) {
        admin = msg.sender;
        which_token = IERC20(_daiAddress);// pointer to DAI
    }

    function contributeToPool(address _from, uint _amount) {
        //require(type of token same as which_token??);
    }

}

Specifically, have a look at contributeToPool().

2

Here are the key topics and keywords to search for:

Generally:

  1. "Whitelist" the tokens you DO accept.
  2. Use the "approve and transferFrom" pattern.

Decide if your token list will be static or changeable and consider the "Ownable" pattern to control access to the maintenance function if the latter. Otherwise, consider populating the list in the constructor.

Consider:

import "./IERC20.sol";

mapping(address => bool) public tokenApproved;

function receiveTokens(address token, uint amount) public {
  require(tokenApproved[token], "We don't accept those");
  IERC20(token).transferFrom(msg.sender, address(this), amount);
  emit Received(msg.sender, token, amount);
}

Hope it helps.

1
  • Upvoted for not only answering, but also taking the time out to educate about the key concepts involved. – Hassan Baig Apr 10 at 10:44

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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