0

I've an ERC20 token:

// SPDX-License-Identifier: MIT
pragma solidity ^0.8.9;

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

contract Token is ERC20 {
    constructor(string memory _name, string memory _symbol, uint256 _initialSupply)
        ERC20(_name, _symbol)
    {
        _mint(msg.sender, _initialSupply);
    }
}

I simply want to transfer this token to another Smart Contract- ContractA & update its variable using the addToContract() function:

// SPDX-License-Identifier: MIT
pragma solidity ^0.8.9;

import "@openzeppelin/contracts/utils/math/SafeMath.sol";
import "@openzeppelin/contracts/token/ERC20/IERC20.sol";

contract ContractA {
    using SafeMath for uint256;

    IERC20 immutable private _token;
    mapping(address => uint256) private _balance;

    constructor(address token) {
        require(token != address(0x0));
        _token = IERC20(token);
    }

    function addToContract(uint256 amount) public {
        _token.transferFrom(msg.sender, address(this), amount);
        _balance[msg.sender].add(amount);
    }

    function withdrawFromContract(uint256 amount) public {
        require(_balance[msg.sender] >= amount, "Insufficient Stake");
        _token.transfer(msg.sender, amount);
        _balance[msg.sender].sub(amount);
    }
}

I hope it's clear what I want to achieve. To simply transfer the tokens the user can call token.transfer(), but I also want to update a variable _balances after that, hence using the function addToContract().

My question is, is the addToContract() function that I've written even possible?

I would highly appreciate any help.

1 Answer 1

0

It’s possible. Your logic is correct :)

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.