From 64c83894996a48add10645f6fced343520e6ffba Mon Sep 17 00:00:00 2001 From: Atticus Date: Mon, 4 Mar 2024 11:30:04 -0500 Subject: [PATCH 1/6] chore(cache): add observations an distributions apis --- src/common/caches/arns-remote-cache.ts | 21 +++++++++++++++++++++ src/common/http.ts | 3 +++ src/types/common.ts | 6 +++++- 3 files changed, 29 insertions(+), 1 deletion(-) diff --git a/src/common/caches/arns-remote-cache.ts b/src/common/caches/arns-remote-cache.ts index 2f93786e..8fbd2e6a 100644 --- a/src/common/caches/arns-remote-cache.ts +++ b/src/common/caches/arns-remote-cache.ts @@ -19,8 +19,10 @@ import { ArIOContract, ArNSNameData, ArNSStateResponse, + EpochDistributionData, Gateway, HTTPClient, + Observations, } from '../../types/index.js'; import { NotFound } from '../error.js'; import { AxiosHTTPService } from '../http.js'; @@ -123,4 +125,23 @@ export class ArNSRemoteCache implements ArIOContract { }); return result; } + + async getObservations(): Promise { + this.logger.debug(`Fetching Observations`); + const { result } = await this.http.get>({ + endpoint: `/contract/${this.contractTxId.toString()}/state/observations` + }) + + return result + } + async getDistributions({ epoch }: { epoch: number; }): Promise { + this.logger.debug(`Fetching distributions for epoch ${epoch}`); + const {result} = await this.http.get>({ + endpoint: `/contract/${this.contractTxId.toString()}/read/epoch`, + params: { + height: epoch + } + }); + return result + } } diff --git a/src/common/http.ts b/src/common/http.ts index d9751152..b84102d1 100644 --- a/src/common/http.ts +++ b/src/common/http.ts @@ -39,16 +39,19 @@ export class AxiosHTTPService implements HTTPClient { signal, allowedStatuses = [200, 202], headers, + params }: { endpoint: string; signal?: AbortSignal; allowedStatuses?: number[]; headers?: Record; + params?:Record }): Promise { this.logger.debug(`Get request to endpoint: ${endpoint}`); const { status, statusText, data } = await this.axios.get(endpoint, { headers, signal, + params }); if (!allowedStatuses.includes(status)) { diff --git a/src/types/common.ts b/src/types/common.ts index 01e074e1..296dc506 100644 --- a/src/types/common.ts +++ b/src/types/common.ts @@ -14,7 +14,7 @@ * You should have received a copy of the GNU Affero General Public License * along with this program. If not, see . */ -import { ArNSNameData, Gateway } from './contract-state.js'; +import { ArNSNameData, EpochDistributionData, Gateway, Observations } from './contract-state.js'; // TODO: extend with additional methods export interface ArIOContract { @@ -24,6 +24,8 @@ export interface ArIOContract { getBalances(): Promise>; getArNSRecord({ domain }: { domain: string }): Promise; getArNSRecords(): Promise>; + getObservations(): Promise + getDistributions(props: {epoch: EpochTimeStamp}):Promise } /* eslint-disable @typescript-eslint/no-explicit-any */ @@ -45,11 +47,13 @@ export interface HTTPClient { signal, headers, allowedStatuses, + params }: { endpoint: string; signal?: AbortSignal; headers?: Record; allowedStatuses?: number[]; + params?: Record }): Promise; // TODO: add post method // post({ From 21e38d1229e640a1cec685f491b1b07b84ee6a56 Mon Sep 17 00:00:00 2001 From: Atticus Date: Tue, 12 Mar 2024 16:48:24 -0600 Subject: [PATCH 2/6] feat(ario contract): add distributions and observation apis --- src/common.ts | 14 +++++++++++++- src/common/ar-io.ts | 23 +++++++++++++++++++++++ 2 files changed, 36 insertions(+), 1 deletion(-) diff --git a/src/common.ts b/src/common.ts index 4bde8de5..09627f0f 100644 --- a/src/common.ts +++ b/src/common.ts @@ -14,7 +14,13 @@ * You should have received a copy of the GNU Affero General Public License * along with this program. If not, see . */ -import { ArIOState, ArNSNameData, Gateway } from './contract-state.js'; +import { + ArIOState, + ArNSNameData, + EpochDistributionData, + Gateway, + Observations, +} from './contract-state.js'; export type BlockHeight = number; export type SortKey = string; @@ -73,6 +79,12 @@ export interface ArIOContract { }: EvaluationParameters): Promise< Record | Record >; + getObservations({ + evaluationOptions, + }: EvaluationParameters): Promise; + getDistributions({ + evaluationOptions, + }: EvaluationParameters): Promise; } /* eslint-disable @typescript-eslint/no-explicit-any */ diff --git a/src/common/ar-io.ts b/src/common/ar-io.ts index b5679b16..dc790946 100644 --- a/src/common/ar-io.ts +++ b/src/common/ar-io.ts @@ -19,8 +19,11 @@ import { ArIOContract, ArIOState, ArNSNameData, + EpochDistributionData, + EvaluationOptions, EvaluationParameters, Gateway, + Observations, SmartWeaveContract, } from '../types.js'; import { RemoteContract } from './contracts/remote-contract.js'; @@ -149,4 +152,24 @@ export class ArIO implements ArIOContract { evaluationOptions, }); } + async getObservations({ + evaluationOptions, + }: { + evaluationOptions?: EvaluationOptions | Record | undefined; + }): Promise { + const { observations } = await this.contract.getContractState({ + evaluationOptions, + }); + return observations; + } + async getDistributions({ + evaluationOptions, + }: { + evaluationOptions?: EvaluationOptions | Record | undefined; + }): Promise { + const { distributions } = await this.contract.getContractState({ + evaluationOptions, + }); + return distributions; + } } From 0208317c211d659afbbc55d06345a0c33cd076eb Mon Sep 17 00:00:00 2001 From: Atticus Date: Tue, 12 Mar 2024 18:06:37 -0600 Subject: [PATCH 3/6] feat(contract): add distribution, observations apis, update readme and examples --- README.md | 83 +++++++++++++++++++++++++++++++++++++++++ examples/node/index.cjs | 12 ++++++ examples/node/index.mjs | 12 ++++++ examples/web/index.html | 72 +++++++++++++++++++++++++++++++++++ src/common.ts | 6 ++- src/common/ar-io.ts | 28 ++++++++------ tests/ar-io.test.ts | 41 ++++++++++++++++++++ 7 files changed, 241 insertions(+), 13 deletions(-) diff --git a/README.md b/README.md index 555d4258..d0053231 100644 --- a/README.md +++ b/README.md @@ -347,6 +347,89 @@ const records = arIO.getArNSRecords(); // } ``` +### `getEpoch({ evaluationOptions })` + +Returns the epoch data for the specified block height. + +```typescript +const arIO = new ArIO(); +const epoch = await arIO.getEpoch({ blockHeight: 1382230 }); + +// output + +// { +// epochStartHeight: 1381660, +// epochEndHeight: 1382379, +// epochZeroStartHeight: 1350700, +// epochDistributionHeight: 1382394, +// epochPeriod: 43, +// epochBlockLength: 720 +// } +``` + +### `getCurrentEpoch({ evaluationOptions })` + +Returns the current epoch data. + +```typescript +const arIO = new ArIO(); +const epoch = await arIO.getCurrentEpoch(); + +// output + +// { +// epochStartHeight: 1381660, +// epochEndHeight: 1382379, +// epochZeroStartHeight: 1350700, +// epochDistributionHeight: 1382394, +// epochPeriod: 43, +// epochBlockLength: 720 +// } +``` + +### `getObservations({ evaluationOptions })` + +Returns the epoch-indexed observation list. + +```typescript +const arIO = new ArIO(); +const observations = await arIO.getObservations(); + +// output + +// { +// "1350700": { +// "failureSummaries": { +// "-Tk2DDk8k4zkwtppp_XFKKI5oUgh6IEHygAoN7mD-w8": [ +// ... +// "reports": { +// "IPdwa3Mb_9pDD8c2IaJx6aad51Ss-_TfStVwBuhtXMs": "B6UUjKWjjEWDBvDSMXWNmymfwvgR9EN27z5FTkEVlX4", +// "Ie2wEEUDKoU26c7IuckHNn3vMFdNQnMvfPBrFzAb3NA": "7tKsiQ2fxv0D8ZVN_QEv29fZ8hwFIgHoEDrpeEG0DIs", +// "osZP4D9cqeDvbVFBaEfjIxwc1QLIvRxUBRAxDIX9je8": "aatgznEvC_UPcxp1v0uw_RqydhIfKm4wtt1KCpONBB0", +// "qZ90I67XG68BYIAFVNfm9PUdM7v1XtFTn7u-EOZFAtk": "Bd8SmFK9-ktJRmwIungS8ur6JM-JtpxrvMtjt5JkB1M" +// } +// } +``` + +### `getDistributions({ evaluationOptions })` + +Returns the current rewards distribution information. + +```typescript +const arIO = new ArIO(); +const distributions = await arIO.getDistributions(); + +// output + +// { +// epochEndHeight: 1382379, +// epochPeriod: 43, +// epochStartHeight: 1381660, +// epochZeroStartHeight: 1350700, +// nextDistributionHeight: 1382394 +// } +``` + ## Developers ### Requirements diff --git a/examples/node/index.cjs b/examples/node/index.cjs index 5be24d02..7cf91ab3 100644 --- a/examples/node/index.cjs +++ b/examples/node/index.cjs @@ -12,6 +12,13 @@ const { }); const ardriveRecord = await arIO.getArNSRecord({ domain: 'ardrive' }); const allRecords = await arIO.getArNSRecords(); + const oldEpoch = await arIO.getEpoch({ + blockHeight: 1382230, + }); + const epoch = await arIO.getCurrentEpoch(); + const observations = await arIO.getObservations(); + const observation = await arIO.getObservations({ epoch: 1350700 }); + const distributions = await arIO.getDistributions(); console.dir( { @@ -22,6 +29,11 @@ const { 'registered domains': Object.keys(allRecords).length, ardrive: allRecords.ardrive, }, + oldEpoch, + epoch, + observations, + observation, + distributions, }, { depth: 2 }, ); diff --git a/examples/node/index.mjs b/examples/node/index.mjs index 13b1fbf4..878e0e14 100644 --- a/examples/node/index.mjs +++ b/examples/node/index.mjs @@ -9,6 +9,13 @@ import { ARNS_TESTNET_REGISTRY_TX, ArIO } from '../../lib/esm/node/index.js'; }); const ardriveRecord = await arIO.getArNSRecord({ domain: 'ardrive' }); const allRecords = await arIO.getArNSRecords(); + const oldEpoch = await arIO.getEpoch({ + blockHeight: 1382230, + }); + const epoch = await arIO.getCurrentEpoch(); + const observations = await arIO.getObservations(); + const observation = await arIO.getObservations({ epoch: 1350700 }); + const distributions = await arIO.getDistributions(); console.dir( { @@ -19,6 +26,11 @@ import { ARNS_TESTNET_REGISTRY_TX, ArIO } from '../../lib/esm/node/index.js'; 'registered domains': Object.keys(allRecords).length, ardrive: allRecords.ardrive, }, + oldEpoch, + epoch, + observations, + observation, + distributions, }, { depth: 2 }, ); diff --git a/examples/web/index.html b/examples/web/index.html index 8cea5943..da0c71b6 100644 --- a/examples/web/index.html +++ b/examples/web/index.html @@ -22,6 +22,7 @@ }; +
Browse Records
+ + +
+

Browse Observations

+
+ + + + + + + + + + + +
Epoch HeightReports CountFailure Summary Count
+
+
+ + +
+

View Distribution Data

+
+ + + + + + + + + + + + +
Epoch PeriodEpoch Start HeightEpoch End HeightNext Epoch Height
+
+
+