Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Typed indexer responses #857

Merged
merged 15 commits into from
Jul 16, 2024
4 changes: 2 additions & 2 deletions examples/indexer.ts
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,7 @@ async function main() {
// example: INDEXER_SEARCH_MIN_AMOUNT

// example: INDEXER_PAGINATE_RESULTS
let nextToken = '';
let nextToken: string | undefined = '';

// nextToken will be undefined if we reached the last page
while (nextToken !== undefined) {
Expand All @@ -51,7 +51,7 @@ async function main() {
.nextToken(nextToken)
.do();

nextToken = response['next-token'];
nextToken = response.nextToken;
const txns = response.transactions;
if (txns.length > 0)
console.log(`Transaction IDs: ${response.transactions.map((t) => t.id)}`);
Expand Down
2 changes: 1 addition & 1 deletion examples/utils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -54,7 +54,7 @@ export async function indexerWaitForRound(
round: number | bigint,
maxAttempts: number
) {
let indexerRound = 0;
let indexerRound = BigInt(0);
let attempts = 0;

for (;;) {
Expand Down
2 changes: 1 addition & 1 deletion src/client/v2/algod/compile.ts
Original file line number Diff line number Diff line change
Expand Up @@ -55,7 +55,7 @@ export default class Compile extends JSONRequest<
query: this.query,
requestHeaders: txHeaders,
});
return res.body;
return this.prepare(res.body);
}

// eslint-disable-next-line class-methods-use-this
Expand Down
13 changes: 12 additions & 1 deletion src/client/v2/algod/getBlockTxids.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,11 @@
import JSONRequest from '../jsonrequest.js';
import { HTTPClient } from '../../client.js';
import { BlockTxidsResponse } from './models/types.js';

export default class GetBlockTxids extends JSONRequest {
export default class GetBlockTxids extends JSONRequest<
BlockTxidsResponse,
Record<string, any>
> {
round: number;

constructor(c: HTTPClient, roundNumber: number) {
Expand All @@ -14,4 +18,11 @@ export default class GetBlockTxids extends JSONRequest {
path() {
return `/v2/blocks/${this.round}/txids`;
}

// eslint-disable-next-line class-methods-use-this
prepare(body: Record<string, any>): BlockTxidsResponse {
return BlockTxidsResponse.fromEncodingData(
BlockTxidsResponse.encodingSchema.fromPreparedJSON(body)
);
}
}
6 changes: 6 additions & 0 deletions src/client/v2/algod/suggestedParams.ts
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,11 @@ export interface SuggestedParamsFromAlgod extends SuggestedParams {
lastValid: bigint;
genesisID: string;
genesisHash: Uint8Array;

/**
* ConsensusVersion indicates the consensus protocol version as of the last round.
*/
consensusVersion: string;
}

/**
Expand All @@ -40,6 +45,7 @@ export default class SuggestedParamsRequest extends JSONRequest<
genesisID: body['genesis-id'],
genesisHash: base64ToBytes(body['genesis-hash']),
minFee: BigInt(body['min-fee']),
consensusVersion: body['consensus-version'],
};
}
/* eslint-enable class-methods-use-this */
Expand Down
13 changes: 12 additions & 1 deletion src/client/v2/indexer/lookupAccountAppLocalStates.ts
Original file line number Diff line number Diff line change
@@ -1,8 +1,12 @@
import JSONRequest from '../jsonrequest.js';
import { HTTPClient } from '../../client.js';
import { Address } from '../../../encoding/address.js';
import { ApplicationLocalStatesResponse } from './models/types.js';

export default class LookupAccountAppLocalStates extends JSONRequest {
export default class LookupAccountAppLocalStates extends JSONRequest<
ApplicationLocalStatesResponse,
Record<string, any>
> {
private account: string | Address;

/**
Expand Down Expand Up @@ -135,4 +139,11 @@ export default class LookupAccountAppLocalStates extends JSONRequest {
this.query['application-id'] = index;
return this;
}

// eslint-disable-next-line class-methods-use-this
prepare(body: Record<string, any>): ApplicationLocalStatesResponse {
return ApplicationLocalStatesResponse.fromEncodingData(
ApplicationLocalStatesResponse.encodingSchema.fromPreparedJSON(body)
);
}
}
13 changes: 12 additions & 1 deletion src/client/v2/indexer/lookupAccountAssets.ts
Original file line number Diff line number Diff line change
@@ -1,8 +1,12 @@
import JSONRequest from '../jsonrequest.js';
import { HTTPClient } from '../../client.js';
import { Address } from '../../../encoding/address.js';
import { AssetHoldingsResponse } from './models/types.js';

export default class LookupAccountAssets extends JSONRequest {
export default class LookupAccountAssets extends JSONRequest<
AssetHoldingsResponse,
Record<string, any>
> {
private account: string;

/**
Expand Down Expand Up @@ -136,4 +140,11 @@ export default class LookupAccountAssets extends JSONRequest {
this.query['asset-id'] = index;
return this;
}

// eslint-disable-next-line class-methods-use-this
prepare(body: Record<string, any>): AssetHoldingsResponse {
return AssetHoldingsResponse.fromEncodingData(
AssetHoldingsResponse.encodingSchema.fromPreparedJSON(body)
);
}
}
13 changes: 12 additions & 1 deletion src/client/v2/indexer/lookupAccountByID.ts
Original file line number Diff line number Diff line change
@@ -1,8 +1,12 @@
import JSONRequest from '../jsonrequest.js';
import { HTTPClient } from '../../client.js';
import { Address } from '../../../encoding/address.js';
import { AccountResponse } from './models/types.js';

export default class LookupAccountByID extends JSONRequest {
export default class LookupAccountByID extends JSONRequest<
AccountResponse,
Record<string, any>
> {
private account: string;

/**
Expand Down Expand Up @@ -104,4 +108,11 @@ export default class LookupAccountByID extends JSONRequest {
this.query.exclude = exclude;
return this;
}

// eslint-disable-next-line class-methods-use-this
prepare(body: Record<string, any>): AccountResponse {
return AccountResponse.fromEncodingData(
AccountResponse.encodingSchema.fromPreparedJSON(body)
);
}
}
13 changes: 12 additions & 1 deletion src/client/v2/indexer/lookupAccountCreatedApplications.ts
Original file line number Diff line number Diff line change
@@ -1,8 +1,12 @@
import JSONRequest from '../jsonrequest.js';
import { HTTPClient } from '../../client.js';
import { Address } from '../../../encoding/address.js';
import { ApplicationsResponse } from './models/types.js';

export default class LookupAccountCreatedApplications extends JSONRequest {
export default class LookupAccountCreatedApplications extends JSONRequest<
ApplicationsResponse,
Record<string, any>
> {
private account: string;

/**
Expand Down Expand Up @@ -136,4 +140,11 @@ export default class LookupAccountCreatedApplications extends JSONRequest {
this.query['application-id'] = index;
return this;
}

// eslint-disable-next-line class-methods-use-this
prepare(body: Record<string, any>): ApplicationsResponse {
return ApplicationsResponse.fromEncodingData(
ApplicationsResponse.encodingSchema.fromPreparedJSON(body)
);
}
}
13 changes: 12 additions & 1 deletion src/client/v2/indexer/lookupAccountCreatedAssets.ts
Original file line number Diff line number Diff line change
@@ -1,8 +1,12 @@
import JSONRequest from '../jsonrequest.js';
import { HTTPClient } from '../../client.js';
import { Address } from '../../../encoding/address.js';
import { AssetsResponse } from './models/types.js';

export default class LookupAccountCreatedAssets extends JSONRequest {
export default class LookupAccountCreatedAssets extends JSONRequest<
AssetsResponse,
Record<string, any>
> {
private account: string;

/**
Expand Down Expand Up @@ -137,4 +141,11 @@ export default class LookupAccountCreatedAssets extends JSONRequest {
this.query['asset-id'] = index;
return this;
}

// eslint-disable-next-line class-methods-use-this
prepare(body: Record<string, any>): AssetsResponse {
return AssetsResponse.fromEncodingData(
AssetsResponse.encodingSchema.fromPreparedJSON(body)
);
}
}
27 changes: 20 additions & 7 deletions src/client/v2/indexer/lookupAccountTransactions.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ import { bytesToBase64 } from '../../../encoding/binarydata.js';
import { HTTPClient } from '../../client.js';
import JSONRequest from '../jsonrequest.js';
import { Address } from '../../../encoding/address.js';
import { TransactionsResponse } from './models/types.js';

/**
* Accept base64 string or Uint8Array and output base64 string
Expand All @@ -15,7 +16,10 @@ export function base64StringFunnel(data: Uint8Array | string) {
return bytesToBase64(data);
}

export default class LookupAccountTransactions extends JSONRequest {
export default class LookupAccountTransactions extends JSONRequest<
TransactionsResponse,
Record<string, any>
> {
private account: string;

/**
Expand Down Expand Up @@ -245,11 +249,12 @@ export default class LookupAccountTransactions extends JSONRequest {
* .do();
* ```
*
* @param before - rfc3339 string
* @param before - rfc3339 string or Date object
* @category query
*/
beforeTime(before: string) {
this.query['before-time'] = before;
beforeTime(before: string | Date) {
this.query['before-time'] =
before instanceof Date ? before.toISOString() : before;
return this;
}

Expand All @@ -266,11 +271,12 @@ export default class LookupAccountTransactions extends JSONRequest {
* .do();
* ```
*
* @param after - rfc3339 string
* @param after - rfc3339 string or Date object
* @category query
*/
afterTime(after: string) {
this.query['after-time'] = after;
afterTime(after: string | Date) {
this.query['after-time'] =
after instanceof Date ? after.toISOString() : after;
return this;
}

Expand Down Expand Up @@ -388,4 +394,11 @@ export default class LookupAccountTransactions extends JSONRequest {
this.query['rekey-to'] = rekeyTo;
return this;
}

// eslint-disable-next-line class-methods-use-this
prepare(body: Record<string, any>): TransactionsResponse {
return TransactionsResponse.fromEncodingData(
TransactionsResponse.encodingSchema.fromPreparedJSON(body)
);
}
}
13 changes: 12 additions & 1 deletion src/client/v2/indexer/lookupApplicationLogs.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,11 @@
import JSONRequest from '../jsonrequest.js';
import { HTTPClient } from '../../client.js';
import { ApplicationLogsResponse } from './models/types.js';

export default class LookupApplicationLogs extends JSONRequest {
export default class LookupApplicationLogs extends JSONRequest<
ApplicationLogsResponse,
Record<string, any>
> {
/**
* Returns log messages generated by the passed in application.
*
Expand Down Expand Up @@ -154,4 +158,11 @@ export default class LookupApplicationLogs extends JSONRequest {
this.query.txid = txid;
return this;
}

// eslint-disable-next-line class-methods-use-this
prepare(body: Record<string, any>): ApplicationLogsResponse {
return ApplicationLogsResponse.fromEncodingData(
ApplicationLogsResponse.encodingSchema.fromPreparedJSON(body)
);
}
}
13 changes: 12 additions & 1 deletion src/client/v2/indexer/lookupApplications.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,11 @@
import JSONRequest from '../jsonrequest.js';
import { HTTPClient } from '../../client.js';
import { ApplicationResponse } from './models/types.js';

export default class LookupApplications extends JSONRequest {
export default class LookupApplications extends JSONRequest<
ApplicationResponse,
Record<string, any>
> {
/**
* Returns information about the passed application.
*
Expand Down Expand Up @@ -57,4 +61,11 @@ export default class LookupApplications extends JSONRequest {
this.query['include-all'] = value;
return this;
}

// eslint-disable-next-line class-methods-use-this
prepare(body: Record<string, any>): ApplicationResponse {
return ApplicationResponse.fromEncodingData(
ApplicationResponse.encodingSchema.fromPreparedJSON(body)
);
}
}
13 changes: 12 additions & 1 deletion src/client/v2/indexer/lookupAssetBalances.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,11 @@
import JSONRequest from '../jsonrequest.js';
import { HTTPClient } from '../../client.js';
import { AssetBalancesResponse } from './models/types.js';

export default class LookupAssetBalances extends JSONRequest {
export default class LookupAssetBalances extends JSONRequest<
AssetBalancesResponse,
Record<string, any>
> {
/**
* Returns the list of accounts which hold the given asset and their balance.
*
Expand Down Expand Up @@ -145,4 +149,11 @@ export default class LookupAssetBalances extends JSONRequest {
this.query['include-all'] = value;
return this;
}

// eslint-disable-next-line class-methods-use-this
prepare(body: Record<string, any>): AssetBalancesResponse {
return AssetBalancesResponse.fromEncodingData(
AssetBalancesResponse.encodingSchema.fromPreparedJSON(body)
);
}
}
13 changes: 12 additions & 1 deletion src/client/v2/indexer/lookupAssetByID.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,11 @@
import JSONRequest from '../jsonrequest.js';
import { HTTPClient } from '../../client.js';
import { AssetResponse } from './models/types.js';

export default class LookupAssetByID extends JSONRequest {
export default class LookupAssetByID extends JSONRequest<
AssetResponse,
Record<string, any>
> {
/**
* Returns asset information of the queried asset.
*
Expand Down Expand Up @@ -56,4 +60,11 @@ export default class LookupAssetByID extends JSONRequest {
this.query['include-all'] = value;
return this;
}

// eslint-disable-next-line class-methods-use-this
prepare(body: Record<string, any>): AssetResponse {
return AssetResponse.fromEncodingData(
AssetResponse.encodingSchema.fromPreparedJSON(body)
);
}
}
Loading