@xyo-network/xl1-rpc 1.2.4 → 1.2.6

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.
@@ -1,68 +0,0 @@
1
- import { type Address, type Hash } from '@xylabs/hex';
2
- import type { ArchivistInstance } from '@xyo-network/archivist-model';
3
- import type { ModuleIdentifier } from '@xyo-network/module-model';
4
- import type { NodeInstance } from '@xyo-network/node-model';
5
- import { type SignedHydratedBlock, type SignedHydratedTransaction, type XyoViewer } from '@xyo-network/xl1-model';
6
- export declare class NodeXyoViewer implements XyoViewer {
7
- protected readonly finalizedArchivistPath: ModuleIdentifier;
8
- protected readonly headValidationDivinerPath: ModuleIdentifier;
9
- protected readonly node: NodeInstance;
10
- private _chainId;
11
- private _finalizedArchivist;
12
- constructor(node: NodeInstance);
13
- accountBalance(address: Address, blockNumber: number, full: boolean): Promise<bigint>;
14
- blockByHash(hash: Hash): Promise<SignedHydratedBlock | null>;
15
- blockByNumber(blockNumber: number): Promise<SignedHydratedBlock | null>;
16
- blocksByHash(hash: Hash, limit?: number): Promise<SignedHydratedBlock[]>;
17
- chainId(): Promise<Address>;
18
- currentBlock(): Promise<SignedHydratedBlock>;
19
- currentBlockHash(): Promise<Hash>;
20
- currentBlockNumber(): Promise<number>;
21
- transactionByBlockHashAndIndex(blockHash: Hash, transactionIndex?: number): Promise<SignedHydratedTransaction | null>;
22
- transactionByBlockNumberAndIndex(blockNumber: number, transactionIndex?: number): Promise<SignedHydratedTransaction | null>;
23
- transactionByHash(transactionHash: Hash): Promise<SignedHydratedTransaction | null>;
24
- protected getArchivist: (identifier: ModuleIdentifier) => Promise<ArchivistInstance<import("@xylabs/base").BaseParamsFields & {
25
- account?: import("@xyo-network/account-model").AccountInstance | "random";
26
- addToResolvers?: boolean;
27
- additionalSigners?: import("@xyo-network/account-model").AccountInstance[];
28
- allowNameResolution?: boolean;
29
- config: import("@xyo-network/module-model").AnyConfigSchema<import("@xyo-network/archivist-model").ArchivistConfig>;
30
- ephemeralQueryAccountEnabled?: boolean;
31
- moduleIdentifierTransformers?: import("@xyo-network/module-model").ModuleIdentifierTransformer[];
32
- privateChildren?: import("@xyo-network/module-model").ModuleInstance[];
33
- publicChildren?: import("@xyo-network/module-model").ModuleInstance[];
34
- } & import("@xyo-network/archivist-model").ArchivistParamFields & object, import("@xyo-network/archivist-model").ArchivistModuleEventData, import("@xyo-network/payload-model").Payload<void, void>>>;
35
- protected getDiviner: (identifier: ModuleIdentifier) => Promise<import("@xyo-network/diviner-model").DivinerInstance<import("@xylabs/base").BaseParamsFields & {
36
- account?: import("@xyo-network/account-model").AccountInstance | "random";
37
- addToResolvers?: boolean;
38
- additionalSigners?: import("@xyo-network/account-model").AccountInstance[];
39
- allowNameResolution?: boolean;
40
- config: import("@xyo-network/module-model").AnyConfigSchema<import("@xyo-network/diviner-model").DivinerConfig>;
41
- ephemeralQueryAccountEnabled?: boolean;
42
- moduleIdentifierTransformers?: import("@xyo-network/module-model").ModuleIdentifierTransformer[];
43
- privateChildren?: import("@xyo-network/module-model").ModuleInstance[];
44
- publicChildren?: import("@xyo-network/module-model").ModuleInstance[];
45
- }, import("@xyo-network/payload-model").Payload<void, void>, import("@xyo-network/payload-model").Payload<void, void>, import("@xyo-network/diviner-model").DivinerModuleEventData<import("@xyo-network/module-model").ModuleInstance<import("@xylabs/base").BaseParamsFields & {
46
- account?: import("@xyo-network/account-model").AccountInstance | "random";
47
- addToResolvers?: boolean;
48
- additionalSigners?: import("@xyo-network/account-model").AccountInstance[];
49
- allowNameResolution?: boolean;
50
- config: import("@xyo-network/module-model").AnyConfigSchema<import("@xyo-network/module-model").ModuleConfig>;
51
- ephemeralQueryAccountEnabled?: boolean;
52
- moduleIdentifierTransformers?: import("@xyo-network/module-model").ModuleIdentifierTransformer[];
53
- privateChildren?: import("@xyo-network/module-model").ModuleInstance[];
54
- publicChildren?: import("@xyo-network/module-model").ModuleInstance[];
55
- }, import("@xyo-network/module-model").ModuleEventData<object>>, import("@xyo-network/payload-model").Payload, import("@xyo-network/payload-model").Payload>>>;
56
- protected getFinalizedArchivist: () => Promise<ArchivistInstance<import("@xylabs/base").BaseParamsFields & {
57
- account?: import("@xyo-network/account-model").AccountInstance | "random";
58
- addToResolvers?: boolean;
59
- additionalSigners?: import("@xyo-network/account-model").AccountInstance[];
60
- allowNameResolution?: boolean;
61
- config: import("@xyo-network/module-model").AnyConfigSchema<import("@xyo-network/archivist-model").ArchivistConfig>;
62
- ephemeralQueryAccountEnabled?: boolean;
63
- moduleIdentifierTransformers?: import("@xyo-network/module-model").ModuleIdentifierTransformer[];
64
- privateChildren?: import("@xyo-network/module-model").ModuleInstance[];
65
- publicChildren?: import("@xyo-network/module-model").ModuleInstance[];
66
- } & import("@xyo-network/archivist-model").ArchivistParamFields & object, import("@xyo-network/archivist-model").ArchivistModuleEventData, import("@xyo-network/payload-model").Payload>>;
67
- }
68
- //# sourceMappingURL=NodeXyoViewer.d.ts.map
@@ -1 +0,0 @@
1
- {"version":3,"file":"NodeXyoViewer.d.ts","sourceRoot":"","sources":["../../../../src/provider/viewer/NodeXyoViewer.ts"],"names":[],"mappings":"AAGA,OAAO,EAAE,KAAK,OAAO,EAAE,KAAK,IAAI,EAAE,MAAM,aAAa,CAAA;AAErD,OAAO,KAAK,EAAE,iBAAiB,EAAE,MAAM,8BAA8B,CAAA;AAOrE,OAAO,KAAK,EAAE,gBAAgB,EAAE,MAAM,2BAA2B,CAAA;AACjE,OAAO,KAAK,EAAE,YAAY,EAAE,MAAM,yBAAyB,CAAA;AAE3D,OAAO,EACiH,KAAK,mBAAmB,EAC9I,KAAK,yBAAyB,EAAE,KAAK,SAAS,EAC/C,MAAM,wBAAwB,CAAA;AAK/B,qBAAa,aAAc,YAAW,SAAS;IAE7C,SAAS,CAAC,QAAQ,CAAC,sBAAsB,EAAE,gBAAgB,CAA6B;IACxF,SAAS,CAAC,QAAQ,CAAC,yBAAyB,EAAE,gBAAgB,CAAyC;IACvG,SAAS,CAAC,Q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import { exists } from '@xylabs/exists'
4
- import { type Address, type Hash } from '@xylabs/hex'
5
- import { hexToBigInt } from '@xylabs/hex'
6
- import type { ArchivistInstance } from '@xyo-network/archivist-model'
7
- import { asArchivistInstance } from '@xyo-network/archivist-model'
8
- import { BoundWitnessSchema, type Signed } from '@xyo-network/boundwitness-model'
9
- import {
10
- analyzeChain, BalanceAnalyzer, findMostRecentBlock, hydrateBlock, hydrateTransaction, isChainSummaryBalances,
11
- } from '@xyo-network/chain-protocol'
12
- import { asDivinerInstance } from '@xyo-network/diviner-model'
13
- import type { ModuleIdentifier } from '@xyo-network/module-model'
14
- import type { NodeInstance } from '@xyo-network/node-model'
15
- import type { WithStorageMeta } from '@xyo-network/payload-model'
16
- import {
17
- asOptionalBlockBoundWitnessWithStorageMeta, asOptionalTransactionBoundWitnessWithStorageMeta, type BlockBoundWitness, type SignedHydratedBlock,
18
- type SignedHydratedTransaction, type XyoViewer,
19
- } from '@xyo-network/xl1-model'
20
-
21
- const WindowLimit = 1000
22
- const WindowLimitBigInt = BigInt(WindowLimit)
23
-
24
- export class NodeXyoViewer implements XyoViewer {
25
- // TODO: Make configurable with defaults
26
- protected readonly finalizedArchivistPath: ModuleIdentifier = 'XYOChain:Chain:Finalized'
27
- protected readonly headValidationDivinerPath: ModuleIdentifier = 'XYOChain:Chain:HeadValidationDiviner'
28
- protected readonly node: NodeInstance
29
-
30
- private _chainId: Address | undefined
31
- private _finalizedArchivist: ArchivistInstance | undefined
32
-
33
- constructor(node: NodeInstance) {
34
- this.node = node
35
- }
36
-
37
- async accountBalance(address: Address, blockNumber: number, full: boolean) {
38
- const head = await this.blockByNumber(blockNumber)
39
- if (!head) {
40
- throw new Error(`Error: Could not find block ${blockNumber}`)
41
- }
42
- const analysis = await analyzeChain(
43
- await this.getFinalizedArchivist(),
44
- [new BalanceAnalyzer()],
45
- head,
46
- null,
47
- full ? -1n : WindowLimitBigInt,
48
- )
49
- const balances = assertEx(analysis.find(isChainSummaryBalances)?.balances, () => 'Failed to sync balances')
50
- return balances[address] ? hexToBigInt(balances[address]) : 0n
51
- }
52
-
53
- async blockByHash(hash: Hash): Promise<SignedHydratedBlock | null> {
54
- const finalizedArchivist = await this.getFinalizedArchivist()
55
- return await hydrateBlock(finalizedArchivist, hash)
56
- }
57
-
58
- async blockByNumber(blockNumber: number): Promise<SignedHydratedBlock | null> {
59
- const finalizedArchivist = await this.getFinalizedArchivist()
60
- const result = await this.currentBlock()
61
- let currentBlock: WithStorageMeta<Signed<BlockBoundWitness>> = result[0]
62
- if (currentBlock.block === blockNumber) return result
63
- if (blockNumber > currentBlock.block) throw new Error(`Error: Block number ${blockNumber} is greater than current block number ${currentBlock.block}`)
64
- if (blockNumber < 0) throw new Error(`Error: Block number ${blockNumber} is less than 0`)
65
- if (blockNumber - currentBlock.block > WindowLimit) throw new Error(`Error: Block number ${blockNumber} is too far in the past`)
66
- do {
67
- const { previous } = currentBlock
68
- if (previous === null) return null
69
- const nextBlock = asOptionalBlockBoundWitnessWithStorageMeta((await finalizedArchivist.get([previous]))[0])
70
- if (!nextBlock) return null
71
- currentBlock = nextBlock
72
- }
73
- while (blockNumber != currentBlock.block)
74
- return currentBlock.block === blockNumber ? await hydrateBlock(finalizedArchivist, currentBlock._hash) : null
75
- }
76
-
77
- async blocksByHash(hash: Hash, limit: number = 10): Promise<SignedHydratedBlock[]> {
78
- assertEx(limit > 0, () => 'Error: limit must be greater than 0')
79
- assertEx(limit <= 100, () => 'Error: limit must be less than 100')
80
- const blocks: SignedHydratedBlock[] = []
81
- let current = await this.blockByHash(hash)
82
- while (current && blocks.length < limit) {
83
- blocks.push(current)
84
- const previousHash = current[0].previous
85
- if (!previousHash) break
86
- current = await this.blockByHash(previousHash)
87
- }
88
- return blocks
89
- }
90
-
91
- async chainId(): Promise<Address> {
92
- if (this._chainId) return this._chainId
93
- const finalizedArchivist = await this.getFinalizedArchivist()
94
- const block = await findMostRecentBlock(finalizedArchivist)
95
- if (!block) throw new Error('Error: Could not find most recent block')
96
- this._chainId = block.chain
97
- return await Promise.resolve(this._chainId)
98
- }
99
-
100
- async currentBlock(): Promise<SignedHydratedBlock> {
101
- const finalizedArchivist = await this.getFinalizedArchivist()
102
- return await hydrateBlock(finalizedArchivist, await this.currentBlockHash())
103
- }
104
-
105
- async currentBlockHash(): Promise<Hash> {
106
- const finalizedArchivist = await this.getFinalizedArchivist()
107
- const block = assertEx(await findMostRecentBlock(finalizedArchivist), () => 'Error: Could not find most recent block')
108
- return block._hash
109
- }
110
-
111
- async currentBlockNumber(): Promise<number> {
112
- const finalizedArchivist = await this.getFinalizedArchivist()
113
- const block = assertEx(await findMostRecentBlock(finalizedArchivist), () => 'Error: Could not find most recent block')
114
- return block.block
115
- }
116
-
117
- async transactionByBlockHashAndIndex(blockHash: Hash, transactionIndex: number = 0): Promise<SignedHydratedTransaction | null> {
118
- assertEx(transactionIndex >= 0, () => 'Error: transactionIndex must be greater than or equal to 0')
119
- try {
120
- const block = await this.blockByHash(blockHash)
121
- if (!block) return null
122
- const blockBoundWitnessIndexes = block[0].payload_schemas.map((schema, index) => schema === BoundWitnessSchema ? index : undefined).filter(exists)
123
- const blockBoundWitnessHashes = new Set(blockBoundWitnessIndexes.map(index => block[0].payload_hashes[index]))
124
- const blockBoundWitnesses = block[1].filter(payload => blockBoundWitnessHashes.has(payload._hash) || blockBoundWitnessHashes.has(payload._dataHash))
125
- const blockTransactionBoundWitnesses = filterAs(blockBoundWitnesses, asOptionalTransactionBoundWitnessWithStorageMeta)
126
- const transaction = blockTransactionBoundWitnesses.at(transactionIndex)
127
- if (!transaction) return null
128
- return await this.transactionByHash(transaction._hash)
129
- } catch {
130
- return null
131
- }
132
- }
133
-
134
- async transactionByBlockNumberAndIndex(blockNumber: number, transactionIndex: number = 0): Promise<SignedHydratedTransaction | null> {
135
- try {
136
- const block = await this.blockByNumber(blockNumber)
137
- if (!block) return null
138
- return await this.transactionByBlockHashAndIndex(block[0]._hash, transactionIndex)
139
- } catch {
140
- return null
141
- }
142
- }
143
-
144
- async transactionByHash(transactionHash: Hash): Promise<SignedHydratedTransaction | null> {
145
- try {
146
- const finalizedArchivist = await this.getFinalizedArchivist()
147
- return await hydrateTransaction(finalizedArchivist, transactionHash)
148
- } catch {
149
- return null
150
- }
151
- }
152
-
153
- protected getArchivist = async (identifier: ModuleIdentifier) => {
154
- const diviner = await this.node.resolve(identifier)
155
- return assertEx(asArchivistInstance(diviner), () => `Error: Could not resolve ${identifier} to an archivist instance`)
156
- }
157
-
158
- protected getDiviner = async (identifier: ModuleIdentifier) => {
159
- const diviner = await this.node.resolve(identifier)
160
- return assertEx(asDivinerInstance(diviner), () => `Error: Could not resolve ${identifier} to a diviner instance`)
161
- }
162
-
163
- protected getFinalizedArchivist = async () => {
164
- if (this._finalizedArchivist) return this._finalizedArchivist
165
- this._finalizedArchivist = await this.getArchivist(this.finalizedArchivistPath)
166
- return this._finalizedArchivist
167
- }
168
- }