Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
42 changes: 18 additions & 24 deletions packages/sdk/src/mintlayer-connect-sdk.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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);
}
}

Expand Down
40 changes: 17 additions & 23 deletions packages/sdk/src/transaction.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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);
}
}

Expand Down
29 changes: 29 additions & 0 deletions packages/sdk/tests/__mocks__/accounts/account_03_single_utxo.ts
Original file line number Diff line number Diff line change
@@ -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',
},
},
},
];
62 changes: 62 additions & 0 deletions packages/sdk/tests/staking-create-delegation.test.ts
Original file line number Diff line number Diff line change
@@ -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',
}),
]),
);
});
Loading