evernode-js-client 0.4.53 → 0.5.1
Sign up to get free protection for your applications and to get access to all the features.
- package/README.md +4 -0
- package/index.js +147 -8
- package/package.json +7 -1
package/README.md
ADDED
package/index.js
CHANGED
@@ -11746,18 +11746,37 @@ class BaseEvernodeClient {
|
|
11746
11746
|
|
11747
11747
|
}
|
11748
11748
|
|
11749
|
+
/**
|
11750
|
+
* Listens to the subscribed events. This will listen for the event without detaching the handler until it's 'off'.
|
11751
|
+
* @param {string} event Event name.
|
11752
|
+
* @param {function(event)} handler Callback function to handle the event.
|
11753
|
+
*/
|
11749
11754
|
on(event, handler) {
|
11750
11755
|
this.events.on(event, handler);
|
11751
11756
|
}
|
11752
11757
|
|
11758
|
+
/**
|
11759
|
+
* Listens to the subscribed events. This will listen only once and detach the handler.
|
11760
|
+
* @param {string} event Event name.
|
11761
|
+
* @param {function(event)} handler Callback function to handle the event.
|
11762
|
+
*/
|
11753
11763
|
once(event, handler) {
|
11754
11764
|
this.events.once(event, handler);
|
11755
11765
|
}
|
11756
11766
|
|
11767
|
+
/**
|
11768
|
+
* Detach the listener event.
|
11769
|
+
* @param {string} event Event name.
|
11770
|
+
* @param {function(event)} handler (optional) Can be sent if a specific handler need to be detached. All the handlers will be detached if not specified.
|
11771
|
+
*/
|
11757
11772
|
off(event, handler = null) {
|
11758
11773
|
this.events.off(event, handler);
|
11759
11774
|
}
|
11760
11775
|
|
11776
|
+
/**
|
11777
|
+
* Connects the client to xrpl server and do the config loading and subscriptions. 'subscribe' is called inside this.
|
11778
|
+
* @returns boolean value, 'true' if success.
|
11779
|
+
*/
|
11761
11780
|
async connect() {
|
11762
11781
|
if (this.connected)
|
11763
11782
|
return true;
|
@@ -11777,6 +11796,9 @@ class BaseEvernodeClient {
|
|
11777
11796
|
return true;
|
11778
11797
|
}
|
11779
11798
|
|
11799
|
+
/**
|
11800
|
+
* Disconnects the client to xrpl server and do the un-subscriptions. 'unsubscribe' is called inside this.
|
11801
|
+
*/
|
11780
11802
|
async disconnect() {
|
11781
11803
|
await this.unsubscribe();
|
11782
11804
|
|
@@ -11784,14 +11806,24 @@ class BaseEvernodeClient {
|
|
11784
11806
|
await this.xrplApi.disconnect();
|
11785
11807
|
}
|
11786
11808
|
|
11809
|
+
/**
|
11810
|
+
* Subscribes to the registry client events.
|
11811
|
+
*/
|
11787
11812
|
async subscribe() {
|
11788
11813
|
await this.xrplAcc.subscribe();
|
11789
11814
|
}
|
11790
11815
|
|
11816
|
+
/**
|
11817
|
+
* Unsubscribes from the registry client events.
|
11818
|
+
*/
|
11791
11819
|
async unsubscribe() {
|
11792
11820
|
await this.xrplAcc.unsubscribe();
|
11793
11821
|
}
|
11794
11822
|
|
11823
|
+
/**
|
11824
|
+
* Get the EVR balance in the registry account.
|
11825
|
+
* @returns The available EVR amount as a 'string'.
|
11826
|
+
*/
|
11795
11827
|
async getEVRBalance() {
|
11796
11828
|
const lines = await this.xrplAcc.getTrustLines(EvernodeConstants.EVR, this.config.evrIssuerAddress);
|
11797
11829
|
if (lines.length > 0)
|
@@ -11800,6 +11832,10 @@ class BaseEvernodeClient {
|
|
11800
11832
|
return '0';
|
11801
11833
|
}
|
11802
11834
|
|
11835
|
+
/**
|
11836
|
+
* Get all XRPL hook states in the registry account.
|
11837
|
+
* @returns The list of hook states including Evernode configuration and hosts.
|
11838
|
+
*/
|
11803
11839
|
async getHookStates() {
|
11804
11840
|
const regAcc = new XrplAccount(this.registryAddress, null, { xrplApi: this.xrplApi });
|
11805
11841
|
const configs = await regAcc.getNamespaceEntries(EvernodeConstants.HOOK_NAMESPACE);
|
@@ -11809,6 +11845,11 @@ class BaseEvernodeClient {
|
|
11809
11845
|
return [];
|
11810
11846
|
}
|
11811
11847
|
|
11848
|
+
/**
|
11849
|
+
* Get the moment from the given XRP ledger index. (1 Moment - 1190 XRP ledgers).
|
11850
|
+
* @param {number} ledgerIndex [Optional] Ledger index to get the moment value.
|
11851
|
+
* @returns The moment of the given XPR ledger index as 'number'. Returns current moment if XRP ledger index is not given.
|
11852
|
+
*/
|
11812
11853
|
async getMoment(ledgerIndex = null) {
|
11813
11854
|
const lv = ledgerIndex || this.xrplApi.ledgerIndex;
|
11814
11855
|
const m = Math.floor((lv - this.config.momentBaseIdx) / this.config.momentSize);
|
@@ -11817,6 +11858,11 @@ class BaseEvernodeClient {
|
|
11817
11858
|
return m;
|
11818
11859
|
}
|
11819
11860
|
|
11861
|
+
/**
|
11862
|
+
* Get start XRP ledger index of the moment (of the given XRPL index).
|
11863
|
+
* @param {number} ledgerIndex [Optional] Ledger index to get the moment value.
|
11864
|
+
* @returns The XRP ledger index of the moment (of the given XRPL index) as a 'number'. Returns the current moment's start XRP ledger index if ledger index parameter is not given.
|
11865
|
+
*/
|
11820
11866
|
async getMomentStartIndex(ledgerIndex = null) {
|
11821
11867
|
const lv = ledgerIndex || this.xrplApi.ledgerIndex;
|
11822
11868
|
const m = Math.floor((lv - this.config.momentBaseIdx) / this.config.momentSize);
|
@@ -11825,6 +11871,10 @@ class BaseEvernodeClient {
|
|
11825
11871
|
return this.config.momentBaseIdx + (m * this.config.momentSize);
|
11826
11872
|
}
|
11827
11873
|
|
11874
|
+
/**
|
11875
|
+
* Get Evernode configuration
|
11876
|
+
* @returns An object with all the configuration and their values.
|
11877
|
+
*/
|
11828
11878
|
async #getEvernodeConfig() {
|
11829
11879
|
let states = await this.getHookStates();
|
11830
11880
|
const configStateKeys = {
|
@@ -11850,10 +11900,18 @@ class BaseEvernodeClient {
|
|
11850
11900
|
return config;
|
11851
11901
|
}
|
11852
11902
|
|
11903
|
+
/**
|
11904
|
+
* Loads the configs from XRPL hook and updates the in-memory config.
|
11905
|
+
*/
|
11853
11906
|
async refreshConfig() {
|
11854
11907
|
this.config = await this.#getEvernodeConfig();
|
11855
11908
|
}
|
11856
11909
|
|
11910
|
+
/**
|
11911
|
+
* Extracts transaction info and emits the Evernode event.
|
11912
|
+
* @param {object} tx XRPL transaction to be handled.
|
11913
|
+
* @param {any} error Error if there's any.
|
11914
|
+
*/
|
11857
11915
|
async #handleEvernodeEvent(tx, error) {
|
11858
11916
|
if (error)
|
11859
11917
|
console.error(error);
|
@@ -11866,6 +11924,11 @@ class BaseEvernodeClient {
|
|
11866
11924
|
}
|
11867
11925
|
}
|
11868
11926
|
|
11927
|
+
/**
|
11928
|
+
* Extracts the transaction info from a given transaction.
|
11929
|
+
* @param {object} tx Transaction to be deserialized and extracted.
|
11930
|
+
* @returns The event object in the format {name: '', data: {}}. Returns null if not handled. Note: You need to deserialize memos before passing the transaction to this function.
|
11931
|
+
*/
|
11869
11932
|
async extractEvernodeEvent(tx) {
|
11870
11933
|
if (tx.TransactionType === 'NFTokenAcceptOffer' && tx.NFTokenSellOffer && tx.Memos.length >= 1 &&
|
11871
11934
|
tx.Memos[0].type === MemoTypes.ACQUIRE_LEASE && tx.Memos[0].format === MemoFormats.BASE64 && tx.Memos[0].data) {
|
@@ -12082,7 +12145,11 @@ class BaseEvernodeClient {
|
|
12082
12145
|
return null;
|
12083
12146
|
}
|
12084
12147
|
|
12085
|
-
|
12148
|
+
/**
|
12149
|
+
* Get the registered host information.
|
12150
|
+
* @param {string} hostAddress [Optional] Address of the host.
|
12151
|
+
* @returns The registered host information object. Returns null is not registered.
|
12152
|
+
*/
|
12086
12153
|
async getHostInfo(hostAddress = this.xrplAcc.address) {
|
12087
12154
|
try {
|
12088
12155
|
const addrStateKey = StateHelpers.generateHostAddrStateKey(hostAddress);
|
@@ -12108,7 +12175,7 @@ class BaseEvernodeClient {
|
|
12108
12175
|
}
|
12109
12176
|
}
|
12110
12177
|
catch (e) {
|
12111
|
-
// If the
|
12178
|
+
// If the exception is entryNotFound from Rippled there's no entry for the host, So return null.
|
12112
12179
|
if (e?.data?.error !== 'entryNotFound')
|
12113
12180
|
throw e;
|
12114
12181
|
}
|
@@ -12116,7 +12183,13 @@ class BaseEvernodeClient {
|
|
12116
12183
|
return null;
|
12117
12184
|
}
|
12118
12185
|
|
12119
|
-
|
12186
|
+
/**
|
12187
|
+
* Get all the hosts registered in Evernode. The result's are paginated. Default page size is 20. Note: Specifying both filter and pagination does not supported.
|
12188
|
+
* @param {object} filters [Optional] Filter criteria to filter the hosts. The filter key can be a either property of the host.
|
12189
|
+
* @param {number} pageSize [Optional] Page size for the results.
|
12190
|
+
* @param {string} nextPageToken [Optional] Next page's token, If received by the previous result set.
|
12191
|
+
* @returns The list of active hosts. The response will be in '{data: [], nextPageToken: ''}' only if there are more pages. Otherwise the response will only contain the host list.
|
12192
|
+
*/
|
12120
12193
|
async getHosts(filters = null, pageSize = null, nextPageToken = null) {
|
12121
12194
|
const hosts = await this.#firestoreHandler.getHosts(filters, pageSize, nextPageToken);
|
12122
12195
|
const curMomentStartIdx = await this.getMomentStartIndex();
|
@@ -12129,7 +12202,10 @@ class BaseEvernodeClient {
|
|
12129
12202
|
return hosts;
|
12130
12203
|
}
|
12131
12204
|
|
12132
|
-
|
12205
|
+
/**
|
12206
|
+
* Get all Evernode configuration without paginating.
|
12207
|
+
* @returns The list of configuration.
|
12208
|
+
*/
|
12133
12209
|
async getAllConfigs() {
|
12134
12210
|
let fullConfigList = [];
|
12135
12211
|
const configs = await this.#firestoreHandler.getConfigs();
|
@@ -12149,7 +12225,10 @@ class BaseEvernodeClient {
|
|
12149
12225
|
return fullConfigList;
|
12150
12226
|
}
|
12151
12227
|
|
12152
|
-
|
12228
|
+
/**
|
12229
|
+
* Get all the hosts without paginating.
|
12230
|
+
* @returns The list of hosts.
|
12231
|
+
*/
|
12153
12232
|
async getAllHosts() {
|
12154
12233
|
let fullHostList = [];
|
12155
12234
|
const hosts = await this.#firestoreHandler.getHosts();
|
@@ -12169,7 +12248,10 @@ class BaseEvernodeClient {
|
|
12169
12248
|
return fullHostList;
|
12170
12249
|
}
|
12171
12250
|
|
12172
|
-
|
12251
|
+
/**
|
12252
|
+
* Remove a host which is inactive for a long period. The inactivity is checked by Evernode it self and only pruned if inactive thresholds are met.
|
12253
|
+
* @param {string} hostAddress XRPL address of the host to be pruned.
|
12254
|
+
*/
|
12173
12255
|
async pruneDeadHost(hostAddress) {
|
12174
12256
|
if (this.xrplAcc.address === this.registryAddress)
|
12175
12257
|
throw 'Invalid function call';
|
@@ -12190,6 +12272,7 @@ module.exports = {
|
|
12190
12272
|
BaseEvernodeClient
|
12191
12273
|
}
|
12192
12274
|
|
12275
|
+
|
12193
12276
|
/***/ }),
|
12194
12277
|
|
12195
12278
|
/***/ 1437:
|
@@ -12574,10 +12657,18 @@ const RegistryEvents = {
|
|
12574
12657
|
|
12575
12658
|
class RegistryClient extends BaseEvernodeClient {
|
12576
12659
|
|
12660
|
+
/**
|
12661
|
+
* Constructs a registry client instance.
|
12662
|
+
* @param {object} options [Optional] An object with 'rippledServer' URL and 'registryAddress'.
|
12663
|
+
*/
|
12577
12664
|
constructor(options = {}) {
|
12578
12665
|
super((options.registryAddress || DefaultValues.registryAddress), null, Object.values(RegistryEvents), false, options);
|
12579
12666
|
}
|
12580
12667
|
|
12668
|
+
/**
|
12669
|
+
* Gets all the active hosts registered in Evernode without paginating.
|
12670
|
+
* @returns The list of active hosts.
|
12671
|
+
*/
|
12581
12672
|
async getActiveHosts() {
|
12582
12673
|
let fullHostList = [];
|
12583
12674
|
const hosts = await this.getHosts();
|
@@ -12629,6 +12720,12 @@ const TenantEvents = {
|
|
12629
12720
|
|
12630
12721
|
class TenantClient extends BaseEvernodeClient {
|
12631
12722
|
|
12723
|
+
/**
|
12724
|
+
* Constructs a tenant client instance.
|
12725
|
+
* @param {string} xrpAddress XRPL address of the tenant.
|
12726
|
+
* @param {string} XRPL secret of the tenant.
|
12727
|
+
* @param {object} options [Optional] An object with 'rippledServer' URL and 'registryAddress'.
|
12728
|
+
*/
|
12632
12729
|
constructor(xrpAddress, xrpSecret, options = {}) {
|
12633
12730
|
super(xrpAddress, xrpSecret, Object.values(TenantEvents), false, options);
|
12634
12731
|
}
|
@@ -12666,6 +12763,13 @@ class TenantClient extends BaseEvernodeClient {
|
|
12666
12763
|
return host;
|
12667
12764
|
}
|
12668
12765
|
|
12766
|
+
/**
|
12767
|
+
*
|
12768
|
+
* @param {string} hostAddress XRPL address of the host to acquire the lease.
|
12769
|
+
* @param {object} requirement The instance requirements and configuration.
|
12770
|
+
* @param {object} options [Optional] Options for the XRPL transaction.
|
12771
|
+
* @returns The transaction result.
|
12772
|
+
*/
|
12669
12773
|
async acquireLeaseSubmit(hostAddress, requirement, options = {}) {
|
12670
12774
|
|
12671
12775
|
const hostAcc = await this.getLeaseHost(hostAddress);
|
@@ -12693,6 +12797,12 @@ class TenantClient extends BaseEvernodeClient {
|
|
12693
12797
|
return this.xrplAcc.buyNft(selectedOfferIndex, [{ type: MemoTypes.ACQUIRE_LEASE, format: MemoFormats.BASE64, data: ecrypted }], options.transactionOptions);
|
12694
12798
|
}
|
12695
12799
|
|
12800
|
+
/**
|
12801
|
+
* Watch for the acquire-success response after the acquire request is made.
|
12802
|
+
* @param {object} tx The transaction returned by the acquireLeaseSubmit function.
|
12803
|
+
* @param {object} options [Optional] Options for the XRPL transaction.
|
12804
|
+
* @returns An object including transaction details,instance info, and acquireReference Id.
|
12805
|
+
*/
|
12696
12806
|
async watchAcquireResponse(tx, options = {}) {
|
12697
12807
|
console.log(`Waiting for acquire response... (txHash: ${tx.id})`);
|
12698
12808
|
|
@@ -12731,6 +12841,13 @@ class TenantClient extends BaseEvernodeClient {
|
|
12731
12841
|
}
|
12732
12842
|
}
|
12733
12843
|
|
12844
|
+
/**
|
12845
|
+
* Acquire an instance from a host
|
12846
|
+
* @param {string} hostAddress XRPL address of the host to acquire the lease.
|
12847
|
+
* @param {object} requirement The instance requirements and configuration.
|
12848
|
+
* @param {object} options [Optional] Options for the XRPL transaction.
|
12849
|
+
* @returns An object including transaction details,instance info, and acquireReference Id.
|
12850
|
+
*/
|
12734
12851
|
acquireLease(hostAddress, requirement, options = {}) {
|
12735
12852
|
return new Promise(async (resolve, reject) => {
|
12736
12853
|
const tx = await this.acquireLeaseSubmit(hostAddress, requirement, options).catch(error => {
|
@@ -12747,13 +12864,27 @@ class TenantClient extends BaseEvernodeClient {
|
|
12747
12864
|
});
|
12748
12865
|
}
|
12749
12866
|
|
12867
|
+
/**
|
12868
|
+
* This function is called by a tenant client to submit the extend lease transaction in certain host. This function will be called inside extendLease function. This function can take four parameters as follows.
|
12869
|
+
* @param {string} hostAddress XRPL account address of the host.
|
12870
|
+
* @param {number} amount Cost for the extended moments , in EVRs.
|
12871
|
+
* @param {string} tokenID Tenant received instance name. this name can be retrieve by performing acquire Lease.
|
12872
|
+
* @param {object} options This is an optional field and contains necessary details for the transactions.
|
12873
|
+
* @returns The transaction result.
|
12874
|
+
*/
|
12750
12875
|
async extendLeaseSubmit(hostAddress, amount, tokenID, options = {}) {
|
12751
12876
|
const host = await this.getLeaseHost(hostAddress);
|
12752
12877
|
return this.xrplAcc.makePayment(host.address, amount.toString(), EvernodeConstants.EVR, this.config.evrIssuerAddress,
|
12753
12878
|
[{ type: MemoTypes.EXTEND_LEASE, format: MemoFormats.HEX, data: tokenID }], options.transactionOptions);
|
12754
12879
|
}
|
12755
12880
|
|
12756
|
-
|
12881
|
+
/**
|
12882
|
+
* This function watches for an extendlease-success response(transaction) and returns the response or throws the error response on extendlease-error response from the host XRPL account. This function is called within the extendLease function.
|
12883
|
+
* @param {object} tx Response of extendLeaseSubmit.
|
12884
|
+
* @param {object} options This is an optional field and contains necessary details for the transactions.
|
12885
|
+
* @returns An object including transaction details.
|
12886
|
+
*/
|
12887
|
+
async watchExtendResponse(tx, options = {}) {
|
12757
12888
|
console.log(`Waiting for extend lease response... (txHash: ${tx.id})`);
|
12758
12889
|
|
12759
12890
|
const failTimeout = setTimeout(() => {
|
@@ -12762,7 +12893,7 @@ class TenantClient extends BaseEvernodeClient {
|
|
12762
12893
|
|
12763
12894
|
let relevantTx = null;
|
12764
12895
|
while (!relevantTx) {
|
12765
|
-
const txList = await this.xrplAcc.getAccountTrx(
|
12896
|
+
const txList = await this.xrplAcc.getAccountTrx(tx.details.ledger_index);
|
12766
12897
|
for (let t of txList) {
|
12767
12898
|
t.tx.Memos = TransactionHelper.deserializeMemos(t.tx.Memos);
|
12768
12899
|
const res = await this.extractEvernodeEvent(t.tx);
|
@@ -12790,6 +12921,14 @@ class TenantClient extends BaseEvernodeClient {
|
|
12790
12921
|
}
|
12791
12922
|
}
|
12792
12923
|
|
12924
|
+
/**
|
12925
|
+
* This function is called by a tenant client to extend an available instance in certain host. This function can take four parameters as follows.
|
12926
|
+
* @param {string} hostAddress XRPL account address of the host.
|
12927
|
+
* @param {number} moments 1190 ledgers (est. 1 hour).
|
12928
|
+
* @param {string} instanceName Tenant received instance name. this name can be retrieve by performing acquire Lease.
|
12929
|
+
* @param {object} options This is an optional field and contains necessary details for the transactions.
|
12930
|
+
* @returns An object including transaction details.
|
12931
|
+
*/
|
12793
12932
|
extendLease(hostAddress, moments, instanceName, options = {}) {
|
12794
12933
|
return new Promise(async (resolve, reject) => {
|
12795
12934
|
const tokenID = instanceName;
|
package/package.json
CHANGED
@@ -1,6 +1,12 @@
|
|
1
1
|
{
|
2
2
|
"name": "evernode-js-client",
|
3
|
-
"
|
3
|
+
"description": "Javascript client library for Evernode.",
|
4
|
+
"keywords": [
|
5
|
+
"Evernode"
|
6
|
+
],
|
7
|
+
"homepage": "https://github.com/HotPocketDev/evernode-js-client",
|
8
|
+
"license": "MIT",
|
9
|
+
"version": "0.5.1",
|
4
10
|
"dependencies": {
|
5
11
|
"elliptic": "6.5.4",
|
6
12
|
"ripple-address-codec": "4.2.0",
|