Skip to content
This repository has been archived by the owner on Jan 13, 2025. It is now read-only.

Commit

Permalink
wip
Browse files Browse the repository at this point in the history
  • Loading branch information
jordaaash committed Apr 16, 2021
1 parent 91b6fff commit 456f32c
Show file tree
Hide file tree
Showing 105 changed files with 2,129 additions and 1,813 deletions.
6 changes: 3 additions & 3 deletions packages/lending/src/App.tsx
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import React from "react";
import "./App.less";
import { Routes } from "./routes";
import React from 'react';
import './App.less';
import { Routes } from './routes';

function App() {
return <Routes />;
Expand Down
Original file line number Diff line number Diff line change
@@ -1,37 +1,33 @@
import {
Account,
Connection,
PublicKey,
TransactionInstruction,
} from '@solana/web3.js';
import {
contexts,
utils,
actions,
contexts,
LEND_HOST_FEE_ADDRESS,
LENDING_PROGRAM_ID,
models,
TokenAccount,
notify,
ParsedAccount,
TokenAccount,
toLamports,
} from '@oyster/common';

import { AccountLayout, MintInfo } from '@solana/spl-token';
import {
accrueInterestInstruction,
LendingReserve,
} from './../models/lending/reserve';
import { AccountLayout, MintInfo, MintLayout } from '@solana/spl-token';

import { createUninitializedObligation } from './obligation';

Account,
Connection,
PublicKey,
TransactionInstruction,
} from '@solana/web3.js';
import {
LendingObligationLayout,
borrowInstruction,
LendingMarket,
BorrowAmountType,
LendingObligation,
borrowObligationLiquidityInstruction,
initObligationInstruction,
Obligation,
ObligationLayout,
refreshReserveInstruction,
Reserve,
} from '../models';
import { createObligation } from './createObligation';

const { approve } = models;
const { toLamports, LENDING_PROGRAM_ID, LEND_HOST_FEE_ADDRESS, notify } = utils;
const { cache, MintParser } = contexts.Accounts;
const { sendTransaction } = contexts.Connection;
const {
Expand All @@ -42,19 +38,15 @@ const {
findOrCreateAccountByMint,
} = actions;

export const borrow = async (
// @FIXME
export const borrowObligationLiquidity = async (
connection: Connection,
wallet: any,

from: TokenAccount,
amount: number,
amountType: BorrowAmountType,

borrowReserve: ParsedAccount<LendingReserve>,

depositReserve: ParsedAccount<LendingReserve>,

existingObligation?: ParsedAccount<LendingObligation>,
borrowReserve: ParsedAccount<Reserve>,
depositReserve: ParsedAccount<Reserve>,
existingObligation: ParsedAccount<Obligation>,

obligationAccount?: PublicKey,
) => {
Expand All @@ -69,7 +61,7 @@ export const borrow = async (
let cleanupInstructions: TransactionInstruction[] = [];
let finalCleanupInstructions: TransactionInstruction[] = [];

const [authority] = await PublicKey.findProgramAddress(
const [lendingMarketAuthority] = await PublicKey.findProgramAddress(
[depositReserve.info.lendingMarket.toBuffer()],
LENDING_PROGRAM_ID,
);
Expand All @@ -80,44 +72,21 @@ export const borrow = async (

const obligation = existingObligation
? existingObligation.pubkey
: createUninitializedObligation(
: createObligation(
instructions,
wallet.publicKey,
await connection.getMinimumBalanceForRentExemption(
LendingObligationLayout.span,
ObligationLayout.span,
),
signers,
);

const obligationMint = existingObligation
? existingObligation.info.tokenMint
: createUninitializedMint(
instructions,
wallet.publicKey,
await connection.getMinimumBalanceForRentExemption(MintLayout.span),
signers,
);

const obligationTokenOutput = obligationAccount
? obligationAccount
: createUninitializedAccount(
instructions,
wallet.publicKey,
accountRentExempt,
signers,
);

if (!obligationAccount) {
instructions.push(
initObligationInstruction(
depositReserve.pubkey,
borrowReserve.pubkey,
obligation,
obligationMint,
obligationTokenOutput,
wallet.publicKey,
depositReserve.info.lendingMarket,
authority,
wallet.publicKey,
),
);
}
Expand All @@ -130,7 +99,7 @@ export const borrow = async (
instructions,
[],
accountRentExempt,
depositReserve.info.collateralMint,
depositReserve.info.collateral.mint,
signers,
)
: undefined;
Expand All @@ -146,22 +115,22 @@ export const borrow = async (

const mint = (await cache.query(
connection,
borrowReserve.info.liquidityMint,
borrowReserve.info.liquidity.mint,
MintParser,
)) as ParsedAccount<MintInfo>;

amountLamports = toLamports(amount, mint?.info);
} else if (amountType === BorrowAmountType.CollateralDepositAmount) {
const mint = (await cache.query(
connection,
depositReserve.info.collateralMint,
depositReserve.info.collateral.mint,
MintParser,
)) as ParsedAccount<MintInfo>;
amountLamports = toLamports(amount, mint?.info);
fromLamports = amountLamports;
}

const fromAccount = ensureSplAccount(
const sourceLiquidity = ensureSplAccount(
instructions,
finalCleanupInstructions,
from,
Expand All @@ -170,13 +139,13 @@ export const borrow = async (
signers,
);

let toAccount = await findOrCreateAccountByMint(
let destinationLiquidity = await findOrCreateAccountByMint(
wallet.publicKey,
wallet.publicKey,
instructions,
finalCleanupInstructions,
accountRentExempt,
borrowReserve.info.liquidityMint,
borrowReserve.info.liquidity.mint,
signers,
);

Expand All @@ -203,72 +172,24 @@ export const borrow = async (
instructions = [];
cleanupInstructions = [...finalCleanupInstructions];

// create approval for transfer transactions
const transferAuthority = approve(
instructions,
cleanupInstructions,
fromAccount,
wallet.publicKey,
fromLamports,
false,
);
signers.push(transferAuthority);

const dexMarketAddress = borrowReserve.info.dexMarketOption
? borrowReserve.info.dexMarket
: depositReserve.info.dexMarket;
const dexMarket = cache.get(dexMarketAddress);

if (!dexMarket) {
throw new Error(`Dex market doesn't exist.`);
}

const market = cache.get(
depositReserve.info.lendingMarket,
) as ParsedAccount<LendingMarket>;
const dexOrderBookSide = market.info.quoteMint.equals(
depositReserve.info.liquidityMint,
)
? dexMarket?.info.asks
: dexMarket?.info.bids;

const memory = createTempMemoryAccount(
instructions,
wallet.publicKey,
signers,
LENDING_PROGRAM_ID,
);

instructions.push(
accrueInterestInstruction(depositReserve.pubkey, borrowReserve.pubkey),
// @FIXME: aggregator needed
refreshReserveInstruction(depositReserve.pubkey),
refreshReserveInstruction(borrowReserve.pubkey),
);
// borrow
instructions.push(
borrowInstruction(
borrowObligationLiquidityInstruction(
amountLamports,
amountType,
fromAccount,
toAccount,
depositReserve.pubkey,
depositReserve.info.collateralSupply,
depositReserve.info.collateralFeesReceiver,

borrowReserve.info.liquidity.supply,
destinationLiquidity,
borrowReserve.pubkey,
borrowReserve.info.liquiditySupply,

borrowReserve.info.liquidity.feeReceiver,
obligation,
obligationMint,
obligationTokenOutput,

depositReserve.info.lendingMarket,
authority,
transferAuthority.publicKey,

dexMarketAddress,
dexOrderBookSide,

memory,

borrowReserve.info.lendingMarket,
lendingMarketAuthority,
// @FIXME: obligation owner
obligationOwner,
hostFeeReceiver,
),
);
Expand Down
Original file line number Diff line number Diff line change
@@ -1,25 +1,26 @@
import { LENDING_PROGRAM_ID } from '@oyster/common';
import {
Account,
PublicKey,
SystemProgram,
TransactionInstruction,
} from '@solana/web3.js';
import { utils } from '@oyster/common';
import { LendingObligationLayout } from '../models';
const { LENDING_PROGRAM_ID } = utils;
export function createUninitializedObligation(

export function createAccount(
instructions: TransactionInstruction[],
payer: PublicKey,
amount: number,
signers: Account[],
space: number,
) {
const account = new Account();

instructions.push(
SystemProgram.createAccount({
fromPubkey: payer,
newAccountPubkey: account.publicKey,
lamports: amount,
space: LendingObligationLayout.span,
space,
programId: LENDING_PROGRAM_ID,
}),
);
Expand Down
18 changes: 18 additions & 0 deletions packages/lending/src/actions/createObligation.tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
import { Account, PublicKey, TransactionInstruction } from '@solana/web3.js';
import { ObligationLayout } from '../models';
import { createAccount } from './createAccount';

export function createObligation(
instructions: TransactionInstruction[],
payer: PublicKey,
amount: number,
signers: Account[],
) {
return createAccount(
instructions,
payer,
amount,
signers,
ObligationLayout.span,
);
}
18 changes: 18 additions & 0 deletions packages/lending/src/actions/createReserve.tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
import { Account, PublicKey, TransactionInstruction } from '@solana/web3.js';
import { ReserveLayout } from '../models';
import { createAccount } from './createAccount';

export function createReserve(
instructions: TransactionInstruction[],
payer: PublicKey,
amount: number,
signers: Account[],
) {
return createAccount(
instructions,
payer,
amount,
signers,
ReserveLayout.span,
);
}
Loading

0 comments on commit 456f32c

Please sign in to comment.