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-5800): epoch apis #15

Merged
merged 4 commits into from
Mar 12, 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
1 change: 1 addition & 0 deletions .prettierignore
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
CHANGELOG.md
9 changes: 9 additions & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,15 @@
"email": "info@ar.io",
"website": "https://ar.io"
},
"keywords": [
"arweave",
"ar",
"blockchain",
"warp",
"contracts",
"ar.io",
"ao"
],
"exports": {
".": {
"import": "./lib/esm/node/index.js",
Expand Down
16 changes: 15 additions & 1 deletion src/common.ts
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,12 @@
* You should have received a copy of the GNU Affero General Public License
* along with this program. If not, see <http://www.gnu.org/licenses/>.
*/
import { ArIOState, ArNSNameData, Gateway } from './contract-state.js';
import {
ArIOState,
ArNSNameData,
EpochDistributionData,
Gateway,
} from './contract-state.js';

export type BlockHeight = number;
export type SortKey = string;
Expand Down Expand Up @@ -73,6 +78,15 @@ export interface ArIOContract {
}: EvaluationParameters): Promise<
Record<string, ArNSNameData> | Record<string, never>
>;
getEpoch({
blockHeight,
evaluationOptions,
}: {
blockHeight: number;
} & EvaluationParameters): Promise<EpochDistributionData>;
getCurrentEpoch({
evaluationOptions,
}: EvaluationParameters): Promise<EpochDistributionData>;
}

/* eslint-disable @typescript-eslint/no-explicit-any */
Expand Down
34 changes: 34 additions & 0 deletions src/common/ar-io.ts
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@ import {
ArIOContract,
ArIOState,
ArNSNameData,
EpochDistributionData,
EvaluationParameters,
Gateway,
SmartWeaveContract,
Expand Down Expand Up @@ -149,4 +150,37 @@ export class ArIO implements ArIOContract {
evaluationOptions,
});
}

/**
* Returns the current epoch.
*/
async getCurrentEpoch({
evaluationOptions,
}: EvaluationParameters = {}): Promise<EpochDistributionData> {
return this.contract.readInteraction({
functionName: 'epoch',
evaluationOptions,
});
}

/**
* Returns the epoch information for the provided block height.
*/
async getEpoch({
blockHeight,
evaluationOptions,
}: {
blockHeight: number;
} & EvaluationParameters): Promise<EpochDistributionData> {
return this.contract.readInteraction<
{ height: number },
EpochDistributionData
>({
functionName: 'epoch',
inputs: {
height: blockHeight,
},
evaluationOptions,
});
}
}
3 changes: 2 additions & 1 deletion src/contract-state.ts
Original file line number Diff line number Diff line change
Expand Up @@ -156,7 +156,8 @@ export type EpochDistributionData = {
epochStartHeight: number; // the current epoch start height
epochEndHeight: number; // the current epoch end height
epochPeriod: number;
nextDistributionHeight: number;
epochDistributionHeight: number;
epochBlockLength: number;
};

export type Vaults = Record<string, VaultData>;
Expand Down
37 changes: 37 additions & 0 deletions tests/ar-io.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -112,4 +112,41 @@ describe('ArIO Client', () => {
});
expect(records[domain]).toBeDefined();
});

it('should return the current epoch information', async () => {
const epoch = await arIO.getCurrentEpoch();
expect(epoch).toBeDefined();
expect(epoch.epochStartHeight).toBeDefined();
expect(epoch.epochBlockLength).toBeDefined();
expect(epoch.epochDistributionHeight).toBeDefined();
expect(epoch.epochEndHeight).toBeDefined();
expect(epoch.epochPeriod).toBeDefined();
expect(epoch.epochZeroStartHeight).toBeDefined();
});

it('should return the current epoch information when evaluated at a given block height', async () => {
const epoch = await arIO.getCurrentEpoch({
evaluationOptions: { evalTo: { blockHeight: evaluateToBlockHeight } },
});
expect(epoch).toBeDefined();
expect(epoch.epochStartHeight).toBeDefined();
expect(epoch.epochBlockLength).toBeDefined();
expect(epoch.epochDistributionHeight).toBeDefined();
expect(epoch.epochEndHeight).toBeDefined();
expect(epoch.epochPeriod).toBeDefined();
expect(epoch.epochZeroStartHeight).toBeDefined();
});

it('should return the epoch information at a given block height', async () => {
const epoch = await arIO.getEpoch({
blockHeight: evaluateToBlockHeight,
});
expect(epoch).toBeDefined();
expect(epoch.epochStartHeight).toBeDefined();
expect(epoch.epochBlockLength).toBeDefined();
expect(epoch.epochDistributionHeight).toBeDefined();
expect(epoch.epochEndHeight).toBeDefined();
expect(epoch.epochPeriod).toBeDefined();
expect(epoch.epochZeroStartHeight).toBeDefined();
});
});