From 5c948d83d313788bd6b4ae5815a904ae520455d5 Mon Sep 17 00:00:00 2001 From: bartosz-lipinski <264380+bartosz-lipinski@users.noreply.github.com> Date: Fri, 22 Jan 2021 22:13:22 -0600 Subject: [PATCH] feat: init obligation. resolves #39 --- src/actions/borrow.tsx | 19 ++++++++++-- src/models/lending/borrow.ts | 23 ++++++-------- src/models/lending/obligation.ts | 51 +++++++++++++++++++++++++++++++- 3 files changed, 76 insertions(+), 17 deletions(-) diff --git a/src/actions/borrow.tsx b/src/actions/borrow.tsx index 220b7c8..df442b2 100644 --- a/src/actions/borrow.tsx +++ b/src/actions/borrow.tsx @@ -26,6 +26,7 @@ import { BorrowAmountType, LendingObligation, approve, + initObligationInstruction, } from "../models"; import { toLamports } from "../utils/utils"; @@ -185,7 +186,22 @@ export const borrow = async ( ) : undefined; - // deposit + if (!obligationAccount) { + instructions.push( + initObligationInstruction( + depositReserve.pubkey, + borrowReserve.pubkey, + obligation, + obligationMint, + obligationTokenOutput, + wallet.publicKey, + depositReserve.info.lendingMarket, + authority, + ) + ); + } + + // borrow instructions.push( borrowInstruction( amountLamports, @@ -202,7 +218,6 @@ export const borrow = async ( obligation, obligationMint, obligationTokenOutput, - wallet.publicKey, depositReserve.info.lendingMarket, authority, diff --git a/src/models/lending/borrow.ts b/src/models/lending/borrow.ts index 999bb75..ab740c4 100644 --- a/src/models/lending/borrow.ts +++ b/src/models/lending/borrow.ts @@ -1,4 +1,4 @@ -import { PublicKey, SYSVAR_CLOCK_PUBKEY, SYSVAR_RENT_PUBKEY, TransactionInstruction } from '@solana/web3.js'; +import { PublicKey, SYSVAR_CLOCK_PUBKEY, TransactionInstruction } from '@solana/web3.js'; import BN from 'bn.js'; import * as BufferLayout from 'buffer-layout'; import { TOKEN_PROGRAM_ID, LENDING_PROGRAM_ID } from '../../utils/ids'; @@ -24,16 +24,14 @@ export enum BorrowAmountType { /// 6. `[writable]` Obligation /// 7. `[writable]` Obligation token mint /// 8. `[writable]` Obligation token output -/// 9. `[]` Obligation token owner -/// 10 `[]` Lending market account. -/// 11 `[]` Derived lending market authority. -/// 12 `[]` User transfer authority ($authority). -/// 13 `[]` Dex market -/// 14 `[]` Dex market order book side -/// 15 `[]` Temporary memory -/// 16 `[]` Clock sysvar -/// 17 `[]` Rent sysvar -/// 18 '[]` Token program id +/// 8 `[]` Lending market account. +/// 10 `[]` Derived lending market authority. +/// 11 `[]` User transfer authority ($authority). +/// 12 `[]` Dex market +/// 13 `[]` Dex market order book side +/// 14 `[]` Temporary memory +/// 15 `[]` Clock sysvar +/// 16 '[]` Token program id export const borrowInstruction = ( amount: number | BN, amountType: BorrowAmountType, @@ -49,7 +47,6 @@ export const borrowInstruction = ( obligation: PublicKey, obligationMint: PublicKey, obligationTokenOutput: PublicKey, - obligationTokenOwner: PublicKey, lendingMarket: PublicKey, lendingMarketAuthority: PublicKey, @@ -98,7 +95,6 @@ export const borrowInstruction = ( { pubkey: obligation, isSigner: false, isWritable: true }, { pubkey: obligationMint, isSigner: false, isWritable: true }, { pubkey: obligationTokenOutput, isSigner: false, isWritable: true }, - { pubkey: obligationTokenOwner, isSigner: false, isWritable: false }, { pubkey: lendingMarket, isSigner: false, isWritable: false }, { pubkey: lendingMarketAuthority, isSigner: false, isWritable: false }, @@ -108,7 +104,6 @@ export const borrowInstruction = ( { pubkey: dexOrderBookSide, isSigner: false, isWritable: false }, { pubkey: memory, isSigner: false, isWritable: false }, { pubkey: SYSVAR_CLOCK_PUBKEY, isSigner: false, isWritable: false }, - { pubkey: SYSVAR_RENT_PUBKEY, isSigner: false, isWritable: false }, { pubkey: TOKEN_PROGRAM_ID, isSigner: false, isWritable: false }, ]; diff --git a/src/models/lending/obligation.ts b/src/models/lending/obligation.ts index 7da30b6..e238e58 100644 --- a/src/models/lending/obligation.ts +++ b/src/models/lending/obligation.ts @@ -1,6 +1,8 @@ -import { AccountInfo, PublicKey } from "@solana/web3.js"; +import { AccountInfo, PublicKey, SYSVAR_CLOCK_PUBKEY, SYSVAR_RENT_PUBKEY, TransactionInstruction } from "@solana/web3.js"; import BN from "bn.js"; import * as BufferLayout from "buffer-layout"; +import { LendingInstruction } from "."; +import { LENDING_PROGRAM_ID, TOKEN_PROGRAM_ID } from "../../utils/ids"; import * as Layout from "./../../utils/layout"; export const LendingObligationLayout: typeof BufferLayout.Structure = BufferLayout.struct( @@ -60,3 +62,50 @@ export const LendingObligationParser = ( export const healthFactorToRiskColor = (health: number) => { return ''; } + +export const initObligationInstruction = ( + depositReserve: PublicKey, + borrowReserve: PublicKey, + obligation: PublicKey, + obligationMint: PublicKey, + obligationTokenOutput: PublicKey, + obligationTokenOwner: PublicKey, + lendingMarket: PublicKey, + lendingMarketAuthority: PublicKey, + + + +): TransactionInstruction => { + const dataLayout = BufferLayout.struct([ + BufferLayout.u8('instruction'), + ]); + + const data = Buffer.alloc(dataLayout.span); + dataLayout.encode( + { + instruction: LendingInstruction.InitObligation, + }, + data + ); + + const keys = [ + { pubkey: depositReserve, isSigner: false, isWritable: true }, + { pubkey: borrowReserve, isSigner: false, isWritable: true }, + { pubkey: obligation, isSigner: false, isWritable: true }, + { pubkey: obligationMint, isSigner: false, isWritable: true }, + { pubkey: obligationTokenOutput, isSigner: false, isWritable: true }, + { pubkey: obligationTokenOwner, isSigner: false, isWritable: false }, + + { pubkey: lendingMarket, isSigner: false, isWritable: false }, + { pubkey: lendingMarketAuthority, isSigner: false, isWritable: false }, + + { pubkey: SYSVAR_CLOCK_PUBKEY, isSigner: false, isWritable: false }, + { pubkey: SYSVAR_RENT_PUBKEY, isSigner: false, isWritable: false }, + { pubkey: TOKEN_PROGRAM_ID, isSigner: false, isWritable: false }, + ]; + return new TransactionInstruction({ + keys, + programId: LENDING_PROGRAM_ID, + data, + }); +};