@magiceden/magiceden-sdk
Version:
A TypeScript SDK for interacting with Magic Eden's API across multiple chains.
114 lines (113 loc) • 5.37 kB
JavaScript
;
Object.defineProperty(exports, "__esModule", { value: true });
exports.SolanaTransactionAdapters = void 0;
const web3_js_1 = require("@solana/web3.js");
const types_1 = require("../../types");
/**
* Solana Transaction Adapters
* Converts between API responses and Solana transaction objects
*/
exports.SolanaTransactionAdapters = {
/**
* Converts a Solana instructions response into a transaction object
* @param response The API response containing transaction data
* @returns A properly formatted Solana transaction
*/
fromInstructionsResponse: (response) => {
// Handle versioned transactions (v0)
if (response.v0?.txSigned) {
return [
{
type: 'transaction',
transactionData: web3_js_1.VersionedTransaction.deserialize(Buffer.from(response.v0.txSigned.data)),
},
];
}
else if (response.v0?.tx) {
return [
{
type: 'transaction',
transactionData: web3_js_1.VersionedTransaction.deserialize(Buffer.from(response.v0.tx.data)),
},
];
}
throw new Error('Invalid transaction response format, only versioned transactions are supported');
},
/**
* Process a V4 transaction response and extract Solana transactions
* @param response The API response containing transaction data
* @returns An array of properly formatted Solana transactions
*/
fromV4TransactionResponse(response) {
if (!response.steps || response.steps.length === 0) {
throw new Error('Invalid transaction response format, only Solana transactions are supported');
}
// Filter for valid Solana transaction steps with signAllAndSendTransactions method
const validTransactionSteps = response.steps.filter((step) => {
if (step.chain !== types_1.Blockchain.SOLANA || step.method !== 'signAllAndSendTransactions') {
return false;
}
const result = types_1.SolanaTransactionParams.safeParse(step.params);
return result.success;
});
if (validTransactionSteps.length === 0) {
throw new Error('Invalid transaction response format, only Solana transactions are supported');
}
// Filter for valid submit steps with the Post method
const validSubmitSteps = response.steps.filter((step) => {
if (step.chain !== types_1.Blockchain.SOLANA || step.method !== 'Post') {
return false;
}
const result = types_1.SolanaSubmitParams.safeParse(step.params);
return result.success;
});
// Get the first valid submit step and convert it to a SolanaSubmitParams object
// Currently the only way to extract the candyMachineId, collectionId, and symbol is to get it from the submit step
// This is a bit of a hack, but it works for now
// Normally for each V4TransactionResponse, there should be one transaction step and one submit step
// # of transaction steps could change, but there should always be only one submit step
const submitParams = validSubmitSteps.length > 0 ? types_1.SolanaSubmitParams.safeParse(validSubmitSteps[0].params).data : undefined;
const transactions = [];
for (const step of validTransactionSteps) {
const result = types_1.SolanaTransactionParams.safeParse(step.params);
if (!result.success) {
continue; // Skip non-Solana steps
}
const solanaParams = result.data;
const stepTransactions = solanaParams.transactions.map((tx) => {
try {
// TODO: Implement tx.signerPubkeys to make sure the transaction gets signed by the respective private keys for any of the extra signers
// Currently we don't allow you to pass in any extra signers where this would be relevant, but we should still make sure to implement this
// Refer to accounts in src/types/services/nft/createLaunchpad.ts for more details
const txBuffer = Buffer.from(tx.transaction, 'base64');
return {
type: 'transaction',
transactionData: web3_js_1.VersionedTransaction.deserialize(txBuffer),
metadata: {
...(submitParams?.payload ? { payload: submitParams.payload } : {}),
},
};
}
catch (error) {
throw error;
}
});
transactions.push(...stepTransactions);
}
return transactions;
},
/**
* Converts raw transaction data into a transaction object
* @param data Raw transaction data buffer
* @returns A properly formatted Solana transaction
*/
fromBuffer: (data) => {
try {
// Try to deserialize as a versioned transaction first
return [web3_js_1.VersionedTransaction.deserialize(data)];
}
catch (e) {
throw new Error('Invalid transaction data, only versioned transactions are supported');
}
},
};