@rainfi/sdk
Version:
This package is used to interact with Rain.fi protocol on Solana
122 lines • 7.51 kB
JavaScript
"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());
});
};
var __importDefault = (this && this.__importDefault) || function (mod) {
return (mod && mod.__esModule) ? mod : { "default": mod };
};
Object.defineProperty(exports, "__esModule", { value: true });
exports.buyEscrow = void 0;
const spl_token_1 = require("@solana/spl-token");
const web3_js_1 = require("@solana/web3.js");
const bn_js_1 = __importDefault(require("bn.js"));
const constant_1 = require("../constant");
const layout_utils_1 = require("../layout.utils");
const metadata_utils_1 = require("../metadata.utils");
const tools_utils_1 = require("../tools.utils");
const pda_utils_1 = require("../pda.utils");
const mpl_token_metadata_1 = require("@metaplex-foundation/mpl-token-metadata");
function buyEscrow(connection, signer, mintAddress, price, shared) {
return __awaiter(this, void 0, void 0, function* () {
var _a, _b, _c;
const mintAccount = new web3_js_1.PublicKey(mintAddress);
const metadata_account = yield (0, metadata_utils_1.getMetadataAddress)(mintAccount);
const metadata = yield mpl_token_metadata_1.Metadata.fromAccountAddress(connection, metadata_account);
if (!metadata) {
throw new Error('No metadata found for ' + mintAddress);
}
const PDA_escrowAccount = web3_js_1.PublicKey.findProgramAddressSync([
Buffer.from('sale'),
mintAccount.toBuffer(),
], constant_1.SOLANART_PROGRAM_V1)[0];
const nftAcc = (0, tools_utils_1.getAtaForMint)(mintAccount, signer);
let encodedEscrowState;
try {
encodedEscrowState = (yield connection.getAccountInfo(PDA_escrowAccount, 'processed')).data;
}
catch (err) {
throw new Error("Unable to find escrow account, please try again in few minutes.");
}
const decodedEscrowLayout = layout_utils_1.ESCROW_ACCOUNT_DATA_LAYOUT.decode(encodedEscrowState);
const PDA = yield web3_js_1.PublicKey.findProgramAddress([Buffer.from("escrow")], constant_1.SOLANART_PROGRAM_V1);
const seller = new web3_js_1.PublicKey(decodedEscrowLayout.initializerPubkey);
const STAKE_account = (yield web3_js_1.PublicKey.findProgramAddress([
Buffer.from('nft'),
seller.toBuffer(),
], constant_1.STAKE_PROGRAM_ID))[0];
const royalties_account = (yield web3_js_1.PublicKey.findProgramAddress([
Buffer.from('fees'),
mintAccount.toBuffer(),
seller.toBuffer(),
], constant_1.SOLANART_PROGRAM_V1))[0];
const isCancel = seller.equals(signer);
const keys = [
{ pubkey: signer, isSigner: true, isWritable: false },
{ pubkey: nftAcc, isSigner: false, isWritable: true },
{ pubkey: new web3_js_1.PublicKey(decodedEscrowLayout.initializerTempTokenAccountPubkey), isSigner: false, isWritable: true },
{ pubkey: seller, isSigner: false, isWritable: true },
{ pubkey: PDA_escrowAccount, isSigner: false, isWritable: true },
{ pubkey: royalties_account, isSigner: false, isWritable: true },
{ pubkey: spl_token_1.TOKEN_PROGRAM_ID, isSigner: false, isWritable: false },
{ pubkey: constant_1.SOLANART_FEE_ADDRESS, isSigner: false, isWritable: true },
{ pubkey: PDA[0], isSigner: false, isWritable: true },
{ pubkey: metadata_account, isSigner: false, isWritable: true },
{ pubkey: mintAccount, isSigner: false, isWritable: true },
{ pubkey: STAKE_account, isSigner: false, isWritable: true },
{ pubkey: constant_1.STAKE_PROGRAM_ID, isSigner: false, isWritable: false },
{ pubkey: web3_js_1.SystemProgram.programId, isSigner: false, isWritable: false }
];
const rulesSet = ((_a = metadata.programmableConfig) === null || _a === void 0 ? void 0 : _a.ruleSet) || web3_js_1.PublicKey.default;
const isPnft = !rulesSet.equals(web3_js_1.PublicKey.default);
const ownerTokenRecordPda = yield (0, pda_utils_1.findTokenRecordPDA)(mintAccount, new web3_js_1.PublicKey(decodedEscrowLayout.initializerTempTokenAccountPubkey));
const destTokenRecordPda = yield (0, pda_utils_1.findTokenRecordPDA)(mintAccount, nftAcc);
!isCancel && ((_c = (_b = metadata.data) === null || _b === void 0 ? void 0 : _b.creators) === null || _c === void 0 ? void 0 : _c.map((creator) => {
keys.push({ pubkey: new web3_js_1.PublicKey(creator === null || creator === void 0 ? void 0 : creator.address), isSigner: false, isWritable: true });
}));
isPnft && keys.push({ pubkey: yield (0, pda_utils_1.getNFTEdition)(mintAccount), isSigner: false, isWritable: true });
isPnft && keys.push({ pubkey: constant_1.ASSOCIATED_TOKEN_PROGRAM_ID, isSigner: false, isWritable: false });
isPnft && keys.push({ pubkey: constant_1.METADATA_PROGRAM_ID, isSigner: false, isWritable: false });
isPnft && keys.push({ pubkey: web3_js_1.SYSVAR_INSTRUCTIONS_PUBKEY, isSigner: false, isWritable: false });
isPnft && keys.push({ pubkey: ownerTokenRecordPda, isSigner: false, isWritable: true });
isPnft && keys.push({ pubkey: destTokenRecordPda, isSigner: false, isWritable: true });
isPnft && keys.push({ pubkey: constant_1.AUTH_PROGRAM_ID, isSigner: false, isWritable: false });
isPnft && keys.push({ pubkey: rulesSet, isSigner: false, isWritable: false });
if (shared) {
keys.push({ pubkey: shared, isSigner: false, isWritable: true }, {
pubkey: (yield web3_js_1.PublicKey.findProgramAddress([
Buffer.from('referrer'),
shared.toBuffer(),
], constant_1.SOLANART_PROGRAM_V1))[0],
isSigner: false, isWritable: true
});
}
price = price * 1000000000 || 0;
const exchangeInstruction = new web3_js_1.TransactionInstruction({
programId: constant_1.SOLANART_PROGRAM_V1,
data: Buffer.from([
5,
...new bn_js_1.default(price).toArray("le", 8),
]),
keys: keys
});
let createAta;
if (!(yield connection.getAccountInfo(nftAcc))) {
createAta = (0, tools_utils_1.createAssociatedTokenAccountInstruction)(signer, nftAcc, signer, mintAccount);
}
const additionalComputeBudgetInstruction = web3_js_1.ComputeBudgetProgram.setComputeUnitLimit({
units: 1400000,
});
return {
fullIx: createAta ? [additionalComputeBudgetInstruction, createAta, exchangeInstruction] : [additionalComputeBudgetInstruction, exchangeInstruction],
buyIx: exchangeInstruction
};
});
}
exports.buyEscrow = buyEscrow;
//# sourceMappingURL=solanart.js.map