Skip to main content

Exchange Account

The Idea of Exchange Account#

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.

Structure of Account#

The data structure is defined as:

struct ExchangeAccount {
pub owner: Pubkey,
pub version: u8,
pub debt_shares: u64,
pub liquidation_deadline: u64,
pub user_staking_data: UserStaking,
pub bump: u8,
pub head: u8,
pub collaterals: [CollateralEntry; 32],
  • 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.

Account Creation#

Function creating Exchange Account is defined here. It takes a single argument of bump (u8) and the following Context:

pub struct CreateExchangeAccount<'info> {
pub exchange_account: Loader<'info, ExchangeAccount>,
pub admin: AccountInfo<'info>,
pub payer: AccountInfo<'info>,
pub rent: Sysvar<'info, Rent>,
pub system_program: AccountInfo<'info>,


The #[account(...)] parts are constraints implemented in Anchor.

  • 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

Interacting using SDK#

Creating an account requires using only a single statement:

const exchangeAccount = await exchange.createExchangeAccount(ownersPublicKey)

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:

const exchangeAccountData = await exchange.getExchangeAccount(exchangeAccount)

This will asynchronously fetch data from the blockchain and parse it to an object.