From 73fd02a0cfdc36924fc9e7c0595eb93924ca7e42 Mon Sep 17 00:00:00 2001 From: Denis Angell Date: Thu, 23 Feb 2023 03:51:30 -0500 Subject: [PATCH] network id network id add network id to server_info option 2 networkID network id option 3 network ID optional --- packages/xrpl/src/client/index.ts | 14 +++++++++++++ .../xrpl/src/models/methods/serverInfo.ts | 4 ++++ .../xrpl/src/models/transactions/common.ts | 5 ++++- packages/xrpl/src/sugar/getNetworkID.ts | 21 +++++++++++++++++++ packages/xrpl/src/sugar/index.ts | 2 ++ 5 files changed, 45 insertions(+), 1 deletion(-) create mode 100644 packages/xrpl/src/sugar/getNetworkID.ts diff --git a/packages/xrpl/src/client/index.ts b/packages/xrpl/src/client/index.ts index 0f812000..7d417e5b 100644 --- a/packages/xrpl/src/client/index.ts +++ b/packages/xrpl/src/client/index.ts @@ -101,6 +101,7 @@ import { getXrpBalance, submit, submitAndWait, + getNetworkID, } from '../sugar' import fundWallet from '../Wallet/fundWallet' @@ -200,6 +201,13 @@ class Client extends EventEmitter { */ public readonly maxFeeXRP: string + /** + * Network ID of the server this sdk is connected to + * + * @category Fee + */ + public networkID: number + /** * Creates a new Client with a websocket connection to a rippled server. * @@ -218,6 +226,7 @@ class Client extends EventEmitter { this.feeCushion = options.feeCushion ?? DEFAULT_FEE_CUSHION this.maxFeeXRP = options.maxFeeXRP ?? DEFAULT_MAX_FEE_XRP + this.networkID = 1 this.connection = new Connection(server, options) @@ -638,6 +647,11 @@ class Client extends EventEmitter { * @category Faucet */ public fundWallet = fundWallet + + /** + * @category Abstraction + */ + public getNetworkID = getNetworkID } export { Client } diff --git a/packages/xrpl/src/models/methods/serverInfo.ts b/packages/xrpl/src/models/methods/serverInfo.ts index bfcfcbbd..f73e41c8 100644 --- a/packages/xrpl/src/models/methods/serverInfo.ts +++ b/packages/xrpl/src/models/methods/serverInfo.ts @@ -136,6 +136,10 @@ export interface ServerInfoResponse extends BaseResponse { * overall network's load factor. */ load_factor?: number + /** + * The network id of this server + */ + network_id?: number /** * Current multiplier to the transaction cost based on * load to this server. diff --git a/packages/xrpl/src/models/transactions/common.ts b/packages/xrpl/src/models/transactions/common.ts index c87374d8..8bceabe6 100644 --- a/packages/xrpl/src/models/transactions/common.ts +++ b/packages/xrpl/src/models/transactions/common.ts @@ -162,7 +162,7 @@ export interface BaseTransaction { /** * The network id of the transaction. */ - NetworkID: string + NetworkID?: number } /** @@ -256,6 +256,9 @@ export function validateBaseTransaction(common: Record): void { ) { throw new ValidationError('BaseTransaction: invalid TxnSignature') } + if (common.NetworkID !== undefined && typeof common.NetworkID !== 'number') { + throw new ValidationError('BaseTransaction: invalid NetworkID') + } } /** diff --git a/packages/xrpl/src/sugar/getNetworkID.ts b/packages/xrpl/src/sugar/getNetworkID.ts new file mode 100644 index 00000000..741392a0 --- /dev/null +++ b/packages/xrpl/src/sugar/getNetworkID.ts @@ -0,0 +1,21 @@ +import type { Client } from '..' +import { XrplError } from '../errors' + +/** + * Returns the network ID of the rippled server. + * + * @param this - The Client used to connect to the ledger. + * @returns The network id. + */ +export default async function getNetworkID(this: Client): Promise { + const response = await this.request({ + command: 'server_info', + }) + const networkID = response.result.info.network_id + if (networkID == null) { + throw new XrplError( + 'getNetworkID: Could not get network_id from server_info', + ) + } + return networkID +} diff --git a/packages/xrpl/src/sugar/index.ts b/packages/xrpl/src/sugar/index.ts index c132bf7f..15ec38e9 100644 --- a/packages/xrpl/src/sugar/index.ts +++ b/packages/xrpl/src/sugar/index.ts @@ -7,6 +7,8 @@ export { default as getLedgerIndex } from './getLedgerIndex' export { default as getOrderbook } from './getOrderbook' export { getFeeXrp, getFeeEstimateXrp } from './getFeeXrp' +export { default as getNetworkID } from './getNetworkID' + export * from './submit' export * from './utils'