@tari-project/indexer-provider 0.5.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
package/LICENSE ADDED
@@ -0,0 +1,29 @@
1
+ BSD 3-Clause License
2
+
3
+ Copyright (c) 2023, The Tari Developer Community
4
+ All rights reserved.
5
+
6
+ Redistribution and use in source and binary forms, with or without
7
+ modification, are permitted provided that the following conditions are met:
8
+
9
+ 1. Redistributions of source code must retain the above copyright notice, this
10
+ list of conditions and the following disclaimer.
11
+
12
+ 2. Redistributions in binary form must reproduce the above copyright notice,
13
+ this list of conditions and the following disclaimer in the documentation
14
+ and/or other materials provided with the distribution.
15
+
16
+ 3. Neither the name of the copyright holder nor the names of its
17
+ contributors may be used to endorse or promote products derived from
18
+ this software without specific prior written permission.
19
+
20
+ THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
21
+ AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
22
+ IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
23
+ DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE
24
+ FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
25
+ DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
26
+ SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
27
+ CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
28
+ OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
29
+ OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
@@ -0,0 +1,2 @@
1
+ export { IndexerProvider, IndexerProviderParameters } from "./provider";
2
+ export * from "./transports";
package/dist/index.js ADDED
@@ -0,0 +1,2 @@
1
+ export { IndexerProvider } from "./provider";
2
+ export * from "./transports";
@@ -0,0 +1,23 @@
1
+ import { GetSubstateRequest, ListSubstatesRequest, ListSubstatesResponse, Substate, TariProvider, TransactionResult } from "@tari-project/tari-provider";
2
+ import { TariPermissions } from "@tari-project/tari-permissions";
3
+ import { IndexerProviderClient } from "./transports";
4
+ import { GetTemplateDefinitionResponse, ListTemplatesResponse } from "@tari-project/typescript-bindings";
5
+ export interface IndexerProviderParameters {
6
+ indexerJrpcUrl: string;
7
+ permissions: TariPermissions;
8
+ optionalPermissions?: TariPermissions;
9
+ onConnection?: () => void;
10
+ }
11
+ export declare class IndexerProvider implements TariProvider {
12
+ providerName: string;
13
+ client: IndexerProviderClient;
14
+ params: IndexerProviderParameters;
15
+ private constructor();
16
+ static build(params: IndexerProviderParameters): Promise<IndexerProvider>;
17
+ isConnected(): boolean;
18
+ listSubstates({ filter_by_template, filter_by_type, limit, offset, }: ListSubstatesRequest): Promise<ListSubstatesResponse>;
19
+ getSubstate({ substate_address, version }: GetSubstateRequest): Promise<Substate>;
20
+ listTemplates(limit?: number): Promise<ListTemplatesResponse>;
21
+ getTransactionResult(transactionId: string): Promise<TransactionResult>;
22
+ getTemplateDefinition(template_address: string): Promise<GetTemplateDefinitionResponse>;
23
+ }
@@ -0,0 +1,66 @@
1
+ import { IndexerProviderClient } from "./transports";
2
+ import { stringToSubstateId, substateIdToString, } from "@tari-project/typescript-bindings";
3
+ import { convertStringToTransactionStatus } from "@tari-project/tarijs-types";
4
+ export class IndexerProvider {
5
+ providerName = "IndexerProvider";
6
+ client;
7
+ params;
8
+ constructor(params, connection) {
9
+ this.params = params;
10
+ this.client = connection;
11
+ }
12
+ static async build(params) {
13
+ const client = IndexerProviderClient.usingFetchTransport(params.indexerJrpcUrl);
14
+ await client.getIdentity();
15
+ params.onConnection?.();
16
+ return new IndexerProvider(params, client);
17
+ }
18
+ isConnected() {
19
+ return this.client.isConnected();
20
+ }
21
+ async listSubstates({ filter_by_template, filter_by_type, limit, offset, }) {
22
+ const resp = await this.client.listSubstates({
23
+ filter_by_template,
24
+ filter_by_type,
25
+ limit,
26
+ offset,
27
+ });
28
+ const substates = resp.substates.map((s) => ({
29
+ substate_id: typeof s.substate_id === "string" ? s.substate_id : substateIdToString(s.substate_id),
30
+ module_name: s.module_name,
31
+ version: s.version,
32
+ template_address: s.template_address,
33
+ }));
34
+ return { substates };
35
+ }
36
+ async getSubstate({ substate_address, version }) {
37
+ const resp = await this.client.getSubstate({
38
+ address: stringToSubstateId(substate_address),
39
+ version: version ?? null,
40
+ local_search_only: false,
41
+ });
42
+ return {
43
+ address: {
44
+ substate_id: substateIdToString(resp.address),
45
+ version: resp.version,
46
+ },
47
+ value: resp.substate,
48
+ };
49
+ }
50
+ async listTemplates(limit = 0) {
51
+ const resp = await this.client.listTemplates({ limit });
52
+ return resp;
53
+ }
54
+ async getTransactionResult(transactionId) {
55
+ const resp = await this.client.getTransactionResult({ transaction_id: transactionId });
56
+ return {
57
+ transaction_id: transactionId,
58
+ status: convertStringToTransactionStatus(resp.status),
59
+ result: resp.result,
60
+ };
61
+ }
62
+ async getTemplateDefinition(template_address) {
63
+ let resp = await this.client.getTemplateDefinition({ template_address });
64
+ return resp;
65
+ }
66
+ }
@@ -0,0 +1,30 @@
1
+ import { GetEpochManagerStatsResponse, GetNonFungibleCollectionsResponse, GetNonFungibleCountRequest, GetNonFungibleCountResponse, GetNonFungiblesRequest, GetNonFungiblesResponse, GetRelatedTransactionsRequest, GetRelatedTransactionsResponse, GetTemplateDefinitionRequest, GetTemplateDefinitionResponse, IndexerGetIdentityResponse, IndexerGetSubstateRequest, IndexerGetSubstateResponse, IndexerSubmitTransactionRequest, IndexerSubmitTransactionResponse, InspectSubstateRequest, InspectSubstateResponse, ListTemplatesRequest, ListTemplatesResponse, SubstatesListRequest, SubstatesListResponse, TransactionGetResultRequest, TransactionGetResultResponse, TransactionWaitResultRequest, TransactionWaitResultResponse } from "@tari-project/typescript-bindings";
2
+ import { RpcTransport } from "./rpc";
3
+ export declare class IndexerProviderClient {
4
+ private token;
5
+ private transport;
6
+ private id;
7
+ private connected;
8
+ constructor(transport: RpcTransport);
9
+ static new(transport: RpcTransport): IndexerProviderClient;
10
+ static usingFetchTransport(url: string): IndexerProviderClient;
11
+ getTransport(): RpcTransport;
12
+ isAuthenticated(): boolean;
13
+ isConnected(): boolean;
14
+ setToken(token: string): void;
15
+ submitTransaction(params: IndexerSubmitTransactionRequest): Promise<IndexerSubmitTransactionResponse>;
16
+ inspectSubstate(params: InspectSubstateRequest): Promise<InspectSubstateResponse>;
17
+ getSubstate(params: IndexerGetSubstateRequest): Promise<IndexerGetSubstateResponse>;
18
+ listSubstates(params: SubstatesListRequest): Promise<SubstatesListResponse>;
19
+ listTemplates(params: ListTemplatesRequest): Promise<ListTemplatesResponse>;
20
+ getTemplateDefinition(params: GetTemplateDefinitionRequest): Promise<GetTemplateDefinitionResponse>;
21
+ getTransactionResult(params: TransactionGetResultRequest): Promise<TransactionGetResultResponse>;
22
+ getSubstateTransactions(params: GetRelatedTransactionsRequest): Promise<GetRelatedTransactionsResponse>;
23
+ getNonFungibles(params: GetNonFungiblesRequest): Promise<GetNonFungiblesResponse>;
24
+ getNonFungibleCollections(): Promise<GetNonFungibleCollectionsResponse>;
25
+ getNonFungibleCount(params: GetNonFungibleCountRequest): Promise<GetNonFungibleCountResponse>;
26
+ getEpochManagerStats(): Promise<GetEpochManagerStatsResponse>;
27
+ waitForTransactionResult(params: TransactionWaitResultRequest): Promise<TransactionWaitResultResponse>;
28
+ getIdentity(): Promise<IndexerGetIdentityResponse | undefined>;
29
+ __invokeRpc(method: string, params?: object | null): Promise<any>;
30
+ }
@@ -0,0 +1,91 @@
1
+ import { FetchRpcTransport } from "./rpc";
2
+ export class IndexerProviderClient {
3
+ token;
4
+ transport;
5
+ id;
6
+ connected;
7
+ constructor(transport) {
8
+ this.token = null;
9
+ this.transport = transport;
10
+ this.id = 0;
11
+ this.connected = false;
12
+ }
13
+ static new(transport) {
14
+ return new IndexerProviderClient(transport);
15
+ }
16
+ static usingFetchTransport(url) {
17
+ return IndexerProviderClient.new(FetchRpcTransport.new(url));
18
+ }
19
+ getTransport() {
20
+ return this.transport;
21
+ }
22
+ isAuthenticated() {
23
+ return this.token !== null;
24
+ }
25
+ isConnected() {
26
+ return this.connected;
27
+ }
28
+ setToken(token) {
29
+ this.token = token;
30
+ }
31
+ submitTransaction(params) {
32
+ return this.__invokeRpc("submit_transaction", params);
33
+ }
34
+ inspectSubstate(params) {
35
+ return this.__invokeRpc("inspect_substate", params);
36
+ }
37
+ getSubstate(params) {
38
+ return this.__invokeRpc("get_substate", params);
39
+ }
40
+ listSubstates(params) {
41
+ return this.__invokeRpc("list_substates", params);
42
+ }
43
+ listTemplates(params) {
44
+ return this.__invokeRpc("list_templates", params);
45
+ }
46
+ getTemplateDefinition(params) {
47
+ return this.__invokeRpc("get_template_definition", params);
48
+ }
49
+ getTransactionResult(params) {
50
+ return this.__invokeRpc("get_transaction_result", params);
51
+ }
52
+ getSubstateTransactions(params) {
53
+ return this.__invokeRpc("get_substate_transactions", params);
54
+ }
55
+ getNonFungibles(params) {
56
+ return this.__invokeRpc("get_non_fungibles", params);
57
+ }
58
+ getNonFungibleCollections() {
59
+ return this.__invokeRpc("get_non_fungible_collections");
60
+ }
61
+ getNonFungibleCount(params) {
62
+ return this.__invokeRpc("get_non_fungible_count", params);
63
+ }
64
+ getEpochManagerStats() {
65
+ return this.__invokeRpc("get_epoch_manager_stats");
66
+ }
67
+ waitForTransactionResult(params) {
68
+ return this.__invokeRpc("transactions.wait_result", params);
69
+ }
70
+ async getIdentity() {
71
+ try {
72
+ const res = await this.__invokeRpc("get_identity");
73
+ this.connected = !!res.public_key;
74
+ return res;
75
+ }
76
+ catch (e) {
77
+ console.error("Failed to get Indexer identity:", e);
78
+ this.connected = false;
79
+ }
80
+ }
81
+ async __invokeRpc(method, params = null) {
82
+ const id = this.id++;
83
+ const response = await this.transport.sendRequest({
84
+ method,
85
+ jsonrpc: "2.0",
86
+ id: id,
87
+ params: params || {},
88
+ }, { token: this.token ?? undefined, timeout_millis: undefined });
89
+ return response;
90
+ }
91
+ }
@@ -0,0 +1,7 @@
1
+ import { RpcRequest, RpcTransport, RpcTransportOptions } from "./rpc";
2
+ export default class FetchRpcTransport implements RpcTransport {
3
+ private url;
4
+ constructor(url: string);
5
+ static new(url: string): FetchRpcTransport;
6
+ sendRequest<T>(data: RpcRequest, options: RpcTransportOptions): Promise<T>;
7
+ }
@@ -0,0 +1,38 @@
1
+ export default class FetchRpcTransport {
2
+ url;
3
+ constructor(url) {
4
+ this.url = url;
5
+ }
6
+ static new(url) {
7
+ return new FetchRpcTransport(url);
8
+ }
9
+ async sendRequest(data, options) {
10
+ const headers = {
11
+ "Content-Type": "application/json",
12
+ };
13
+ if (options?.token) {
14
+ headers["Authorization"] = `Bearer ${options.token}`;
15
+ }
16
+ let controller = new AbortController();
17
+ let signal = controller.signal;
18
+ const timeoutId = options.timeout_millis
19
+ ? setTimeout(() => {
20
+ controller.abort("Timeout");
21
+ }, options.timeout_millis)
22
+ : null;
23
+ const response = await fetch(this.url, {
24
+ method: "POST",
25
+ body: JSON.stringify(data),
26
+ headers,
27
+ signal,
28
+ });
29
+ if (timeoutId) {
30
+ clearTimeout(timeoutId);
31
+ }
32
+ const json = await response.json();
33
+ if (json.error) {
34
+ throw new Error(`${json.error.code}: ${json.error.message}`);
35
+ }
36
+ return json.result;
37
+ }
38
+ }
@@ -0,0 +1,3 @@
1
+ export { IndexerProviderClient } from "./IndexerProviderClient";
2
+ export * from "./fetch";
3
+ export * from "./rpc";
@@ -0,0 +1,3 @@
1
+ export { IndexerProviderClient } from "./IndexerProviderClient";
2
+ export * from "./fetch";
3
+ export * from "./rpc";
@@ -0,0 +1,15 @@
1
+ import FetchRpcTransport from "./fetch";
2
+ export { FetchRpcTransport };
3
+ export interface RpcTransport {
4
+ sendRequest<T>(request: RpcRequest, options: RpcTransportOptions): Promise<T>;
5
+ }
6
+ export interface RpcTransportOptions {
7
+ token?: string;
8
+ timeout_millis?: number;
9
+ }
10
+ export interface RpcRequest {
11
+ id: number;
12
+ jsonrpc: string;
13
+ method: string;
14
+ params: any;
15
+ }
@@ -0,0 +1,2 @@
1
+ import FetchRpcTransport from "./fetch";
2
+ export { FetchRpcTransport };
package/package.json ADDED
@@ -0,0 +1,31 @@
1
+ {
2
+ "name": "@tari-project/indexer-provider",
3
+ "version": "0.5.0",
4
+ "description": "",
5
+ "type": "module",
6
+ "publishConfig": {
7
+ "access": "public"
8
+ },
9
+ "keywords": [],
10
+ "author": "",
11
+ "license": "ISC",
12
+ "dependencies": {
13
+ "@tari-project/typescript-bindings": "^1.5.1",
14
+ "@tari-project/wallet_jrpc_client": "^1.5.1",
15
+ "@tari-project/tari-permissions": "^0.5.0",
16
+ "@tari-project/tarijs-types": "^0.5.0",
17
+ "@tari-project/tari-provider": "^0.5.0"
18
+ },
19
+ "devDependencies": {
20
+ "@types/node": "^22.13.1",
21
+ "typescript": "^5.0.4"
22
+ },
23
+ "files": [
24
+ "/dist"
25
+ ],
26
+ "main": "dist/index.js",
27
+ "types": "dist/index.d.ts",
28
+ "scripts": {
29
+ "build": "tsc -b"
30
+ }
31
+ }