@magiceden/magiceden-sdk
Version:
A TypeScript SDK for interacting with Magic Eden's API across multiple chains.
171 lines (170 loc) • 5.86 kB
JavaScript
"use strict";
Object.defineProperty(exports, "__esModule", { value: true });
exports.BaseNftService = void 0;
const types_1 = require("../../types");
const v2_1 = require("../../api/clients/v2");
const v4_1 = require("../../api/clients/v4");
const v3_1 = require("../../api/clients/v3");
/**
* Base class for NFT services
*/
class BaseNftService {
constructor(config) {
this.config = config;
const apiOptions = {
chain: config.chain,
apiKey: config.apiKey,
...config.apiOptions,
};
this.v2ApiClient = new v2_1.V2ApiClient(apiOptions);
this.v3ApiClient = new v3_1.V3ApiClient(apiOptions);
this.v4ApiClient = new v4_1.V4ApiClient(apiOptions);
}
/**
* Publish a launchpad
*
* Only available for Solana. Meant to be called after calling createLaunchpad and creating a launchpad on-chain.
*/
async publishLaunchpad(params) {
return await this.getPublishLaunchpadResponse(params);
}
/**
* Creates a new launchpad
*/
async createLaunchpad(params) {
const operations = await this.getCreateLaunchpadOperations(params);
return await this.processOperations(operations);
}
/**
* Updates an existing launchpad
*/
async updateLaunchpad(params) {
const operations = await this.getUpdateLaunchpadOperations(params);
return await this.processOperations(operations);
}
/**
* Mints an NFT from a launchpad
*/
async mint(params) {
const operations = await this.getMintOperations(params);
return await this.processOperations(operations);
}
/**
* Lists an NFT for sale.
*
* - Supported on EVM.
* - Supported on Solana (every NFT type excluding cNFT).
*
* @param params - The parameters for the list operation
* @returns The operation response
*/
async list(params) {
const operations = await this.getListOperations(params);
return await this.processOperations(operations);
}
/**
* Cancels an NFT listing
*/
async cancelListing(params) {
const operations = await this.getCancelListingOperations(params);
return await this.processOperations(operations);
}
/**
* Takes an item offer on an NFT
*/
async takeItemOffer(params) {
const operations = await this.getTakeItemOfferOperations(params);
return await this.processOperations(operations);
}
/**
* Makes an item offer on an NFT
*/
async makeItemOffer(params) {
const operations = await this.getMakeItemOfferOperations(params);
return await this.processOperations(operations);
}
/**
* Cancels an item offer
*/
async cancelItemOffer(params) {
const operations = await this.getCancelItemOfferOperations(params);
return await this.processOperations(operations);
}
/**
* Buys an NFT
*/
async buy(params) {
const operations = await this.getBuyOperations(params);
return await this.processOperations(operations);
}
/**
* Transfers an NFT to another wallet
*/
async transfer(params) {
const operations = await this.getTransferOperations(params);
return await this.processOperations(operations);
}
/**
* Process a sequence of operations
*/
async processOperations(operations) {
const results = [];
for (const operation of operations) {
switch (operation.type) {
case 'transaction':
const txResult = await this.processTransactionOperation(operation);
results.push(txResult);
break;
case 'signature':
const sigResult = await this.processSignatureOperation(operation);
results.push(sigResult);
break;
default:
throw new Error(`Unsupported operation type: ${operation.type}`);
}
}
return results;
}
/**
* Process a transaction operation
*/
async processTransactionOperation(operation) {
try {
const signature = await this.config.wallet.signAndSendTransaction(operation.transactionData);
switch (this.config.transactionOptions?.strategy || types_1.TransactionStrategy.SignSendAndConfirm) {
case types_1.TransactionStrategy.SignSendAndConfirm:
const txReceipt = await this.config.wallet.waitForTransactionConfirmation(signature);
return {
txId: txReceipt.txId,
status: txReceipt.status,
error: txReceipt.error,
metadata: {
operation: operation.metadata,
receipt: txReceipt.metadata,
},
};
case types_1.TransactionStrategy.SignAndSend:
return {
txId: signature,
status: 'pending',
metadata: {
operation: operation.metadata,
},
};
default:
throw new Error(`Unsupported transaction strategy: ${this.config.transactionOptions?.strategy}`);
}
}
catch (error) {
return {
txId: '',
status: 'failed',
error: error instanceof Error ? error.message : 'Unknown error',
metadata: {
operation: operation.metadata
}
};
}
}
}
exports.BaseNftService = BaseNftService;