Skip to content

Commit b36307a

Browse files
committed
pool+batches: fetch lease durations into batchStore
1 parent 301a840 commit b36307a

File tree

4 files changed

+90
-10
lines changed

4 files changed

+90
-10
lines changed

app/src/__tests__/store/batchStore.spec.ts

Lines changed: 40 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,11 @@ import { waitFor } from '@testing-library/react';
55
import * as config from 'config';
66
import { hex } from 'util/strings';
77
import { injectIntoGrpcUnary } from 'util/tests';
8-
import { poolBatchSnapshot, sampleApiResponses } from 'util/tests/sampleData';
8+
import {
9+
poolBatchSnapshot,
10+
poolLeaseDurations,
11+
sampleApiResponses,
12+
} from 'util/tests/sampleData';
913
import { BatchStore, createStore, Store } from 'store';
1014
import { BATCH_QUERY_LIMIT } from 'store/stores/batchStore';
1115

@@ -23,15 +27,20 @@ describe('BatchStore', () => {
2327
// mock the BatchSnapshot response to return a unique id for each batch
2428
// to avoid overwriting the same record in the store
2529
index = 0;
26-
grpcMock.unary.mockImplementation((_, opts) => {
27-
const res = {
28-
batchesList: [...Array(20)].map((_, i) => ({
29-
...poolBatchSnapshot,
30-
batchId: `${index + i}-${poolBatchSnapshot.batchId}`,
31-
prevBatchId: `${index + i}-${poolBatchSnapshot.prevBatchId}`,
32-
})),
33-
};
34-
index += BATCH_QUERY_LIMIT;
30+
grpcMock.unary.mockImplementation((desc, opts) => {
31+
let res: any;
32+
if (desc.methodName === 'BatchSnapshots') {
33+
res = {
34+
batchesList: [...Array(20)].map((_, i) => ({
35+
...poolBatchSnapshot,
36+
batchId: `${index + i}-${poolBatchSnapshot.batchId}`,
37+
prevBatchId: `${index + i}-${poolBatchSnapshot.prevBatchId}`,
38+
})),
39+
};
40+
index += BATCH_QUERY_LIMIT;
41+
} else if (desc.methodName === 'LeaseDurations') {
42+
res = poolLeaseDurations;
43+
}
3544
opts.onEnd({
3645
status: grpc.Code.OK,
3746
message: { toObject: () => res },
@@ -149,6 +158,27 @@ describe('BatchStore', () => {
149158
);
150159
});
151160

161+
it('should fetch lease durations', async () => {
162+
expect(store.leaseDurations.size).toBe(0);
163+
await store.fetchLeaseDurations();
164+
expect(store.leaseDurations.size).toBe(
165+
poolLeaseDurations.leaseDurationBucketsMap.length,
166+
);
167+
expect(store.selectedLeaseDuration).toBe(
168+
poolLeaseDurations.leaseDurationBucketsMap[0][0],
169+
);
170+
});
171+
172+
it('should handle errors when fetching lease durations', async () => {
173+
grpcMock.unary.mockImplementationOnce(() => {
174+
throw new Error('test-err');
175+
});
176+
expect(rootStore.appView.alerts.size).toBe(0);
177+
await store.fetchLeaseDurations();
178+
expect(rootStore.appView.alerts.size).toBe(1);
179+
expect(values(rootStore.appView.alerts)[0].message).toBe('test-err');
180+
});
181+
152182
it('should fetch node tier', async () => {
153183
// return sample data from gRPC requests instead of the batches defined in beforeEach()
154184
grpcMock.unary.mockImplementation((desc, opts) => {

app/src/api/pool.ts

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -261,6 +261,15 @@ class PoolApi extends BaseApi<PoolEvents> {
261261
return res.toObject();
262262
}
263263

264+
/**
265+
* call the pool `LeaseDurations` RPC and return the response
266+
*/
267+
async leaseDurations(): Promise<POOL.LeaseDurationResponse.AsObject> {
268+
const req = new POOL.LeaseDurationRequest();
269+
const res = await this._grpc.request(Trader.LeaseDurations, req, this._meta);
270+
return res.toObject();
271+
}
272+
264273
//
265274
// Utility functions to convert user-facing units to API units
266275
//

app/src/store/stores/batchStore.ts

Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,12 @@ export default class BatchStore {
2323
private _pollingInterval?: NodeJS.Timeout;
2424
private _nextBatchTimer?: NodeJS.Timeout;
2525

26+
/** the selected lease duration */
27+
selectedLeaseDuration = 0;
28+
29+
/** the collection of lease durations as a mapping from duration (blocks) to DurationBucketState */
30+
leaseDurations: ObservableMap<number, number> = observable.map();
31+
2632
/** the collection of batches */
2733
batches: ObservableMap<string, Batch> = observable.map();
2834

@@ -185,6 +191,30 @@ export default class BatchStore {
185191
}
186192
}
187193

194+
/**
195+
* fetches the list of lease durations from the API
196+
*/
197+
async fetchLeaseDurations() {
198+
this._store.log.info('fetching lease durations');
199+
try {
200+
const res = await this._store.api.pool.leaseDurations();
201+
runInAction(() => {
202+
res.leaseDurationBucketsMap.forEach(([duration, state]) => {
203+
this.leaseDurations.set(duration, state);
204+
205+
// set the active lease duration if it is not set
206+
if (!this.selectedLeaseDuration) this.selectedLeaseDuration = duration;
207+
});
208+
this._store.log.info(
209+
'updated batchStore.leaseDurations',
210+
toJS(this.leaseDurations),
211+
);
212+
});
213+
} catch (error) {
214+
this._store.appView.handleError(error, 'Unable to fetch lease durations');
215+
}
216+
}
217+
188218
/**
189219
* sets the nextBatchTimestamp and creates a timer to fetch the latest batch, which
190220
* will trigger 3 seconds after the next batch timestamp to allow some time for the

app/src/util/tests/sampleData.ts

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -643,6 +643,16 @@ export const poolBatchSnapshots: AUCT.BatchSnapshotsResponse.AsObject = {
643643
})),
644644
};
645645

646+
export const poolLeaseDurations: POOL.LeaseDurationResponse.AsObject = {
647+
leaseDurationsMap: [], // deprecated
648+
leaseDurationBucketsMap: [
649+
[2016, AUCT.DurationBucketState.MARKET_OPEN],
650+
[4032, AUCT.DurationBucketState.MARKET_CLOSED],
651+
[6048, AUCT.DurationBucketState.ACCEPTING_ORDERS],
652+
[8064, AUCT.DurationBucketState.NO_MARKET],
653+
],
654+
};
655+
646656
export const poolNextBatchInfo: POOL.NextBatchInfoResponse.AsObject = {
647657
clearTimestamp: 1605936138,
648658
confTarget: 6,
@@ -772,6 +782,7 @@ export const sampleApiResponses: Record<string, any> = {
772782
'poolrpc.Trader.CancelOrder': poolCancelOrder,
773783
'poolrpc.Trader.BatchSnapshot': poolBatchSnapshot,
774784
'poolrpc.Trader.BatchSnapshots': poolBatchSnapshots,
785+
'poolrpc.Trader.LeaseDurations': poolLeaseDurations,
775786
'poolrpc.Trader.NextBatchInfo': poolNextBatchInfo,
776787
'poolrpc.Trader.NodeRatings': poolNodeRatings,
777788
'poolrpc.Trader.Leases': poolLeases,

0 commit comments

Comments
 (0)