Skip to content

Commit 2da76f8

Browse files
committed
fix: throw error on not enough utxos
1 parent 9c38031 commit 2da76f8

2 files changed

Lines changed: 56 additions & 1 deletion

File tree

packages/sdk/src/mintlayer-connect-sdk.ts

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1873,8 +1873,14 @@ class Client {
18731873
const totalInputValueCoin = inputObjCoin.reduce((acc, item) => acc + BigInt(item.utxo!.value.amount.atoms), 0n);
18741874
const totalInputValueToken = inputObjToken.reduce((acc, item) => acc + BigInt(item.utxo!.value.amount.atoms), 0n);
18751875

1876+
if (type !== 'DelegationWithdraw') {
1877+
if (totalInputValueCoin < input_amount_coin_req) {
1878+
throw new Error('Not enough coin UTXOs');
1879+
}
1880+
}
1881+
18761882
if (totalInputValueToken < input_amount_token_req) {
1877-
console.log('Not enough token UTXOs');
1883+
throw new Error('Not enough token UTXOs');
18781884
}
18791885

18801886
const changeAmountCoin = totalInputValueCoin - input_amount_coin_req;

packages/sdk/tests/transfer.test.ts

Lines changed: 49 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -114,3 +114,52 @@ test('buildTransaction for transfer - snapshot', async () => {
114114

115115
expect(result).toMatchSnapshot();
116116
});
117+
118+
119+
test('transfer returns signed tx', async () => {
120+
const client = await Client.create({ network: 'testnet', autoRestore: false });
121+
await client.connect();
122+
123+
const result = await client.transfer({
124+
to: 'tmt1q9mfg7d6ul2nt5yhmm7l7r6wwyqkd822rymr83uc',
125+
amount: 10,
126+
});
127+
128+
expect(result).toBe('signed-transaction');
129+
});
130+
131+
test('buildTransaction called with correct params', async () => {
132+
const spy = jest.spyOn(Client.prototype as any, 'buildTransaction');
133+
const client = await Client.create({ network: 'testnet', autoRestore: false });
134+
135+
await client.connect();
136+
137+
await client.transfer({
138+
to: 'tmt1q9mfg7d6ul2nt5yhmm7l7r6wwyqkd822rymr83uc',
139+
amount: 5,
140+
});
141+
142+
expect(spy).toHaveBeenCalledWith({
143+
type: 'Transfer',
144+
params: expect.objectContaining({
145+
to: 'tmt1q9mfg7d6ul2nt5yhmm7l7r6wwyqkd822rymr83uc',
146+
amount: 5,
147+
}),
148+
});
149+
});
150+
151+
test('fails transfer if not enough utxo', async () => {
152+
fetchMock.mockIf('https://api.mintini.app/account', async () => {
153+
return {
154+
body: JSON.stringify({ utxos: [] }), // no utxos
155+
};
156+
});
157+
158+
const client = await Client.create({ network: 'testnet', autoRestore: false });
159+
await client.connect();
160+
161+
await expect(client.transfer({
162+
to: 'tmt1q9mfg7d6ul2nt5yhmm7l7r6wwyqkd822rymr83uc',
163+
amount: 999999999,
164+
})).rejects.toThrow('Not enough coin UTXOs');
165+
});

0 commit comments

Comments
 (0)