diff --git a/packages/sdk/src/mintlayer-connect-sdk.ts b/packages/sdk/src/mintlayer-connect-sdk.ts index 9ba9536..772577a 100644 --- a/packages/sdk/src/mintlayer-connect-sdk.ts +++ b/packages/sdk/src/mintlayer-connect-sdk.ts @@ -2871,35 +2871,29 @@ class Client { } }).flat(); - // @ts-ignore - if (transactionJSONrepresentation.inputs[0].input.account_type === 'DelegationBalance') { - // @ts-ignore - inputAddresses.push(transactionJSONrepresentation.outputs[0].destination); + const firstInput = transactionJSONrepresentation.inputs[0]?.input as any; + + if (firstInput?.account_type === 'DelegationBalance') { + inputAddresses.push((transactionJSONrepresentation.outputs[0] as any).destination); } - // @ts-ignore - if (transactionJSONrepresentation.inputs[0].input.input_type === 'AccountCommand') { - // @ts-ignore - if (transactionJSONrepresentation.inputs[0].input.destination) { - // @ts-ignore - inputAddresses.push(transactionJSONrepresentation.inputs[0].input.destination); + + if (firstInput?.input_type === 'AccountCommand') { + if (firstInput.destination) { + inputAddresses.push(firstInput.destination); } - // @ts-ignore - if (transactionJSONrepresentation.inputs[0].input.authority) { - // @ts-ignore - inputAddresses.push(transactionJSONrepresentation.inputs[0].input.authority); + + if (firstInput.authority) { + inputAddresses.push(firstInput.authority); } } - // @ts-ignore - if (transactionJSONrepresentation.inputs[0].input.input_type === 'Account') { - // @ts-ignore - if (transactionJSONrepresentation.inputs[0].input.destination) { - // @ts-ignore - inputAddresses.push(transactionJSONrepresentation.inputs[0].input.destination); + + if (firstInput?.input_type === 'Account') { + if (firstInput.destination) { + inputAddresses.push(firstInput.destination); } - // @ts-ignore - if (transactionJSONrepresentation.inputs[0].input.authority) { - // @ts-ignore - inputAddresses.push(transactionJSONrepresentation.inputs[0].input.authority); + + if (firstInput.authority) { + inputAddresses.push(firstInput.authority); } } diff --git a/packages/sdk/src/transaction.ts b/packages/sdk/src/transaction.ts index 036c9c9..65cda1a 100644 --- a/packages/sdk/src/transaction.ts +++ b/packages/sdk/src/transaction.ts @@ -654,35 +654,29 @@ export class Transaction { } }).flat(); - // @ts-ignore - if (transactionJSONrepresentation.inputs[0].input.account_type === 'DelegationBalance') { - // @ts-ignore + const firstInput = transactionJSONrepresentation.inputs[0]?.input as any; + + if (firstInput?.account_type === 'DelegationBalance') { inputAddresses.push(transactionJSONrepresentation.outputs[0].destination); } - // @ts-ignore - if (transactionJSONrepresentation.inputs[0].input.input_type === 'AccountCommand') { - // @ts-ignore - if (transactionJSONrepresentation.inputs[0].input.destination) { - // @ts-ignore - inputAddresses.push(transactionJSONrepresentation.inputs[0].input.destination); + + if (firstInput?.input_type === 'AccountCommand') { + if (firstInput.destination) { + inputAddresses.push(firstInput.destination); } - // @ts-ignore - if (transactionJSONrepresentation.inputs[0].input.authority) { - // @ts-ignore - inputAddresses.push(transactionJSONrepresentation.inputs[0].input.authority); + + if (firstInput.authority) { + inputAddresses.push(firstInput.authority); } } - // @ts-ignore - if (transactionJSONrepresentation.inputs[0].input.input_type === 'Account') { - // @ts-ignore - if (transactionJSONrepresentation.inputs[0].input.destination) { - // @ts-ignore - inputAddresses.push(transactionJSONrepresentation.inputs[0].input.destination); + + if (firstInput?.input_type === 'Account') { + if (firstInput.destination) { + inputAddresses.push(firstInput.destination); } - // @ts-ignore - if (transactionJSONrepresentation.inputs[0].input.authority) { - // @ts-ignore - inputAddresses.push(transactionJSONrepresentation.inputs[0].input.authority); + + if (firstInput.authority) { + inputAddresses.push(firstInput.authority); } } diff --git a/packages/sdk/tests/__mocks__/accounts/account_03_single_utxo.ts b/packages/sdk/tests/__mocks__/accounts/account_03_single_utxo.ts new file mode 100644 index 0000000..6b4ebd7 --- /dev/null +++ b/packages/sdk/tests/__mocks__/accounts/account_03_single_utxo.ts @@ -0,0 +1,29 @@ +export const addresses: any = { + addressesByChain: { + mintlayer: { + receiving: ['tmt1q9874wgx6enm2mzfu0yxhzleu84pp00l95l7er5z'], + change: ['tmt1qxrwc3gy2lgf4kvqwwfa388vn3cavgrqyyrgswe6'], + }, + }, +}; + +export const utxos: any = [ + { + outpoint: { + index: 0, + source_id: 'af3b5fad20f6f97eb210934e942176f7f7d0f70423590659ee0e0217053a7cab', + source_type: 'Transaction', + }, + utxo: { + destination: 'tmt1q9874wgx6enm2mzfu0yxhzleu84pp00l95l7er5z', + type: 'Transfer', + value: { + amount: { + atoms: '2000000000000', + decimal: '20', + }, + type: 'Coin', + }, + }, + }, +]; diff --git a/packages/sdk/tests/staking-create-delegation.test.ts b/packages/sdk/tests/staking-create-delegation.test.ts new file mode 100644 index 0000000..c2bd1c3 --- /dev/null +++ b/packages/sdk/tests/staking-create-delegation.test.ts @@ -0,0 +1,62 @@ +import { Client } from '../src/mintlayer-connect-sdk'; +import fetchMock from 'jest-fetch-mock'; + +import { addresses, utxos } from './__mocks__/accounts/account_03_single_utxo'; + +beforeEach(() => { + fetchMock.resetMocks(); + + (window as any).mojito = { + isExtension: true, + connect: jest.fn().mockResolvedValue(addresses), + restore: jest.fn().mockResolvedValue(addresses), + disconnect: jest.fn().mockResolvedValue(undefined), + request: jest.fn().mockResolvedValue('signed-transaction'), + }; + + fetchMock.doMock(); + + fetchMock.mockResponse(async (req) => { + const url = req.url; + + if (url.endsWith('/chain/tip')) { + return JSON.stringify({ height: 200000 }); + } + + if (url.endsWith('/batch')) { + return { + body: JSON.stringify({ + results: [utxos], + }), + }; + } + + console.warn('No mock for:', url); + return JSON.stringify({ error: 'No mock defined' }); + }); +}); + +test('create delegation uses a single coin UTXO to calculate the fee', async () => { + const client = await Client.create({ network: 'testnet', autoRestore: false }); + + await client.connect(); + + const tx: any = await client.buildDelegationCreate({ + pool_id: 'tpool1dwpe7zy0mhagnwl36ywt5q20xxvu5dwmph4z6q8sc0a3srz5h8jqr0r2yg', + destination: 'tmt1q9874wgx6enm2mzfu0yxhzleu84pp00l95l7er5z', + }); + + expect(tx.JSONRepresentation.inputs).toHaveLength(1); + expect(tx.JSONRepresentation.inputs[0]).toMatchObject({ + input: { + input_type: 'UTXO', + }, + }); + expect(tx.JSONRepresentation.outputs).toEqual( + expect.arrayContaining([ + expect.objectContaining({ + type: 'CreateDelegationId', + }), + ]), + ); +});