0

Here is a sample contract which is designed to take xyz token. How can the contract verify that the sent is the xyz token?

pragma "^0.7.0"

contract myContract {
  address public payable owner;
  uint public value;  //<<==xyz token

  constructor(uint _value) payable {
    owner = msg.sender;
    value = _value;  //<<== how does the contract verify it is the _value of xyz token?
  }
}
  • 2
    It's not really clear what you want to achieve, but any token transfers will need to send a transaction to the token contract. You might want to read a bit about how to interact with tokens. – Markus - soliditydeveloper.com Oct 18 at 6:51
  • Do you have a good post to recommend? I look online and there are few talking about the interaction between token contract and the contracts which use the token. My understanding is that the token contract shall have a deploying address and the contract which uses the token shall have a way to refer the token contract. – user938363 Oct 18 at 21:58
2

To receive ERC20 payments you usually use the combination of user's approval and transferFrom from the contract.

  1. User calls token's approve function with the recipient contract address. Using a wallet, perhaps Metamask. In javascript it will look like:

     token.approve(recipientContactAddress, amount, { from: userAddress });
    
  2. User calls a function from the recipient contract, deposit, let's call that function deposit.

     recipientContract.deposit(amount, { from: userAddress });
    
  3. Recipient contract calls token's transferFrom

     function deposit(uint256 amount) public {
         require(token.transferFrom(msg.sender, address(this), amount), "transferFrom failed");
     }
    
| improve this answer | |
  • How can a contract which uses the token creates an instance of the token for reference? The token may likely have already been deployed (with deploying address). Maybe the question of how a child contract can inherit from the token contract? I am looking for a way to refer the token in child contract which uses the token. – user938363 Oct 18 at 22:03
  • @user938363 If you know the address you can hardcode in the contract, or pass as parameter to the constructor. For example using OpenZeppelins IERC20 token = IERC20(0x12341234..); – Ismael Oct 19 at 11:27
0

In this post, the answer for case 1 There is a contract A and a contract B and they need to communicate. A is not B and B is not A, but A needs enough information about B in order to communicate with it, and the reverse. answers my question.

| improve this answer | |

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.