solidity-mentorship
solidity-mentorship copied to clipboard
Vault 1
Users can send a given Token to a Vault contract, that records their balance, only that owner can retrieve that token. Think of it as a multi-user safe.
To complete this you will need two contracts, one of them an ERC20 token that will be moved around, and another one the Vault that will store it for the users.
There are a couple of patterns to achieve this, but I suggest that you take the traditional approach:
- Vault should import the IERC20 interface, take the
Tokenaddress in theVaultconstructor, and cast it into an IERC20 state variable. - The user
approvetheVaultto takeTokenfrom them in one transaction. - The user calls a function that instructs the
Vaultto take from them an amount ofToken, which now belongs to theVault, in exchange, theVaultrecords the user'sdeposit. - Whenever the user feels like it, it can
withdrawits deposit. TheTokenis returned, and theVaultupdates its user records.