Skip to content
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: assets endpoints #533

Merged
merged 80 commits into from
May 11, 2021
Merged
Show file tree
Hide file tree
Changes from 29 commits
Commits
Show all changes
80 commits
Select commit Hold shift + click to select a range
526fd1c
feat: assets endpoint
TarikGul Apr 30, 2021
007af5e
lint, add controller configs
TarikGul Apr 30, 2021
6d6c8c2
types | account assets boilerplate
TarikGul Apr 30, 2021
3d2e0db
asset-balancese endpoint
TarikGul May 3, 2021
b58d069
asset-approvals
TarikGul May 4, 2021
cb5c8ee
assets endpoint tests
TarikGul May 4, 2021
69d74f8
asset-balance tests
TarikGul May 4, 2021
0b8ddce
remove consoles
TarikGul May 4, 2021
5de8175
fix: asset-balances response and tests
TarikGul May 4, 2021
ccd94a6
asset-approval test and cleanup
TarikGul May 4, 2021
26de3ee
lint
TarikGul May 4, 2021
4a1504b
add accountAssets to exports
TarikGul May 4, 2021
36561ad
add comment docs
TarikGul May 4, 2021
bdf4a61
lint
TarikGul May 4, 2021
cc01fe9
fix: mockApi `assetsAccount`
TarikGul May 5, 2021
3774082
add refactor storageKeyDoubleMaap inside TypeFactory
TarikGul May 5, 2021
1f3c1c7
Update src/controllers/accounts/AccountsAssetsController.ts
TarikGul May 5, 2021
a19a4ed
Update src/services/accounts/AccountsAssetsService.ts
TarikGul May 5, 2021
4ab745b
Update src/controllers/accounts/AccountsAssetsController.ts
TarikGul May 5, 2021
429cc72
Update src/services/assets/AssetsService.ts
TarikGul May 5, 2021
c1c3ef5
Update src/controllers/assets/AssetsController.ts
TarikGul May 5, 2021
9f77ce1
create queryAssets
TarikGul May 5, 2021
1ce18a0
Merge branch 'tarik-assets-endpoint' of github.com:paritytech/substra…
TarikGul May 5, 2021
c9ba59b
type docs, IAccountAssetsBalances
TarikGul May 5, 2021
c2d608e
fix error message
TarikGul May 5, 2021
c0c5e50
remove sorting assetIds
TarikGul May 5, 2021
88879ec
add docs for queryAssets
TarikGul May 5, 2021
a762b2b
lint
TarikGul May 5, 2021
ed6674c
Update src/services/accounts/AccountsAssetsService.ts
TarikGul May 6, 2021
8f56a70
Update src/services/accounts/AccountsAssetsService.spec.ts
TarikGul May 6, 2021
a84c180
Update src/controllers/assets/AssetsController.ts
TarikGul May 6, 2021
8aabbd8
lint
TarikGul May 6, 2021
ff5565a
Update src/controllers/accounts/AccountsAssetsController.ts
TarikGul May 6, 2021
4440118
Update src/controllers/accounts/AccountsAssetsController.ts
TarikGul May 6, 2021
6569eef
fix parsing for query parameters
TarikGul May 6, 2021
eefa3b1
lint
TarikGul May 6, 2021
ef6a40f
lint
TarikGul May 6, 2021
3e7fc89
Merge branch 'master' of github.com:paritytech/substrate-api-sidecar …
TarikGul May 7, 2021
a29e3f7
change index naming
TarikGul May 7, 2021
331a9be
fix storageKeyDoubleMap naming
TarikGul May 7, 2021
3e51866
lint
TarikGul May 7, 2021
3a84fcd
fix docs
TarikGul May 7, 2021
5f4103c
comments
TarikGul May 7, 2021
fd73394
fix: get keys from singleMap
TarikGul May 7, 2021
6676fd5
refactor queryAllAssets
TarikGul May 7, 2021
1559951
Merge branch 'master' of github.com:paritytech/substrate-api-sidecar …
TarikGul May 7, 2021
81a7f0e
add assets to kilt
TarikGul May 7, 2021
e64a480
docs: AccountsAssetController
TarikGul May 8, 2021
f4adf64
lint
TarikGul May 8, 2021
ac2a6df
docs: AssetsController
TarikGul May 8, 2021
05e2535
lint
TarikGul May 9, 2021
3c3d855
fix routes
TarikGul May 9, 2021
9dc4e82
add error handling to asset-approval
TarikGul May 9, 2021
e52266a
isEmpty to isNone
TarikGul May 9, 2021
584f9ae
fix: change /assets to /pallets/assets
TarikGul May 10, 2021
4f320bd
fix(/accounts/:address/asset-approvals): remove 400 badrequest, repla…
TarikGul May 10, 2021
d07ddbd
fix: change isNone to isSome
TarikGul May 10, 2021
46b5a1e
docs: udpate API docs
TarikGul May 10, 2021
f6fb250
fix indenting
TarikGul May 10, 2021
2da2b29
Update docs/src/openapi-v1.yaml
TarikGul May 11, 2021
0f7ecc4
Update docs/src/openapi-v1.yaml
TarikGul May 11, 2021
df23bf3
Update docs/src/openapi-v1.yaml
TarikGul May 11, 2021
90985fb
Update docs/src/openapi-v1.yaml
TarikGul May 11, 2021
9194d38
Update docs/src/openapi-v1.yaml
TarikGul May 11, 2021
ee571b7
Update docs/src/openapi-v1.yaml
TarikGul May 11, 2021
6b415dd
Update docs/src/openapi-v1.yaml
TarikGul May 11, 2021
e28417d
Update src/services/accounts/AccountsAssetsService.ts
TarikGul May 11, 2021
4be6088
Update src/services/accounts/AccountsAssetsService.ts
TarikGul May 11, 2021
225313c
Update src/types/responses/AccountAssets.ts
TarikGul May 11, 2021
ed40309
Update src/services/accounts/AccountsAssetsService.ts
TarikGul May 11, 2021
d4670f3
Update src/services/accounts/AccountsAssetsService.ts
TarikGul May 11, 2021
78bc6db
Update src/services/accounts/AccountsAssetsService.ts
TarikGul May 11, 2021
fb05b14
Update docs/src/openapi-v1.yaml
TarikGul May 11, 2021
3e39f61
Update docs/src/openapi-v1.yaml
TarikGul May 11, 2021
0b4ca9b
update docs
TarikGul May 11, 2021
45ff124
fix parseQueryParamArrayOrThrow
TarikGul May 11, 2021
95b7af7
docs: update them with suggestions rebuild
TarikGul May 11, 2021
b3f2678
Removee isArray sanity check
TarikGul May 11, 2021
3e02d71
Use beta for polkadot deps
emostov May 11, 2021
7a00886
docs: fix pattern for acountId
TarikGul May 11, 2021
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions src/chains-config/defaultControllers.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,10 +8,12 @@ export const defaultControllers: ControllerConfig = {
controllers: {
Blocks: true,
BlocksExtrinsics: true,
AccountsAssets: true,
AccountsStakingPayouts: true,
AccountsBalanceInfo: true,
AccountsStakingInfo: true,
AccountsVestingInfo: true,
Assets: true,
NodeNetwork: true,
NodeVersion: true,
NodeTransactionPool: true,
Expand Down
2 changes: 2 additions & 0 deletions src/chains-config/dockMainnetControllers.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,10 +8,12 @@ export const dockMainnetControllers: ControllerConfig = {
controllers: {
Blocks: true,
BlocksExtrinsics: true,
AccountsAssets: false,
AccountsStakingPayouts: false,
AccountsBalanceInfo: true,
AccountsStakingInfo: false,
AccountsVestingInfo: false,
Assets: false,
NodeNetwork: true,
NodeVersion: true,
NodeTransactionPool: true,
Expand Down
2 changes: 2 additions & 0 deletions src/chains-config/dockTestnetControllers.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,10 +7,12 @@ export const dockTestnetControllers: ControllerConfig = {
controllers: {
Blocks: true,
BlocksExtrinsics: true,
AccountsAssets: false,
AccountsStakingPayouts: false,
AccountsBalanceInfo: true,
AccountsStakingInfo: false,
AccountsVestingInfo: false,
Assets: false,
NodeNetwork: true,
NodeVersion: true,
NodeTransactionPool: true,
Expand Down
2 changes: 2 additions & 0 deletions src/chains-config/kulupuControllers.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,10 +4,12 @@ export const kulupuControllers: ControllerConfig = {
controllers: {
Blocks: true,
BlocksExtrinsics: true,
AccountsAssets: false,
AccountsStakingPayouts: false,
AccountsBalanceInfo: true,
AccountsStakingInfo: false,
AccountsVestingInfo: false,
Assets: false,
NodeNetwork: true,
NodeVersion: true,
NodeTransactionPool: true,
Expand Down
2 changes: 2 additions & 0 deletions src/chains-config/kusamaControllers.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,10 +8,12 @@ export const kusamaControllers: ControllerConfig = {
controllers: {
Blocks: true,
BlocksExtrinsics: true,
AccountsAssets: true,
AccountsStakingPayouts: true,
AccountsBalanceInfo: true,
AccountsStakingInfo: true,
AccountsVestingInfo: true,
Assets: true,
NodeNetwork: true,
NodeVersion: true,
NodeTransactionPool: true,
Expand Down
2 changes: 2 additions & 0 deletions src/chains-config/mandalaControllers.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,10 +7,12 @@ export const mandalaControllers: ControllerConfig = {
controllers: {
Blocks: true,
BlocksExtrinsics: true,
AccountsAssets: false,
AccountsStakingPayouts: true,
AccountsBalanceInfo: true,
AccountsStakingInfo: true,
AccountsVestingInfo: true,
Assets: false,
NodeNetwork: true,
NodeVersion: true,
NodeTransactionPool: true,
Expand Down
2 changes: 2 additions & 0 deletions src/chains-config/polkadotControllers.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,10 +8,12 @@ export const polkadotControllers: ControllerConfig = {
controllers: {
Blocks: true,
BlocksExtrinsics: true,
AccountsAssets: true,
AccountsStakingPayouts: true,
AccountsBalanceInfo: true,
AccountsStakingInfo: true,
AccountsVestingInfo: true,
Assets: true,
NodeNetwork: true,
NodeVersion: true,
NodeTransactionPool: true,
Expand Down
2 changes: 2 additions & 0 deletions src/chains-config/westendControllers.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,10 +8,12 @@ export const westendControllers: ControllerConfig = {
controllers: {
Blocks: true,
BlocksExtrinsics: true,
AccountsAssets: true,
AccountsStakingPayouts: true,
AccountsBalanceInfo: true,
AccountsStakingInfo: true,
AccountsVestingInfo: true,
Assets: true,
NodeNetwork: true,
NodeVersion: true,
NodeTransactionPool: true,
Expand Down
20 changes: 20 additions & 0 deletions src/controllers/AbstractController.ts
Original file line number Diff line number Diff line change
Expand Up @@ -164,6 +164,26 @@ export default abstract class AbstractController<T extends AbstractService> {
return num;
}

protected parseQueryParamArrayOrThrow(
n: string,
errorMessage: string
): number[] {
const isValidParam = /[^,]+/.test(n);

if (!isValidParam) {
throw new BadRequest(errorMessage);
}

return n
.split(',')
.map((str) =>
this.parseNumberOrThrow(
str,
`Incorrect AssetId format: ${str} is not a positive integer.`
)
);
}

protected verifyAndCastOr(
name: string,
str: unknown,
Expand Down
65 changes: 65 additions & 0 deletions src/controllers/accounts/AccountsAssetsController.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,65 @@
import { ApiPromise } from '@polkadot/api';
import { RequestHandler } from 'express';
import { BadRequest } from 'http-errors';

import { validateAddress } from '../../middleware';
import { AccountsAssetsService } from '../../services/accounts';
import AbstractController from '../AbstractController';

export default class AccountsAssetsController extends AbstractController<AccountsAssetsService> {
constructor(api: ApiPromise) {
super(api, '/accounts/:address/', new AccountsAssetsService(api));
}

protected initRoutes(): void {
this.router.use(this.path, validateAddress);

this.safeMountAsyncGetHandlers([
['asset-balances', this.getAssetBalances],
['asset-approvals', this.getAssetApprovals],
]);
}

private getAssetBalances: RequestHandler = async (
{ params: { address }, query: { at, assets } },
res
): Promise<void> => {
const hash = await this.getHashFromAt(at);

let assetsArray: number[] = [];
if (typeof assets === 'string') {
assetsArray = this.parseQueryParamArrayOrThrow(
assets,
'assets query parameter is not in the correct format.'
);
}

AccountsAssetsController.sanitizedSend(
res,
await this.service.fetchAssetBalances(hash, address, assetsArray)
);
};

private getAssetApprovals: 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 accountId as a query param'
);
}

const id = this.parseNumberOrThrow(
assetId,
'`assetId` provided is not a number.'
);

AccountsAssetsController.sanitizedSend(
res,
await this.service.fetchAssetApproval(hash, address, id, delegate)
);
};
}
1 change: 1 addition & 0 deletions src/controllers/accounts/index.ts
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
export { default as AccountsAssets } from './AccountsAssetsController';
export { default as AccountsBalanceInfo } from './AccountsBalanceInfoController';
export { default as AccountsStakingInfo } from './AccountsStakingInfoController';
export { default as AccountsStakingPayouts } from './AccountsStakingPayoutsController';
Expand Down
38 changes: 38 additions & 0 deletions src/controllers/assets/AssetsController.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
import { ApiPromise } from '@polkadot/api';
import { RequestHandler } from 'express';

import { AssetsService } from '../../services';
import AbstractController from '../AbstractController';

export default class AssetsController extends AbstractController<AssetsService> {
constructor(api: ApiPromise) {
super(api, '/assets', new AssetsService(api));
this.initRoutes();
}

protected initRoutes(): void {
this.safeMountAsyncGetHandlers([
['/:assetsId/assetInfo', this.getAssetById],
]);
}

private getAssetById: RequestHandler = async (
{ params: { assetId }, query: { at } },
res
): Promise<void> => {
const hash = await this.getHashFromAt(at);
/**
* Verify our param `assetId` is an integer represented as a string, and return
* it as an integer
*/
const index = this.parseNumberOrThrow(
assetId,
'`assetId` path param is not a number'
);

AssetsController.sanitizedSend(
res,
this.service.fetchAssetById(hash, index)
);
};
}
1 change: 1 addition & 0 deletions src/controllers/assets/index.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
export { default as Assets } from './AssetsController';
4 changes: 4 additions & 0 deletions src/controllers/index.ts
Original file line number Diff line number Diff line change
@@ -1,9 +1,11 @@
import {
AccountsAssets,
AccountsBalanceInfo,
AccountsStakingInfo,
AccountsStakingPayouts,
AccountsVestingInfo,
} from './accounts';
import { Assets } from './assets';
import { Blocks, BlocksExtrinsics } from './blocks';
import { NodeNetwork, NodeTransactionPool, NodeVersion } from './node';
import { PalletsStakingProgress, PalletsStorage } from './pallets';
Expand All @@ -22,10 +24,12 @@ import {
export const controllers = {
Blocks,
BlocksExtrinsics,
AccountsAssets,
AccountsBalanceInfo,
AccountsStakingInfo,
AccountsVestingInfo,
AccountsStakingPayouts,
Assets,
PalletsStakingProgress,
PalletsStorage,
NodeNetwork,
Expand Down
107 changes: 107 additions & 0 deletions src/services/accounts/AccountsAssetsService.spec.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,107 @@
import { sanitizeNumbers } from '../../sanitize/sanitizeNumbers';
import { blockHash789629, mockApi } from '../test-helpers/mock';
import { AccountsAssetsService } from './AccountsAssetsService';

const accountsAssetsService = new AccountsAssetsService(mockApi);

describe('AccountsAssetsService', () => {
const at = {
hash: '0x7b713de604a99857f6c25eacc115a4f28d2611a23d9ddff99ab0e4f1c17a8578',
height: '789629',
};

describe('AccountsAssetsService.fetchAssetBalances', () => {
it('Should return the correct response with the assets param', async () => {
const expectedResponse = {
at,
assets: [
{
assetId: '10',
balance: '10000000',
isFrozen: false,
isSufficient: true,
},
{
assetId: '20',
balance: '20000000',
isFrozen: true,
isSufficient: true,
},
],
};

const response = await accountsAssetsService.fetchAssetBalances(
blockHash789629,
'0xffff',
[10, 20]
);

expect(sanitizeNumbers(response)).toStrictEqual(expectedResponse);
});

it('Should return the correct response without the assets param', async () => {
const expectedResponse = {
at,
assets: [
{
assetId: '10',
balance: '10000000',
isFrozen: false,
isSufficient: true,
},
{
assetId: '20',
balance: '20000000',
isFrozen: true,
isSufficient: true,
},
{
assetId: '30',
balance: '20000000',
isFrozen: false,
isSufficient: false,
},
{
assetId: '30',
balance: '20000000',
isFrozen: false,
isSufficient: false,
},
{
assetId: '30',
balance: '20000000',
isFrozen: false,
isSufficient: false,
},
],
};

const response = await accountsAssetsService.fetchAssetBalances(
blockHash789629,
'0xffff',
[]
);

expect(sanitizeNumbers(response)).toStrictEqual(expectedResponse);
});
});

describe('AccountsAssetsService.fetchAssetApproval', () => {
it('Should return the correct response', async () => {
const expectedResponse = {
at,
amount: '10000000',
deposit: '2000000',
};

const response = await accountsAssetsService.fetchAssetApproval(
blockHash789629,
'',
10,
''
);

expect(sanitizeNumbers(response)).toStrictEqual(expectedResponse);
});
});
});
Loading