-
Notifications
You must be signed in to change notification settings - Fork 151
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
feat: add pool-assets endpoints #1338
Changes from 1 commit
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Large diffs are not rendered by default.
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,123 @@ | ||
// 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 assetsSplit = (assets as string)?.split(',') || []; | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. So my only concern with this is it's not following Express convention of dealing with query params as an array, and will add two different ways to within sidecar to add the query param. I think for this we should stick to the conventional method of using expresses built in format. |
||
|
||
const assetsArray = Array.isArray(assetsSplit) | ||
? this.parseQueryParamArrayOrThrow(assetsSplit) | ||
: []; | ||
|
||
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) | ||
); | ||
}; | ||
} |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Similar to the comment about changing the query param structure.