Skip to content

Commit afb3ba6

Browse files
authored
feat: add includeClaimable query param to vesting-info endpoints (#1860)
* Abstract relay chain logic into its own module * Abstract vesting calculation * Adjust types for accounts vesting * Refactor para inclusion service to use the new abstracted relay logic * update account vesting service * Update test response * Update controllers * Change unlockable to claimable * Update implementation to use locks * Update types * Updates test-helpers * Update service * linter * Update docs * update docs * Optimize for useRcBlock
1 parent 3bad62f commit afb3ba6

File tree

13 files changed

+1596
-120
lines changed

13 files changed

+1596
-120
lines changed

docs-v2/dist/bundle.js

Lines changed: 1 addition & 1 deletion
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

docs-v2/openapi-v1.yaml

Lines changed: 39 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@ info:
88
license:
99
name: GPL-3.0-or-later
1010
url: https://github.com/paritytech/substrate-api-sidecar/blob/master/LICENSE
11-
version: 20.11.0
11+
version: 20.12.0
1212
servers:
1313
- url: https://polkadot-public-sidecar.parity-chains.parity.io/
1414
description: Polkadot Parity public sidecar
@@ -742,6 +742,12 @@ paths:
742742
schema:
743743
type: boolean
744744
description: When set to 'true', uses relay chain block mode with the 'at' parameter.
745+
- name: includeClaimable
746+
in: query
747+
description: When set to 'true', calculates and includes vested amounts for each vesting schedule plus the claimable amount. For Asset Hub post-migration queries, this requires a relay chain connection since vesting schedules use relay chain block numbers.
748+
required: false
749+
schema:
750+
type: boolean
745751
responses:
746752
"200":
747753
description: successful operation
@@ -978,6 +984,12 @@ paths:
978984
description: Block height (as a non-negative integer) or hash
979985
(as a hex string).
980986
format: unsignedInteger or $hex
987+
- name: includeClaimable
988+
in: query
989+
description: When set to 'true', calculates and includes vested amounts for each vesting schedule plus the claimable amount. For relay chain pre-migration queries, vested amounts are calculated using the relay chain's own block number.
990+
required: false
991+
schema:
992+
type: boolean
981993
responses:
982994
"200":
983995
description: successful operation
@@ -5332,6 +5344,28 @@ components:
53325344
type: string
53335345
description: The Asset Hub block timestamp. Only present when `useRcBlock` parameter is used.
53345346
format: unsignedInteger
5347+
vestedBalance:
5348+
type: string
5349+
description: Total vested amount across all schedules based on time elapsed. Only present when `includeClaimable` parameter is used.
5350+
format: unsignedInteger
5351+
vestingTotal:
5352+
type: string
5353+
description: Total locked amount across all vesting schedules. Only present when `includeClaimable` parameter is used.
5354+
format: unsignedInteger
5355+
vestedClaimable:
5356+
type: string
5357+
description: Actual amount that can be claimed now (vestedBalance minus already claimed). Only present when `includeClaimable` parameter is used.
5358+
format: unsignedInteger
5359+
blockNumberForCalculation:
5360+
type: string
5361+
description: The block number used for vesting calculations. Only present when `includeClaimable` parameter is used.
5362+
format: unsignedInteger
5363+
blockNumberSource:
5364+
type: string
5365+
description: Which chain's block number was used for calculations ('relay' or 'self'). Only present when `includeClaimable` parameter is used.
5366+
enum:
5367+
- relay
5368+
- self
53355369
AddressDetails:
53365370
type: object
53375371
properties:
@@ -8125,6 +8159,10 @@ components:
81258159
type: string
81268160
description: Starting block for unlocking (vesting).
81278161
format: unsignedInteger
8162+
vested:
8163+
type: string
8164+
description: Amount that has vested based on time elapsed for this schedule. Only present when `includeClaimable` parameter is used.
8165+
format: unsignedInteger
81288166
description: Vesting schedule for an account.
81298167
WeightsV2:
81308168
type: object

docs/dist/app.bundle.js

Lines changed: 1 addition & 1 deletion
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

docs/src/openapi-v1.yaml

Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -742,6 +742,12 @@ paths:
742742
schema:
743743
type: boolean
744744
description: When set to 'true', uses relay chain block mode with the 'at' parameter.
745+
- name: includeClaimable
746+
in: query
747+
description: When set to 'true', calculates and includes vested amounts for each vesting schedule plus the claimable amount. For Asset Hub post-migration queries, this requires a relay chain connection since vesting schedules use relay chain block numbers.
748+
required: false
749+
schema:
750+
type: boolean
745751
responses:
746752
"200":
747753
description: successful operation
@@ -978,6 +984,12 @@ paths:
978984
description: Block height (as a non-negative integer) or hash
979985
(as a hex string).
980986
format: unsignedInteger or $hex
987+
- name: includeClaimable
988+
in: query
989+
description: When set to 'true', calculates and includes vested amounts for each vesting schedule plus the claimable amount. For relay chain pre-migration queries, vested amounts are calculated using the relay chain's own block number.
990+
required: false
991+
schema:
992+
type: boolean
981993
responses:
982994
"200":
983995
description: successful operation
@@ -5332,6 +5344,28 @@ components:
53325344
type: string
53335345
description: The Asset Hub block timestamp. Only present when `useRcBlock` parameter is used.
53345346
format: unsignedInteger
5347+
vestedBalance:
5348+
type: string
5349+
description: Total vested amount across all schedules based on time elapsed. Only present when `includeClaimable` parameter is used.
5350+
format: unsignedInteger
5351+
vestingTotal:
5352+
type: string
5353+
description: Total locked amount across all vesting schedules. Only present when `includeClaimable` parameter is used.
5354+
format: unsignedInteger
5355+
vestedClaimable:
5356+
type: string
5357+
description: Actual amount that can be claimed now (vestedBalance minus already claimed). Only present when `includeClaimable` parameter is used.
5358+
format: unsignedInteger
5359+
blockNumberForCalculation:
5360+
type: string
5361+
description: The block number used for vesting calculations. Only present when `includeClaimable` parameter is used.
5362+
format: unsignedInteger
5363+
blockNumberSource:
5364+
type: string
5365+
description: Which chain's block number was used for calculations ('relay' or 'self'). Only present when `includeClaimable` parameter is used.
5366+
enum:
5367+
- relay
5368+
- self
53355369
AddressDetails:
53365370
type: object
53375371
properties:
@@ -8125,6 +8159,10 @@ components:
81258159
type: string
81268160
description: Starting block for unlocking (vesting).
81278161
format: unsignedInteger
8162+
vested:
8163+
type: string
8164+
description: Amount that has vested based on time elapsed for this schedule. Only present when `includeClaimable` parameter is used.
8165+
format: unsignedInteger
81288166
description: Vesting schedule for an account.
81298167
WeightsV2:
81308168
type: object

src/controllers/accounts/AccountsVestingInfoController.ts

Lines changed: 30 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,7 @@
1414
// You should have received a copy of the GNU General Public License
1515
// along with this program. If not, see <http://www.gnu.org/licenses/>.
1616

17+
import BN from 'bn.js';
1718
import { RequestHandler } from 'express';
1819
import { IAddressParam } from 'src/types/requests';
1920

@@ -30,7 +31,12 @@ import AbstractController from '../AbstractController';
3031
* Query params:
3132
* - (Optional)`at`: Block at which to retrieve runtime version information at. Block
3233
* identifier, as the block height or block hash. Defaults to most recent block.
33-
* - (Optional)`useRcBlock`: When set to 'true', uses the relay chain block specified in the 'at' parameter to determine corresponding Asset Hub block(s). Only supported for Asset Hub endpoints.
34+
* - (Optional)`includeClaimable`: When set to 'true', calculates and includes vested
35+
* amounts for each vesting schedule plus the claimable amount. This may require a
36+
* relay chain connection for Asset Hub post-migration queries. Defaults to 'false'.
37+
* - (Optional)`useRcBlock`: When set to 'true', uses the relay chain block specified in the
38+
* 'at' parameter to determine corresponding Asset Hub block(s). Only supported for
39+
* Asset Hub endpoints.
3440
*
3541
* Returns:
3642
* - When using `useRcBlock` parameter: An array of response objects, one for each Asset Hub block found
@@ -39,13 +45,23 @@ import AbstractController from '../AbstractController';
3945
*
4046
* Response object structure:
4147
* - `at`: Block number and hash at which the call was made.
42-
* - `vesting`: Vesting schedule for an account.
48+
* - `vesting`: Array of vesting schedules for an account.
4349
* - `locked`: Number of tokens locked at start.
4450
* - `perBlock`: Number of tokens that gets unlocked every block after `startingBlock`.
4551
* - `startingBlock`: Starting block for unlocking(vesting).
46-
* - `rcBlockNumber`: The relay chain block number used for the query. Only present when `useRcBlock` parameter is used.
52+
* - `vested`: (Only when `includeClaimable=true`) Amount that has vested based on time elapsed.
53+
* - `vestedBalance`: (Only when `includeClaimable=true`) Total vested across all schedules.
54+
* - `vestingTotal`: (Only when `includeClaimable=true`) Total locked across all schedules.
55+
* - `vestedClaimable`: (Only when `includeClaimable=true`) Actual amount that can be claimed now.
56+
* - `blockNumberForCalculation`: (Only when `includeClaimable=true`) The block number used for calculations.
57+
* - `blockNumberSource`: (Only when `includeClaimable=true`) Which chain's block number was used ('relay' or 'self').
58+
* - `rcBlockHash`: The relay chain block hash. Only present when `useRcBlock` parameter is used.
59+
* - `rcBlockNumber`: The relay chain block number. Only present when `useRcBlock` parameter is used.
4760
* - `ahTimestamp`: The Asset Hub block timestamp. Only present when `useRcBlock` parameter is used.
4861
*
62+
* Note: When `includeClaimable=true` for Asset Hub post-migration queries, a relay chain
63+
* connection is required since vesting schedules use relay chain block numbers.
64+
*
4965
* Substrate Reference:
5066
* - Vesting Pallet: https://crates.parity.io/pallet_vesting/index.html
5167
* - `VestingInfo`: https://crates.parity.io/pallet_vesting/struct.VestingInfo.html
@@ -72,9 +88,11 @@ export default class AccountsVestingInfoController extends AbstractController<Ac
7288
* @param res Express Response
7389
*/
7490
private getAccountVestingInfo: RequestHandler<IAddressParam> = async (
75-
{ params: { address }, query: { at, useRcBlock } },
91+
{ params: { address }, query: { at, useRcBlock, includeClaimable } },
7692
res,
7793
): Promise<void> => {
94+
const shouldIncludeClaimable = includeClaimable === 'true';
95+
7896
if (useRcBlock === 'true') {
7997
const rcAtResults = await this.getHashFromRcAt(at);
8098

@@ -87,7 +105,13 @@ export default class AccountsVestingInfoController extends AbstractController<Ac
87105
// Process each Asset Hub block found
88106
const results = [];
89107
for (const { ahHash, rcBlockHash, rcBlockNumber } of rcAtResults) {
90-
const result = await this.service.fetchAccountVestingInfo(ahHash, address);
108+
// Pass rcBlockNumber to skip relay block search when includeClaimable is used
109+
const result = await this.service.fetchAccountVestingInfo(
110+
ahHash,
111+
address,
112+
shouldIncludeClaimable,
113+
new BN(rcBlockNumber),
114+
);
91115

92116
const apiAt = await this.api.at(ahHash);
93117
const ahTimestamp = await apiAt.query.timestamp.now();
@@ -105,7 +129,7 @@ export default class AccountsVestingInfoController extends AbstractController<Ac
105129
AccountsVestingInfoController.sanitizedSend(res, results);
106130
} else {
107131
const hash = await this.getHashFromAt(at);
108-
const result = await this.service.fetchAccountVestingInfo(hash, address);
132+
const result = await this.service.fetchAccountVestingInfo(hash, address, shouldIncludeClaimable);
109133
AccountsVestingInfoController.sanitizedSend(res, result);
110134
}
111135
};

src/controllers/rc/accounts/RcAccountsVestingInfoController.ts

Lines changed: 15 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -31,13 +31,24 @@ import AbstractController from '../../AbstractController';
3131
* Query params:
3232
* - (Optional)`at`: Block at which to retrieve vesting information at. Block
3333
* identifier, as the block height or block hash. Defaults to most recent block.
34+
* - (Optional)`includeClaimable`: When set to 'true', calculates and includes vested
35+
* amounts for each vesting schedule plus the claimable amount. Defaults to 'false'.
3436
*
3537
* Returns:
3638
* - `at`: Block number and hash at which the call was made.
37-
* - `vesting`: Vesting schedule for an account.
39+
* - `vesting`: Array of vesting schedules for an account.
3840
* - `locked`: Number of tokens locked at start.
3941
* - `perBlock`: Number of tokens that gets unlocked every block after `startingBlock`.
4042
* - `startingBlock`: Starting block for unlocking(vesting).
43+
* - `vested`: (Only when `includeClaimable=true`) Amount that has vested based on time elapsed.
44+
* - `vestedBalance`: (Only when `includeClaimable=true`) Total vested across all schedules.
45+
* - `vestingTotal`: (Only when `includeClaimable=true`) Total locked across all schedules.
46+
* - `vestedClaimable`: (Only when `includeClaimable=true`) Actual amount that can be claimed now.
47+
* - `blockNumberForCalculation`: (Only when `includeClaimable=true`) The block number used for calculations.
48+
* - `blockNumberSource`: (Only when `includeClaimable=true`) Which chain's block number was used ('relay' or 'self').
49+
*
50+
* Note: For relay chain pre-migration queries, vested amounts are calculated using the relay
51+
* chain's own block number. Post-migration, vesting has moved to Asset Hub.
4152
*
4253
* Substrate Reference:
4354
* - Vesting Pallet: https://crates.parity.io/pallet_vesting/index.html
@@ -70,7 +81,7 @@ export default class RcAccountsVestingInfoController extends AbstractController<
7081
* @param res Express Response
7182
*/
7283
private getAccountVestingInfo: RequestHandler<IAddressParam> = async (
73-
{ params: { address }, query: { at } },
84+
{ params: { address }, query: { at, includeClaimable } },
7485
res,
7586
): Promise<void> => {
7687
const rcApi = ApiPromiseRegistry.getApiByType('relay')[0]?.api;
@@ -79,8 +90,9 @@ export default class RcAccountsVestingInfoController extends AbstractController<
7990
throw new Error('Relay chain API not found, please use SAS_SUBSTRATE_MULTI_CHAIN_URL env variable');
8091
}
8192

93+
const shouldIncludeClaimable = includeClaimable === 'true';
8294
const hash = await this.getHashFromAt(at, { api: rcApi });
83-
const result = await this.service.fetchAccountVestingInfo(hash, address);
95+
const result = await this.service.fetchAccountVestingInfo(hash, address, shouldIncludeClaimable);
8496

8597
RcAccountsVestingInfoController.sanitizedSend(res, result);
8698
};

0 commit comments

Comments
 (0)