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

feat(PE-5759): observations and distributions apis #16

Merged
merged 9 commits into from
Mar 15, 2024
83 changes: 83 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -347,6 +347,89 @@ const records = arIO.getArNSRecords();
// }
```

### `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. The resulting object is pruned, to get older distributions use the `evaluationOptions` to `evalTo` a previous state.

```typescript
const arIO = new ArIO();
const distributions = await arIO.getDistributions();

// output

// {
// epochEndHeight: 1382379,
// epochPeriod: 43,
// epochStartHeight: 1381660,
// epochZeroStartHeight: 1350700,
// nextDistributionHeight: 1382394
// }
```

### `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
// }
```
dtfiedler marked this conversation as resolved.
Show resolved Hide resolved

### `getPrescribedObservers({ evaluationOptions })`

Retrieves the prescribed observers of the ArIO contract. To fetch prescribed observers for a previous epoch set the `evaluationOptions` to the desired epoch.
Expand Down
12 changes: 12 additions & 0 deletions examples/node/index.cjs
Original file line number Diff line number Diff line change
Expand Up @@ -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({ epochStartHeight: 1350700 });
const distributions = await arIO.getDistributions();

console.dir(
{
Expand All @@ -22,6 +29,11 @@ const {
'registered domains': Object.keys(allRecords).length,
ardrive: allRecords.ardrive,
},
oldEpoch,
epoch,
observations,
observation,
distributions,
},
{ depth: 2 },
);
Expand Down
12 changes: 12 additions & 0 deletions examples/node/index.mjs
Original file line number Diff line number Diff line change
Expand Up @@ -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({ epochStartHeight: 1350700 });
const distributions = await arIO.getDistributions();

console.dir(
{
Expand All @@ -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 },
);
Expand Down
72 changes: 72 additions & 0 deletions examples/web/index.html
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@
};
</script>
</head>

<body class="bg-background flex flex-col items-center p-10 gap-10">
<!-- gateways -->
<div
Expand Down Expand Up @@ -83,6 +84,52 @@ <h1 class="text-textPrimary w-full font-bold">Browse Records</h1>
</table>
</div>
</div>

<!-- Get all observations -->
<div
class="bg-surface flex flex-col gap-5 items-end justify-center p-5 rounded-md h-full"
style="width: 750px; height: 500px"
>
<h1 class="text-textPrimary w-full font-bold">Browse Observations</h1>
<div class="h-full w-full" style="overflow-y: scroll">
<table class="w-full bg-background text-textPrimary">
<thead>
<tr>
<th class="px-4 py-2">Epoch Height</th>
<th class="px-4 py-2">Reports Count</th>
<th class="px-4 py-2">Failure Summary Count</th>
</tr>
</thead>
<tbody id="observations-table-body">
<!-- Add more rows as needed -->
</tbody>
</table>
</div>
</div>

<!-- Distribution Data -->
<div
class="bg-surface flex flex-col gap-5 items-end justify-center p-5 rounded-md h-full"
style="width: 750px; height: 200px"
>
<h1 class="text-textPrimary w-full font-bold">View Distribution Data</h1>
<div class="h-full w-full" style="overflow-y: scroll">
<table class="w-full bg-background text-textPrimary">
<thead>
<tr>
<th class="px-4 py-2">Epoch Period</th>
<th class="px-4 py-2">Epoch Start Height</th>
<th class="px-4 py-2">Epoch End Height</th>
<th class="px-4 py-2">Next Epoch Height</th>
</tr>
</thead>
<tbody id="distributions-table-body">
<!-- Add more rows as needed -->
</tbody>
</table>
</div>
</div>

<script type="module">
import { ArIO } from './web.bundle.min.js';

Expand All @@ -97,6 +144,8 @@ <h1 class="text-textPrimary w-full font-bold">Browse Records</h1>
});
const record = await arIO.getArNSRecord({ domain: 'ardrive' });
const records = await arIO.getArNSRecords();
const observations = await arIO.getObservations();
const distributions = await arIO.getDistributions();

// update the UI

Expand Down Expand Up @@ -135,6 +184,29 @@ <h1 class="text-textPrimary w-full font-bold">Browse Records</h1>
`;
})
.join('');

document.getElementById('observations-table-body').innerHTML =
Object.entries(observations)
.map(([epoch, observationData]) => {
return `
<tr>
<td class="border border-surface px-4 py-2 text-primary"><a href="https://arscan.io/block/${epoch}" target="_blank">${epoch}</a></td>
<td class="border border-surface px-4 py-2 text-primary">${Object.keys(observationData.reports).length}</td>
<td class="border border-surface px-4 py-2">${Object.keys(observationData.failureSummaries).length}</td>
</tr>
`;
})
.join('');

document.getElementById('distributions-table-body').innerHTML = `
<tr>
<td class="border border-surface px-4 py-2 text-primary">${distributions.epochPeriod}</a></td>
<td class="border border-surface px-4 py-2 text-primary">${distributions.epochStartHeight}</td>
<td class="border border-surface px-4 py-2">${distributions.epochEndHeight}</td>
<td class="border border-surface px-4 py-2">${distributions.nextDistributionHeight}</td>
</tr>
`;
// end init
}

window.addEventListener('load', init);
Expand Down
9 changes: 9 additions & 0 deletions src/common.ts
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@ import {
ArNSNameData,
EpochDistributionData,
Gateway,
Observations,
WeightedObserver,
} from './contract-state.js';

Expand Down Expand Up @@ -91,6 +92,14 @@ export interface ArIOContract {
getPrescribedObservers({
evaluationOptions,
}: EvaluationParameters): Promise<WeightedObserver[]>;
getObservations({
evaluationOptions,
}: EvaluationParameters<{
epochStartHeight?: number;
}>): Promise<Observations>;
getDistributions({
evaluationOptions,
}: EvaluationParameters): Promise<EpochDistributionData>;
}

/* eslint-disable @typescript-eslint/no-explicit-any */
Expand Down
19 changes: 19 additions & 0 deletions src/common/ar-io.ts
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

looks like you have some formatting comments from github - does prettier resolve?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yes - not sure why husky skipped it. I notice we dont have lint-staged, do we need that?

Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

yeah we don't automatically add files that get formatted - will add in a separate PR

Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@ import {
EpochDistributionData,
EvaluationParameters,
Gateway,
Observations,
SmartWeaveContract,
WeightedObserver,
} from '../types.js';
Expand Down Expand Up @@ -196,4 +197,22 @@ export class ArIO implements ArIOContract {
evaluationOptions,
});
}
async getObservations({
evaluationOptions,
}: EvaluationParameters<{
epochStartHeight?: number;
}> = {}): Promise<Observations> {
const { observations } = await this.contract.getContractState({
evaluationOptions,
});
return observations;
}
async getDistributions({
evaluationOptions,
}: EvaluationParameters = {}): Promise<EpochDistributionData> {
const { distributions } = await this.contract.getContractState({
evaluationOptions,
});
return distributions;
}
}
41 changes: 41 additions & 0 deletions tests/ar-io.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -191,4 +191,45 @@ describe('ArIO Client', () => {
}
},
);

it('should return observation information', async () => {
const observations = await arIO.getObservations();
const observation = await arIO.getObservations({
epochStartHeight: parseInt(Object.keys(observations)[0]),
});
expect(observations).toBeDefined();
expect(observation).toBeDefined();
});

it('should get the observation information at a given block height', async () => {
const observations = await arIO.getObservations({
evaluationOptions: { evalTo: { blockHeight: evaluateToBlockHeight } },
});
expect(observations).toBeDefined();
});

it('should return observations at a sortkey', async () => {
const observations = await arIO.getObservations({
evaluationOptions: { evalTo: { sortKey: evaluateToSortKey.toString() } },
});
expect(observations).toBeDefined();
});

it('should return distributions', async () => {
const distributions = await arIO.getDistributions();
expect(distributions).toBeDefined();
});

it('should return distributions at a blockheight', async () => {
const distributions = await arIO.getDistributions({
evaluationOptions: { evalTo: { blockHeight: evaluateToBlockHeight } },
});
expect(distributions).toBeDefined();
});
it('should return distributions at a sortkey', async () => {
const distributions = await arIO.getDistributions({
evaluationOptions: { evalTo: { sortKey: evaluateToSortKey.toString() } },
});
expect(distributions).toBeDefined();
});
});