getInterchain: perChain + chains options (default first anchor per chain); bump 1.5.0
transaction.getInterchain / block.getInterchain take an optional
{ perChain?, chains? } that maps to prime-node's ?perChain=&chains= params.
Default (no options) returns one anchor per chain. Shared buildInterchainQuery
helper + InterchainOptions type, exported; jest-tested.
This commit is contained in:
@@ -3,6 +3,7 @@
|
||||
*/
|
||||
|
||||
import { DragonchainClient } from './client';
|
||||
import { InterchainOptions, buildInterchainQuery } from './interchain';
|
||||
import { Block, InterchainTrace } from './types';
|
||||
|
||||
export class BlockClient {
|
||||
@@ -23,7 +24,9 @@ export class BlockClient {
|
||||
* Traces a block to the validator (verification) blocks that validated it and
|
||||
* the public-chain interchain anchors those validator blocks were bundled into.
|
||||
*/
|
||||
async getInterchain(blockId: string): Promise<InterchainTrace> {
|
||||
return this.client.get<InterchainTrace>(`/api/v1/block/${blockId}/interchain`);
|
||||
async getInterchain(blockId: string, options?: InterchainOptions): Promise<InterchainTrace> {
|
||||
return this.client.get<InterchainTrace>(
|
||||
`/api/v1/block/${blockId}/interchain${buildInterchainQuery(options)}`
|
||||
);
|
||||
}
|
||||
}
|
||||
|
||||
@@ -85,6 +85,7 @@ export { BlockClient } from './block';
|
||||
export { SystemClient } from './system';
|
||||
export { ProofMeasureClient, PROOF_MEASURE_DEFAULT_BASE_URL } from './proofMeasure';
|
||||
export { UnauthHttpClient, UnauthClientConfig } from './unauthHttpClient';
|
||||
export { InterchainOptions, buildInterchainQuery } from './interchain';
|
||||
|
||||
// Default export
|
||||
export default DragonchainSDK;
|
||||
|
||||
37
src/interchain.ts
Normal file
37
src/interchain.ts
Normal file
@@ -0,0 +1,37 @@
|
||||
/**
|
||||
* Options + query builder for the interchain-trace endpoints
|
||||
* (transaction.getInterchain / block.getInterchain).
|
||||
*
|
||||
* Anchor proofs are chained, so by default the trace returns the first anchor
|
||||
* per public chain; these options change how many and which chains are returned.
|
||||
*/
|
||||
|
||||
export interface InterchainOptions {
|
||||
/**
|
||||
* Max interchain anchors returned per public chain, earliest-first.
|
||||
* 1 = the first anchor per chain (the service default); 0 = all anchors.
|
||||
*/
|
||||
perChain?: number;
|
||||
/**
|
||||
* Interchain chain ids to include ("1" = ETH mainnet, "0" = BTC; testnet ids
|
||||
* differ). Omit to include every chain found.
|
||||
*/
|
||||
chains?: string[];
|
||||
}
|
||||
|
||||
/**
|
||||
* Builds the "?perChain=...&chains=..." suffix for the interchain trace
|
||||
* endpoints. Returns "" when no options are set (the server then applies its
|
||||
* defaults: one anchor per chain, all chains).
|
||||
*/
|
||||
export function buildInterchainQuery(options?: InterchainOptions): string {
|
||||
if (!options) return '';
|
||||
const parts: string[] = [];
|
||||
if (options.perChain !== undefined) {
|
||||
parts.push(`perChain=${encodeURIComponent(String(options.perChain))}`);
|
||||
}
|
||||
if (options.chains && options.chains.length > 0) {
|
||||
parts.push(`chains=${options.chains.map(encodeURIComponent).join(',')}`);
|
||||
}
|
||||
return parts.length ? `?${parts.join('&')}` : '';
|
||||
}
|
||||
@@ -3,6 +3,7 @@
|
||||
*/
|
||||
|
||||
import { DragonchainClient } from './client';
|
||||
import { InterchainOptions, buildInterchainQuery } from './interchain';
|
||||
import {
|
||||
ContentType,
|
||||
TransactionCreateRequest,
|
||||
@@ -56,8 +57,13 @@ export class TransactionClient {
|
||||
* blocks were bundled into. If the transaction is still pending (not yet in a
|
||||
* block) the trace's arrays are empty.
|
||||
*/
|
||||
async getInterchain(transactionId: string): Promise<InterchainTrace> {
|
||||
return this.client.get<InterchainTrace>(`/api/v1/transaction/${transactionId}/interchain`);
|
||||
async getInterchain(
|
||||
transactionId: string,
|
||||
options?: InterchainOptions
|
||||
): Promise<InterchainTrace> {
|
||||
return this.client.get<InterchainTrace>(
|
||||
`/api/v1/transaction/${transactionId}/interchain${buildInterchainQuery(options)}`
|
||||
);
|
||||
}
|
||||
|
||||
/**
|
||||
|
||||
Reference in New Issue
Block a user