Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Deposit and withdraw obligation collateral #65

Closed
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
67 changes: 67 additions & 0 deletions src/models/lending/depositObligationCollateral.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,67 @@
import {
PublicKey,
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";
import * as Layout from "./../../utils/layout";
import { LendingInstruction } from "./lending";

/// Deposit additional collateral to an obligation.
///
/// 0. `[writable]` Source collateral token account, minted by deposit reserve collateral mint,
/// $authority can transfer $collateral_amount
/// 1. `[writable]` Destination deposit reserve collateral supply SPL Token account
/// 2. `[]` Deposit reserve account.
/// 3. `[writable]` Obligation
/// 4. `[writable]` Obligation token mint
/// 5. `[writable]` Obligation token output
/// 6. `[]` Lending market account.
/// 7. `[]` Derived lending market authority.
/// 8. `[]` User transfer authority ($authority).
/// 9. '[]` Token program id
export const depositObligationCollateralInstruction = (
collateralAmount: number | BN,
from: PublicKey,
to: PublicKey,
depositReserve: PublicKey,
obligation: PublicKey,
obligationMint: PublicKey,
obligationTokenOutput: PublicKey,
lendingMarket: PublicKey,
lendingMarketAuthority: PublicKey,
transferAuthority: PublicKey,
): TransactionInstruction => {
const dataLayout = BufferLayout.struct([
BufferLayout.u8("instruction"),
Layout.uint64("collateralAmount"),
]);

const data = Buffer.alloc(dataLayout.span);
dataLayout.encode(
{
instruction: LendingInstruction.DepositObligationCollateral,
collateralAmount: new BN(collateralAmount),
},
data
);

const keys = [
{ pubkey: from, isSigner: false, isWritable: true },
{ pubkey: to, isSigner: false, isWritable: true },
{ pubkey: depositReserve, isSigner: false, isWritable: false },
{ pubkey: obligation, isSigner: false, isWritable: true },
{ pubkey: obligationMint, isSigner: false, isWritable: true },
{ pubkey: obligationTokenOutput, isSigner: false, isWritable: true },
{ pubkey: lendingMarket, isSigner: false, isWritable: false },
{ pubkey: lendingMarketAuthority, isSigner: false, isWritable: false },
{ pubkey: transferAuthority, isSigner: true, isWritable: false },
{ pubkey: TOKEN_PROGRAM_ID, isSigner: false, isWritable: false },
];
return new TransactionInstruction({
keys,
programId: LENDING_PROGRAM_ID,
data,
});
};
2 changes: 2 additions & 0 deletions src/models/lending/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,3 +5,5 @@ export * from "./lending";
export * from "./borrow";
export * from "./deposit";
export * from "./withdraw";
export * from "./depositObligationCollateral";
export * from "./withdrawObligationCollateral";
4 changes: 4 additions & 0 deletions src/models/lending/lending.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,8 @@ export enum LendingInstruction {
RepayObligationLiquidity = 6,
LiquidateObligation = 7,
AccrueReserveInterest = 8,
DepositObligationCollateral = 9,
WithdrawObligationCollateral = 10,
}

export const TransactionListLookup: { [key: number]: string } = {
Expand All @@ -16,4 +18,6 @@ export const TransactionListLookup: { [key: number]: string } = {
5: "Borrow",
6: "Repay",
7: "Liquidate",
9: "DepositObligationCollateral",
10: "WithdrawObligationCollateral",
};
82 changes: 82 additions & 0 deletions src/models/lending/withdrawObligationCollateral.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,82 @@
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";
import * as Layout from "./../../utils/layout";
import { LendingInstruction } from "./lending";

/// Withdraw excess collateral from an obligation. The loan must remain healthy.
///
/// 0. `[writable]` Source withdraw reserve collateral supply SPL Token account
/// 1. `[writable]` Destination collateral token account, minted by withdraw reserve
/// collateral mint. $authority can transfer $collateral_amount
/// 2. `[]` Withdraw reserve account.
/// 3. `[]` Borrow reserve account.
/// 4. `[writable]` Obligation
/// 5. `[writable]` Obligation token mint
/// 6. `[writable]` Obligation token input
/// 7. `[]` Lending market account.
/// 8. `[]` Derived lending market authority.
/// 9. `[]` User transfer authority ($authority).
/// 10 `[]` Dex market
/// 11 `[]` Dex market order book side
/// 12 `[]` Temporary memory
/// 13 `[]` Clock sysvar
/// 14 '[]` Token program id
export const withdrawObligationCollateralInstruction = (
collateralAmount: number | BN,
from: PublicKey,
to: PublicKey,
withdrawReserve: PublicKey,
borrowReserve: PublicKey,
obligation: PublicKey,
obligationMint: PublicKey,
obligationTokenInput: PublicKey,
lendingMarket: PublicKey,
lendingMarketAuthority: PublicKey,
transferAuthority: PublicKey,
dexMarket: PublicKey,
dexOrderBookSide: PublicKey,
memory: PublicKey,
): TransactionInstruction => {
const dataLayout = BufferLayout.struct([
BufferLayout.u8("instruction"),
Layout.uint64("collateralAmount"),
]);

const data = Buffer.alloc(dataLayout.span);
dataLayout.encode(
{
instruction: LendingInstruction.WithdrawObligationCollateral,
collateralAmount: new BN(collateralAmount),
},
data
);

const keys = [
{ pubkey: from, isSigner: false, isWritable: true },
{ pubkey: to, isSigner: false, isWritable: true },
{ pubkey: withdrawReserve, isSigner: false, isWritable: false },
{ pubkey: borrowReserve, isSigner: false, isWritable: false },
{ pubkey: obligation, isSigner: false, isWritable: true },
{ pubkey: obligationMint, isSigner: false, isWritable: true },
{ pubkey: obligationTokenInput, isSigner: false, isWritable: true },
{ pubkey: lendingMarket, isSigner: false, isWritable: false },
{ pubkey: lendingMarketAuthority, isSigner: false, isWritable: false },
{ pubkey: transferAuthority, isSigner: true, isWritable: false },
{ pubkey: dexMarket, isSigner: false, isWritable: false },
{ pubkey: dexOrderBookSide, isSigner: false, isWritable: false },
{ pubkey: memory, isSigner: false, isWritable: false },
{ pubkey: SYSVAR_CLOCK_PUBKEY, isSigner: false, isWritable: false },
{ pubkey: TOKEN_PROGRAM_ID, isSigner: false, isWritable: false },
];
return new TransactionInstruction({
keys,
programId: LENDING_PROGRAM_ID,
data,
});
};