πŸͺ™Token Vault

Simple ERC-20 Storage

Design Ethos

ERC-20s operate differently than Ether. Their interface and protocol for moving funds is divergent enough from the gas token to warrant its own interface. As such, the TokenVault takes the same mode as EtherVault, with additions required for the ERC-20 standard.

Storage

// Locksmith verifies key-holdership.
ILocksmith public locksmith;

// The Locksmith provides access to mutate the ledger.
ILedger public ledger;

// witnessed token addresses
// trust => [registered addresses]
mapping(uint256 => EnumerableSet.AddressSet) private witnessedTokenAddresses;
mapping(bytes32 => address) public arnContracts;

// we need to keep track of the deposit balances safely
mapping(address => uint256) tokenBalances;

witnessedTokenAddresses

ARNs are designed to be opaque because they don't inform the asset's behavior, just its uniqueness. For that reason, the TokenVault ensures that it keeps track of every ERC20 token address it's witnessed for each individual trust model.

arnContracts

During the deposit flow we compute the ARN using the token contract address. We store a mapping between the ARN and the contract for easy access.

tokenBalances

Similar in nature to EtherVault's etherBalance, is tracked for each token address.

Operations

The operations support the ability to deposit and withdrawal directly or through the ICollateralProvider ARN interface.

deposit

The message sender calls this after approving the vault's contract the proper amount to move funds from the sender to the contract. The contract ensures the caller is holding the claimed key, ensures the message caller is properly funded and then moves the assets into the vault. After they safely arrive, the deposit is recorded on the ledger. This operation will fail if the vault isn't trusted.

withdrawal

The withdrawal method supports multiple interfaces because it is a multi-asset vault. For this, it facilitates both the direct ERC20 token withdrawal interface, and the ARN-based interface for wallet orchestration.

The direct token method ultimately computes the ARN and calls the internal method.

The ICollateralProvider interface method is a direct bridge to the internal implementation:

With the implementation that feeds both interfaces here:

getTokenTypes

The TokenVault also provides an introspection method to audit all of the token addresses that have been witnessed as deposits for a given trust model.

Last updated