Skip to content

Commit 9a44323

Browse files
chore: 🤖 add isAssetId helper to check for asset ID
1 parent a6cb4c1 commit 9a44323

File tree

2 files changed

+11
-9
lines changed

2 files changed

+11
-9
lines changed

‎src/assets/assets.service.ts‎

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -12,13 +12,13 @@ import {
1212
ResultSet,
1313
} from '@polymeshassociation/polymesh-sdk/types';
1414

15-
import { ASSET_ID_LENGTH } from '~/assets/assets.consts';
1615
import { ControllerTransferDto } from '~/assets/dto/controller-transfer.dto';
1716
import { CreateAssetDto } from '~/assets/dto/create-asset.dto';
1817
import { IssueDto } from '~/assets/dto/issue.dto';
1918
import { RedeemTokensDto } from '~/assets/dto/redeem-tokens.dto';
2019
import { RequiredMediatorsDto } from '~/assets/dto/required-mediators.dto';
2120
import { SetAssetDocumentsDto } from '~/assets/dto/set-asset-documents.dto';
21+
import { isAssetId } from '~/common/decorators';
2222
import { TransactionBaseDto } from '~/common/dto/transaction-base-dto';
2323
import { TransferOwnershipDto } from '~/common/dto/transfer-ownership.dto';
2424
import { AppNotFoundError } from '~/common/errors';
@@ -37,7 +37,7 @@ export class AssetsService {
3737

3838
public async findOne(asset: string): Promise<Asset> {
3939
let getAssetPromise;
40-
if (asset.length === ASSET_ID_LENGTH) {
40+
if (isAssetId(asset)) {
4141
getAssetPromise = this.polymeshService.polymeshApi.assets.getAsset({ assetId: asset });
4242
} else {
4343
getAssetPromise = this.polymeshService.polymeshApi.assets.getAsset({ ticker: asset });
@@ -49,7 +49,7 @@ export class AssetsService {
4949

5050
public async findFungible(asset: string): Promise<FungibleAsset> {
5151
let getAssetPromise;
52-
if (asset.length === ASSET_ID_LENGTH) {
52+
if (isAssetId(asset)) {
5353
getAssetPromise = this.polymeshService.polymeshApi.assets.getFungibleAsset({
5454
assetId: asset,
5555
});

‎src/common/decorators/validation.ts‎

Lines changed: 8 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -42,6 +42,11 @@ export function IsTicker(validationOptions?: ValidationOptions) {
4242
);
4343
}
4444

45+
const assetIdRegex = /0x[0-9A-F]{32}/;
46+
export const isAssetId = (id: string): boolean => {
47+
return assetIdRegex.test(id);
48+
};
49+
4550
export function IsAsset(validationOptions?: ValidationOptions) {
4651
// eslint-disable-next-line @typescript-eslint/ban-types
4752
return function (object: Object, propertyName: string) {
@@ -52,12 +57,9 @@ export function IsAsset(validationOptions?: ValidationOptions) {
5257
options: validationOptions,
5358
validator: {
5459
validate(value: string) {
55-
if (value.startsWith('0x')) {
56-
// check for Asset ID
57-
return value.length === ASSET_ID_LENGTH;
58-
}
59-
60-
return value.length <= MAX_TICKER_LENGTH && value === value.toUpperCase();
60+
return (
61+
isAssetId(value) || (value.length <= MAX_TICKER_LENGTH && value === value.toUpperCase())
62+
);
6163
},
6264
defaultMessage(args: ValidationArguments) {
6365
return `${args.property} must be either a Ticker (${MAX_TICKER_LENGTH} characters uppercase string) or an Asset ID (${ASSET_ID_LENGTH} characters long hex string)`;

0 commit comments

Comments
 (0)