-
Notifications
You must be signed in to change notification settings - Fork 150
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: add pool-assets endpoints (#1338)
* feat: add pool-assets endpoints * fix: made function private * Changed assets query param to an array & updated docs
- Loading branch information
Showing
25 changed files
with
1,291 additions
and
60 deletions.
There are no files selected for viewing
Large diffs are not rendered by default.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
121 changes: 121 additions & 0 deletions
121
src/controllers/accounts/AccountsPoolAssetsController.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,121 @@ | ||
// Copyright 2017-2023 Parity Technologies (UK) Ltd. | ||
// This file is part of Substrate API Sidecar. | ||
// | ||
// Substrate API Sidecar is free software: you can redistribute it and/or modify | ||
// it under the terms of the GNU General Public License as published by | ||
// the Free Software Foundation, either version 3 of the License, or | ||
// (at your option) any later version. | ||
// | ||
// This program is distributed in the hope that it will be useful, | ||
// but WITHOUT ANY WARRANTY; without even the implied warranty of | ||
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the | ||
// GNU General Public License for more details. | ||
// | ||
// You should have received a copy of the GNU General Public License | ||
// along with this program. If not, see <http://www.gnu.org/licenses/>. | ||
|
||
import { ApiPromise } from '@polkadot/api'; | ||
import { RequestHandler } from 'express'; | ||
import { BadRequest } from 'http-errors'; | ||
|
||
import { validateAddress } from '../../middleware'; | ||
import { AccountsPoolAssetsService } from '../../services/accounts'; | ||
import AbstractController from '../AbstractController'; | ||
|
||
/** | ||
* Get pool asset information for an address. | ||
* | ||
* Paths: | ||
* - `address`: The address to query | ||
* | ||
* Query: | ||
* - (Optional)`at`: Block at which to retrieve runtime version information at. Block | ||
* identifier, as the block height or block hash. Defaults to most recent block. | ||
* - (Optional for `/accounts/:address/pool-asset-balances`)`assets` | ||
* - (Required for `/accounts/:address/pool-asset-approvals)`assetId` The assetId associated | ||
* with the `AssetApproval`. | ||
* - (Required for `/accounts/:address/pool-asset-approvals)`delegate` The delegate associated | ||
* with the `ApprovalKey` which is tied to a `Approval`. The `ApprovalKey` consists | ||
* of an `owner` which is the `address` path parameter, and a `delegate`. | ||
* | ||
* `/accounts/:address/pool-asset-balances` | ||
* Returns: | ||
* - `at`: Block number and hash at which the call was made. | ||
* - `poolAssets`: An array of `AssetBalance` objects which have a AssetId attached to them | ||
* - `assetId`: The identifier of the asset. | ||
* - `balance`: The balance of the asset. | ||
* - `isFrozen`: Whether the pool asset is frozen for non-admin transfers. | ||
* - `isSufficient`: Whether a non-zero balance of this pool asset is a deposit of sufficient | ||
* value to account for the state bloat associated with its balance storage. If set to | ||
* `true`, then non-zero balances may be stored without a `consumer` reference (and thus | ||
* an ED in the Balances pallet or whatever else is used to control user-account state | ||
* growth). | ||
* | ||
* `/accounts/:address/pool-asset-approvals` | ||
* Returns: | ||
* - `at`: Block number and hash at which the call was made. | ||
* - `amount`: The amount of funds approved for the balance transfer from the owner | ||
* to some delegated target. | ||
* - `deposit`: The amount reserved on the owner's account to hold this item in storage. | ||
* | ||
* Substrate Reference: | ||
* - PoolAssets Pallet: instance of Assets Pallet https://crates.parity.io/pallet_assets/index.html | ||
* - `AssetBalance`: https://crates.parity.io/pallet_assets/struct.AssetBalance.html | ||
* - `ApprovalKey`: https://crates.parity.io/pallet_assets/struct.ApprovalKey.html | ||
* - `Approval`: https://crates.parity.io/pallet_assets/struct.Approval.html | ||
* | ||
*/ | ||
export default class AccountsPoolAssetsController extends AbstractController<AccountsPoolAssetsService> { | ||
constructor(api: ApiPromise) { | ||
super(api, '/accounts/:address', new AccountsPoolAssetsService(api)); | ||
this.initRoutes(); | ||
} | ||
|
||
protected initRoutes(): void { | ||
this.router.use(this.path, validateAddress); | ||
|
||
this.safeMountAsyncGetHandlers([ | ||
['/pool-asset-balances', this.getPoolAssetBalances], | ||
['/pool-asset-approvals', this.getPoolAssetApprovals], | ||
]); | ||
} | ||
|
||
private getPoolAssetBalances: RequestHandler = async ( | ||
{ params: { address }, query: { at, assets } }, | ||
res | ||
): Promise<void> => { | ||
const hash = await this.getHashFromAt(at); | ||
|
||
const assetsArray = Array.isArray(assets) | ||
? this.parseQueryParamArrayOrThrow(assets as string[]) | ||
: []; | ||
|
||
AccountsPoolAssetsController.sanitizedSend( | ||
res, | ||
await this.service.fetchPoolAssetBalances(hash, address, assetsArray) | ||
); | ||
}; | ||
|
||
private getPoolAssetApprovals: RequestHandler = async ( | ||
{ params: { address }, query: { at, delegate, assetId } }, | ||
res | ||
): Promise<void> => { | ||
const hash = await this.getHashFromAt(at); | ||
|
||
if (typeof delegate !== 'string' || typeof assetId !== 'string') { | ||
throw new BadRequest( | ||
'Must include a `delegate` and `assetId` query param' | ||
); | ||
} | ||
|
||
const id = this.parseNumberOrThrow( | ||
assetId, | ||
'`assetId` provided is not a number.' | ||
); | ||
|
||
AccountsPoolAssetsController.sanitizedSend( | ||
res, | ||
await this.service.fetchPoolAssetApprovals(hash, address, id, delegate) | ||
); | ||
}; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.