UNPKG

@magiceden/magiceden-sdk

Version:

A TypeScript SDK for interacting with Magic Eden's API across multiple chains.

210 lines (209 loc) 9.61 kB
"use strict"; Object.defineProperty(exports, "__esModule", { value: true }); const transactions_1 = require("../../adapters/transactions"); const chains_1 = require("../../types/chains"); describe('EvmTransactionAdapters', () => { describe('fromV4TransactionResponse', () => { // Remove console.log statements for cleaner test output beforeEach(() => { jest.spyOn(console, 'log').mockImplementation(() => { }); }); afterEach(() => { jest.restoreAllMocks(); }); // Helper function to create a valid EVM transaction step const createValidEvmStep = (id, from, to, value, data) => ({ id, chain: chains_1.Blockchain.ETHEREUM, method: 'eth_sendTransaction', params: { from, to, ...(value ? { value } : {}), ...(data ? { data } : {}), }, }); it('should handle basic V4 transaction response with eth_sendTransaction method', () => { const mockResponse = { steps: [ createValidEvmStep('step1', '0x1234567890123456789012345678901234567890', '0xabcdefabcdefabcdefabcdefabcdefabcdefabcd'), ], }; const result = transactions_1.EvmTransactionAdapters.fromV4TransactionResponse(mockResponse); expect(result).toBeInstanceOf(Array); expect(result).toHaveLength(1); expect(result[0]).toEqual({ from: '0x1234567890123456789012345678901234567890', to: '0xabcdefabcdefabcdefabcdefabcdefabcdefabcd', }); }); it('should handle V4CreateLaunchpadResponse with metadata', () => { const mockResponse = { steps: [ createValidEvmStep('step1', '0x1234567890123456789012345678901234567890', '0xabcdefabcdefabcdefabcdefabcdefabcdefabcd', '0x123', '0x0123456789abcdef'), ], metadata: { imageUrl: 'https://example.com/image.png', tokenImage: 'https://example.com/token.png', metadataUrl: 'https://example.com/metadata.json', }, }; const result = transactions_1.EvmTransactionAdapters.fromV4TransactionResponse(mockResponse); expect(result).toBeInstanceOf(Array); expect(result).toHaveLength(1); expect(result[0]).toEqual({ from: '0x1234567890123456789012345678901234567890', to: '0xabcdefabcdefabcdefabcdefabcdefabcdefabcd', value: BigInt('0x123'), data: '0x0123456789abcdef', }); }); it('should handle V4UpdateLaunchpadResponse with metadata', () => { const mockResponse = { steps: [ createValidEvmStep('step1', '0x1234567890123456789012345678901234567890', '0xabcdefabcdefabcdefabcdefabcdefabcdefabcd', '0x0', '0xabcdef'), ], metadata: { imageUrl: 'https://example.com/updated-image.png', metadataUrl: 'https://example.com/updated-metadata.json', }, }; const result = transactions_1.EvmTransactionAdapters.fromV4TransactionResponse(mockResponse); expect(result).toBeInstanceOf(Array); expect(result).toHaveLength(1); expect(result[0]).toEqual({ from: '0x1234567890123456789012345678901234567890', to: '0xabcdefabcdefabcdefabcdefabcdefabcdefabcd', value: BigInt('0x0'), data: '0xabcdef', }); }); it('should handle V4MintResponse', () => { const mockResponse = { steps: [ createValidEvmStep('step1', '0x1234567890123456789012345678901234567890', '0xabcdefabcdefabcdefabcdefabcdefabcdefabcd', undefined, '0xabcdef0123456789'), ], }; const result = transactions_1.EvmTransactionAdapters.fromV4TransactionResponse(mockResponse); expect(result).toBeInstanceOf(Array); expect(result).toHaveLength(1); expect(result[0]).toEqual({ from: '0x1234567890123456789012345678901234567890', to: '0xabcdefabcdefabcdefabcdefabcdefabcdefabcd', data: '0xabcdef0123456789', }); }); it('should handle multiple steps with transactions', () => { const mockResponse = { steps: [ createValidEvmStep('step1', '0x1234567890123456789012345678901234567890', '0xabcdefabcdefabcdefabcdefabcdefabcdefabcd', '0x123'), createValidEvmStep('step2', '0x2234567890123456789012345678901234567890', '0xbbcdefabcdefabcdefabcdefabcdefabcdefabcd', '0x456', '0xfedcba'), ], }; const result = transactions_1.EvmTransactionAdapters.fromV4TransactionResponse(mockResponse); expect(result).toBeInstanceOf(Array); expect(result).toHaveLength(2); expect(result[0]).toEqual({ from: '0x1234567890123456789012345678901234567890', to: '0xabcdefabcdefabcdefabcdefabcdefabcdefabcd', value: BigInt('0x123'), }); expect(result[1]).toEqual({ from: '0x2234567890123456789012345678901234567890', to: '0xbbcdefabcdefabcdefabcdefabcdefabcdefabcd', value: BigInt('0x456'), data: '0xfedcba', }); }); it('should handle transaction with chainId and gas parameters', () => { const mockResponse = { steps: [ { id: 'step1', chain: chains_1.Blockchain.ETHEREUM, method: 'eth_sendTransaction', params: { from: '0x1234567890123456789012345678901234567890', to: '0xabcdefabcdefabcdefabcdefabcdefabcdefabcd', value: '0x123', chainId: 1, gas: '0x5208', // 21000 in hex }, }, ], }; const result = transactions_1.EvmTransactionAdapters.fromV4TransactionResponse(mockResponse); expect(result).toBeInstanceOf(Array); expect(result).toHaveLength(1); expect(result[0]).toEqual({ from: '0x1234567890123456789012345678901234567890', to: '0xabcdefabcdefabcdefabcdefabcdefabcdefabcd', value: BigInt('0x123'), chainId: 1, gas: BigInt('0x5208'), }); }); it('should throw error for empty steps array', () => { const mockResponse = { steps: [], }; expect(() => { transactions_1.EvmTransactionAdapters.fromV4TransactionResponse(mockResponse); }).toThrow('Invalid transaction response: missing steps'); }); it('should throw error for unsupported method', () => { const mockResponse = { steps: [ { id: 'step1', chain: chains_1.Blockchain.ETHEREUM, method: 'unsupportedMethod', params: { from: '0x1234567890123456789012345678901234567890', to: '0xabcdefabcdefabcdefabcdefabcdefabcdefabcd', }, }, ], }; expect(() => { transactions_1.EvmTransactionAdapters.fromV4TransactionResponse(mockResponse); }).toThrow('No valid EVM transaction steps found in response'); }); it('should throw error for invalid from address', () => { const mockResponse = { steps: [ { id: 'step1', chain: chains_1.Blockchain.ETHEREUM, method: 'eth_sendTransaction', params: { from: 'invalid-address', to: '0xabcdefabcdefabcdefabcdefabcdefabcdefabcd', }, }, ], }; expect(() => { transactions_1.EvmTransactionAdapters.fromV4TransactionResponse(mockResponse); }).toThrow('No valid EVM transaction steps found in response'); }); it('should throw error for invalid to address', () => { const mockResponse = { steps: [ { id: 'step1', chain: chains_1.Blockchain.ETHEREUM, method: 'eth_sendTransaction', params: { from: '0x1234567890123456789012345678901234567890', to: 'invalid-address', }, }, ], }; expect(() => { transactions_1.EvmTransactionAdapters.fromV4TransactionResponse(mockResponse); }).toThrow('No valid EVM transaction steps found in response'); }); }); });