ERC-20 tokens can be integrated into other smart contracts in the ecosystem by calling the functions defined in the ERC-20 interface. Here is an example of a smart contract that integrates an ERC-20 token:
In this example, the MyContract smart contract uses the ERC-20 interface to transfer tokens. The token variable is an instance of the IERC20 interface and is initialized with the ERC-20 token contract address. The transferToken function is called by the smart contract user and uses the transfer function from the ERC-20 interface to transfer tokens to the recipient.
To transfer tokens to a smart contract and allow the smart contract to account for that transfer, you can use the approve() and transferFrom() functions of the ERC20 contract.
Here's an example of how it works:
First, the user needs to approve the smart contract to spend a certain amount of tokens. This is done by calling the approve() function on the ERC20 contract:
This approves the smart contract to spend amount of tokens from the user's account.
Next, the smart contract can call the transferFrom() function on the ERC20 contract to transfer the tokens from the user's account to the smart contract's account:
This transfers amount of tokens from the user's account to the smart contract's account. The smart contract can then account for this transfer in its logic.
It's important to note that the smart contract will need to implement the ERC20 interface and have a balance mapping to keep track of the token balances. Here's an example of how this could be done:
In this case, we would call poolTokens on Spender and this contract would pull those tokens from the ERC20 contract. Then the Spender contract can account for this balance change, by keeping its own record in the pooled mapping.