Skip to content

Commit f973216

Browse files
committed
fix: remove request to account endpoint
1 parent cdfe657 commit f973216

12 files changed

Lines changed: 47 additions & 32 deletions

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

Lines changed: 24 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -161,14 +161,14 @@ export interface ApiProvider {
161161

162162
export class MintlayerApiProvider implements ApiProvider {
163163
private readonly baseUrl: string;
164-
private readonly utxoUrl: string;
164+
private readonly batchUrl: string;
165165

166166
constructor(
167167
baseUrl: string,
168-
utxoUrl: string = 'https://api.mintini.app',
168+
batchUrl: string,
169169
) {
170170
this.baseUrl = baseUrl.replace(/\/$/, '');
171-
this.utxoUrl = utxoUrl.replace(/\/$/, '');
171+
this.batchUrl = batchUrl.replace(/\/$/, '');
172172
}
173173

174174
private async get(path: string): Promise<any> {
@@ -239,15 +239,20 @@ export class MintlayerApiProvider implements ApiProvider {
239239
}
240240

241241
async getAccountUtxos(addresses: string[], network: number): Promise<any> {
242-
const response = await fetch(`${this.utxoUrl}/account`, {
242+
const response = await fetch(this.batchUrl, {
243243
method: 'POST',
244244
headers: { 'Content-Type': 'application/json' },
245-
body: JSON.stringify({ addresses, network }),
245+
body: JSON.stringify({
246+
ids: addresses,
247+
type: '/address/:address/spendable-utxos',
248+
network,
249+
}),
246250
});
247251
if (!response.ok) {
248252
throw new Error(`Failed to fetch utxos: ${response.status}`);
249253
}
250-
return response.json();
254+
const data = await response.json();
255+
return (data.results ?? []).flat();
251256
}
252257
}
253258

@@ -1222,7 +1227,7 @@ class Client {
12221227
this.publicKeys = { receiving: [], change: [] };
12231228
this.isInitialized = false;
12241229
this.accountProvider = options.accountProvider || new MojitoAccountProvider();
1225-
this.apiProvider = options.apiProvider || new MintlayerApiProvider(this.getDefaultApiServer());
1230+
this.apiProvider = options.apiProvider || new MintlayerApiProvider(this.getDefaultApiServer(), this.getDefaultBatchServer());
12261231
}
12271232

12281233
/**
@@ -1332,6 +1337,16 @@ class Client {
13321337
: 'https://api-server.mintlayer.org/api/v2';
13331338
}
13341339

1340+
/**
1341+
* Returns the default batch server URL based on the network.
1342+
* @private
1343+
*/
1344+
private getDefaultBatchServer(): string {
1345+
return this.network === 'testnet'
1346+
? 'https://mojito-api.mintlayer.org/mintlayer/testnet/batch'
1347+
: 'https://mojito-api.mintlayer.org/mintlayer/mainnet/batch';
1348+
}
1349+
13351350
/**
13361351
* Initializes the SDK.
13371352
* @private
@@ -2419,7 +2434,7 @@ class Client {
24192434
const currentAddress = address;
24202435
const addressList = [...currentAddress.receiving, ...currentAddress.change];
24212436

2422-
const data = await this.apiProvider.getAccountUtxos(
2437+
const data_utxos = await this.apiProvider.getAccountUtxos(
24232438
addressList,
24242439
this.network === 'mainnet' ? 0 : 1,
24252440
);
@@ -2433,7 +2448,7 @@ class Client {
24332448
utxo: item.utxo,
24342449
})) : [];
24352450

2436-
const utxos: UtxoEntry[] = data.utxos.filter((item: UtxoEntry) => {
2451+
const utxos: UtxoEntry[] = data_utxos.filter((item: UtxoEntry) => {
24372452
// filter out UTXO with type htlc, they have to be added manually
24382453
if (item.utxo.type === 'Htlc') {
24392454
return false;

packages/sdk/tests/htlc.test.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -279,10 +279,10 @@ beforeEach(() => {
279279
return JSON.stringify({ a: 'b' });
280280
}
281281

282-
if(url.endsWith('/account')) {
282+
if(url.endsWith('/batch')) {
283283
return {
284284
body: JSON.stringify({
285-
utxos: utxos,
285+
results: [utxos],
286286
}),
287287
};
288288
}

packages/sdk/tests/issue-token.test.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -85,10 +85,10 @@ beforeEach(() => {
8585
return JSON.stringify({ a: 'b' });
8686
}
8787

88-
if(url.endsWith('/account')) {
88+
if(url.endsWith('/batch')) {
8989
return {
9090
body: JSON.stringify({
91-
utxos: utxos,
91+
results: [utxos],
9292
}),
9393
};
9494
}

packages/sdk/tests/orders.test.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -183,10 +183,10 @@ beforeEach(() => {
183183
}
184184
}
185185

186-
if(url.endsWith('/account')) {
186+
if(url.endsWith('/batch')) {
187187
return {
188188
body: JSON.stringify({
189-
utxos: utxos,
189+
results: [utxos],
190190
}),
191191
};
192192
}

packages/sdk/tests/signer.test.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -101,10 +101,10 @@ beforeEach(() => {
101101
return JSON.stringify({ a: 'b' });
102102
}
103103

104-
if(url.endsWith('/account')) {
104+
if(url.endsWith('/batch')) {
105105
return {
106106
body: JSON.stringify({
107-
utxos: utxos,
107+
results: [utxos],
108108
}),
109109
};
110110
}

packages/sdk/tests/staking.test.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -142,10 +142,10 @@ beforeEach(() => {
142142
}
143143
}
144144

145-
if(url.endsWith('/account')) {
145+
if(url.endsWith('/batch')) {
146146
return {
147147
body: JSON.stringify({
148-
utxos: utxos,
148+
results: [utxos],
149149
}),
150150
};
151151
}

packages/sdk/tests/standalone-providers.test.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -35,7 +35,7 @@ function setupFetchMock() {
3535
fetchMock.doMock();
3636
fetchMock.mockResponse(async (req) => {
3737
if (req.url.endsWith('/chain/tip')) return JSON.stringify({ height: 200000 });
38-
if (req.url.endsWith('/account')) return JSON.stringify({ utxos: UTXOS });
38+
if (req.url.endsWith('/batch')) return JSON.stringify({ results: [UTXOS] });
3939
return JSON.stringify({});
4040
});
4141
}

packages/sdk/tests/token-commands.test.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -85,10 +85,10 @@ beforeEach(() => {
8585
return JSON.stringify({ a: 'b' });
8686
}
8787

88-
if(url.endsWith('/account')) {
88+
if(url.endsWith('/batch')) {
8989
return {
9090
body: JSON.stringify({
91-
utxos: utxos,
91+
results: [utxos],
9292
}),
9393
};
9494
}

packages/sdk/tests/transfer-nft.test.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -158,10 +158,10 @@ beforeEach(() => {
158158
return JSON.stringify({ a: 'b' });
159159
}
160160

161-
if(url.endsWith('/account')) {
161+
if(url.endsWith('/batch')) {
162162
return {
163163
body: JSON.stringify({
164-
utxos: utxos,
164+
results: [utxos],
165165
}),
166166
};
167167
}

packages/sdk/tests/transfer-token.test.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -85,10 +85,10 @@ beforeEach(() => {
8585
return JSON.stringify({ a: 'b' });
8686
}
8787

88-
if(url.endsWith('/account')) {
88+
if(url.endsWith('/batch')) {
8989
return {
9090
body: JSON.stringify({
91-
utxos: utxos,
91+
results: [utxos],
9292
}),
9393
};
9494
}

0 commit comments

Comments
 (0)