@magiceden/magiceden-sdk
Version:
A TypeScript SDK for interacting with Magic Eden's API across multiple chains.
116 lines (115 loc) • 5.04 kB
JavaScript
;
Object.defineProperty(exports, "__esModule", { value: true });
exports.EvmTransactionAdapters = void 0;
const types_1 = require("../../types");
const steps_1 = require("../../types/services/nft/shared/steps");
const validation_1 = require("../../validation");
/**
* Ethereum Transaction Adapters
* Converts between API responses and Ethereum transaction objects
*/
exports.EvmTransactionAdapters = {
fromV3TransactionResponse: (response) => {
if (response.errors && response.errors.length > 0) {
throw new Error(`Magic Eden API errors: ${JSON.stringify(response.errors)}`);
}
const operations = [];
// Process each step in the API response.
for (const step of response.steps) {
if (!step.items || step.items.length === 0)
continue;
// Process each item in the step.
for (const [index, item] of step.items.entries()) {
if (item.status !== 'incomplete') {
continue;
}
if (step.kind === 'transaction') {
const transaction = {
to: item.data.to,
from: item.data.from,
data: item.data.data,
...(item.data.value ? { value: BigInt(item.data.value) } : {}),
};
operations.push({
type: 'transaction',
transactionData: transaction,
});
}
else if (step.kind === 'signature') {
const signData = item.data?.sign;
if (!signData) {
throw new Error('Invalid signature response: missing signature data');
}
const signatureOperation = {
api: 'v3',
chainId: signData.domain.chainId,
domain: signData.domain,
types: signData.types,
message: signData.value,
primaryType: signData.primaryType,
post: item.data.post,
};
operations.push({
type: 'signature',
signatureData: signatureOperation,
});
}
else {
throw new Error(`Unsupported step kind "${step.kind}" for step "${step.action}"`);
}
}
}
return operations;
},
/**
* Converts a V4 transaction response into an array of Ethereum transactions
* @param response The API response containing transaction data
* @returns An array of properly formatted Ethereum transactions
*/
fromV4TransactionResponse: (response) => {
if (!response.steps || response.steps.length === 0) {
throw new Error('Invalid transaction response: missing steps');
}
// Filter for valid EVM steps with eth_sendTransaction method
const validSteps = response.steps.filter((step) => {
if (!step.chain || step.method !== 'eth_sendTransaction') {
return false;
}
// Check if the chain is a valid EVM blockchain
const evmChainResult = types_1.ZodEvmBlockchain.safeParse(step.chain);
if (!evmChainResult.success) {
return false;
}
const result = steps_1.EvmTransactionParams.safeParse(step.params);
return result.success;
});
if (validSteps.length === 0) {
throw new Error('No valid EVM transaction steps found in response');
}
const transactions = [];
for (const step of validSteps) {
const result = steps_1.EvmTransactionParams.safeParse(step.params);
if (!result.success) {
continue; // Skip invalid steps
}
if (!(0, validation_1.isHexPrefixedString)(result.data.from) || !(0, validation_1.isHexPrefixedString)(result.data.to)) {
throw new Error('Invalid transaction response: invalid from or to address');
}
const evmParams = result.data;
// Create a transaction request object
const transaction = {
from: evmParams.from,
to: evmParams.to,
...(evmParams.value ? { value: BigInt(evmParams.value) } : {}),
...(evmParams.data && (0, validation_1.isHexPrefixedString)(evmParams.data) ? { data: evmParams.data } : {}),
...(evmParams.chainId ? { chainId: evmParams.chainId } : {}),
...(evmParams.gas ? { gas: BigInt(evmParams.gas) } : {}),
};
transactions.push(transaction);
}
return transactions.map((tx) => ({
type: 'transaction',
transactionData: tx,
}));
},
};