UNPKG

@frakt-protocol/frakt-sdk

Version:

Frakt SDK for interacting with frakt.xyz protocols

75 lines (74 loc) 3.8 kB
"use strict"; 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.proposeLoanIx = void 0; const mpl_token_metadata_1 = require("@metaplex-foundation/mpl-token-metadata"); const anchor_1 = require("@project-serum/anchor"); const common_1 = require("../../../common"); const constants_1 = require("../../constants"); const helpers_1 = require("../../helpers"); const proposeLoanIx = ({ proposedNftPrice, programId, connection, user, nftMint, isPriceBased, loanToValue, admin, }) => __awaiter(void 0, void 0, void 0, function* () { var _a; const program = (0, helpers_1.returnAnchorProgram)(programId, connection); const loan = anchor_1.web3.Keypair.generate(); const encoder = new TextEncoder(); const [communityPoolsAuthority, bumpPoolsAuth] = yield anchor_1.web3.PublicKey.findProgramAddress([encoder.encode('nftlendingv2'), programId.toBuffer()], programId); const nftUserTokenAccount = yield (0, common_1.findAssociatedTokenAddress)(user, nftMint); const editionId = (0, helpers_1.getMetaplexEditionPda)(nftMint); const nftMetadata = (0, helpers_1.getMetaplexMetadata)(nftMint); const tokenRecordInfo = (0, helpers_1.findTokenRecordPda)(nftMint, nftUserTokenAccount); const metadataAccount = yield mpl_token_metadata_1.Metadata.fromAccountAddress(connection, nftMetadata); const ruleSet = (_a = metadataAccount.programmableConfig) === null || _a === void 0 ? void 0 : _a.ruleSet; const tokenRecordData = metadataAccount.tokenStandard === mpl_token_metadata_1.TokenStandard.ProgrammableNonFungible ? yield mpl_token_metadata_1.TokenRecord.fromAccountAddress(connection, tokenRecordInfo) : { delegate: null }; const delegatePubkey = tokenRecordData.delegate; const ix = yield program.methods .proposeLoanNew(isPriceBased, proposedNftPrice, loanToValue) .accountsStrict({ loan: loan.publicKey, user: user, nftUserTokenAccount, nftMint: nftMint, communityPoolsAuthority, instructions: anchor_1.web3.SYSVAR_INSTRUCTIONS_PUBKEY, authorizationRulesProgram: constants_1.AUTHORIZATION_RULES_PROGRAM, nftMetadata, tokenRecordInfo, tokenProgram: anchor_1.utils.token.TOKEN_PROGRAM_ID, rent: anchor_1.web3.SYSVAR_RENT_PUBKEY, systemProgram: anchor_1.web3.SystemProgram.programId, metadataProgram: constants_1.METADATA_PROGRAM_PUBKEY, admin, editionInfo: editionId, }) .remainingAccounts([ { pubkey: ruleSet || constants_1.METADATA_PROGRAM_PUBKEY, isSigner: false, isWritable: false, }, { pubkey: delegatePubkey || constants_1.METADATA_PROGRAM_PUBKEY, isSigner: false, isWritable: false, }, ]) .instruction(); const ixs = []; ixs.push(anchor_1.web3.ComputeBudgetProgram.setComputeUnitLimit({ units: Math.random() * 50000 + 425000, // additionalFee: 0, })); ixs.push(ix); return { loan: loan, ixs }; }); exports.proposeLoanIx = proposeLoanIx;