The provider and wallet that can sign for this margin account
Optional
pools: Record<string, Pool>Optional
walletTokens: MarginWalletTokensThe address of the MarginAccount
The address of the airspace this account is part of
Optional
infoThe raw accounts associated with the margin account.
Optional
liquidationThe decoded LiquidationData. This may only be present during liquidation.
The decoded MarginAccountData.
The decoded position data in the margin account.
The owner of the MarginAccount
The summarized PoolPosition array of pool deposits and borrows.
Optional
poolsThe parsed AccountPosition array of the margin account.
The provider and wallet that can sign for this margin account
The Valuation of the margin account.
Optional
walletStatic
Readonly
RISK_Static
Readonly
RISK_Static
Readonly
RISK_Static
Readonly
SEED_The maximum MarginAccount seed value equal to 65535
.
Seeds are a 16 bit number and therefor only 2^16 margin accounts may exist per wallet.
Static
Readonly
SETUP_The maximum risk indicator allowed by the library when setting up a
Returns true if a LiquidationData account exists and is associated with the MarginAccount. Certain actions are not allowed while liquidation is in progress.
Please use marginAccount.info.liquidation
instead
A qualitative measure of the the health of a margin account. A higher value means more risk in a qualitative sense. Properties: non-negative, range is [0, infinity) zero only when an account has no exposure at all account is subject to liquidation at a value of one
Send a transaction to close the MarginAccount and return rent to the owner. All positions must have a zero balance and be closed first.
// Close all positions. A non zero balance results in an error
for (const position of marginAccount.getPositions()) {
await marginAccount.closePosition(position)
}
// Close the account and send the transaction
await marginAccount.closeAccount()
MarginAccount
Send a transaction to close a position. A non-zero balance will result in a transaction error. There is a limited capacity for positions so it is recommended to close positions that are no longer needed.
// Load the pools
const poolManager = new PoolManager(programs, provider)
const pools = await poolManager.loadAll()
// Get the SOL position
const depositNoteMint = pools["SOL"].addresses.depositNoteMint
const position = marginAccount.getPosition(depositNoteMint)
await marginAccount.closePosition(position)
MarginAccount
Compute the risk indicator using components from Valuation
Create the margin account if it does not exist. If no seed is provided, one will be located.
// Load programs
const config = await MarginClient.getConfig("devnet")
const programs = MarginClient.getPrograms(provider, config)
// Load tokens and wallet
const pools = await poolManager.loadAll()
const walletTokens = await MarginAccount.loadTokens(programs, walletPubkey)
// Create margin account
const marginAccount = new MarginAccount({
programs,
provider,
walletPubkey,
0,
pools,
walletTokens
})
await marginAccount.createAccount()
MarginAccount
Derive the address of a metadata account.
Some account types such as pools, adapters and position mints have metadata associated with them. The metadata type is determined by the account type.
MarginAccount
Derive the address of a position token account associated with a MarginAccount and position token mint.
It is recommended to use other functions to find specfic position types. e.g. using Pool.findDepositPositionAddress
MarginAccount
Get the AccountPosition PublicKey and sends a transaction to create it if it doesn't exist.
It is recommended to use other functions to register specfic position types. e.g. using Pool.withRegisterDepositPosition
In web apps it's recommended to call withGetOrCreatePosition
as part of a larger
transaction to prompt for a wallet signature less often.
// Load margin pools
const poolManager = new PoolManager(programs, provider)
const pools = await poolManager.loadAll()
const depositNote = pools["SOL"].addresses.depositNoteMint
await marginAccount.getOrRegisterPosition(depositNote)
MarginAccount
Get the registerd AccountPosition associated with the position mint. Throws an error if the position does not exist.
MarginAccount
The position mint. For example a Pool deposit note mint.
Get the registerd AccountPosition associated with the position mint.
MarginAccount
The position mint. For example a Pool deposit note mint.
Get the time remaining on a liquidation until timeout in seconds.
If getRemainingLiquidationTime
is a negative number then liquidationEnd
can be called
by the margin account owner regardless of the current margin account health.
MarginAccount
Send a transaction to end a liquidation.
The MarginAccount can enter liquidation while it's riskIndicator
is at or above 1.0.
Liquidation is in progress when isBeingLiquidated
returns true.
Liquidation can only end when enough collateral is deposited or enough collateral is liquidated to lower riskIndicator
sufficiently.
MarginAccount
Sends a transaction to refresh the metadata for a position.
When a position is registered some position mint metadata is copied to the position. This data can become out of sync if the mint metadata is changed. Refreshing the position metadata may at the benefit or detriment to the owner.
MarginAccount
positionMint }
Sends a transaction to register an AccountPosition for the mint. When registering a Pool position,
the mint would not be Bitcoin or SOL, but rather the depositNoteMint
or loanNoteMint
found in pool.addresses
.
A margin account has a limited capacity of positions.
It is recommended to use other functions to register specfic position types. e.g. using Pool.withRegisterDepositPosition
In web apps it's is recommended to use withRegisterPosition
as part of a larget transaction
to prompt for a wallet signature less often.
// Load the pools
const poolManager = new PoolManager(programs, provider)
const pools = await poolManager.loadAll()
// Register the SOL deposit position
const depositNoteMint = pools["SOL"].addresses.depositNoteMint
await marginAccount.registerPosition(depositNoteMint)
MarginAccount
Sends a collection of transactions using the MarginAccount AnchorProvider.
This function has 2 additional features compared to sendAll
from web3.js or anchor.
TransactionInstruction[][]
this function will send those transactions in parallel.MarginAccount
Updates a single position balance. withUpdatePositionBalance
is often included
in transactions after modifying balances to synchronize with the margin account.
MarginAccount
position }
Create an instruction to perform an action by invoking other adapter programs, allowing them only to refresh the state of the margin account to be consistent with the actual underlying prices or positions, but not permitting new position changes.
This instruction is not invoked directly, but rather internally for example by Pool when depositing. Accounting invoke is necessary when several position values have to be refreshed but in the interim there aren't enough fresh positions to satisfy margin requirements.
MarginAccount
instructions, adapterProgram, adapterMetadata, adapterInstruction }
Optional
adapterOptional
adapterCreate an instruction that performs an action by invoking other adapter programs, allowing them to alter the balances of the token accounts belonging to this margin account. The transaction fails if the MarginAccount does not have sufficent collateral.
This instruction is not invoked directly, but rather internally for example by Pool when depositing.
MarginAccount
instructions, adapterInstruction }
Create an instruction to close the MarginAccount and return rent to the owner. All positions must have a zero balance and be closed first.
const instructions: TransactionInstruction[] = []
// Close all positions. A non zero balance results in an error
for (const position of marginAccount.getPositions()) {
await marginAccount.withClosePosition(instructions, position)
}
// Close the account and send the transaction
await marginAccount.withCloseAccount(instructions)
await marginAccount.sendAndConfirm(instructions)
MarginAccount
Create an instruction to close a position. A non-zero balance will result in a transaction error. There is a limited capacity for positions so it is recommended to close positions that are no longer needed.
// Load the pools
const poolManager = new PoolManager(programs, provider)
const pools = await poolManager.loadAll()
// Get the SOL position
const depositNoteMint = pools["SOL"].addresses.depositNoteMint
const position = marginAccount.getPosition(depositNoteMint)
const instructions: TransactionInstruction[] = []
await marginAccount.closePosition(instructions, position)
await marginAccount.sendAndConfirm(instructions)
MarginAccount
Get instruction to create the account if it does not exist.
const instructions: TransactionInstruction[] = []
await marginAccount.withCreateAccount(instructions)
if (instructions.length > 0) {
await marginAccount.sendAndConfirm(instructions)
}
MarginAccount
Get instruction to create a new deposit position
A deposit position are tokens deposited directly into a margin account, without the use of any other programs (like pools).
Returns the address of the token account to be created for the position.
Instructions array to append to.
The mint for the relevant token for the position
Get the AccountPosition PublicKey and appends an instructon to create it if it doesn't exist.
It is recommended to use other functions to register specfic position types. e.g. using Pool.withRegisterDepositPosition
// Load margin pools
const poolManager = new PoolManager(programs, provider)
const pools = await poolManager.loadAll()
// Register position
const positionTokenMint = pools["SOL"].addresses.depositNoteMint
const instructions: TransactionInstruction[] = []
await marginAccount.withGetOrRegisterPosition({ instructions, positionTokenMint })
await marginAccount.sendAndConfirm(instructions)
MarginAccount
The instructions to append to
The position mint to register a position for
Get instruction to end a liquidation
The MarginAccount can enter liquidation while it's riskIndicator
is at or above 1.0.
Liquidation is in progress when isBeingLiquidated
returns true.
The MarginAccount.provider
.wallet
will be used as the authority for the transaction.
The liquidator may end the liquidation at any time.
The margin account owner may end the liquidation only when at least one condition is true:
riskIndicator
sufficiently..getRemainingLiquidationTime()
is negativeMarginAccount
The instructions to append to
Creates an instruction to refresh the metadata for a position.
When a position is registered some position mint metadata is copied to the position. This data can become out of sync if the mint metadata is changed. Refreshing the position metadata may at the benefit or detriment to the owner.
MarginAccount
instructions, positionMint }
Get instruction to register new position
It is recommended to use other functions to register specfic position types. e.g. using Pool.withRegisterDepositPosition
// Load the pools
const poolManager = new PoolManager(programs, provider)
const pools = await poolManager.loadAll()
// Register the SOL deposit position
const positionTokenMint = pools["SOL"].addresses.depositNoteMint
const instructions: TransactionInstruction[] = []
const position = await marginAccount.withRegisterPosition({ instructions, positionTokenMint })
await marginAccount.sendAndConfirm(instructions)
Returns the instruction, and the address of the token account to be created for the position.
MarginAccount
Instructions array to append to.
The mint for the relevant token for the position
Create instructions to update all position balances. withUpdatePositionBalance
often included in
transactions after modifying balances ot synchronize with the margin account.
const instructions: TransactionInstruction[] = []
await marginAccount.withUpdateAllPositionBalances({ instructions })
await marginAccount.sendAndConfirm(instructions)
MarginAccount
instructions }
Get instruction to update the accounting for assets in the custody of the margin account.
// Load the pools
const poolManager = new PoolManager(programs, provider)
const pools = await poolManager.loadAll()
// Find the position
const depositNote = pools["SOL"].addresses.depositNoteMint
const position = marginAccount.getPosition(depositNote).address
// Update the position balance
const instructions: TransactionInstruction[] = []
await marginAccount.withUpdatePositionBalance({ instructions, position })
await marginAccount.sendAndConfirm(instructions)
{Promise
MarginAccount
instructions, position }
Static
createCreate the margin account if it does not exist. If no seed is provided, one will be located.
// Load programs
const config = await MarginClient.getConfig("devnet")
const programs = MarginClient.getPrograms(provider, config)
// Load tokens and wallet
const pools = await poolManager.loadAll()
const walletTokens = await MarginAccount.loadTokens(programs, walletPubkey)
// Create margin account
const marginAccount = await MarginAccount.createAccount({
programs,
provider,
owner: wallet.publicKey,
seed: 0,
pools,
walletTokens
})
MarginAccount
The address of the MarginAccount owner
Optional
pools?: Record<string, Pool>A Pool collection to calculate pool positions.
A provider that may be used to sign transactions modifying the account
Optional
seed?: numberThe seed or ID of the MarginAccount in the range of (0, 65535]
Optional
walletThe tokens in the owners wallet to determine max trade amounts.
Static
existsFetches the account and returns if it exists.
MarginAccount
Static
getSearches for a margin account that does not exist yet and returns its seed.
MarginAccount
Static
loadOptional
pools?: Record<string, Pool>Collection of Pool to calculate pool positions and prices.
The provider and wallet that can sign for this margin account
Optional
walletTokens owned by the wallet to calculate max deposit amounts.
Static
loadLoad all margin accounts for a wallet with an optional filter.
MarginAccount
programs, provider, pools, walletTokens, filters }
Optional
filters?: GetProgramAccountsFilter[]Optional
pools?: Record<string, Pool>Optional
walletStatic
loadLoads all tokens in the users wallet. Provides an array and a map of tokens mapped by pool.
MarginAccount
Generated using TypeDoc
Creates an instance of margin account.
Memberof
MarginAccount