Skip to content

Commit

Permalink
Added Horizon.Server.root to obtain information from the Horizon ro…
Browse files Browse the repository at this point in the history
…ot endpoint. (#1122)
  • Loading branch information
overcat authored Dec 16, 2024
1 parent 3304995 commit 3988271
Show file tree
Hide file tree
Showing 5 changed files with 154 additions and 0 deletions.
2 changes: 2 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,8 @@ A breaking change will get clearly marked in this log.
### Fixed
* When using a friendbot that points to a Horizon instance that has ledger metadata disabled, you can no longer extract the account sequence from the response. Instead, we hit RPC directly ([#1107](https://github.com/stellar/js-stellar-sdk/pull/1107/)).

### Added
* Added `Horizon.Server.root` to obtain information from the Horizon root endpoint. ([#1122](https://github.com/stellar/js-stellar-sdk/pull/1122/))

## [v13.0.0](https://github.com/stellar/js-stellar-sdk/compare/v12.3.0...v13.0.0)
This is a direct re-tag of rc.2 with the only change being an upgrade to the `stellar-base` library to incorporate a patch release. Nonetheless, the entire changelog from the prior major version here is replicated for a comprehensive view on what's broken, added, and fixed.
Expand Down
1 change: 1 addition & 0 deletions src/horizon/call_builder.ts
Original file line number Diff line number Diff line change
Expand Up @@ -47,6 +47,7 @@ export class CallBuilder<
T extends
| HorizonApi.FeeStatsResponse
| HorizonApi.BaseResponse
| HorizonApi.RootResponse
| ServerApi.CollectionPage<HorizonApi.BaseResponse>
> {
protected url: URI;
Expand Down
14 changes: 14 additions & 0 deletions src/horizon/horizon_api.ts
Original file line number Diff line number Diff line change
Expand Up @@ -707,4 +707,18 @@ export namespace HorizonApi {
};
result_xdr: string;
}

export interface RootResponse {
horizon_version: string;
core_version: string;
ingest_latest_ledger: number;
history_latest_ledger: number;
history_latest_ledger_closed_at: string;
history_elder_ledger: number;
core_latest_ledger: number;
network_passphrase: string;
current_protocol_version: number;
supported_protocol_version: number;
core_supported_protocol_version: number;
}
}
11 changes: 11 additions & 0 deletions src/horizon/server.ts
Original file line number Diff line number Diff line change
Expand Up @@ -199,6 +199,17 @@ export class HorizonServer {
return cb.call();
}

/**
* Fetch the Horizon server's root endpoint.
* @returns {Promise<HorizonApi.RootResponse>} Promise that resolves to the root endpoint returned by Horizon.
*/
public async root(): Promise<HorizonApi.RootResponse> {
const cb = new CallBuilder<HorizonApi.RootResponse>(
URI(this.serverURL as any),
);
return cb.call();
}

/**
* Submits a transaction to the network.
*
Expand Down
126 changes: 126 additions & 0 deletions test/unit/server/horizon/server_test.js
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,132 @@ describe("server.js non-transaction tests", function () {
});
});

describe("Server.root", function () {
let response = {
_links: {
account: {
href: "https://horizon.stellar.org/accounts/{account_id}",
templated: true,
},
accounts: {
href: "https://horizon.stellar.org/accounts{?signer,sponsor,asset,liquidity_pool,cursor,limit,order}",
templated: true,
},
account_transactions: {
href: "https://horizon.stellar.org/accounts/{account_id}/transactions{?cursor,limit,order}",
templated: true,
},
claimable_balances: {
href: "https://horizon.stellar.org/claimable_balances{?asset,sponsor,claimant,cursor,limit,order}",
templated: true,
},
assets: {
href: "https://horizon.stellar.org/assets{?asset_code,asset_issuer,cursor,limit,order}",
templated: true,
},
effects: {
href: "https://horizon.stellar.org/effects{?cursor,limit,order}",
templated: true,
},
fee_stats: {
href: "https://horizon.stellar.org/fee_stats",
},
ledger: {
href: "https://horizon.stellar.org/ledgers/{sequence}",
templated: true,
},
ledgers: {
href: "https://horizon.stellar.org/ledgers{?cursor,limit,order}",
templated: true,
},
liquidity_pools: {
href: "https://horizon.stellar.org/liquidity_pools{?reserves,account,cursor,limit,order}",
templated: true,
},
offer: {
href: "https://horizon.stellar.org/offers/{offer_id}",
templated: true,
},
offers: {
href: "https://horizon.stellar.org/offers{?selling,buying,seller,sponsor,cursor,limit,order}",
templated: true,
},
operation: {
href: "https://horizon.stellar.org/operations/{id}",
templated: true,
},
operations: {
href: "https://horizon.stellar.org/operations{?cursor,limit,order,include_failed}",
templated: true,
},
order_book: {
href: "https://horizon.stellar.org/order_book{?selling_asset_type,selling_asset_code,selling_asset_issuer,buying_asset_type,buying_asset_code,buying_asset_issuer,limit}",
templated: true,
},
payments: {
href: "https://horizon.stellar.org/payments{?cursor,limit,order,include_failed}",
templated: true,
},
self: {
href: "https://horizon.stellar.org/",
},
strict_receive_paths: {
href: "https://horizon.stellar.org/paths/strict-receive{?source_assets,source_account,destination_account,destination_asset_type,destination_asset_issuer,destination_asset_code,destination_amount}",
templated: true,
},
strict_send_paths: {
href: "https://horizon.stellar.org/paths/strict-send{?destination_account,destination_assets,source_asset_type,source_asset_issuer,source_asset_code,source_amount}",
templated: true,
},
trade_aggregations: {
href: "https://horizon.stellar.org/trade_aggregations?base_asset_type={base_asset_type}\u0026base_asset_code={base_asset_code}\u0026base_asset_issuer={base_asset_issuer}\u0026counter_asset_type={counter_asset_type}\u0026counter_asset_code={counter_asset_code}\u0026counter_asset_issuer={counter_asset_issuer}",
templated: true,
},
trades: {
href: "https://horizon.stellar.org/trades?base_asset_type={base_asset_type}\u0026base_asset_code={base_asset_code}\u0026base_asset_issuer={base_asset_issuer}\u0026counter_asset_type={counter_asset_type}\u0026counter_asset_code={counter_asset_code}\u0026counter_asset_issuer={counter_asset_issuer}",
templated: true,
},
transaction: {
href: "https://horizon.stellar.org/transactions/{hash}",
templated: true,
},
transactions: {
href: "https://horizon.stellar.org/transactions{?cursor,limit,order}",
templated: true,
},
},
horizon_version: "22.0.1-dd8a9b473a303cfcdd383d1db45dace93ea0861c",
core_version:
"stellar-core 22.1.0.rc1 (fdd833d57c86cfe0c5057da5b2319953ab841de0)",
ingest_latest_ledger: 54837706,
history_latest_ledger: 54837706,
history_latest_ledger_closed_at: "2024-12-15T11:39:19Z",
history_elder_ledger: 48530161,
core_latest_ledger: 54837706,
network_passphrase: "Public Global Stellar Network ; September 2015",
current_protocol_version: 22,
supported_protocol_version: 22,
core_supported_protocol_version: 22,
};

it("returns the root endpoint", function (done) {
this.axiosMock
.expects("get")
.withArgs(sinon.match("https://horizon-live.stellar.org:1337/"))
.returns(Promise.resolve({ data: response }));

this.server
.root()
.then((root) => {
expect(root).to.be.equal(response);
done();
})
.catch(function (err) {
done(err);
});
});
});

describe("Server.fetchTimebounds", function () {
let clock;

Expand Down

0 comments on commit 3988271

Please sign in to comment.