UNPKG

@magiceden/magiceden-sdk

Version:

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

189 lines (188 loc) 7.78 kB
"use strict"; Object.defineProperty(exports, "__esModule", { value: true }); const solanaKeypairWalletProvider_1 = require("../../wallet/solana/solanaKeypairWalletProvider"); const web3_js_1 = require("@solana/web3.js"); // Mock the Connection class and other Solana objects jest.mock('@solana/web3.js', () => { const original = jest.requireActual('@solana/web3.js'); // Create a mock Keypair class class MockKeypair { constructor() { this.publicKey = { toBase58: () => 'mock-public-key', toBytes: () => new Uint8Array(32), toString: () => 'mock-public-key', }; this.secretKey = new Uint8Array(64).fill(1); } } // Create a mock VersionedTransaction class class MockVersionedTransaction { constructor() { this.sign = jest.fn(); // Empty constructor } } return { ...original, Keypair: { ...original.Keypair, generate: () => new MockKeypair(), fromSecretKey: () => new MockKeypair(), }, Connection: jest.fn().mockImplementation(() => ({ getBalance: jest.fn().mockResolvedValue(1000000000), // 1 SOL getLatestBlockhash: jest.fn().mockResolvedValue({ blockhash: 'mock-blockhash', lastValidBlockHeight: 123456, }), sendTransaction: jest.fn().mockResolvedValue('mock-signature'), confirmTransaction: jest.fn().mockResolvedValue({ context: { slot: 1 }, value: { err: null }, }), getSignatureStatus: jest.fn().mockResolvedValue({ context: { slot: 1 }, value: { slot: 1, confirmations: 1, err: null, confirmationStatus: 'confirmed' }, }), rpcEndpoint: 'https://api.devnet.solana.com', })), VersionedTransaction: MockVersionedTransaction, MessageV0: { compile: jest.fn().mockReturnValue({}), }, SystemProgram: { transfer: jest.fn().mockReturnValue({}), }, ComputeBudgetProgram: { setComputeUnitPrice: jest.fn().mockReturnValue({}), setComputeUnitLimit: jest.fn().mockReturnValue({}), }, PublicKey: jest.fn().mockImplementation((key) => ({ toBase58: () => (typeof key === 'string' ? key : 'mock-public-key'), toString: () => (typeof key === 'string' ? key : 'mock-public-key'), })), }; }); // Mock bs58 to avoid actual encoding/decoding jest.mock('bs58', () => ({ encode: jest.fn().mockReturnValue('mock-base58-encoded-string'), decode: jest.fn().mockReturnValue(new Uint8Array(64).fill(1)), })); // Mock nacl for signature verification jest.mock('tweetnacl', () => { return { sign: { detached: jest.fn().mockReturnValue(new Uint8Array(64).fill(1)), }, }; }); // Add the verify function to the mocked nacl const mockedNacl = require('tweetnacl'); mockedNacl.sign.detached.verify = jest.fn().mockReturnValue(true); describe('SolanaKeypairWalletProvider', () => { let wallet; let keypair; beforeEach(() => { // Generate a new keypair for each test keypair = web3_js_1.Keypair.generate(); wallet = new solanaKeypairWalletProvider_1.SolanaKeypairWalletProvider({ secretKey: new Uint8Array(64).fill(1), // Mock secret key rpcEndpoint: 'https://api.devnet.solana.com', }); }); describe('constructor', () => { it('should create a wallet from a Uint8Array secret key', () => { const wallet = new solanaKeypairWalletProvider_1.SolanaKeypairWalletProvider({ secretKey: new Uint8Array(64).fill(1), rpcEndpoint: 'https://api.devnet.solana.com', }); expect(wallet.getAddress()).toBe('mock-public-key'); }); it('should create a wallet from a base58 encoded secret key', () => { const wallet = new solanaKeypairWalletProvider_1.SolanaKeypairWalletProvider({ secretKey: 'mock-base58-key', rpcEndpoint: 'https://api.devnet.solana.com', }); expect(wallet.getAddress()).toBe('mock-public-key'); }); }); describe('getAddress', () => { it('should return the correct address', () => { expect(wallet.getAddress()).toBe('mock-public-key'); }); }); describe('getBalance', () => { it('should return the wallet balance', async () => { const balance = await wallet.getBalance(); expect(balance).toBe(BigInt(1000000000)); // 1 SOL }); }); describe('signMessage', () => { it('should sign a string message', async () => { const message = 'Hello, world!'; const signature = await wallet.signMessage(message); expect(signature).toBe('mock-base58-encoded-string'); }); it('should sign a Uint8Array message', async () => { const messageBytes = new TextEncoder().encode('Hello, world!'); const signature = await wallet.signMessage(messageBytes); expect(signature).toBe('mock-base58-encoded-string'); }); }); describe('transaction operations', () => { let mockTransaction; beforeEach(() => { // Create a mock transaction with a dummy message mockTransaction = new web3_js_1.VersionedTransaction( // Pass a mock message to the constructor {}); }); describe('signTransaction', () => { it('should sign a transaction', async () => { const signedTx = await wallet.signTransaction(mockTransaction); expect(mockTransaction.sign).toHaveBeenCalled(); expect(signedTx).toBe(mockTransaction); }); }); describe('sendTransaction', () => { it('should send a transaction', async () => { const signature = await wallet.signAndSendTransaction(mockTransaction); expect(signature).toBe('mock-signature'); }); }); describe('signAndSendTransaction', () => { it('should sign and send a transaction', async () => { const signSpy = jest.spyOn(wallet, 'signTransaction'); const sendSpy = jest.spyOn(wallet, 'signAndSendTransaction'); const signature = await wallet.signAndSendTransaction(mockTransaction); expect(signSpy).toHaveBeenCalledWith(mockTransaction); expect(sendSpy).toHaveBeenCalled(); expect(signature).toBe('mock-signature'); }); }); }); describe('waitForTransactionConfirmation', () => { it('should wait for transaction confirmation', async () => { const result = await wallet.waitForTransactionConfirmation('mock-signature'); expect(result).toEqual({ txId: 'mock-signature', status: 'confirmed', error: undefined, metadata: { blockhash: 'mock-blockhash', lastValidBlockHeight: 123456, }, }); }); }); describe('getSignatureStatus', () => { it('should get the signature status', async () => { const status = await wallet.getSignatureStatus('mock-signature'); expect(status).toEqual({ context: { slot: 1 }, value: { slot: 1, confirmations: 1, err: null, confirmationStatus: 'confirmed' }, }); }); }); });