Skip to content

Commit

Permalink
new sdk layout
Browse files Browse the repository at this point in the history
  • Loading branch information
0xodia committed Apr 29, 2024
1 parent efc3925 commit 8fe7a0a
Show file tree
Hide file tree
Showing 5 changed files with 373 additions and 94 deletions.
123 changes: 96 additions & 27 deletions solend-sdk/src/core/actions.ts
Original file line number Diff line number Diff line change
@@ -1,10 +1,13 @@
import {
AddressLookupTableAccount,
Connection,
PublicKey,
SystemProgram,
Transaction,
TransactionInstruction,
TransactionMessage,
TransactionSignature,
VersionedTransaction,
} from "@solana/web3.js";
import {
NATIVE_MINT,
Expand Down Expand Up @@ -94,6 +97,8 @@ export class SolendActionCore {

borrowReserves: Array<PublicKey>;

lookupTableAccount?: AddressLookupTableAccount;

private constructor(
programId: PublicKey,
connection: Connection,
Expand All @@ -109,7 +114,8 @@ export class SolendActionCore {
amount: BN,
depositReserves: Array<PublicKey>,
borrowReserves: Array<PublicKey>,
hostAta?: PublicKey
hostAta?: PublicKey,
lookupTableAccount?: AddressLookupTableAccount
) {
this.programId = programId;
this.connection = connection;
Expand All @@ -131,6 +137,7 @@ export class SolendActionCore {
this.postTxnIxs = [];
this.depositReserves = depositReserves;
this.borrowReserves = borrowReserves;
this.lookupTableAccount = lookupTableAccount;
}

static async initialize(
Expand All @@ -143,7 +150,8 @@ export class SolendActionCore {
environment: EnvironmentType = "production",
customObligationAddress?: PublicKey,
hostAta?: PublicKey,
customObligationSeed?: string
customObligationSeed?: string,
lookupTableAddress?: PublicKey
) {
const seed = customObligationSeed ?? pool.address.slice(0, 32);
const programId = getProgramId(environment);
Expand Down Expand Up @@ -211,6 +219,10 @@ export class SolendActionCore {
true
);

const lookupTableAccount = lookupTableAddress
? (await connection.getAddressLookupTable(lookupTableAddress)).value
: undefined;

return new SolendActionCore(
programId,
connection,
Expand All @@ -226,7 +238,8 @@ export class SolendActionCore {
amount,
depositReserves,
borrowReserves,
hostAta
hostAta,
lookupTableAccount ?? undefined
);
}

Expand All @@ -237,7 +250,8 @@ export class SolendActionCore {
amount: string,
publicKey: PublicKey,
obligationAddress: PublicKey,
environment: EnvironmentType = "production"
environment: EnvironmentType = "production",
lookupTableAddress?: PublicKey
) {
const axn = await SolendActionCore.initialize(
pool,
Expand All @@ -247,7 +261,10 @@ export class SolendActionCore {
publicKey,
connection,
environment,
obligationAddress
obligationAddress,
undefined,
undefined,
lookupTableAddress
);

await axn.addSupportIxs("forgive");
Expand All @@ -264,7 +281,8 @@ export class SolendActionCore {
publicKey: PublicKey,
environment: EnvironmentType = "production",
obligationAddress?: PublicKey,
obligationSeed?: string
obligationSeed?: string,
lookupTableAddress?: PublicKey
) {
const axn = await SolendActionCore.initialize(
pool,
Expand All @@ -276,7 +294,8 @@ export class SolendActionCore {
environment,
obligationAddress,
undefined,
obligationSeed
obligationSeed,
lookupTableAddress
);

await axn.addSupportIxs("deposit");
Expand All @@ -292,7 +311,8 @@ export class SolendActionCore {
amount: string,
publicKey: PublicKey,
environment: EnvironmentType = "production",
hostAta?: PublicKey
hostAta?: PublicKey,
lookupTableAddress?: PublicKey
) {
const axn = await SolendActionCore.initialize(
pool,
Expand All @@ -303,7 +323,9 @@ export class SolendActionCore {
connection,
environment,
undefined,
hostAta
hostAta,
undefined,
lookupTableAddress
);

await axn.addSupportIxs("borrow");
Expand All @@ -317,7 +339,8 @@ export class SolendActionCore {
connection: Connection,
amount: string,
publicKey: PublicKey,
environment: EnvironmentType = "production"
environment: EnvironmentType = "production",
lookupTableAddress?: PublicKey
) {
const axn = await SolendActionCore.initialize(
pool,
Expand All @@ -326,7 +349,11 @@ export class SolendActionCore {
new BN(amount),
publicKey,
connection,
environment
environment,
undefined,
undefined,
undefined,
lookupTableAddress
);
await axn.addSupportIxs("mint");
await axn.addDepositReserveLiquidityIx();
Expand All @@ -339,7 +366,8 @@ export class SolendActionCore {
connection: Connection,
amount: string,
publicKey: PublicKey,
environment: EnvironmentType = "production"
environment: EnvironmentType = "production",
lookupTableAddress?: PublicKey
) {
const axn = await SolendActionCore.initialize(
pool,
Expand All @@ -348,7 +376,11 @@ export class SolendActionCore {
new BN(amount),
publicKey,
connection,
environment
environment,
undefined,
undefined,
undefined,
lookupTableAddress
);
await axn.addSupportIxs("redeem");
await axn.addRedeemReserveCollateralIx();
Expand All @@ -361,7 +393,8 @@ export class SolendActionCore {
connection: Connection,
amount: string,
publicKey: PublicKey,
environment: EnvironmentType = "production"
environment: EnvironmentType = "production",
lookupTableAddress?: PublicKey
) {
const axn = await SolendActionCore.initialize(
pool,
Expand All @@ -370,7 +403,11 @@ export class SolendActionCore {
new BN(amount),
publicKey,
connection,
environment
environment,
undefined,
undefined,
undefined,
lookupTableAddress
);
await axn.addSupportIxs("depositCollateral");
await axn.addDepositObligationCollateralIx();
Expand All @@ -383,7 +420,8 @@ export class SolendActionCore {
connection: Connection,
amount: string,
publicKey: PublicKey,
environment: EnvironmentType = "production"
environment: EnvironmentType = "production",
lookupTableAddress?: PublicKey
) {
const axn = await SolendActionCore.initialize(
pool,
Expand All @@ -392,7 +430,11 @@ export class SolendActionCore {
new BN(amount),
publicKey,
connection,
environment
environment,
undefined,
undefined,
undefined,
lookupTableAddress
);

await axn.addSupportIxs("withdrawCollateral");
Expand All @@ -407,7 +449,10 @@ export class SolendActionCore {
connection: Connection,
amount: string,
publicKey: PublicKey,
environment: EnvironmentType = "production"
environment: EnvironmentType = "production",
obligationAddress?: PublicKey,
obligationSeed?: string,
lookupTableAddress?: PublicKey
) {
const axn = await SolendActionCore.initialize(
pool,
Expand All @@ -416,7 +461,11 @@ export class SolendActionCore {
new BN(amount),
publicKey,
connection,
environment
environment,
obligationAddress,
undefined,
obligationSeed,
lookupTableAddress
);

await axn.addSupportIxs("withdraw");
Expand All @@ -431,7 +480,8 @@ export class SolendActionCore {
connection: Connection,
amount: string,
publicKey: PublicKey,
environment: EnvironmentType = "production"
environment: EnvironmentType = "production",
lookupTableAddress?: PublicKey
) {
const axn = await SolendActionCore.initialize(
pool,
Expand All @@ -440,7 +490,11 @@ export class SolendActionCore {
new BN(amount),
publicKey,
connection,
environment
environment,
undefined,
undefined,
undefined,
lookupTableAddress
);

await axn.addSupportIxs("repay");
Expand All @@ -449,6 +503,24 @@ export class SolendActionCore {
return axn;
}

async getVersionedTransaction() {
return new VersionedTransaction(
new TransactionMessage({
payerKey: this.publicKey,
recentBlockhash: (await this.connection.getRecentBlockhash()).blockhash,
instructions: [
...this.preTxnIxs,
...this.setupIxs,
...this.lendingIxs,
...this.cleanupIxs,
...this.postTxnIxs,
],
}).compileToV0Message(
this.lookupTableAccount ? [this.lookupTableAccount] : []
)
);
}

async getTransactions() {
const txns: {
preLendingTxn: Transaction | null;
Expand Down Expand Up @@ -854,7 +926,7 @@ export class SolendActionCore {
new PublicKey(this.reserve.mintAddress)
);

if (this.positions === POSITION_LIMIT && this.hostAta) {
if (!this.lookupTableAccount) {
this.preTxnIxs.push(createUserTokenAccountIx);
} else {
this.setupIxs.push(createUserTokenAccountIx);
Expand All @@ -876,10 +948,7 @@ export class SolendActionCore {
new PublicKey(this.reserve.cTokenMint)
);

if (
this.positions === POSITION_LIMIT &&
this.reserve.mintAddress === NATIVE_MINT.toBase58()
) {
if (!this.lookupTableAccount) {
this.preTxnIxs.push(createUserCollateralAccountIx);
} else {
this.setupIxs.push(createUserCollateralAccountIx);
Expand Down Expand Up @@ -985,7 +1054,7 @@ export class SolendActionCore {
postIxs.push(closeWSOLAccountIx);
}

if (this.positions && this.positions >= POSITION_LIMIT) {
if (!this.lookupTableAccount) {
this.preTxnIxs.push(...preIxs);
this.postTxnIxs.push(...postIxs);
} else {
Expand Down
Loading

0 comments on commit 8fe7a0a

Please sign in to comment.