UNPKG

@rainfi/sdk

Version:

This package is used to interact with Rain.fi protocol on Solana

130 lines 7.08 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.publicBuyAndExecuteSale = void 0; const web3_js_1 = require("@solana/web3.js"); const anchor = __importStar(require("@coral-xyz/anchor")); const constant_1 = require("../constant"); const tools_utils_1 = require("../tools.utils"); const AUCTION_HOUSE_PROGRAM_ID = new web3_js_1.PublicKey("hausS13jsjafwWwGqZTUQRmWyvyxn9EQpqMwV1PBBmk"); const getAuctionHouseProgramAsSigner = () => __awaiter(void 0, void 0, void 0, function* () { return yield anchor.web3.PublicKey.findProgramAddress([Buffer.from("auction_house"), Buffer.from("signer")], AUCTION_HOUSE_PROGRAM_ID); }); const getAuctionHouseTradeState = (auctionHouse, wallet, tokenAccount, treasuryMint, tokenMint, tokenSize, buyPrice) => __awaiter(void 0, void 0, void 0, function* () { return yield anchor.web3.PublicKey.findProgramAddress([ Buffer.from("auction_house"), wallet.toBuffer(), auctionHouse.toBuffer(), tokenAccount.toBuffer(), treasuryMint.toBuffer(), tokenMint.toBuffer(), buyPrice.toArrayLike(Buffer, 'le', 8), tokenSize.toArrayLike(Buffer, 'le', 8), ], AUCTION_HOUSE_PROGRAM_ID); }); const getAuctionHouseBuyerEscrow = (auctionHouse, wallet) => __awaiter(void 0, void 0, void 0, function* () { return yield anchor.web3.PublicKey.findProgramAddress([Buffer.from("auction_house"), auctionHouse.toBuffer(), wallet.toBuffer()], AUCTION_HOUSE_PROGRAM_ID); }); const publicBuyAndExecuteSale = (connection, buyer, seller, nft, auctionHousePubkey, price, buyerTokenAccount) => __awaiter(void 0, void 0, void 0, function* () { const { createBuyInstruction, createExecuteSaleInstruction, AuctionHouse } = (yield Promise.resolve().then(() => __importStar(require("@metaplex-foundation/mpl-auction-house/dist/src/generated")))); const auctionHouse = yield AuctionHouse.fromAccountAddress(connection, auctionHousePubkey); const metadata_account = (yield web3_js_1.PublicKey.findProgramAddress([ Buffer.from("metadata"), constant_1.METADATA_PROGRAM_ID.toBuffer(), new web3_js_1.PublicKey(nft.mint).toBuffer(), ], constant_1.METADATA_PROGRAM_ID))[0]; const remainingAccounts = []; if (nft.data.creators) { for (let i = 0; i < nft.data.creators.length; i++) { remainingAccounts.push({ pubkey: new web3_js_1.PublicKey(nft.data.creators[i].address), isWritable: true, isSigner: false, }); } } const [programAsSigner, programAsSignerBump] = yield getAuctionHouseProgramAsSigner(); const tokenAccount = yield (0, tools_utils_1.getAtaForMint)(new web3_js_1.PublicKey(nft.mint), seller); const [buyerTradeState, tradeStateBump] = yield getAuctionHouseTradeState(auctionHousePubkey, buyer, tokenAccount, auctionHouse.treasuryMint, new web3_js_1.PublicKey(nft.mint), new anchor.BN(1), price); const [sellerTradeState, sellerTradeStateBump] = yield getAuctionHouseTradeState(auctionHousePubkey, seller, tokenAccount, auctionHouse.treasuryMint, new web3_js_1.PublicKey(nft.mint), new anchor.BN(1), price); const [freeTradeState, freeTradeStateBump] = yield getAuctionHouseTradeState(auctionHousePubkey, seller, tokenAccount, auctionHouse.treasuryMint, new web3_js_1.PublicKey(nft.mint), new anchor.BN(1), new anchor.BN(0)); const [escrowPaymentAccount, escrowPaymentBump] = yield getAuctionHouseBuyerEscrow(auctionHousePubkey, buyer); const buyIx = createBuyInstruction({ wallet: buyer, paymentAccount: buyer, transferAuthority: buyer, treasuryMint: auctionHouse.treasuryMint, tokenAccount: tokenAccount, metadata: metadata_account, escrowPaymentAccount: escrowPaymentAccount, authority: auctionHouse.authority, auctionHouse: auctionHousePubkey, auctionHouseFeeAccount: auctionHouse.auctionHouseFeeAccount, buyerTradeState: buyerTradeState, }, { tradeStateBump: tradeStateBump, escrowPaymentBump: escrowPaymentBump, buyerPrice: price, tokenSize: 1, }); const executeSaleIx = createExecuteSaleInstruction({ buyer: buyer, seller: seller, tokenAccount: tokenAccount, metadata: metadata_account, treasuryMint: auctionHouse.treasuryMint, escrowPaymentAccount: escrowPaymentAccount, sellerPaymentReceiptAccount: seller, buyerReceiptTokenAccount: buyerTokenAccount, authority: auctionHouse.authority, auctionHouse: auctionHousePubkey, auctionHouseFeeAccount: auctionHouse.auctionHouseFeeAccount, auctionHouseTreasury: auctionHouse.auctionHouseTreasury, buyerTradeState: buyerTradeState, sellerTradeState: sellerTradeState, freeTradeState: freeTradeState, programAsSigner: programAsSigner, tokenMint: new web3_js_1.PublicKey(nft.mint), }, { escrowPaymentBump: escrowPaymentBump, freeTradeStateBump: freeTradeStateBump, programAsSignerBump: programAsSignerBump, buyerPrice: price, tokenSize: 1, }); executeSaleIx.keys.push(...remainingAccounts); return [buyIx, executeSaleIx]; }); exports.publicBuyAndExecuteSale = publicBuyAndExecuteSale; //# sourceMappingURL=auctionHouse.js.map