UNPKG

@frakters/nft-lending-v2

Version:

Client library for interacting with nft lenging solana program

970 lines 58.2 kB
"use strict"; var __createBinding = (this && this.__createBinding) || (Object.create ? (function(o, m, k, k2) { if (k2 === undefined) k2 = k; var desc = Object.getOwnPropertyDescriptor(m, k); if (!desc || ("get" in desc ? !m.__esModule : desc.writable || desc.configurable)) { desc = { enumerable: true, get: function() { return m[k]; } }; } Object.defineProperty(o, k2, desc); }) : (function(o, m, k, k2) { if (k2 === undefined) k2 = k; o[k2] = m[k]; })); var __setModuleDefault = (this && this.__setModuleDefault) || (Object.create ? (function(o, v) { Object.defineProperty(o, "default", { enumerable: true, value: v }); }) : function(o, v) { o["default"] = v; }); var __importStar = (this && this.__importStar) || function (mod) { if (mod && mod.__esModule) return mod; var result = {}; if (mod != null) for (var k in mod) if (k !== "default" && Object.prototype.hasOwnProperty.call(mod, k)) __createBinding(result, mod, k); __setModuleDefault(result, mod); return result; }; var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, generator) { function adopt(value) { return value instanceof P ? value : new P(function (resolve) { resolve(value); }); } return new (P || (P = Promise))(function (resolve, reject) { function fulfilled(value) { try { step(generator.next(value)); } catch (e) { reject(e); } } function rejected(value) { try { step(generator["throw"](value)); } catch (e) { reject(e); } } function step(result) { result.done ? resolve(result.value) : adopt(result.value).then(fulfilled, rejected); } step((generator = generator.apply(thisArg, _arguments || [])).next()); }); }; Object.defineProperty(exports, "__esModule", { value: true }); exports.anchor = exports.objectBNsAndPubkeysToNums = exports.CalculateStatFromAccounts = exports.decodeLoan = exports.objectBNsToNums = exports.getSpecificAccountInfo = exports.claimGemFarmStaking = exports.unstakeGemFarmStakingByAdmin = exports.unstakeGemFarmStaking = exports.stakeGemFarmStaking = exports.getAllProgramAccounts = exports.revealLotTicketByAdmin = exports.rejectLotTicketByAdmin = exports.redeemWinningLotTicket = exports.paybackLoanWithGrace = exports.liquidateNftToRaffles = exports.initializeNftAttempts = exports.withdrawFromReserveFund = exports.rejectLoanByAdmin = exports.proposeLoan = exports.paybackLoan = exports.updatePriceBasedLiquidityPool = exports.initializePriceBasedLiquidityPool = exports.updateTimeBasedLiquidityPool = exports.initializeCollectionInfo = exports.updateCollectionInfo = exports.harvestLiquidity = exports.unstakeLiquidity = exports.depositLiquidity = exports.approveLoanByAdmin = exports.METADATA_PROGRAM_PUBKEY = exports.accounts = exports.utils = exports.returnAnchorProgram = exports.onAccountsChange = void 0; // import { TOKEN_PROGRAM_ID, ASSOCIATED_TOKEN_PROGRAM_ID } from '@solana/spl-token'; const anchor = __importStar(require("@project-serum/anchor")); const idl_gem_farm_1 = require("./idl/types/idl-gem-farm"); exports.onAccountsChange = __importStar(require("./accountsChanged/onAccountsChanged")); const web3_js_1 = require("@solana/web3.js"); const utils = __importStar(require("./utils")); const accounts = __importStar(require("./accounts")); var utils_1 = require("./utils"); Object.defineProperty(exports, "returnAnchorProgram", { enumerable: true, get: function () { return utils_1.returnAnchorProgram; } }); exports.utils = __importStar(require("./utils")); exports.accounts = __importStar(require("./accounts")); exports.METADATA_PROGRAM_PUBKEY = new web3_js_1.PublicKey('metaqbxxUerdq28cj1RbAWkYQm3ybzjb6a8bt518x1s'); const encoder = new TextEncoder(); function approveLoanByAdmin({ programId, connection, admin, loan, liquidityPool, collectionInfo, nftPrice, discount, user, sendTxn, }) { return __awaiter(this, void 0, void 0, function* () { // const accountRentExempt = await connection.getMinimumBalanceForRentExemption(AccountLayout.span); // let signers: Keypair[] = []; const program = yield utils.returnAnchorProgram(programId, connection); const [liqOwner, liqOwnerBump] = yield anchor.web3.PublicKey.findProgramAddress([encoder.encode('nftlendingv2'), liquidityPool.toBuffer()], programId); // const instructions: TransactionInstruction[] = []; // instructions.push( const instr = program.instruction.approveLoanByAdmin(new anchor.BN(nftPrice), new anchor.BN(discount), { accounts: { loan: loan, user, liquidityPool, liqOwner, collectionInfo, admin, systemProgram: anchor.web3.SystemProgram.programId, }, }); // ); const transaction = new web3_js_1.Transaction().add(instr); // for (let ix of instructions) transaction.add(ix); yield sendTxn(transaction); }); } exports.approveLoanByAdmin = approveLoanByAdmin; function depositLiquidity({ programId, connection, liquidityPool, user, amount, sendTxn, }) { return __awaiter(this, void 0, void 0, function* () { const program = yield utils.returnAnchorProgram(programId, connection); const [liqOwner, liqOwnerBump] = yield anchor.web3.PublicKey.findProgramAddress([encoder.encode('nftlendingv2'), liquidityPool.toBuffer()], program.programId); const [deposit, depositBump] = yield anchor.web3.PublicKey.findProgramAddress([encoder.encode('deposit'), liquidityPool.toBuffer(), user.toBuffer()], program.programId); const instr = program.instruction.depositLiquidity(new anchor.BN(amount), { accounts: { liquidityPool: liquidityPool, liqOwner, deposit, user: user, rent: anchor.web3.SYSVAR_RENT_PUBKEY, systemProgram: anchor.web3.SystemProgram.programId, }, }); const transaction = new web3_js_1.Transaction().add(instr); yield sendTxn(transaction); return deposit; }); } exports.depositLiquidity = depositLiquidity; function unstakeLiquidity({ programId, connection, liquidityPool, user, amount, admin, sendTxn, }) { return __awaiter(this, void 0, void 0, function* () { const program = yield utils.returnAnchorProgram(programId, connection); const [liqOwner, liqOwnerBump] = yield anchor.web3.PublicKey.findProgramAddress([encoder.encode('nftlendingv2'), liquidityPool.toBuffer()], program.programId); const [deposit, depositBump] = yield anchor.web3.PublicKey.findProgramAddress([encoder.encode('deposit'), liquidityPool.toBuffer(), user.toBuffer()], program.programId); const ix = program.instruction.unstakeLiquidity(depositBump, new anchor.BN(amount), { accounts: { liquidityPool, user, deposit, liqOwner, systemProgram: anchor.web3.SystemProgram.programId, admin, }, }); const transaction = new web3_js_1.Transaction().add(ix); yield sendTxn(transaction); }); } exports.unstakeLiquidity = unstakeLiquidity; function harvestLiquidity({ programId, connection, liquidityPool, user, admin, sendTxn, }) { return __awaiter(this, void 0, void 0, function* () { const program = yield utils.returnAnchorProgram(programId, connection); const [liqOwner, liqOwnerBump] = yield anchor.web3.PublicKey.findProgramAddress([encoder.encode('nftlendingv2'), liquidityPool.toBuffer()], program.programId); const [deposit, depositBump] = yield anchor.web3.PublicKey.findProgramAddress([encoder.encode('deposit'), liquidityPool.toBuffer(), user.toBuffer()], program.programId); const ix = program.instruction.harvestLiquidity(depositBump, { accounts: { liquidityPool, user, deposit, liqOwner, systemProgram: anchor.web3.SystemProgram.programId, admin, }, }); const transaction = new web3_js_1.Transaction().add(ix); yield sendTxn(transaction); }); } exports.harvestLiquidity = harvestLiquidity; function updateCollectionInfo({ programId, connection, liquidityPool, admin, creatorAddress, pricingLookupAddress, loanToValue, collaterizationRate, royaltyAddress, collectionInfo, royaltyFeeTime, royaltyFeePrice, expirationTime, isPriceBased, sendTxn, }) { return __awaiter(this, void 0, void 0, function* () { const program = yield utils.returnAnchorProgram(programId, connection); const ix = yield program.instruction.updateCollectionInfo({ loanToValue: new anchor.BN(loanToValue), collaterizationRate: new anchor.BN(collaterizationRate), royaltyFeeTime: new anchor.BN(royaltyFeeTime), royaltyFeePrice: new anchor.BN(royaltyFeePrice), expirationTime: new anchor.BN(expirationTime), isPriceBased, }, { accounts: { liquidityPool: liquidityPool, collectionInfo: collectionInfo, admin: admin, creatorAddress: creatorAddress, royaltyAddress, pricingLookupAddress: pricingLookupAddress, }, }); const transaction = new web3_js_1.Transaction().add(ix); yield sendTxn(transaction); // return collectionInfo; }); } exports.updateCollectionInfo = updateCollectionInfo; function initializeCollectionInfo({ programId, connection, liquidityPool, admin, creatorAddress, pricingLookupAddress, loanToValue, collaterizationRate, royaltyAddress, royaltyFeeTime, royaltyFeePrice, expirationTime, isPriceBased, sendTxn, }) { return __awaiter(this, void 0, void 0, function* () { const program = yield utils.returnAnchorProgram(programId, connection); const collectionInfo = web3_js_1.Keypair.generate(); const ix = yield program.instruction.initializeCollectionInfo({ loanToValue: new anchor.BN(loanToValue), collaterizationRate: new anchor.BN(collaterizationRate), royaltyFeeTime: new anchor.BN(royaltyFeeTime), royaltyFeePrice: new anchor.BN(royaltyFeePrice), expirationTime: new anchor.BN(expirationTime), isPriceBased, }, { accounts: { liquidityPool: liquidityPool, collectionInfo: collectionInfo.publicKey, admin: admin, creatorAddress: creatorAddress, royaltyAddress, pricingLookupAddress: pricingLookupAddress, rent: anchor.web3.SYSVAR_RENT_PUBKEY, systemProgram: anchor.web3.SystemProgram.programId, }, }); const transaction = new web3_js_1.Transaction().add(ix); yield sendTxn(transaction, [collectionInfo]); return collectionInfo.publicKey; }); } exports.initializeCollectionInfo = initializeCollectionInfo; // export async function initializeTimeBasedLiquidityPool({ // programId, // connection, // admin, // rewardInterestRateTime, // rewardInterestRatePrice, // feeInterestRateTime, // feeInterestRatePrice, // id, // period, // sendTxn, // }: { // programId: PublicKey; // connection: Connection; // admin: PublicKey; // rewardInterestRateTime: number | anchor.BN; // feeInterestRateTime: number | anchor.BN; // rewardInterestRatePrice: number | anchor.BN; // feeInterestRatePrice: number | anchor.BN; // id: number | anchor.BN; // period: number | anchor.BN; // sendTxn: (transaction: Transaction, signers: Keypair[]) => Promise<void>; // }) { // const program = await utils.returnAnchorProgram(programId, connection); // const liquidityPool = Keypair.generate(); // const [liqOwner, liqOwnerBump] = await anchor.web3.PublicKey.findProgramAddress( // [encoder.encode('nftlendingv2'), liquidityPool.publicKey.toBuffer()], // program.programId, // ); // const ix = program.instruction.initializeTimeBasedLiquidityPool( // liqOwnerBump, // { // rewardInterestRateTime: new anchor.BN(rewardInterestRateTime), // rewardInterestRatePrice: new anchor.BN(rewardInterestRatePrice), // feeInterestRateTime: new anchor.BN(feeInterestRateTime), // feeInterestRatePrice: new anchor.BN(feeInterestRatePrice), // id: new anchor.BN(id), // period: new anchor.BN(period), // }, // { // accounts: { // liquidityPool: liquidityPool.publicKey, // liqOwner, // admin: admin, // rent: anchor.web3.SYSVAR_RENT_PUBKEY, // systemProgram: anchor.web3.SystemProgram.programId, // }, // }, // ); // const transaction = new Transaction().add(ix); // await sendTxn(transaction, [liquidityPool]); // return liquidityPool.publicKey; // } function updateTimeBasedLiquidityPool({ programId, connection, admin, liquidityPool, rewardInterestRateTime, rewardInterestRatePrice, feeInterestRateTime, feeInterestRatePrice, id, period, sendTxn, }) { return __awaiter(this, void 0, void 0, function* () { const program = utils.returnAnchorProgram(programId, connection); const ix = program.instruction.updateTimeBasedLiquidityPool({ rewardInterestRateTime: new anchor.BN(rewardInterestRateTime), rewardInterestRatePrice: new anchor.BN(rewardInterestRatePrice), feeInterestRateTime: new anchor.BN(feeInterestRateTime), feeInterestRatePrice: new anchor.BN(feeInterestRatePrice), id: new anchor.BN(id), period: new anchor.BN(period), }, { accounts: { liquidityPool: liquidityPool, admin: admin, rent: anchor.web3.SYSVAR_RENT_PUBKEY, systemProgram: anchor.web3.SystemProgram.programId, }, }); const transaction = new web3_js_1.Transaction().add(ix); yield sendTxn(transaction); // return liquidityPool; }); } exports.updateTimeBasedLiquidityPool = updateTimeBasedLiquidityPool; function initializePriceBasedLiquidityPool({ programId, connection, admin, baseBorrowRate, variableSlope1, variableSlope2, utilizationRateOptimal, reserveFactor, depositCommission, borrowCommission, id, sendTxn, }) { return __awaiter(this, void 0, void 0, function* () { const program = yield utils.returnAnchorProgram(programId, connection); const liquidityPool = web3_js_1.Keypair.generate(); const [liqOwner, liqOwnerBump] = yield anchor.web3.PublicKey.findProgramAddress([encoder.encode('nftlendingv2'), liquidityPool.publicKey.toBuffer()], program.programId); const ix = program.instruction.initializePriceBasedLiquidityPool(liqOwnerBump, { id: id, baseBorrowRate: baseBorrowRate, variableSlope1: variableSlope1, variableSlope2: variableSlope2, utilizationRateOptimal: utilizationRateOptimal, reserveFactor: reserveFactor, depositCommission, borrowCommission, }, { accounts: { liquidityPool: liquidityPool.publicKey, liqOwner, admin: admin, rent: anchor.web3.SYSVAR_RENT_PUBKEY, systemProgram: anchor.web3.SystemProgram.programId, }, }); const transaction = new web3_js_1.Transaction().add(ix); yield sendTxn(transaction, [liquidityPool]); return liquidityPool.publicKey; }); } exports.initializePriceBasedLiquidityPool = initializePriceBasedLiquidityPool; function updatePriceBasedLiquidityPool({ programId, liquidityPool, connection, admin, baseBorrowRate, variableSlope1, variableSlope2, utilizationRateOptimal, reserveFactor, depositCommission, borrowCommission, id, sendTxn, }) { return __awaiter(this, void 0, void 0, function* () { const program = utils.returnAnchorProgram(programId, connection); const ix = program.instruction.updatePriceBasedLiquidityPool({ id, baseBorrowRate, variableSlope1, variableSlope2, utilizationRateOptimal, reserveFactor, depositCommission, borrowCommission, }, { accounts: { liquidityPool: liquidityPool, admin: admin, rent: anchor.web3.SYSVAR_RENT_PUBKEY, systemProgram: anchor.web3.SystemProgram.programId, }, }); const transaction = new web3_js_1.Transaction().add(ix); yield sendTxn(transaction); // return liquidityPool; }); } exports.updatePriceBasedLiquidityPool = updatePriceBasedLiquidityPool; // export async function liquidateLoanByAdmin({ // programId, // connection, // liquidator, // user, // loan, // nftMint, // sendTxn, // }: { // programId: PublicKey; // connection: Connection; // liquidator: PublicKey; // user: PublicKey; // loan: PublicKey; // nftMint: PublicKey; // sendTxn: (transaction: Transaction) => Promise<void>; // }) { // const program = await utils.returnAnchorProgram(programId, connection); // const [communityPoolsAuthority, bumpPoolsAuth] = await anchor.web3.PublicKey.findProgramAddress( // [encoder.encode('nftlendingv2'), programId.toBuffer()], // program.programId, // ); // const nftUserTokenAccount = await utils.findAssociatedTokenAddress(user, nftMint); // const nftLiquidatorTokenAccount = await utils.findAssociatedTokenAddress(liquidator, nftMint); // const editionId = utils.getMetaplexEditionPda(nftMint); // const instr = program.instruction.liquidateLoanByAdmin(bumpPoolsAuth, { // accounts: { // loan: loan, // liquidator: liquidator, // nftMint: nftMint, // nftLiquidatorTokenAccount: nftLiquidatorTokenAccount, // user: user, // nftUserTokenAccount: nftUserTokenAccount, // communityPoolsAuthority, // rent: anchor.web3.SYSVAR_RENT_PUBKEY, // systemProgram: anchor.web3.SystemProgram.programId, // tokenProgram: utils.TOKEN_PROGRAM_ID, // associatedTokenProgram: utils.SPL_ASSOCIATED_TOKEN_ACCOUNT_PROGRAM_ID, // metadataProgram: METADATA_PROGRAM_PUBKEY, // editionInfo: editionId, // }, // }); // const transaction = new Transaction().add(instr); // await sendTxn(transaction); // } function paybackLoan({ programId, connection, user, admin, loan, nftMint, liquidityPool, collectionInfo, royaltyAddress, amount, sendTxn, }) { return __awaiter(this, void 0, void 0, function* () { // const instructions: TransactionInstruction[] = []; const program = yield utils.returnAnchorProgram(programId, connection); const [communityPoolsAuthority, bumpPoolsAuth] = yield anchor.web3.PublicKey.findProgramAddress([encoder.encode('nftlendingv2'), programId.toBuffer()], program.programId); const [liqOwner, liqOwnerBump] = yield anchor.web3.PublicKey.findProgramAddress([encoder.encode('nftlendingv2'), liquidityPool.toBuffer()], program.programId); const nftUserTokenAccount = yield utils.findAssociatedTokenAddress(user, nftMint); const editionId = utils.getMetaplexEditionPda(nftMint); const instr = program.instruction.paybackLoan(bumpPoolsAuth, amount, { accounts: { loan: loan, liquidityPool: liquidityPool, collectionInfo, user: user, admin, nftMint: nftMint, nftUserTokenAccount: nftUserTokenAccount, royaltyAddress, liqOwner, communityPoolsAuthority, systemProgram: anchor.web3.SystemProgram.programId, tokenProgram: utils.TOKEN_PROGRAM_ID, // associatedTokenProgram: ASSOCIATED_TOKEN_PROGRAM_ID, metadataProgram: exports.METADATA_PROGRAM_PUBKEY, editionInfo: editionId, }, }); const transaction = new web3_js_1.Transaction().add(instr); yield sendTxn(transaction); }); } exports.paybackLoan = paybackLoan; function proposeLoan({ proposedNftPrice, programId, connection, user, nftMint, isPriceBased, loanToValue, admin, sendTxn, }) { return __awaiter(this, void 0, void 0, function* () { const program = yield utils.returnAnchorProgram(programId, connection); const loan = web3_js_1.Keypair.generate(); const [communityPoolsAuthority, bumpPoolsAuth] = yield anchor.web3.PublicKey.findProgramAddress([encoder.encode('nftlendingv2'), programId.toBuffer()], programId); const editionId = utils.getMetaplexEditionPda(nftMint); const nftUserTokenAccount = yield utils.findAssociatedTokenAddress(user, nftMint); const ix = program.instruction.proposeLoan(bumpPoolsAuth, isPriceBased, proposedNftPrice, loanToValue, { accounts: { loan: loan.publicKey, user: user, nftUserTokenAccount, nftMint: nftMint, communityPoolsAuthority, tokenProgram: utils.TOKEN_PROGRAM_ID, rent: anchor.web3.SYSVAR_RENT_PUBKEY, systemProgram: anchor.web3.SystemProgram.programId, metadataProgram: exports.METADATA_PROGRAM_PUBKEY, admin, editionInfo: editionId, }, // signers: [loan] }); const transaction = new web3_js_1.Transaction().add(ix); yield sendTxn(transaction, [loan]); return { loanPubkey: loan.publicKey }; }); } exports.proposeLoan = proposeLoan; function rejectLoanByAdmin({ programId, connection, loan, nftUserTokenAccount, admin, user, nftMint, sendTxn, }) { return __awaiter(this, void 0, void 0, function* () { const program = yield utils.returnAnchorProgram(programId, connection); const [communityPoolsAuthority, bumpPoolsAuth] = yield anchor.web3.PublicKey.findProgramAddress([encoder.encode('nftlendingv2'), programId.toBuffer()], programId); // const nftUserTokenAccount = await utils.findAssociatedTokenAddress(user, nftMint); const editionId = utils.getMetaplexEditionPda(nftMint); const ix = program.instruction.rejectLoanByAdmin(bumpPoolsAuth, { accounts: { loan: loan, admin: admin, nftMint: nftMint, nftUserTokenAccount: nftUserTokenAccount, user: user, communityPoolsAuthority, tokenProgram: utils.TOKEN_PROGRAM_ID, systemProgram: anchor.web3.SystemProgram.programId, metadataProgram: exports.METADATA_PROGRAM_PUBKEY, editionInfo: editionId, }, }); const transaction = new web3_js_1.Transaction().add(ix); yield sendTxn(transaction); }); } exports.rejectLoanByAdmin = rejectLoanByAdmin; // export async function closeLoanByAdmin({ // programId, // connection, // loan, // admin, // sendTxn, // }: { // programId: PublicKey; // connection: Connection; // loan: PublicKey; // admin: PublicKey; // sendTxn: (transaction: Transaction) => Promise<void>; // }) { // const program = await utils.returnAnchorProgram(programId, connection); // const [communityPoolsAuthority, bumpPoolsAuth] = await anchor.web3.PublicKey.findProgramAddress( // [encoder.encode('nftlendingv2'), programId.toBuffer()], // programId, // ); // const ix = await program.methods // .closeLoan(bumpPoolsAuth) // .accounts({ // loan: loan, // admin: admin, // communityPoolsAuthority, // }) // .instruction(); // const transaction = new Transaction().add(ix); // await sendTxn(transaction); // } // export async function topupFromLiqLoans({ // programId, // connection, // liquidityPool, // admin, // amount, // sendTxn, // }: { // programId: PublicKey; // connection: Connection; // liquidityPool: PublicKey; // admin: PublicKey; // amount: anchor.BN; // sendTxn: (transaction: Transaction) => Promise<void>; // }) { // const program = await utils.returnAnchorProgram(programId, connection); // const [liqOwner, liqOwnerBump] = await anchor.web3.PublicKey.findProgramAddress( // [encoder.encode('nftlendingv2'), liquidityPool.toBuffer()], // programId, // ); // const ix = program.instruction.topupFromLiqLoans(amount, { // accounts: { // admin, // liquidityPool, // liqOwner, // systemProgram: anchor.web3.SystemProgram.programId, // }, // }); // const transaction = new Transaction().add(ix); // await sendTxn(transaction); // } function withdrawFromReserveFund({ programId, connection, priceBasedLiquidityPool, admin, sendTxn, }) { return __awaiter(this, void 0, void 0, function* () { const program = yield utils.returnAnchorProgram(programId, connection); const [liqOwner, liqOwnerBump] = yield anchor.web3.PublicKey.findProgramAddress([encoder.encode('nftlendingv2'), priceBasedLiquidityPool.toBuffer()], programId); const ix = program.instruction.withdrawFromReserveFund({ accounts: { admin, liquidityPool: priceBasedLiquidityPool, liqOwner, systemProgram: anchor.web3.SystemProgram.programId, }, }); const transaction = new web3_js_1.Transaction().add(ix); yield sendTxn(transaction); }); } exports.withdrawFromReserveFund = withdrawFromReserveFund; function initializeNftAttempts({ programId, connection, attemptsNftMint, user, sendTxn, }) { return __awaiter(this, void 0, void 0, function* () { const program = utils.returnAnchorProgram(programId, connection); const [nftAttempts, nftAttemptsBump] = yield anchor.web3.PublicKey.findProgramAddress([encoder.encode('nftattempts'), programId.toBuffer(), attemptsNftMint.toBuffer()], program.programId); const ix = program.instruction.initializeNftAttempts({ accounts: { nftAttempts, user, nftMint: attemptsNftMint, rent: anchor.web3.SYSVAR_RENT_PUBKEY, systemProgram: anchor.web3.SystemProgram.programId, }, }); const transaction = new web3_js_1.Transaction().add(ix); yield sendTxn(transaction); return nftAttempts; }); } exports.initializeNftAttempts = initializeNftAttempts; function liquidateNftToRaffles({ programId, connection, user, liquidator, gracePeriod, loan, nftMint, sendTxn, }) { return __awaiter(this, void 0, void 0, function* () { const program = utils.returnAnchorProgram(programId, connection); const [communityPoolsAuthority, bumpPoolsAuth] = yield anchor.web3.PublicKey.findProgramAddress([encoder.encode('nftlendingv2'), programId.toBuffer()], program.programId); const nftUserTokenAccount = yield utils.findAssociatedTokenAddress(user, nftMint); const vaultNftTokenAccount = yield utils.findAssociatedTokenAddress(communityPoolsAuthority, nftMint); const editionId = utils.getMetaplexEditionPda(nftMint); const liquidationLot = web3_js_1.Keypair.generate(); const ix = program.instruction.liquidateNftToRaffles(bumpPoolsAuth, new anchor.BN(gracePeriod), { accounts: { loan, liquidationLot: liquidationLot.publicKey, user, liquidator: liquidator, nftMint, vaultNftTokenAccount, nftUserTokenAccount, communityPoolsAuthority, systemProgram: anchor.web3.SystemProgram.programId, tokenProgram: utils.TOKEN_PROGRAM_ID, associatedTokenProgram: utils.SPL_ASSOCIATED_TOKEN_ACCOUNT_PROGRAM_ID, metadataProgram: exports.METADATA_PROGRAM_PUBKEY, editionInfo: editionId, rent: anchor.web3.SYSVAR_RENT_PUBKEY, }, }); const transaction = new web3_js_1.Transaction().add(ix); yield sendTxn(transaction, [liquidationLot]); return liquidationLot.publicKey; }); } exports.liquidateNftToRaffles = liquidateNftToRaffles; function paybackLoanWithGrace({ programId, connection, user, admin, liquidationLot, loan, nftMint, liquidityPool, collectionInfo, royaltyAddress, sendTxn, }) { return __awaiter(this, void 0, void 0, function* () { // const instructions: TransactionInstruction[] = []; const program = utils.returnAnchorProgram(programId, connection); const [communityPoolsAuthority, bumpPoolsAuth] = yield anchor.web3.PublicKey.findProgramAddress([encoder.encode('nftlendingv2'), programId.toBuffer()], program.programId); const [liqOwner, liqOwnerBump] = yield anchor.web3.PublicKey.findProgramAddress([encoder.encode('nftlendingv2'), liquidityPool.toBuffer()], program.programId); const nftUserTokenAccount = yield utils.findAssociatedTokenAddress(user, nftMint); const vaultNftTokenAccount = yield utils.findAssociatedTokenAddress(communityPoolsAuthority, nftMint); const editionId = utils.getMetaplexEditionPda(nftMint); const instr = program.instruction.paybackWithGrace(bumpPoolsAuth, { accounts: { loan: loan, liquidityPool, liquidationLot, collectionInfo, user: user, admin, nftMint: nftMint, nftUserTokenAccount: nftUserTokenAccount, royaltyAddress, liqOwner, communityPoolsAuthority, vaultNftTokenAccount, systemProgram: anchor.web3.SystemProgram.programId, tokenProgram: utils.TOKEN_PROGRAM_ID, associatedTokenProgram: utils.SPL_ASSOCIATED_TOKEN_ACCOUNT_PROGRAM_ID, metadataProgram: exports.METADATA_PROGRAM_PUBKEY, editionInfo: editionId, }, }); const transaction = new web3_js_1.Transaction().add(instr); yield sendTxn(transaction); }); } exports.paybackLoanWithGrace = paybackLoanWithGrace; function redeemWinningLotTicket({ programId, connection, user, liquidationLot, liquidityPool, collectionInfo, loan, admin, lotTicket, royaltyAddress, nftMint, sendTxn, }) { return __awaiter(this, void 0, void 0, function* () { const program = utils.returnAnchorProgram(programId, connection); const [communityPoolsAuthority, bumpPoolsAuth] = yield anchor.web3.PublicKey.findProgramAddress([encoder.encode('nftlendingv2'), programId.toBuffer()], program.programId); const [liqOwner, liqOwnerBump] = yield anchor.web3.PublicKey.findProgramAddress([encoder.encode('nftlendingv2'), liquidityPool.toBuffer()], program.programId); const nftUserTokenAccount = yield utils.findAssociatedTokenAddress(user, nftMint); const vaultNftTokenAccount = yield utils.findAssociatedTokenAddress(communityPoolsAuthority, nftMint); const editionId = utils.getMetaplexEditionPda(nftMint); const instr = program.instruction.redeemWinningLotTicket(bumpPoolsAuth, { accounts: { loan: loan, liquidityPool, liquidationLot, lotTicket, collectionInfo, user: user, admin, nftMint: nftMint, nftUserTokenAccount: nftUserTokenAccount, royaltyAddress, liqOwner, communityPoolsAuthority, vaultNftTokenAccount, systemProgram: anchor.web3.SystemProgram.programId, tokenProgram: utils.TOKEN_PROGRAM_ID, associatedTokenProgram: utils.SPL_ASSOCIATED_TOKEN_ACCOUNT_PROGRAM_ID, metadataProgram: exports.METADATA_PROGRAM_PUBKEY, editionInfo: editionId, rent: anchor.web3.SYSVAR_RENT_PUBKEY, }, }); const transaction = new web3_js_1.Transaction().add(instr); yield sendTxn(transaction); }); } exports.redeemWinningLotTicket = redeemWinningLotTicket; function rejectLotTicketByAdmin({ programId, connection, admin, lotTicket, sendTxn, }) { return __awaiter(this, void 0, void 0, function* () { const program = utils.returnAnchorProgram(programId, connection); const ix = program.instruction.rejectLotTicketByAdmin({ accounts: { admin, lotTicket, }, }); const transaction = new web3_js_1.Transaction().add(ix); yield sendTxn(transaction); }); } exports.rejectLotTicketByAdmin = rejectLotTicketByAdmin; function revealLotTicketByAdmin({ programId, connection, admin, lotTicket, isWinning, sendTxn, }) { return __awaiter(this, void 0, void 0, function* () { const program = utils.returnAnchorProgram(programId, connection); const ix = program.instruction.revealLotTicketByAdmin(isWinning, { accounts: { admin, lotTicket, }, }); const transaction = new web3_js_1.Transaction().add(ix); yield sendTxn(transaction); }); } exports.revealLotTicketByAdmin = revealLotTicketByAdmin; // export async function topupFromLiqLoansToPool({ // programId, // connection, // admin, // liquidityPool, // amount, // sendTxn, // }: { // programId: PublicKey; // connection: Connection; // admin: PublicKey; // liquidityPool: PublicKey; // amount: anchor.BN; // sendTxn: (transaction: Transaction) => Promise<void>; // }) { // const program = await utils.returnAnchorProgram(programId, connection); // const [liqOwner, liqOwnerBump] = await anchor.web3.PublicKey.findProgramAddress( // [encoder.encode('nftlendingv2'), liquidityPool.toBuffer()], // programId, // ); // const ix = program.instruction.topupFromLiqLoans(amount, { // accounts: { // admin: admin, // liquidityPool, // liqOwner, // systemProgram: anchor.web3.SystemProgram.programId, // }, // }); // const transaction = new Transaction().add(ix); // await sendTxn(transaction); // } function getAllProgramAccounts(programId, connection) { return __awaiter(this, void 0, void 0, function* () { let program = utils.returnAnchorProgram(programId, connection); const collectionInfoRaws = yield program.account.collectionInfo.all(); const depositRaws = yield program.account.deposit.all(); const liquidityPoolRaws = yield program.account.liquidityPool.all(); const priceBasedLiquidityPoolRaws = yield program.account.priceBasedLiquidityPool.all(); const loanRaws = yield program.account.loan.all(); const liquidationLotRaws = yield program.account.liquidationLot.all(); const lotTicketRaws = yield program.account.lotTicket.all(); const nftAttemptsRaws = yield program.account.nftAttempts.all(); const lendingStakeRaws = yield program.account.lendingStake.all(); const collectionInfos = collectionInfoRaws.map((raw) => accounts.decodedCollectionInfo(raw.account, raw.publicKey)); const deposits = depositRaws.map((raw) => accounts.decodedDeposit(raw.account, raw.publicKey)); const timeBasedLiquidityPools = liquidityPoolRaws.map((raw) => accounts.decodedTimeBasedLiquidityPool(raw.account, raw.publicKey)); const priceBasedLiquidityPools = priceBasedLiquidityPoolRaws.map((raw) => accounts.decodedPriceBasedLiquidityPool(raw.account, raw.publicKey)); const loans = loanRaws.map((raw) => accounts.decodedLoan(raw.account, raw.publicKey)); const lendingStakes = lendingStakeRaws.map((raw) => accounts.decodedLendingStake(raw.account, raw.publicKey)); const liquidationLots = liquidationLotRaws.map(objectBNsAndPubkeysToNums); const lotTickets = lotTicketRaws.map(objectBNsAndPubkeysToNums); const nftAttempts = nftAttemptsRaws.map(objectBNsAndPubkeysToNums); // return { collectionInfos, deposits, liquidityPools, loans }; return { collectionInfos, deposits, timeBasedLiquidityPools, priceBasedLiquidityPools, loans, liquidationLots, lotTickets, nftAttempts, lendingStakes }; }); } exports.getAllProgramAccounts = getAllProgramAccounts; function stakeGemFarmStaking({ programId, connection, user, gemFarm, farm, bank, gemBank, feeAcc, nftMint, loan, isDegod, sendTxn, }) { return __awaiter(this, void 0, void 0, function* () { const program = utils.returnAnchorProgram(programId, connection); const [communityPoolsAuthority, bumpPoolsAuth] = yield anchor.web3.PublicKey.findProgramAddress([encoder.encode('nftlendingv2'), programId.toBuffer()], program.programId); const [identity, bumpAuth] = yield anchor.web3.PublicKey.findProgramAddress([encoder.encode('degod_stake'), nftMint.toBuffer(), loan.toBuffer()], programId); const editionId = utils.getMetaplexEditionPda(nftMint); const [farmer, bumpFarmer] = yield anchor.web3.PublicKey.findProgramAddress([encoder.encode('farmer'), farm.toBuffer(), identity.toBuffer()], gemFarm); const [lendingStake] = yield anchor.web3.PublicKey.findProgramAddress([encoder.encode('stake_acc'), loan.toBuffer()], programId); const [vault, bumpVault] = yield anchor.web3.PublicKey.findProgramAddress([encoder.encode('vault'), bank.toBuffer(), identity.toBuffer()], gemBank); const [bankAuthority, bumpAuthVaultAuthority] = yield anchor.web3.PublicKey.findProgramAddress([vault.toBuffer()], gemBank); const [gemBox, bumpGemBox] = yield anchor.web3.PublicKey.findProgramAddress([encoder.encode('gem_box'), vault.toBuffer(), nftMint.toBuffer()], gemBank); const [gemDepositReceipt, bumpGdr] = yield anchor.web3.PublicKey.findProgramAddress([encoder.encode('gem_deposit_receipt'), vault.toBuffer(), nftMint.toBuffer()], gemBank); const [gemRarity, bumpRarity] = yield anchor.web3.PublicKey.findProgramAddress([encoder.encode('gem_rarity'), bank.toBuffer(), nftMint.toBuffer()], gemBank); const [farmAuthority, bumpAuthAuthority] = yield anchor.web3.PublicKey.findProgramAddress([farm.toBuffer()], gemFarm); const [gemMetadata] = yield web3_js_1.PublicKey.findProgramAddress([ Buffer.from(utils.METADATA_PREFIX), exports.METADATA_PROGRAM_PUBKEY.toBuffer(), new web3_js_1.PublicKey(nftMint).toBuffer(), ], exports.METADATA_PROGRAM_PUBKEY); const [mintWhitelistProof] = yield web3_js_1.PublicKey.findProgramAddress([Buffer.from("whitelist"), bank.toBuffer(), nftMint.toBuffer()], gemBank); const nftUserTokenAccount = yield utils.findAssociatedTokenAddress(user, nftMint); const ix = program.instruction.stakeGemFarmStaking({ bumpPoolsAuth, bumpAuth, bumpAuthVaultAuthority, bumpRarity, bumpGdr, bumpGemBox, isDegod, bumpVault, bumpFarmer, }, { accounts: { user, gemFarm, farm, farmAuthority, lendingStake, farmer, loan, identity, bank, gemBank, feeAcc, vault, authority: bankAuthority, gemBox, gemDepositReceipt, gemSource: nftUserTokenAccount, gemMint: nftMint, gemRarity, communityPoolsAuthority, metadataProgram: exports.METADATA_PROGRAM_PUBKEY, editionInfo: editionId, rent: anchor.web3.SYSVAR_RENT_PUBKEY, systemProgram: anchor.web3.SystemProgram.programId, tokenProgram: utils.TOKEN_PROGRAM_ID, }, remainingAccounts: [ // Mint whitelist proof. { pubkey: mintWhitelistProof, isSigner: false, isWritable: false, }, // Gem metadata. { pubkey: gemMetadata, isSigner: false, isWritable: false, }, // Creator whitelist proof. { pubkey: new web3_js_1.PublicKey("23WWUsREVP7e8DcVAvnoVHHdVAJY9ANf4A5sMCZNKvYo"), isSigner: false, isWritable: false, }, ] }); const additionalComputeBudgetInstruction = web3_js_1.ComputeBudgetProgram.requestUnits({ units: 400000, additionalFee: 0, }); const transaction = new web3_js_1.Transaction().add(additionalComputeBudgetInstruction).add(ix); yield sendTxn(transaction); }); } exports.stakeGemFarmStaking = stakeGemFarmStaking; function unstakeGemFarmStaking({ programId, connection, user, gemFarm, farm, bank, gemBank, feeAcc, nftMint, isDegod, loan, sendTxn, }) { return __awaiter(this, void 0, void 0, function* () { const program = utils.returnAnchorProgram(programId, connection); const [communityPoolsAuthority, bumpPoolsAuth] = yield anchor.web3.PublicKey.findProgramAddress([encoder.encode('nftlendingv2'), programId.toBuffer()], program.programId); const [identity, bumpAuth] = yield anchor.web3.PublicKey.findProgramAddress([encoder.encode('degod_stake'), nftMint.toBuffer(), loan.toBuffer()], programId); const editionId = utils.getMetaplexEditionPda(nftMint); const [farmer, bumpFarmer] = yield anchor.web3.PublicKey.findProgramAddress([encoder.encode('farmer'), farm.toBuffer(), identity.toBuffer()], gemFarm); const [lendingStake] = yield anchor.web3.PublicKey.findProgramAddress([encoder.encode('stake_acc'), loan.toBuffer()], programId); const [vault, _bumpVault] = yield anchor.web3.PublicKey.findProgramAddress([encoder.encode('vault'), bank.toBuffer(), identity.toBuffer()], gemBank); const [bankAuthority, bumpAuthVaultAuthority] = yield anchor.web3.PublicKey.findProgramAddress([vault.toBuffer()], gemBank); const [gemBox, bumpGemBox] = yield anchor.web3.PublicKey.findProgramAddress([encoder.encode('gem_box'), vault.toBuffer(), nftMint.toBuffer()], gemBank); const [gemDepositReceipt, bumpGdr] = yield anchor.web3.PublicKey.findProgramAddress([encoder.encode('gem_deposit_receipt'), vault.toBuffer(), nftMint.toBuffer()], gemBank); const [gemRarity, bumpRarity] = yield anchor.web3.PublicKey.findProgramAddress([encoder.encode('gem_rarity'), bank.toBuffer(), nftMint.toBuffer()], gemBank); const [farmTreasury, bumpTreasury] = yield anchor.web3.PublicKey.findProgramAddress([encoder.encode('treasury'), farm.toBuffer()], gemFarm); const [farmAuthority, bumpAuthAuthority] = yield anchor.web3.PublicKey.findProgramAddress([farm.toBuffer()], gemFarm); const nftUserTokenAccount = yield utils.findAssociatedTokenAddress(user, nftMint); const additionalComputeBudgetInstruction = web3_js_1.ComputeBudgetProgram.requestUnits({ units: 400000, additionalFee: 0, }); const ix = program.instruction.unstakeGemFarmStaking({ bumpPoolsAuth, bumpAuth, bumpAuthVaultAuthority, bumpTreasury, bumpFarmer, bumpAuthAuthority, bumpGemBox, bumpGdr, isDegod, bumpRarity }, { accounts: { user, gemFarm, farm, farmAuthority, farmer, farmTreasury, lendingStake, loan, identity, bank, gemBank, feeAcc, vault, authority: bankAuthority, gemBox, gemDepositReceipt, gemSource: nftUserTokenAccount, gemMint: nftMint, gemRarity, communityPoolsAuthority, metadataProgram: exports.METADATA_PROGRAM_PUBKEY, editionInfo: editionId, rent: anchor.web3.SYSVAR_RENT_PUBKEY, systemProgram: anchor.web3.SystemProgram.programId, tokenProgram: utils.TOKEN_PROGRAM_ID, associatedTokenProgram: utils.SPL_ASSOCIATED_TOKEN_ACCOUNT_PROGRAM_ID, } }); const transaction = new web3_js_1.Transaction().add(additionalComputeBudgetInstruction).add(ix); yield sendTxn(transaction); }); } exports.unstakeGemFarmStaking = unstakeGemFarmStaking; function unstakeGemFarmStakingByAdmin({ programId, connection, admin, gemFarm, farm, bank, gemBank, feeAcc, nftMint, isDegod, loan, sendTxn, }) { return __awaiter(this, void 0, void 0, function* () { const program = utils.returnAnchorProgram(programId, connection); const [communityPoolsAuthority, bumpPoolsAuth] = yield anchor.web3.PublicKey.findProgramAddress([encoder.encode('nftlendingv2'), programId.toBuffer()], program.programId); const [identity, bumpAuth] = yield anchor.web3.PublicKey.findProgramAddress([encoder.encode('degod_stake'), nftMint.toBuffer(), loan.toBuffer()], programId); const editionId = utils.getMetaplexEditionPda(nftMint); const [farmer, bumpFarmer] = yield anchor.web3.PublicKey.findProgramAddress([encoder.encode('farmer'), farm.toBuffer(), identity.toBuffer()], gemFarm); const [lendingStake] = yield anchor.web3.PublicKey.findProgramAddress([encoder.encode('stake_acc'), loan.toBuffer()], programId); const [vault, _bumpVault] = yield anchor.web3.PublicKey.findProgramAddress([encoder.encode('vault'), bank.toBuffer(), identity.toBuffer()], gemBank); const [bankAuthority, bumpAuthVaultAuthority] = yield anchor.web3.PublicKey.findProgramAddress([vault.toBuffer()], gemBank); const [gemBox, bumpGemBox] = yield anchor.web3.PublicKey.findProgramAddress([encoder.encode('gem_box'), vault.toBuffer(), nftMint.toBuffer()], gemBank); const [gemDepositReceipt, bumpGdr] = yield anchor.web3.PublicKey.findProgramAddress([encoder.encode('gem_deposit_receipt'), vault.toBuffer(), nftMint.toBuffer()], gemBank); const [gemRarity, bumpRarity] = yield anchor.web3.PublicKey.findProgramAddress([encoder.encode('gem_rarity'), bank.toBuffer(), nftMint.toBuffer()], gemBank); const [farmTreasury, bumpTreasury] = yield anchor.web3.PublicKey.findProgramAddress([encoder.encode('treasury'), farm.toBuffer()], gemFarm); const [farmAuthority, bumpAuthAuthority] = yield anchor.web3.PublicKey.findProgramAddress([farm.toBuffer()], gemFarm); const nftUserTokenAccount = yield utils.findAssociatedTokenAddress(admin, nftMint); const additionalComputeBudgetInstruction = web3_js_1.ComputeBudgetProgram.requestUnits({ units: 400000, additionalFee: 0, }); const ix = program.instruction.unstakeGemFarmStakingByAdmin({ bumpPoolsAuth, bumpAuth, bumpAuthVaultAuthority, bumpTreasury, bumpFarmer, bumpAuthAuthority, bumpGemBox, bumpGdr, isDegod, bumpRarity }, { accounts: { admin, gemFarm, farm, farmAuthority, farmer, farmTreasury, lendingStake, loan, identity, bank, gemBank, feeAcc, vault, authority: bankAuthority, gemBox, gemDepositReceipt, gemSource: nftUserTokenAccount, gemMint: nftMint, gemRarity, communityPoolsAuthority, metadataProgram: exports.METADATA_PROGRAM_PUBKEY, editionInfo: editionId, rent: anchor.web3.SYSVAR_RENT_PUBKEY, systemProgram: anchor.web3.SystemProgram.programId, tokenProgram: utils.TOKEN_PROGRAM_ID, associatedTokenProgram: utils.SPL_ASSOCIATED_TOKEN_ACCOUNT_PROGRAM_ID, } }); const transaction = new web3_js_1.Transaction().add(additionalComputeBudgetInstruction).add(ix); yield sendTxn(transaction); }); } exports.unstakeGemFarmStakingByAdmin = unstakeGemFarmStakingByAdmin; function claimGemFarmStaking({ programId, connection, user, gemFarm, farm, nftMint, loan, isDegod, rewardAMint, rewardBMint, sendTxn, }) { return __awaiter(this, void 0, void 0, function* () { const program = utils.returnAnchorProgram(programId, connection); const [identity, bumpAuth] = yield anchor.web3.PublicKey.findProgramAddress([encoder.encode('degod_stake'), nftMint.toBuffer(), loan.toBuffer()], programId); const [farmer, bumpFarmer] = yield anchor.web3.PublicKey.findProgramAddress([encoder.encode('farmer'), farm.toBuffer(), identity.toBuffer()], gemFarm); const [farmAuthority, bumpAuthAuthority] = yield anchor.web3.PublicKey.findProgramAddress([farm.toBuffer()], gemFarm); const [lendingStake]