update state and instructions

This commit is contained in:
jordansexton 2021-06-10 22:43:35 -05:00
parent 002723fa44
commit 955ade8ae5
16 changed files with 126 additions and 76 deletions

View File

@ -10,6 +10,7 @@ import * as Layout from '../../utils/layout';
import { calculateUtilizationRatio, Reserve } from '../state/reserve'; import { calculateUtilizationRatio, Reserve } from '../state/reserve';
import { LendingInstruction } from './instruction'; import { LendingInstruction } from './instruction';
/// 10
/// Borrow liquidity from a reserve by depositing collateral tokens. Requires a refreshed /// Borrow liquidity from a reserve by depositing collateral tokens. Requires a refreshed
/// obligation and reserve. /// obligation and reserve.
/// ///
@ -28,6 +29,11 @@ import { LendingInstruction } from './instruction';
/// 8. `[]` Clock sysvar. /// 8. `[]` Clock sysvar.
/// 9. `[]` Token program id. /// 9. `[]` Token program id.
/// 10 `[optional, writable]` Host fee receiver account. /// 10 `[optional, writable]` Host fee receiver account.
///
/// BorrowObligationLiquidity {
/// /// Amount of liquidity to borrow - u64::MAX for 100% of borrowing power
/// liquidity_amount: u64,
/// },
export const borrowObligationLiquidityInstruction = ( export const borrowObligationLiquidityInstruction = (
liquidityAmount: number | BN, liquidityAmount: number | BN,
sourceLiquidity: PublicKey, sourceLiquidity: PublicKey,

View File

@ -9,6 +9,7 @@ import * as BufferLayout from 'buffer-layout';
import * as Layout from './../../utils/layout'; import * as Layout from './../../utils/layout';
import { LendingInstruction } from './instruction'; import { LendingInstruction } from './instruction';
/// 8
/// Deposit collateral to an obligation. Requires a refreshed reserve. /// Deposit collateral to an obligation. Requires a refreshed reserve.
/// ///
/// Accounts expected by this instruction: /// Accounts expected by this instruction:
@ -25,6 +26,11 @@ import { LendingInstruction } from './instruction';
/// 7. `[signer]` User transfer authority ($authority). /// 7. `[signer]` User transfer authority ($authority).
/// 8. `[]` Clock sysvar. /// 8. `[]` Clock sysvar.
/// 9. `[]` Token program id. /// 9. `[]` Token program id.
///
/// DepositObligationCollateral {
/// /// Amount of collateral tokens to deposit
/// collateral_amount: u64,
/// },
export const depositObligationCollateralInstruction = ( export const depositObligationCollateralInstruction = (
collateralAmount: number | BN, collateralAmount: number | BN,
sourceCollateral: PublicKey, sourceCollateral: PublicKey,

View File

@ -11,6 +11,7 @@ import { calculateUtilizationRatio, Reserve } from '../state/reserve';
import { calculateBorrowAPY } from './borrowObligationLiquidity'; import { calculateBorrowAPY } from './borrowObligationLiquidity';
import { LendingInstruction } from './instruction'; import { LendingInstruction } from './instruction';
/// 4
/// Deposit liquidity into a reserve in exchange for collateral. Collateral represents a share /// Deposit liquidity into a reserve in exchange for collateral. Collateral represents a share
/// of the reserve liquidity pool. /// of the reserve liquidity pool.
/// ///
@ -27,6 +28,11 @@ import { LendingInstruction } from './instruction';
/// 7. `[signer]` User transfer authority ($authority). /// 7. `[signer]` User transfer authority ($authority).
/// 8. `[]` Clock sysvar. /// 8. `[]` Clock sysvar.
/// 9. `[]` Token program id. /// 9. `[]` Token program id.
///
/// DepositReserveLiquidity {
/// /// Amount of liquidity to deposit in exchange for collateral tokens
/// liquidity_amount: u64,
/// },
export const depositReserveLiquidityInstruction = ( export const depositReserveLiquidityInstruction = (
liquidityAmount: number | BN, liquidityAmount: number | BN,
sourceLiquidity: PublicKey, sourceLiquidity: PublicKey,

View File

@ -8,26 +8,38 @@ import * as BufferLayout from 'buffer-layout';
import * as Layout from '../../utils/layout'; import * as Layout from '../../utils/layout';
import { LendingInstruction } from './instruction'; import { LendingInstruction } from './instruction';
// @TODO: move to @oyster/common
export const ORACLE_PROGRAM_ID = new PublicKey(
// @FIXME: replace with Pyth
'XXX',
);
/// 0
/// Initializes a new lending market. /// Initializes a new lending market.
/// ///
/// Accounts expected by this instruction: /// Accounts expected by this instruction:
/// ///
/// 0. `[writable]` Lending market account - uninitialized. /// 0. `[writable]` Lending market account - uninitialized.
/// 1. `[]` Quote currency SPL Token mint. /// 1. `[]` Rent sysvar.
/// 2. `[]` Rent sysvar. /// 2. `[]` Token program id.
/// 3. `[]` Token program id. /// 3. `[]` Oracle program id.
// InitLendingMarket { ///
// /// Owner authority which can add new reserves /// InitLendingMarket {
// owner: Pubkey, /// /// Owner authority which can add new reserves
// }, /// owner: Pubkey,
/// /// Currency market prices are quoted in
/// /// e.g. "USD" null padded (`*b"USD\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0"`) or SPL token mint pubkey
/// quote_currency: [u8; 32],
/// },
export const initLendingMarketInstruction = ( export const initLendingMarketInstruction = (
owner: PublicKey, owner: PublicKey,
quoteCurrency: Buffer,
lendingMarket: PublicKey, lendingMarket: PublicKey,
quoteTokenMint: PublicKey,
): TransactionInstruction => { ): TransactionInstruction => {
const dataLayout = BufferLayout.struct([ const dataLayout = BufferLayout.struct([
BufferLayout.u8('instruction'), BufferLayout.u8('instruction'),
Layout.publicKey('owner'), Layout.publicKey('owner'),
BufferLayout.blob(32, 'quoteCurrency'),
]); ]);
const data = Buffer.alloc(dataLayout.span); const data = Buffer.alloc(dataLayout.span);
@ -35,15 +47,16 @@ export const initLendingMarketInstruction = (
{ {
instruction: LendingInstruction.InitLendingMarket, instruction: LendingInstruction.InitLendingMarket,
owner, owner,
quoteCurrency,
}, },
data, data,
); );
const keys = [ const keys = [
{ pubkey: lendingMarket, isSigner: false, isWritable: true }, { pubkey: lendingMarket, isSigner: false, isWritable: true },
{ pubkey: quoteTokenMint, isSigner: false, isWritable: false },
{ pubkey: SYSVAR_RENT_PUBKEY, isSigner: false, isWritable: false }, { pubkey: SYSVAR_RENT_PUBKEY, isSigner: false, isWritable: false },
{ pubkey: TOKEN_PROGRAM_ID, isSigner: false, isWritable: false }, { pubkey: TOKEN_PROGRAM_ID, isSigner: false, isWritable: false },
{ pubkey: ORACLE_PROGRAM_ID, isSigner: false, isWritable: false },
]; ];
return new TransactionInstruction({ return new TransactionInstruction({

View File

@ -8,6 +8,7 @@ import {
import * as BufferLayout from 'buffer-layout'; import * as BufferLayout from 'buffer-layout';
import { LendingInstruction } from './instruction'; import { LendingInstruction } from './instruction';
/// 6
/// Initializes a new lending market obligation. /// Initializes a new lending market obligation.
/// ///
/// Accounts expected by this instruction: /// Accounts expected by this instruction:

View File

@ -11,6 +11,7 @@ import * as Layout from '../../utils/layout';
import { ReserveConfig } from '../state'; import { ReserveConfig } from '../state';
import { LendingInstruction } from './instruction'; import { LendingInstruction } from './instruction';
/// 2
/// Initializes a new lending market reserve. /// Initializes a new lending market reserve.
/// ///
/// Accounts expected by this instruction: /// Accounts expected by this instruction:
@ -22,25 +23,25 @@ import { LendingInstruction } from './instruction';
/// 3. `[]` Reserve liquidity SPL Token mint. /// 3. `[]` Reserve liquidity SPL Token mint.
/// 4. `[writable]` Reserve liquidity supply SPL Token account - uninitialized. /// 4. `[writable]` Reserve liquidity supply SPL Token account - uninitialized.
/// 5. `[writable]` Reserve liquidity fee receiver - uninitialized. /// 5. `[writable]` Reserve liquidity fee receiver - uninitialized.
/// 6. `[writable]` Reserve collateral SPL Token mint - uninitialized. /// 6. `[]` Pyth product account.
/// 7. `[writable]` Reserve collateral token supply - uninitialized. /// 7. `[]` Pyth price account.
/// 8. `[]` Quote currency SPL Token mint. /// This will be used as the reserve liquidity oracle account.
/// 9. `[]` Lending market account. /// 8. `[writable]` Reserve collateral SPL Token mint - uninitialized.
/// 10 `[]` Derived lending market authority. /// 9. `[writable]` Reserve collateral token supply - uninitialized.
/// 11 `[signer]` Lending market owner. /// 10 `[]` Lending market account.
/// 12 `[signer]` User transfer authority ($authority). /// 11 `[]` Derived lending market authority.
/// 13 `[]` Clock sysvar. /// 12 `[signer]` Lending market owner.
/// 13 `[]` Rent sysvar. /// 13 `[signer]` User transfer authority ($authority).
/// 14 `[]` Token program id. /// 14 `[]` Clock sysvar.
/// 15 `[optional]` Reserve liquidity oracle account. /// 15 `[]` Rent sysvar.
/// Not required for quote currency reserves. /// 16 `[]` Token program id.
/// Must match base and quote currency mint, and quote currency decimals. ///
// InitReserve { /// InitReserve {
// /// Initial amount of liquidity to deposit into the new reserve /// /// Initial amount of liquidity to deposit into the new reserve
// liquidity_amount: u64, /// liquidity_amount: u64,
// /// Reserve configuration values /// /// Reserve configuration values
// config: ReserveConfig, /// config: ReserveConfig,
// }, /// },
export const initReserveInstruction = ( export const initReserveInstruction = (
liquidityAmount: number | BN, liquidityAmount: number | BN,
config: ReserveConfig, config: ReserveConfig,
@ -50,13 +51,14 @@ export const initReserveInstruction = (
liquidityMint: PublicKey, liquidityMint: PublicKey,
liquiditySupply: PublicKey, liquiditySupply: PublicKey,
liquidityFeeReceiver: PublicKey, liquidityFeeReceiver: PublicKey,
pythProduct: PublicKey,
pythPrice: PublicKey,
collateralMint: PublicKey, collateralMint: PublicKey,
collateralSupply: PublicKey, collateralSupply: PublicKey,
lendingMarket: PublicKey, lendingMarket: PublicKey,
lendingMarketAuthority: PublicKey, lendingMarketAuthority: PublicKey,
lendingMarketOwner: PublicKey, lendingMarketOwner: PublicKey,
transferAuthority: PublicKey, transferAuthority: PublicKey,
oracle?: PublicKey,
): TransactionInstruction => { ): TransactionInstruction => {
const dataLayout = BufferLayout.struct([ const dataLayout = BufferLayout.struct([
BufferLayout.u8('instruction'), BufferLayout.u8('instruction'),
@ -97,6 +99,8 @@ export const initReserveInstruction = (
{ pubkey: liquidityMint, isSigner: false, isWritable: false }, { pubkey: liquidityMint, isSigner: false, isWritable: false },
{ pubkey: liquiditySupply, isSigner: false, isWritable: true }, { pubkey: liquiditySupply, isSigner: false, isWritable: true },
{ pubkey: liquidityFeeReceiver, isSigner: false, isWritable: true }, { pubkey: liquidityFeeReceiver, isSigner: false, isWritable: true },
{ pubkey: pythProduct, isSigner: false, isWritable: false },
{ pubkey: pythPrice, isSigner: false, isWritable: false },
{ pubkey: collateralMint, isSigner: false, isWritable: true }, { pubkey: collateralMint, isSigner: false, isWritable: true },
{ pubkey: collateralSupply, isSigner: false, isWritable: true }, { pubkey: collateralSupply, isSigner: false, isWritable: true },
{ pubkey: lendingMarket, isSigner: false, isWritable: true }, { pubkey: lendingMarket, isSigner: false, isWritable: true },
@ -107,10 +111,6 @@ export const initReserveInstruction = (
{ pubkey: TOKEN_PROGRAM_ID, isSigner: false, isWritable: false }, { pubkey: TOKEN_PROGRAM_ID, isSigner: false, isWritable: false },
]; ];
if (oracle) {
keys.push({ pubkey: oracle, isSigner: false, isWritable: false });
}
return new TransactionInstruction({ return new TransactionInstruction({
keys, keys,
programId: LENDING_PROGRAM_ID, programId: LENDING_PROGRAM_ID,

View File

@ -9,6 +9,7 @@ import * as BufferLayout from 'buffer-layout';
import * as Layout from '../../utils/layout'; import * as Layout from '../../utils/layout';
import { LendingInstruction } from './instruction'; import { LendingInstruction } from './instruction';
/// 12
/// Repay borrowed liquidity to a reserve to receive collateral at a discount from an unhealthy /// Repay borrowed liquidity to a reserve to receive collateral at a discount from an unhealthy
/// obligation. Requires a refreshed obligation and reserves. /// obligation. Requires a refreshed obligation and reserves.
/// ///
@ -29,6 +30,11 @@ import { LendingInstruction } from './instruction';
/// 9. `[signer]` User transfer authority ($authority). /// 9. `[signer]` User transfer authority ($authority).
/// 10 `[]` Clock sysvar. /// 10 `[]` Clock sysvar.
/// 11 `[]` Token program id. /// 11 `[]` Token program id.
///
/// LiquidateObligation {
/// /// Amount of liquidity to repay - u64::MAX for up to 100% of borrowed amount
/// liquidity_amount: u64,
/// },
export const liquidateObligationInstruction = ( export const liquidateObligationInstruction = (
liquidityAmount: number | BN, liquidityAmount: number | BN,
sourceLiquidity: PublicKey, sourceLiquidity: PublicKey,

View File

@ -9,6 +9,7 @@ import * as BufferLayout from 'buffer-layout';
import * as Layout from '../../utils/layout'; import * as Layout from '../../utils/layout';
import { LendingInstruction } from './instruction'; import { LendingInstruction } from './instruction';
/// 5
/// Redeem collateral from a reserve in exchange for liquidity. /// Redeem collateral from a reserve in exchange for liquidity.
/// ///
/// Accounts expected by this instruction: /// Accounts expected by this instruction:
@ -24,6 +25,11 @@ import { LendingInstruction } from './instruction';
/// 7. `[signer]` User transfer authority ($authority). /// 7. `[signer]` User transfer authority ($authority).
/// 8. `[]` Clock sysvar. /// 8. `[]` Clock sysvar.
/// 9. `[]` Token program id. /// 9. `[]` Token program id.
///
/// RedeemReserveCollateral {
/// /// Amount of collateral tokens to redeem in exchange for liquidity
/// collateral_amount: u64,
/// },
export const redeemReserveCollateralInstruction = ( export const redeemReserveCollateralInstruction = (
collateralAmount: number | BN, collateralAmount: number | BN,
sourceCollateral: PublicKey, sourceCollateral: PublicKey,

View File

@ -7,6 +7,7 @@ import {
import BufferLayout from 'buffer-layout'; import BufferLayout from 'buffer-layout';
import { LendingInstruction } from './instruction'; import { LendingInstruction } from './instruction';
/// 7
/// Refresh an obligation's accrued interest and collateral and liquidity prices. Requires /// Refresh an obligation's accrued interest and collateral and liquidity prices. Requires
/// refreshed reserves, as all obligation collateral deposit reserves in order, followed by all /// refreshed reserves, as all obligation collateral deposit reserves in order, followed by all
/// liquidity borrow reserves in order. /// liquidity borrow reserves in order.

View File

@ -7,18 +7,18 @@ import {
import * as BufferLayout from 'buffer-layout'; import * as BufferLayout from 'buffer-layout';
import { LendingInstruction } from './instruction'; import { LendingInstruction } from './instruction';
/// 3
/// Accrue interest and update market price of liquidity on a reserve. /// Accrue interest and update market price of liquidity on a reserve.
/// ///
/// Accounts expected by this instruction: /// Accounts expected by this instruction:
/// ///
/// 0. `[writable]` Reserve account. /// 0. `[writable]` Reserve account.
/// 1. `[]` Clock sysvar. /// 1. `[]` Reserve liquidity oracle account.
/// 2. `[optional]` Reserve liquidity oracle account. /// Must be the Pyth price account specified at InitReserve.
/// Required if the reserve currency is not the lending market quote /// 2. `[]` Clock sysvar.
/// currency.
export const refreshReserveInstruction = ( export const refreshReserveInstruction = (
reserve: PublicKey, reserve: PublicKey,
oracle?: PublicKey, oracle: PublicKey,
): TransactionInstruction => { ): TransactionInstruction => {
const dataLayout = BufferLayout.struct([BufferLayout.u8('instruction')]); const dataLayout = BufferLayout.struct([BufferLayout.u8('instruction')]);
@ -27,13 +27,10 @@ export const refreshReserveInstruction = (
const keys = [ const keys = [
{ pubkey: reserve, isSigner: false, isWritable: true }, { pubkey: reserve, isSigner: false, isWritable: true },
{ pubkey: oracle, isSigner: false, isWritable: false },
{ pubkey: SYSVAR_CLOCK_PUBKEY, isSigner: false, isWritable: false }, { pubkey: SYSVAR_CLOCK_PUBKEY, isSigner: false, isWritable: false },
]; ];
if (oracle) {
keys.push({ pubkey: oracle, isSigner: false, isWritable: false });
}
return new TransactionInstruction({ return new TransactionInstruction({
keys, keys,
programId: LENDING_PROGRAM_ID, programId: LENDING_PROGRAM_ID,

View File

@ -9,6 +9,7 @@ import * as BufferLayout from 'buffer-layout';
import * as Layout from '../../utils/layout'; import * as Layout from '../../utils/layout';
import { LendingInstruction } from './instruction'; import { LendingInstruction } from './instruction';
/// 11
/// Repay borrowed liquidity to a reserve. Requires a refreshed obligation and reserve. /// Repay borrowed liquidity to a reserve. Requires a refreshed obligation and reserve.
/// ///
/// Accounts expected by this instruction: /// Accounts expected by this instruction:
@ -20,10 +21,14 @@ import { LendingInstruction } from './instruction';
/// 2. `[writable]` Repay reserve account - refreshed. /// 2. `[writable]` Repay reserve account - refreshed.
/// 3. `[writable]` Obligation account - refreshed. /// 3. `[writable]` Obligation account - refreshed.
/// 4. `[]` Lending market account. /// 4. `[]` Lending market account.
/// 5. `[]` Derived lending market authority. /// 5. `[signer]` User transfer authority ($authority).
/// 6. `[signer]` User transfer authority ($authority). /// 6. `[]` Clock sysvar.
/// 7. `[]` Clock sysvar. /// 7. `[]` Token program id.
/// 8. `[]` Token program id. ///
/// RepayObligationLiquidity {
/// /// Amount of liquidity to repay - u64::MAX for 100% of borrowed amount
/// liquidity_amount: u64,
/// },
export const repayObligationLiquidityInstruction = ( export const repayObligationLiquidityInstruction = (
liquidityAmount: number | BN, liquidityAmount: number | BN,
sourceLiquidity: PublicKey, sourceLiquidity: PublicKey,
@ -31,7 +36,6 @@ export const repayObligationLiquidityInstruction = (
repayReserve: PublicKey, repayReserve: PublicKey,
obligation: PublicKey, obligation: PublicKey,
lendingMarket: PublicKey, lendingMarket: PublicKey,
lendingMarketAuthority: PublicKey,
transferAuthority: PublicKey, transferAuthority: PublicKey,
): TransactionInstruction => { ): TransactionInstruction => {
const dataLayout = BufferLayout.struct([ const dataLayout = BufferLayout.struct([
@ -54,7 +58,6 @@ export const repayObligationLiquidityInstruction = (
{ pubkey: repayReserve, isSigner: false, isWritable: true }, { pubkey: repayReserve, isSigner: false, isWritable: true },
{ pubkey: obligation, isSigner: false, isWritable: true }, { pubkey: obligation, isSigner: false, isWritable: true },
{ pubkey: lendingMarket, isSigner: false, isWritable: false }, { pubkey: lendingMarket, isSigner: false, isWritable: false },
{ pubkey: lendingMarketAuthority, isSigner: false, isWritable: false },
{ pubkey: transferAuthority, isSigner: true, isWritable: false }, { pubkey: transferAuthority, isSigner: true, isWritable: false },
{ pubkey: SYSVAR_CLOCK_PUBKEY, isSigner: false, isWritable: false }, { pubkey: SYSVAR_CLOCK_PUBKEY, isSigner: false, isWritable: false },
{ pubkey: TOKEN_PROGRAM_ID, isSigner: false, isWritable: false }, { pubkey: TOKEN_PROGRAM_ID, isSigner: false, isWritable: false },

View File

@ -4,16 +4,18 @@ import * as BufferLayout from 'buffer-layout';
import * as Layout from '../../utils/layout'; import * as Layout from '../../utils/layout';
import { LendingInstruction } from './instruction'; import { LendingInstruction } from './instruction';
/// 1
/// Sets the new owner of a lending market. /// Sets the new owner of a lending market.
/// ///
/// Accounts expected by this instruction: /// Accounts expected by this instruction:
/// ///
/// 0. `[writable]` Lending market account. /// 0. `[writable]` Lending market account.
/// 1. `[signer]` Current owner. /// 1. `[signer]` Current owner.
// SetLendingMarketOwner { ///
// /// The new owner /// SetLendingMarketOwner {
// new_owner: Pubkey, /// /// The new owner
// }, /// new_owner: Pubkey,
/// },
export const setLendingMarketOwnerInstruction = ( export const setLendingMarketOwnerInstruction = (
newOwner: PublicKey, newOwner: PublicKey,
lendingMarket: PublicKey, lendingMarket: PublicKey,

View File

@ -9,6 +9,7 @@ import * as BufferLayout from 'buffer-layout';
import * as Layout from './../../utils/layout'; import * as Layout from './../../utils/layout';
import { LendingInstruction } from './instruction'; import { LendingInstruction } from './instruction';
/// 9
/// Withdraw collateral from an obligation. Requires a refreshed obligation and reserve. /// Withdraw collateral from an obligation. Requires a refreshed obligation and reserve.
/// ///
/// Accounts expected by this instruction: /// Accounts expected by this instruction:
@ -23,6 +24,11 @@ import { LendingInstruction } from './instruction';
/// 6. `[signer]` Obligation owner. /// 6. `[signer]` Obligation owner.
/// 7. `[]` Clock sysvar. /// 7. `[]` Clock sysvar.
/// 8. `[]` Token program id. /// 8. `[]` Token program id.
///
/// WithdrawObligationCollateral {
/// /// Amount of collateral tokens to withdraw - u64::MAX for up to 100% of deposited amount
/// collateral_amount: u64,
/// },
export const withdrawObligationCollateralInstruction = ( export const withdrawObligationCollateralInstruction = (
collateralAmount: number | BN, collateralAmount: number | BN,
sourceCollateral: PublicKey, sourceCollateral: PublicKey,

View File

@ -5,7 +5,7 @@ import * as Layout from '../../utils/layout';
export interface LendingMarket { export interface LendingMarket {
version: number; version: number;
isInitialized: boolean; isInitialized: boolean;
quoteTokenMint: PublicKey; quoteCurrency: Buffer;
tokenProgramId: PublicKey; tokenProgramId: PublicKey;
} }
@ -14,9 +14,9 @@ export const LendingMarketLayout: typeof BufferLayout.Structure = BufferLayout.s
BufferLayout.u8('version'), BufferLayout.u8('version'),
BufferLayout.u8('bumpSeed'), BufferLayout.u8('bumpSeed'),
Layout.publicKey('owner'), Layout.publicKey('owner'),
Layout.publicKey('quoteTokenMint'), BufferLayout.blob(32, 'quoteCurrency'),
Layout.publicKey('tokenProgramId'), Layout.publicKey('tokenProgramId'),
Layout.publicKey('oracleProgramId'),
BufferLayout.blob(128, 'padding'), BufferLayout.blob(128, 'padding'),
], ],
); );

View File

@ -55,7 +55,7 @@ export const ObligationCollateralLayout: typeof BufferLayout.Structure = BufferL
[ [
Layout.publicKey('depositReserve'), Layout.publicKey('depositReserve'),
Layout.uint64('depositedAmount'), Layout.uint64('depositedAmount'),
Layout.uint64('marketValue'), Layout.uint128('marketValue'),
], ],
); );
@ -64,7 +64,7 @@ export const ObligationLiquidityLayout: typeof BufferLayout.Structure = BufferLa
Layout.publicKey('borrowReserve'), Layout.publicKey('borrowReserve'),
Layout.uint128('cumulativeBorrowRateWads'), Layout.uint128('cumulativeBorrowRateWads'),
Layout.uint128('borrowedAmountWads'), Layout.uint128('borrowedAmountWads'),
Layout.uint64('marketValue'), Layout.uint128('marketValue'),
], ],
); );
@ -109,19 +109,16 @@ export const ObligationParser = (
return; return;
} }
const depositsBuffer = dataFlat.slice( const depositsSpan = depositsLen * ObligationCollateralLayout.span;
0, const borrowsSpan = borrowsLen * ObligationLiquidityLayout.span;
depositsLen * ObligationCollateralLayout.span,
); const depositsBuffer = dataFlat.slice(0, depositsSpan);
const deposits = BufferLayout.seq( const deposits = BufferLayout.seq(
ObligationCollateralLayout, ObligationCollateralLayout,
depositsLen, depositsLen,
).decode(depositsBuffer) as ObligationCollateral[]; ).decode(depositsBuffer) as ObligationCollateral[];
const borrowsBuffer = dataFlat.slice( const borrowsBuffer = dataFlat.slice(depositsSpan, depositsSpan + borrowsSpan);
depositsBuffer.length,
borrowsLen * ObligationLiquidityLayout.span,
);
const borrows = BufferLayout.seq( const borrows = BufferLayout.seq(
ObligationLiquidityLayout, ObligationLiquidityLayout,
borrowsLen, borrowsLen,

View File

@ -23,9 +23,9 @@ export interface ReserveLiquidity {
oracleOption: number; oracleOption: number;
oraclePubkey: PublicKey; oraclePubkey: PublicKey;
availableAmount: BN; availableAmount: BN;
borrowedAmountWads: BN; borrowedAmountWads: BN; // decimals
cumulativeBorrowRateWads: BN; cumulativeBorrowRateWads: BN; // decimals
marketPrice: BN; marketPrice: BN; // decimals
} }
export interface ReserveCollateral { export interface ReserveCollateral {
@ -62,14 +62,11 @@ export const ReserveLayout: typeof BufferLayout.Structure = BufferLayout.struct(
BufferLayout.u8('mintDecimals'), BufferLayout.u8('mintDecimals'),
Layout.publicKey('supplyPubkey'), Layout.publicKey('supplyPubkey'),
Layout.publicKey('feeReceiver'), Layout.publicKey('feeReceiver'),
// @FIXME: oracle option Layout.publicKey('oraclePubkey'),
// TODO: replace u32 option with generic equivalent
BufferLayout.u32('oracleOption'),
Layout.publicKey('oracle'),
Layout.uint64('availableAmount'), Layout.uint64('availableAmount'),
Layout.uint128('borrowedAmountWads'), Layout.uint128('borrowedAmountWads'),
Layout.uint128('cumulativeBorrowRateWads'), Layout.uint128('cumulativeBorrowRateWads'),
Layout.uint64('marketPrice'), Layout.uint128('marketPrice'),
], ],
'liquidity', 'liquidity',
), ),
@ -93,14 +90,18 @@ export const ReserveLayout: typeof BufferLayout.Structure = BufferLayout.struct(
BufferLayout.u8('optimalBorrowRate'), BufferLayout.u8('optimalBorrowRate'),
BufferLayout.u8('maxBorrowRate'), BufferLayout.u8('maxBorrowRate'),
BufferLayout.struct( BufferLayout.struct(
[Layout.uint64('borrowFeeWad'), BufferLayout.u8('hostFeePercentage')], [
Layout.uint64('borrowFeeWad'),
Layout.uint64('flashLoanFeeWad'),
BufferLayout.u8('hostFeePercentage')
],
'fees', 'fees',
), ),
], ],
'config' 'config'
), ),
BufferLayout.blob(256, 'padding'), BufferLayout.blob(248, 'padding'),
], ],
); );
@ -146,7 +147,6 @@ export const reserveMarketCap = (reserve?: Reserve) => {
reserve?.liquidity.borrowedAmountWads, reserve?.liquidity.borrowedAmountWads,
).toNumber(); ).toNumber();
const total = available + borrowed; const total = available + borrowed;
return total; return total;
}; };