From 0e4a0b02d619dd1ef8c5c51e61c07f730c871581 Mon Sep 17 00:00:00 2001 From: George Date: Thu, 15 Feb 2024 12:22:54 -0800 Subject: [PATCH] Drop all usage of array-based passing (#924) --- CHANGELOG.md | 3 ++ src/soroban/jsonrpc.ts | 31 ++----------------- src/soroban/server.ts | 19 ++++++------ test/unit/server/soroban/get_account_test.js | 4 +-- .../server/soroban/get_contract_data_test.js | 4 +-- .../server/soroban/get_ledger_entries_test.js | 2 +- .../server/soroban/get_transaction_test.js | 2 +- .../server/soroban/request_airdrop_test.js | 2 +- .../server/soroban/send_transaction_test.js | 4 +-- 9 files changed, 24 insertions(+), 47 deletions(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index 2d2b01bde..e1a0f4f42 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -6,6 +6,9 @@ A breaking change will get clearly marked in this log. ## Unreleased +### Fixed +* `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)). + ## [v11.2.2](https://github.com/stellar/js-stellar-sdk/compare/v11.2.1...v11.2.2) diff --git a/src/soroban/jsonrpc.ts b/src/soroban/jsonrpc.ts index 18dcfd65c..bd93ebbef 100644 --- a/src/soroban/jsonrpc.ts +++ b/src/soroban/jsonrpc.ts @@ -26,38 +26,11 @@ export interface Error { data?: E; } -/** - * Sends the jsonrpc 'params' as an array. - */ -export async function post( - url: string, - method: string, - ...params: any -): Promise { - if (params && params.length < 1) { - params = null; - } - const response = await axios.post>(url, { - jsonrpc: "2.0", - // TODO: Generate a unique request id - id: 1, - method, - params, - }); - if (hasOwnProperty(response.data, "error")) { - throw response.data.error; - } else { - return response.data?.result; - } -} - -/** - * Sends the jsonrpc 'params' as the single 'param' obj, no array wrapper is applied. - */ +/** Sends the jsonrpc 'params' as a single 'param' object (no array support). */ export async function postObject( url: string, method: string, - param: any, + param: any = null, ): Promise { const response = await axios.post>(url, { jsonrpc: "2.0", diff --git a/src/soroban/server.ts b/src/soroban/server.ts index 19a886627..c7e897b84 100644 --- a/src/soroban/server.ts +++ b/src/soroban/server.ts @@ -141,7 +141,7 @@ export class Server { * }); */ public async getHealth(): Promise { - return jsonrpc.post( + return jsonrpc.postObject( this.serverURL.toString(), 'getHealth' ); @@ -276,10 +276,11 @@ export class Server { public async _getLedgerEntries(...keys: xdr.LedgerKey[]) { return jsonrpc - .post( + .postObject( this.serverURL.toString(), - 'getLedgerEntries', - keys.map((k) => k.toXDR('base64')) + 'getLedgerEntries', { + keys: keys.map((k) => k.toXDR('base64')) + } ); } @@ -350,7 +351,7 @@ export class Server { public async _getTransaction( hash: string ): Promise { - return jsonrpc.post(this.serverURL.toString(), 'getTransaction', hash); + return jsonrpc.postObject(this.serverURL.toString(), 'getTransaction', {hash}); } /** @@ -427,7 +428,7 @@ export class Server { * }); */ public async getNetwork(): Promise { - return await jsonrpc.post(this.serverURL.toString(), 'getNetwork'); + return await jsonrpc.postObject(this.serverURL.toString(), 'getNetwork'); } /** @@ -446,7 +447,7 @@ export class Server { * }); */ public async getLatestLedger(): Promise { - return jsonrpc.post(this.serverURL.toString(), 'getLatestLedger'); + return jsonrpc.postObject(this.serverURL.toString(), 'getLatestLedger'); } /** @@ -647,10 +648,10 @@ export class Server { public async _sendTransaction( transaction: Transaction | FeeBumpTransaction ): Promise { - return jsonrpc.post( + return jsonrpc.postObject( this.serverURL.toString(), 'sendTransaction', - transaction.toXDR() + { transaction: transaction.toXDR() } ); } diff --git a/test/unit/server/soroban/get_account_test.js b/test/unit/server/soroban/get_account_test.js index 161a5d556..b6781f76c 100644 --- a/test/unit/server/soroban/get_account_test.js +++ b/test/unit/server/soroban/get_account_test.js @@ -25,7 +25,7 @@ describe("Server#getAccount", function () { jsonrpc: "2.0", id: 1, method: "getLedgerEntries", - params: [[key.toXDR("base64")]], + params: { keys: [ key.toXDR("base64") ] }, }) .returns( Promise.resolve({ @@ -62,7 +62,7 @@ describe("Server#getAccount", function () { jsonrpc: "2.0", id: 1, method: "getLedgerEntries", - params: [[key.toXDR("base64")]], + params: { keys: [ key.toXDR("base64") ] }, }) .returns( Promise.resolve({ diff --git a/test/unit/server/soroban/get_contract_data_test.js b/test/unit/server/soroban/get_contract_data_test.js index 4fa8d6036..1fb004a61 100644 --- a/test/unit/server/soroban/get_contract_data_test.js +++ b/test/unit/server/soroban/get_contract_data_test.js @@ -55,7 +55,7 @@ describe("Server#getContractData", function () { jsonrpc: "2.0", id: 1, method: "getLedgerEntries", - params: [[ledgerKey.toXDR("base64")]], + params: { keys: [ledgerKey.toXDR("base64")] }, }) .returns( Promise.resolve({ @@ -99,7 +99,7 @@ describe("Server#getContractData", function () { jsonrpc: "2.0", id: 1, method: "getLedgerEntries", - params: [[ledgerKeyDupe.toXDR("base64")]], + params: { keys: [ledgerKeyDupe.toXDR("base64")] }, }) .returns(Promise.resolve({ data: { result: { entries: [] } } })); diff --git a/test/unit/server/soroban/get_ledger_entries_test.js b/test/unit/server/soroban/get_ledger_entries_test.js index 2ed43d55f..cf11dc5f3 100644 --- a/test/unit/server/soroban/get_ledger_entries_test.js +++ b/test/unit/server/soroban/get_ledger_entries_test.js @@ -44,7 +44,7 @@ describe("Server#getLedgerEntries", function () { jsonrpc: "2.0", id: 1, method: "getLedgerEntries", - params: [requests], + params: {keys: requests}, }) .returns( Promise.resolve({ diff --git a/test/unit/server/soroban/get_transaction_test.js b/test/unit/server/soroban/get_transaction_test.js index 84beac445..71c2bae95 100644 --- a/test/unit/server/soroban/get_transaction_test.js +++ b/test/unit/server/soroban/get_transaction_test.js @@ -42,7 +42,7 @@ describe("Server#getTransaction", function () { jsonrpc: "2.0", id: 1, method: "getTransaction", - params: [this.hash], + params: { hash: this.hash }, }) .returns(Promise.resolve({ data: { id: 1, result } })); }; diff --git a/test/unit/server/soroban/request_airdrop_test.js b/test/unit/server/soroban/request_airdrop_test.js index f86dedb41..74e0c6486 100644 --- a/test/unit/server/soroban/request_airdrop_test.js +++ b/test/unit/server/soroban/request_airdrop_test.js @@ -126,7 +126,7 @@ describe("Server#requestAirdrop", function () { jsonrpc: "2.0", id: 1, method: "getLedgerEntries", - params: [[accountKey.toXDR("base64")]], + params: { keys: [accountKey.toXDR("base64")] }, }) .returns( Promise.resolve({ diff --git a/test/unit/server/soroban/send_transaction_test.js b/test/unit/server/soroban/send_transaction_test.js index 901fcc7c5..be235e8bb 100644 --- a/test/unit/server/soroban/send_transaction_test.js +++ b/test/unit/server/soroban/send_transaction_test.js @@ -42,7 +42,7 @@ describe("Server#sendTransaction", function () { jsonrpc: "2.0", id: 1, method: "sendTransaction", - params: [this.blob], + params: { transaction: this.blob }, }) .returns( Promise.resolve({ @@ -78,7 +78,7 @@ describe("Server#sendTransaction", function () { jsonrpc: "2.0", id: 1, method: "sendTransaction", - params: [this.blob], + params: { transaction: this.blob }, }) .returns( Promise.resolve({