Skip to content

Commit

Permalink
fix(ParasService): adjust endpoint to use historicApi, fix endingOffs…
Browse files Browse the repository at this point in the history
…et bug (#735)

* add historicApi to crowdloans-info

* add assertQueryModule check

* add historicApi to crowdloans

* add historicApi to leases-info and leases/current

* add historicApi to auctions-current

* add historicApi to paras

* fix endingOffset bug

* adjust tests to reflect historicApi

* cleanup

* inline docs

* lint

* Update src/services/paras/ParasService.ts

Co-authored-by: Zeke Mostov <32168567+emostov@users.noreply.github.com>

* adjust lease_period_index

* lease period index edgecase

* adjust leasePeriodIndex to be null not BN_ZERO

* update docs

Co-authored-by: Zeke Mostov <32168567+emostov@users.noreply.github.com>
  • Loading branch information
TarikGul and emostov committed Nov 10, 2021
1 parent 42ae857 commit ce2ff0b
Show file tree
Hide file tree
Showing 5 changed files with 167 additions and 98 deletions.
2 changes: 1 addition & 1 deletion docs/dist/app.bundle.js

Large diffs are not rendered by default.

6 changes: 4 additions & 2 deletions docs/src/openapi-v1.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -2218,11 +2218,13 @@ components:
leasePeriodIndex:
type: string
format: unsignedInteger
description: Current lease period index.
description: Current lease period index. This value may be null when the current block now,
substracted by the leaseOffset is less then zero.
endOfLeasePeriod:
type: string
format: unsignedInteger
description: Last block (number) of the current lease period.
description: Last block (number) of the current lease period. This value may be null when
`leasePeriodIndex` is null.
currentLeaseHolders:
type: array
items:
Expand Down
83 changes: 46 additions & 37 deletions src/services/paras/ParasService.spec.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,8 @@
import { ApiPromise } from '@polkadot/api';
import { ApiDecoration } from '@polkadot/api/types';
import { Option, Tuple, Vec } from '@polkadot/types';
import { AbstractInt } from '@polkadot/types/codec/AbstractInt';
import { BlockNumber } from '@polkadot/types/interfaces';
import { BlockNumber, Hash } from '@polkadot/types/interfaces';
import { Codec } from '@polkadot/types/types';
import BN from 'bn.js';

Expand Down Expand Up @@ -252,8 +253,7 @@ const auctionsWinningsAt = () =>
return optionWinnings;
});

const mockApi = {
...defaultMockApi,
const historicApi = {
consts: {
auctions: {
endingPeriod: new BN(20000),
Expand All @@ -266,42 +266,49 @@ const mockApi = {
},
query: {
auctions: {
auctionInfo: {
at: auctionsInfoAt,
},
auctionCounter: {
at: auctionCounterAt,
},
winning: {
at: auctionsWinningsAt,
},
auctionInfo: auctionsInfoAt,
auctionCounter: auctionCounterAt,
winning: auctionsWinningsAt,
},
crowdloan: {
funds: {
entriesAt: fundsEntries,
keys: fundsKeys,
at: fundsAt,
},
funds: fundsAt,
},
paras: {
paraLifecycles: {
entriesAt: parasLifecyclesEntriesAt,
at: parasLifecyclesAt,
},
paraGenesisArgs: {
at: parasGenesisArgsAt,
},
upcomingParasGenesis: {
at: upcomingParasGenesisAt,
},
paraLifecycles: parasLifecyclesAt,
paraGenesisArgs: parasGenesisArgsAt,
upcomingParasGenesis: upcomingParasGenesisAt,
},
slots: {
leases: {
entriesAt: slotsLeasesEntriesAt,
at: slotsLeasesAt,
},
leases: slotsLeasesAt,
},
},
} as unknown as ApiDecoration<'promise'>;

/**
* Assign necessary keys to crowdloan.funds
*/
Object.assign(historicApi.query.crowdloan.funds, {
entries: fundsEntries,
keys: fundsKeys,
});

/**
* Assign necessary keys to paras.paraLifecycles
*/
Object.assign(historicApi.query.paras.paraLifecycles, {
entries: parasLifecyclesEntriesAt,
});

/**
* Assign necessary keys to slots.leases
*/
Object.assign(historicApi.query.slots.leases, {
entries: slotsLeasesEntriesAt,
});

const mockApi = {
...defaultMockApi,
at: (_hash: Hash) => historicApi,
} as unknown as ApiPromise;

const parasService = new ParasService(mockApi);
Expand Down Expand Up @@ -393,7 +400,7 @@ describe('ParasService', () => {
const emptyLeasesAt = () =>
Promise.resolve().then(() => emptyVectorLeases);

(mockApi.query.slots.leases.at as unknown) = emptyLeasesAt;
(historicApi.query.slots.leases as unknown) = emptyLeasesAt;

const expectedResponse = {
at: expectedAt,
Expand All @@ -406,7 +413,7 @@ describe('ParasService', () => {

expect(sanitizeNumbers(response)).toMatchObject(expectedResponse);

(mockApi.query.slots.leases.at as unknown) = slotsLeasesAt;
(historicApi.query.slots.leases as unknown) = slotsLeasesAt;
});
});

Expand Down Expand Up @@ -464,8 +471,10 @@ describe('ParasService', () => {
describe('ParasService.auctionsCurrent', () => {
it('Should return the correct data during an ongoing auction', async () => {
const leasePeriodIndex = new BN(39);
const leaseIndexArray =
parasService['enumerateLeaseSets'](leasePeriodIndex);
const leaseIndexArray = parasService['enumerateLeaseSets'](
historicApi,
leasePeriodIndex
);
// Remove the first two entries with splice because we have them in the expectedResponse.
// `LEASE_PERIODS_PER_SLOT_FALLBACK` is 4 we need 10 slots for winning.
const additionalWinningOptions = leaseIndexArray
Expand Down Expand Up @@ -541,7 +550,7 @@ describe('ParasService', () => {
});

it('Should return the correct null values when `auctionInfo` is `None`', async () => {
(mockApi.query.auctions.auctionInfo.at as unknown) = noneAuctionsInfoAt;
(historicApi.query.auctions.auctionInfo as unknown) = noneAuctionsInfoAt;

const expectedResponse = {
at: expectedAt,
Expand All @@ -557,7 +566,7 @@ describe('ParasService', () => {

expect(sanitizeNumbers(response)).toMatchObject(expectedResponse);

(mockApi.query.auctions.auctionInfo.at as unknown) = auctionsInfoAt;
(historicApi.query.auctions.auctionInfo as unknown) = auctionsInfoAt;
});
});
});
Loading

0 comments on commit ce2ff0b

Please sign in to comment.