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(observers): add API for fetching prescribed observers #17

Merged
merged 1 commit into from
Mar 13, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
50 changes: 50 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -347,6 +347,56 @@ const records = arIO.getArNSRecords();
// }
```

### `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.

```typescript
const arIO = new ArIO();
const observers = arIO.getPrescribedObservers();

// outputs:

// [
// {
// "gatewayAddress": "BpQlyhREz4lNGS-y3rSS1WxADfxPpAuing9Lgfdrj2U",
// "observerAddress": "2Fk8lCmDegPg6jjprl57-UCpKmNgYiKwyhkU4vMNDnE",
// "stake": 10000,
// "start": 1296976,
// "stakeWeight": 1,
// "tenureWeight": 0.41453703703703704,
// "gatewayRewardRatioWeight": 1,
// "observerRewardRatioWeight": 1,
// "compositeWeight": 0.41453703703703704,
// "normalizedCompositeWeight": 0.0018972019546783507
// },
// ...
// ]

// observers from a previous epoch
const previousEpochObservers = arIO.getPrescribedObservers({
evaluationOptions: {
evalTo: { blockHeight: 1296975 }, // some block height from a previous epoch
},
});

// [
// {
// "gatewayAddress": "2Ic0ZIpt85tjiVRaD_qoTSo9jgT7w0rbf4puSTRidcU",
// "observerAddress": "2Ic0ZIpt85tjiVRaD_qoTSo9jgT7w0rbf4puSTRidcU",
// "stake": 10000,
// "start": 1292450,
// "stakeWeight": 1,
// "tenureWeight": 0.4494598765432099,
// "gatewayRewardRatioWeight": 1,
// "observerRewardRatioWeight": 1,
// "compositeWeight": 0.4494598765432099,
// "normalizedCompositeWeight": 0.002057032496835938
// },
// ...
// ]
```

## Developers

### Requirements
Expand Down
4 changes: 4 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,
WeightedObserver,
} from './contract-state.js';

export type BlockHeight = number;
Expand Down Expand Up @@ -87,6 +88,9 @@ export interface ArIOContract {
getCurrentEpoch({
evaluationOptions,
}: EvaluationParameters): Promise<EpochDistributionData>;
getPrescribedObservers({
evaluationOptions,
}: EvaluationParameters): Promise<WeightedObserver[]>;
}

/* eslint-disable @typescript-eslint/no-explicit-any */
Expand Down
13 changes: 13 additions & 0 deletions src/common/ar-io.ts
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@ import {
EvaluationParameters,
Gateway,
SmartWeaveContract,
WeightedObserver,
} from '../types.js';
import { RemoteContract } from './contracts/remote-contract.js';

Expand Down Expand Up @@ -183,4 +184,16 @@ export class ArIO implements ArIOContract {
evaluationOptions,
});
}

/**
* Returns the prescribed observers for the current epoch. If you are looking for prescribed observers for a past epoch, use `evaluationOptions: { blockHeight: <blockHeightDuringEpoch> }`.
*/
async getPrescribedObservers({
evaluationOptions,
}: EvaluationParameters = {}): Promise<WeightedObserver[]> {
return this.contract.readInteraction<never, WeightedObserver[]>({
functionName: 'prescribedObservers',
evaluationOptions,
});
}
}
42 changes: 42 additions & 0 deletions tests/ar-io.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -149,4 +149,46 @@ describe('ArIO Client', () => {
expect(epoch.epochPeriod).toBeDefined();
expect(epoch.epochZeroStartHeight).toBeDefined();
});

it('should return the prescribed observers for the current epoch', async () => {
const observers = await arIO.getPrescribedObservers();
expect(observers).toBeDefined();
for (const observer of observers) {
expect(observer.gatewayAddress).toBeDefined();
expect(observer.observerAddress).toBeDefined();
expect(observer.stake).toBeDefined();
expect(observer.start).toBeDefined();
expect(observer.stakeWeight).toBeDefined();
expect(observer.tenureWeight).toBeDefined();
expect(observer.gatewayRewardRatioWeight).toBeDefined();
expect(observer.observerRewardRatioWeight).toBeDefined();
expect(observer.compositeWeight).toBeDefined();
expect(observer.normalizedCompositeWeight).toBeDefined();
}
});

it.each([
[{ sortKey: evaluateToSortKey.toString() }],
[{ blockHeight: evaluateToBlockHeight }],
])(
Comment on lines +170 to +173
Copy link
Collaborator Author

Choose a reason for hiding this comment

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

@atticusofsparta a quick way to test various evaluation options

`should return the prescribed observers for provided evaluation options: ${JSON.stringify('%s')}`,
async (evalTo) => {
const observers = await arIO.getPrescribedObservers({
evaluationOptions: { evalTo },
});
expect(observers).toBeDefined();
for (const observer of observers) {
expect(observer.gatewayAddress).toBeDefined();
expect(observer.observerAddress).toBeDefined();
expect(observer.stake).toBeDefined();
expect(observer.start).toBeDefined();
expect(observer.stakeWeight).toBeDefined();
expect(observer.tenureWeight).toBeDefined();
expect(observer.gatewayRewardRatioWeight).toBeDefined();
expect(observer.observerRewardRatioWeight).toBeDefined();
expect(observer.compositeWeight).toBeDefined();
expect(observer.normalizedCompositeWeight).toBeDefined();
}
},
);
});