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
8 changes: 7 additions & 1 deletion packages/sdk/src/mintlayer-connect-sdk.ts
Original file line number Diff line number Diff line change
Expand Up @@ -1873,8 +1873,14 @@ class Client {
const totalInputValueCoin = inputObjCoin.reduce((acc, item) => acc + BigInt(item.utxo!.value.amount.atoms), 0n);
const totalInputValueToken = inputObjToken.reduce((acc, item) => acc + BigInt(item.utxo!.value.amount.atoms), 0n);

if (type !== 'DelegationWithdraw') {
if (totalInputValueCoin < input_amount_coin_req) {
throw new Error('Not enough coin UTXOs');
}
}

if (totalInputValueToken < input_amount_token_req) {
console.log('Not enough token UTXOs');
throw new Error('Not enough token UTXOs');
}

const changeAmountCoin = totalInputValueCoin - input_amount_coin_req;
Expand Down
49 changes: 49 additions & 0 deletions packages/sdk/tests/transfer.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -114,3 +114,52 @@ test('buildTransaction for transfer - snapshot', async () => {

expect(result).toMatchSnapshot();
});


test('transfer returns signed tx', async () => {
const client = await Client.create({ network: 'testnet', autoRestore: false });
await client.connect();

const result = await client.transfer({
to: 'tmt1q9mfg7d6ul2nt5yhmm7l7r6wwyqkd822rymr83uc',
amount: 10,
});

expect(result).toBe('signed-transaction');
});

test('buildTransaction called with correct params', async () => {
const spy = jest.spyOn(Client.prototype as any, 'buildTransaction');
const client = await Client.create({ network: 'testnet', autoRestore: false });

await client.connect();

await client.transfer({
to: 'tmt1q9mfg7d6ul2nt5yhmm7l7r6wwyqkd822rymr83uc',
amount: 5,
});

expect(spy).toHaveBeenCalledWith({
type: 'Transfer',
params: expect.objectContaining({
to: 'tmt1q9mfg7d6ul2nt5yhmm7l7r6wwyqkd822rymr83uc',
amount: 5,
}),
});
});

test('fails transfer if not enough utxo', async () => {
Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

this test fails, need to fix transfer method

fetchMock.mockIf('https://api.mintini.app/account', async () => {
return {
body: JSON.stringify({ utxos: [] }), // no utxos
};
});

const client = await Client.create({ network: 'testnet', autoRestore: false });
await client.connect();

await expect(client.transfer({
to: 'tmt1q9mfg7d6ul2nt5yhmm7l7r6wwyqkd822rymr83uc',
amount: 999999999,
})).rejects.toThrow('Not enough coin UTXOs');
});