Skip to content

Commit 0e4a0b0

Browse files
authored
Drop all usage of array-based passing (stellar#924)
1 parent 4c42a4d commit 0e4a0b0

9 files changed

+24
-47
lines changed

CHANGELOG.md

+3
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,9 @@ A breaking change will get clearly marked in this log.
66

77
## Unreleased
88

9+
### Fixed
10+
* `SorobanRpc`: remove all instances of array-based parsing to conform to future breaking changes in Soroban RPC ([#924](https://github.com/stellar/js-stellar-sdk/pull/924)).
11+
912

1013
## [v11.2.2](https://github.com/stellar/js-stellar-sdk/compare/v11.2.1...v11.2.2)
1114

src/soroban/jsonrpc.ts

+2-29
Original file line numberDiff line numberDiff line change
@@ -26,38 +26,11 @@ export interface Error<E = any> {
2626
data?: E;
2727
}
2828

29-
/**
30-
* Sends the jsonrpc 'params' as an array.
31-
*/
32-
export async function post<T>(
33-
url: string,
34-
method: string,
35-
...params: any
36-
): Promise<T> {
37-
if (params && params.length < 1) {
38-
params = null;
39-
}
40-
const response = await axios.post<Response<T>>(url, {
41-
jsonrpc: "2.0",
42-
// TODO: Generate a unique request id
43-
id: 1,
44-
method,
45-
params,
46-
});
47-
if (hasOwnProperty(response.data, "error")) {
48-
throw response.data.error;
49-
} else {
50-
return response.data?.result;
51-
}
52-
}
53-
54-
/**
55-
* Sends the jsonrpc 'params' as the single 'param' obj, no array wrapper is applied.
56-
*/
29+
/** Sends the jsonrpc 'params' as a single 'param' object (no array support). */
5730
export async function postObject<T>(
5831
url: string,
5932
method: string,
60-
param: any,
33+
param: any = null,
6134
): Promise<T> {
6235
const response = await axios.post<Response<T>>(url, {
6336
jsonrpc: "2.0",

src/soroban/server.ts

+10-9
Original file line numberDiff line numberDiff line change
@@ -141,7 +141,7 @@ export class Server {
141141
* });
142142
*/
143143
public async getHealth(): Promise<Api.GetHealthResponse> {
144-
return jsonrpc.post<Api.GetHealthResponse>(
144+
return jsonrpc.postObject<Api.GetHealthResponse>(
145145
this.serverURL.toString(),
146146
'getHealth'
147147
);
@@ -276,10 +276,11 @@ export class Server {
276276

277277
public async _getLedgerEntries(...keys: xdr.LedgerKey[]) {
278278
return jsonrpc
279-
.post<Api.RawGetLedgerEntriesResponse>(
279+
.postObject<Api.RawGetLedgerEntriesResponse>(
280280
this.serverURL.toString(),
281-
'getLedgerEntries',
282-
keys.map((k) => k.toXDR('base64'))
281+
'getLedgerEntries', {
282+
keys: keys.map((k) => k.toXDR('base64'))
283+
}
283284
);
284285
}
285286

@@ -350,7 +351,7 @@ export class Server {
350351
public async _getTransaction(
351352
hash: string
352353
): Promise<Api.RawGetTransactionResponse> {
353-
return jsonrpc.post(this.serverURL.toString(), 'getTransaction', hash);
354+
return jsonrpc.postObject(this.serverURL.toString(), 'getTransaction', {hash});
354355
}
355356

356357
/**
@@ -427,7 +428,7 @@ export class Server {
427428
* });
428429
*/
429430
public async getNetwork(): Promise<Api.GetNetworkResponse> {
430-
return await jsonrpc.post(this.serverURL.toString(), 'getNetwork');
431+
return await jsonrpc.postObject(this.serverURL.toString(), 'getNetwork');
431432
}
432433

433434
/**
@@ -446,7 +447,7 @@ export class Server {
446447
* });
447448
*/
448449
public async getLatestLedger(): Promise<Api.GetLatestLedgerResponse> {
449-
return jsonrpc.post(this.serverURL.toString(), 'getLatestLedger');
450+
return jsonrpc.postObject(this.serverURL.toString(), 'getLatestLedger');
450451
}
451452

452453
/**
@@ -647,10 +648,10 @@ export class Server {
647648
public async _sendTransaction(
648649
transaction: Transaction | FeeBumpTransaction
649650
): Promise<Api.RawSendTransactionResponse> {
650-
return jsonrpc.post(
651+
return jsonrpc.postObject(
651652
this.serverURL.toString(),
652653
'sendTransaction',
653-
transaction.toXDR()
654+
{ transaction: transaction.toXDR() }
654655
);
655656
}
656657

test/unit/server/soroban/get_account_test.js

+2-2
Original file line numberDiff line numberDiff line change
@@ -25,7 +25,7 @@ describe("Server#getAccount", function () {
2525
jsonrpc: "2.0",
2626
id: 1,
2727
method: "getLedgerEntries",
28-
params: [[key.toXDR("base64")]],
28+
params: { keys: [ key.toXDR("base64") ] },
2929
})
3030
.returns(
3131
Promise.resolve({
@@ -62,7 +62,7 @@ describe("Server#getAccount", function () {
6262
jsonrpc: "2.0",
6363
id: 1,
6464
method: "getLedgerEntries",
65-
params: [[key.toXDR("base64")]],
65+
params: { keys: [ key.toXDR("base64") ] },
6666
})
6767
.returns(
6868
Promise.resolve({

test/unit/server/soroban/get_contract_data_test.js

+2-2
Original file line numberDiff line numberDiff line change
@@ -55,7 +55,7 @@ describe("Server#getContractData", function () {
5555
jsonrpc: "2.0",
5656
id: 1,
5757
method: "getLedgerEntries",
58-
params: [[ledgerKey.toXDR("base64")]],
58+
params: { keys: [ledgerKey.toXDR("base64")] },
5959
})
6060
.returns(
6161
Promise.resolve({
@@ -99,7 +99,7 @@ describe("Server#getContractData", function () {
9999
jsonrpc: "2.0",
100100
id: 1,
101101
method: "getLedgerEntries",
102-
params: [[ledgerKeyDupe.toXDR("base64")]],
102+
params: { keys: [ledgerKeyDupe.toXDR("base64")] },
103103
})
104104
.returns(Promise.resolve({ data: { result: { entries: [] } } }));
105105

test/unit/server/soroban/get_ledger_entries_test.js

+1-1
Original file line numberDiff line numberDiff line change
@@ -44,7 +44,7 @@ describe("Server#getLedgerEntries", function () {
4444
jsonrpc: "2.0",
4545
id: 1,
4646
method: "getLedgerEntries",
47-
params: [requests],
47+
params: {keys: requests},
4848
})
4949
.returns(
5050
Promise.resolve({

test/unit/server/soroban/get_transaction_test.js

+1-1
Original file line numberDiff line numberDiff line change
@@ -42,7 +42,7 @@ describe("Server#getTransaction", function () {
4242
jsonrpc: "2.0",
4343
id: 1,
4444
method: "getTransaction",
45-
params: [this.hash],
45+
params: { hash: this.hash },
4646
})
4747
.returns(Promise.resolve({ data: { id: 1, result } }));
4848
};

test/unit/server/soroban/request_airdrop_test.js

+1-1
Original file line numberDiff line numberDiff line change
@@ -126,7 +126,7 @@ describe("Server#requestAirdrop", function () {
126126
jsonrpc: "2.0",
127127
id: 1,
128128
method: "getLedgerEntries",
129-
params: [[accountKey.toXDR("base64")]],
129+
params: { keys: [accountKey.toXDR("base64")] },
130130
})
131131
.returns(
132132
Promise.resolve({

test/unit/server/soroban/send_transaction_test.js

+2-2
Original file line numberDiff line numberDiff line change
@@ -42,7 +42,7 @@ describe("Server#sendTransaction", function () {
4242
jsonrpc: "2.0",
4343
id: 1,
4444
method: "sendTransaction",
45-
params: [this.blob],
45+
params: { transaction: this.blob },
4646
})
4747
.returns(
4848
Promise.resolve({
@@ -78,7 +78,7 @@ describe("Server#sendTransaction", function () {
7878
jsonrpc: "2.0",
7979
id: 1,
8080
method: "sendTransaction",
81-
params: [this.blob],
81+
params: { transaction: this.blob },
8282
})
8383
.returns(
8484
Promise.resolve({

0 commit comments

Comments
 (0)