The platform needs a place to keep user-specific data. Inside the project, it is called an Exchange Account. It stores data such as deposited collaterals and rewards for staking.
The data structure is defined as:
- owner - public key belonging to the owner of the account. One owner can have only one account
- version - version of the structure. When it changes, old accounts migrates to a new one
- debt_shares - number of user's debt shares. Used to calculate debt. More about it here
- liquidation_deadline - slot when a user can be liquidated
- user_staking_data - all data that are needed for staking
- bump - seed used to ensure the generated address doesn't collide with any other existing one
- head - index pointing to the last used fields in collaterals array
- collaterals - an array of collaterals owned by account, having up to 32 different ones at the same time.
Function creating Exchange Account is defined here. It takes a single argument of bump (u8) and the following Context:
- exchange_account - address of the account. Constrains make sure it is uninitialized, has correct version and bump
- admin - public key belonging to the owner of the account
- payer - account that pays for the creation of the account
- rent - a data structure related to rent, used by Solana
- system_program - Solana's System Program needed to create an account
Creating an account requires using only a single statement:
Where the exchange is an instance of Exchange singleton and ownersPublicKey has a type of PublicKey, which is part of Solana's web3 package.
The exchangeAccount has a type of PublicKey, which is used as an address to a data structure. It can be passed to methods or used to fetch the whole structure. An example:
This will asynchronously fetch data from the blockchain and parse it to an object.