Skip to content

Commit

Permalink
fix!: change the response for vesting-info (#717)
Browse files Browse the repository at this point in the history
* BREAKING CHANGE: default return value is an array instead of an object

* add docs

* fix: vesting-info tests, add another test as well

* docs

* lint

* add test

* lint

* refactor to avoid duplicate unwrap calls
  • Loading branch information
TarikGul committed Oct 14, 2021
1 parent 60f5abb commit 8b9866d
Show file tree
Hide file tree
Showing 5 changed files with 92 additions and 9 deletions.
2 changes: 1 addition & 1 deletion docs/dist/app.bundle.js

Large diffs are not rendered by default.

8 changes: 7 additions & 1 deletion docs/src/openapi-v1.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -1396,11 +1396,17 @@ components:
$ref: '#/components/schemas/Payouts'
AccountVestingInfo:
type: object
description: Sidecar version's <= v10.0.0 have a`vesting` return value that defaults to an object for
when there is no available vesting-info data. It also returns a `VestingInfo` as an object.
For Sidecar >=11.0.0, that value will now default as an array when there is no value, and `Vec<PalletsPalletVestingInfo>`
is returned when there is.
properties:
at:
$ref: '#/components/schemas/BlockIdentifiers'
vesting:
$ref: '#/components/schemas/VestingSchedule'
type: array
items:
$ref: '#/components/schemas/VestingSchedule'
AssetsBalance:
type: object
properties:
Expand Down
68 changes: 66 additions & 2 deletions src/services/accounts/AccountsVestingInfoService.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,12 @@ import { ApiPromise } from '@polkadot/api';
import { Hash } from '@polkadot/types/interfaces';

import { sanitizeNumbers } from '../../sanitize/sanitizeNumbers';
import { polkadotMetadataRpcV9110 } from '../../test-helpers/metadata/polkadotV9110Metadata';
import { polkadotRegistry } from '../../test-helpers/registries';
import {
createApiWithAugmentations,
TypeFactory,
} from '../../test-helpers/typeFactory';
import {
blockHash789629,
defaultMockApi,
Expand All @@ -11,9 +16,29 @@ import {
import response789629 from '../test-helpers/responses/accounts/vestingInfo789629.json';
import { AccountsVestingInfoService } from './AccountsVestingInfoService';

const typeFactorApiV9110 = createApiWithAugmentations(polkadotMetadataRpcV9110);
const factory = new TypeFactory(typeFactorApiV9110);

const vestingRes = {
locked: '1749990000000000',
perBlock: '166475460',
startingBlock: '4961000',
};

const vestingAt = (_hash: Hash, _address: string) =>
Promise.resolve().then(() => {
const vestingInfo = typeFactorApiV9110.createType(
'PalletVestingVestingInfo',
vestingRes
);
const vecVestingInfo = factory.vecOf([vestingInfo]);

return factory.optionOf(vecVestingInfo);
});

const historicVestingAt = (_hash: Hash, _address: string) =>
Promise.resolve().then(() =>
polkadotRegistry.createType('Option<VestingInfo>', null)
polkadotRegistry.createType('Option<VestingInfo>', vestingRes)
);

const mockApi = {
Expand All @@ -29,7 +54,45 @@ const accountsVestingInfoService = new AccountsVestingInfoService(mockApi);

describe('AccountVestingInfoService', () => {
describe('fetchAccountVestingInfo', () => {
it('works when ApiPromise works (block 789629)', async () => {
it('works when ApiPromise works (block 789629) with V14 metadata', async () => {
expect(
sanitizeNumbers(
await accountsVestingInfoService.fetchAccountVestingInfo(
blockHash789629,
testAddress
)
)
).toStrictEqual(response789629);
});

it('Vesting should return an empty array for None responses', async () => {
const tempVest = () =>
Promise.resolve().then(() =>
polkadotRegistry.createType('Option<VestingInfo>', null)
);
(mockApi.query.vesting.vesting.at as unknown) = tempVest;

const expectedResponse = {
at: {
hash: '0x7b713de604a99857f6c25eacc115a4f28d2611a23d9ddff99ab0e4f1c17a8578',
height: '789629',
},
vesting: [],
};

expect(
sanitizeNumbers(
await accountsVestingInfoService.fetchAccountVestingInfo(
blockHash789629,
testAddress
)
)
).toStrictEqual(expectedResponse);
(mockApi.query.vesting.vesting.at as unknown) = vestingAt;
});

it('Should correctly adjust `Option<VestingInfo>` for pre V14 blocks to return an array', async () => {
(mockApi.query.vesting.vesting.at as unknown) = historicVestingAt;
expect(
sanitizeNumbers(
await accountsVestingInfoService.fetchAccountVestingInfo(
Expand All @@ -38,6 +101,7 @@ describe('AccountVestingInfoService', () => {
)
)
).toStrictEqual(response789629);
(mockApi.query.vesting.vesting.at as unknown) = vestingAt;
});
});
});
17 changes: 13 additions & 4 deletions src/services/accounts/AccountsVestingInfoService.ts
Original file line number Diff line number Diff line change
Expand Up @@ -26,9 +26,18 @@ export class AccountsVestingInfoService extends AbstractService {
height: number.unwrap().toString(10),
};

return {
at,
vesting: vesting.isNone ? {} : vesting.unwrap(),
};
if (vesting.isNone) {
return {
at,
vesting: [],
};
} else {
const unwrapVesting = vesting.unwrap();

return {
at,
vesting: Array.isArray(unwrapVesting) ? unwrapVesting : [unwrapVesting],
};
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -3,5 +3,9 @@
"hash": "0x7b713de604a99857f6c25eacc115a4f28d2611a23d9ddff99ab0e4f1c17a8578",
"height": "789629"
},
"vesting": {}
"vesting": [{
"locked": "1749990000000000",
"perBlock": "166475460",
"startingBlock": "4961000"
}]
}

0 comments on commit 8b9866d

Please sign in to comment.