Skip to content

Commit

Permalink
feat: 🎸 Add dual version support for 6.x-7.x chain
Browse files Browse the repository at this point in the history
  • Loading branch information
prashantasdeveloper authored and sansan committed Oct 4, 2024
1 parent fc731b8 commit a1369fc
Show file tree
Hide file tree
Showing 104 changed files with 2,026 additions and 1,555 deletions.
198 changes: 159 additions & 39 deletions src/api/client/Assets.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import { PolymeshPrimitivesTicker } from '@polkadot/types/lookup';
import { U8aFixed } from '@polkadot/types-codec';

import {
Context,
Expand Down Expand Up @@ -26,6 +27,7 @@ import {
UnsubCallback,
} from '~/types';
import {
assetIdToString,
bytesToString,
meshMetadataSpecToMetadataSpec,
stringToIdentityId,
Expand All @@ -36,6 +38,7 @@ import {
asAsset,
assembleAssetQuery,
createProcedureMethod,
getAssetIdForTicker,
getDid,
isPrintableAscii,
requestPaginated,
Expand Down Expand Up @@ -142,28 +145,41 @@ export class Assets {
}): Promise<TickerReservation[]> {
const {
context: {
polymeshApi: { query },
polymeshApi: {
query: { asset },
},
isV6,
},
context,
} = this;

const did = await getDid(args?.owner, context);
const rawDid = stringToIdentityId(did, context);

const entries = await query.asset.assetOwnershipRelations.entries(
stringToIdentityId(did, context)
);
if (isV6) {
// eslint-disable-next-line @typescript-eslint/no-explicit-any
const entries = await (asset as any).assetOwnershipRelations.entries(rawDid);

return entries.reduce<TickerReservation[]>((result, [key, relation]) => {
if (relation.isTickerOwned) {
const ticker = tickerToString(key.args[1]);
// eslint-disable-next-line @typescript-eslint/no-explicit-any
return (entries as any[]).reduce<TickerReservation[]>((result, [key, relation]) => {
if (relation.isTickerOwned) {
const ticker = tickerToString(key.args[1]);

if (isPrintableAscii(ticker)) {
return [...result, new TickerReservation({ ticker }, context)];
if (isPrintableAscii(ticker)) {
return [...result, new TickerReservation({ ticker }, context)];
}
}
}

return result;
}, []);
return result;
}, []);
}

const entries = await asset.tickersOwnedByUser.entries(rawDid);

return entries.map(([key]) => {
const ticker = tickerToString(key.args[1]);
return new TickerReservation({ ticker }, context);
});
}

/**
Expand Down Expand Up @@ -200,45 +216,109 @@ export class Assets {
public async getAssets(args?: { owner: string | Identity }): Promise<Asset[]> {
const {
context: {
polymeshApi: { query },
polymeshApi: {
query: { asset },
},
isV6,
},
context,
} = this;

const did = await getDid(args?.owner, context);
const rawDid = stringToIdentityId(did, context);

if (isV6) {
// eslint-disable-next-line @typescript-eslint/no-explicit-any
const entries = await (asset as any).assetOwnershipRelations.entries(rawDid);

const ownedTickers: string[] = [];
const rawTickers: PolymeshPrimitivesTicker[] = [];

// eslint-disable-next-line @typescript-eslint/no-explicit-any
(entries as any[]).forEach(([key, relation]) => {
if (relation.isAssetOwned) {
const rawTicker = key.args[1];
const ticker = tickerToString(rawTicker);
if (isPrintableAscii(ticker)) {
ownedTickers.push(ticker);
rawTickers.push(rawTicker);
}
}
});

const entries = await query.asset.assetOwnershipRelations.entries(
stringToIdentityId(did, context)
);
// eslint-disable-next-line @typescript-eslint/no-explicit-any
const ownedDetails = await (asset as any).tokens.multi(rawTickers);

const ownedTickers: string[] = [];
const rawTickers: PolymeshPrimitivesTicker[] = [];
return assembleAssetQuery(ownedDetails, ownedTickers, context);
}

entries.forEach(([key, relation]) => {
if (relation.isAssetOwned) {
const rawTicker = key.args[1];
const ticker = tickerToString(rawTicker);
if (isPrintableAscii(ticker)) {
ownedTickers.push(ticker);
rawTickers.push(rawTicker);
}
const entries = await asset.securityTokensOwnedByuser.entries(rawDid);

const ownedAssets: string[] = [];
const rawAssetIds: U8aFixed[] = [];

entries.forEach(([key]) => {
const rawAssetId = key.args[1];
const ticker = assetIdToString(rawAssetId);
if (isPrintableAscii(ticker)) {
ownedAssets.push(ticker);
rawAssetIds.push(rawAssetId);
}
});
const ownedDetails = await asset.securityTokens.multi(rawAssetIds);

const ownedDetails = await query.asset.tokens.multi(rawTickers);

return assembleAssetQuery(ownedDetails, ownedTickers, context);
return assembleAssetQuery(ownedDetails, ownedAssets, context);
}

/**
* Retrieve a FungibleAsset
*
* @param args.ticker - Asset ticker
* @param args.assetId - Unique Id of the Asset (for spec version 6.x, this is same as ticker)
*/
public async getFungibleAsset(args: { ticker: string }): Promise<FungibleAsset> {
const { ticker } = args;
public async getFungibleAsset(args: { assetId: string }): Promise<FungibleAsset>;
public async getFungibleAsset(args: { ticker: string }): Promise<FungibleAsset>;
// eslint-disable-next-line require-jsdoc
public async getFungibleAsset(args: {
ticker?: string;
assetId?: string;
}): Promise<FungibleAsset> {
const { ticker, assetId } = args;

const {
context,
context: { isV6 },
} = this;

let asset: FungibleAsset;
if (isV6) {
const id = (assetId || ticker)!;
asset = new FungibleAsset({ assetId: id }, context);
const exists = await asset.exists();

if (!exists) {
throw new PolymeshError({
code: ErrorCode.DataUnavailable,
message: `There is no Asset with ticker "${id}"`,
});
}

return asset;
}

if (ticker) {
const id = await getAssetIdForTicker(ticker, context);
if (!id.length) {
throw new PolymeshError({
code: ErrorCode.DataUnavailable,
message: `There is no Asset with ticker "${ticker}"`,
});
}
asset = new FungibleAsset({ assetId: id }, context);
} else {
asset = new FungibleAsset({ assetId: assetId! }, context);
}

const asset = new FungibleAsset({ ticker }, this.context);
const exists = await asset.exists();

if (!exists) {
Expand All @@ -256,11 +336,51 @@ export class Assets {
*
* @param args.ticker - NftCollection ticker
*/
public async getNftCollection(args: { ticker: string }): Promise<NftCollection> {
const { ticker } = args;
public async getNftCollection(args: { ticker: string }): Promise<NftCollection>;
public async getNftCollection(args: { assetId: string }): Promise<NftCollection>;

// eslint-disable-next-line require-jsdoc
public async getNftCollection(args: {
ticker?: string;
assetId?: string;
}): Promise<NftCollection> {
const { ticker, assetId } = args;

const {
context,
context: { isV6 },
} = this;

let collection: NftCollection;
if (isV6) {
const id = (assetId || ticker)!;
collection = new NftCollection({ assetId: id }, context);
const exists = await collection.exists();

if (!exists) {
throw new PolymeshError({
code: ErrorCode.DataUnavailable,
message: `There is no NftCollection with ticker "${id}"`,
});
}

return collection;
}

if (ticker) {
const id = await getAssetIdForTicker(ticker, context);
if (!id.length) {
throw new PolymeshError({
code: ErrorCode.DataUnavailable,
message: `There is no NftCollection with ticker "${ticker}"`,
});
}
collection = new NftCollection({ assetId: id }, context);
} else {
collection = new NftCollection({ assetId: assetId! }, context);
}

const nftCollection = new NftCollection({ ticker }, this.context);
const exists = await nftCollection.exists();
const exists = await collection.exists();

if (!exists) {
throw new PolymeshError({
Expand All @@ -269,7 +389,7 @@ export class Assets {
});
}

return nftCollection;
return collection;
}

/**
Expand All @@ -282,7 +402,7 @@ export class Assets {
context: {
polymeshApi: {
query: {
asset: { assetNames, tokens },
asset: { assetNames, securityTokens },
},
},
},
Expand All @@ -307,7 +427,7 @@ export class Assets {
}
);

const details = await tokens.multi(rawTickers);
const details = await securityTokens.multi(rawTickers);

const data = assembleAssetQuery(details, tickers, context);

Expand Down
6 changes: 3 additions & 3 deletions src/api/client/Claims.ts
Original file line number Diff line number Diff line change
Expand Up @@ -215,7 +215,7 @@ export class Claims {
let targetIssuers;

const filters = {
scope: scope ? await scopeToMiddlewareScope(scope, context) : undefined,
scope: scope ? scopeToMiddlewareScope(scope) : undefined,
trustedClaimIssuers: trustedClaimIssuers?.map(trustedClaimIssuer =>
signerToString(trustedClaimIssuer)
),
Expand Down Expand Up @@ -360,7 +360,7 @@ export class Claims {
issuedAt: momentToDate(issuanceDate),
lastUpdatedAt: momentToDate(lastUpdateDate),
expiry,
claim: meshClaimToClaim(claim) as CddClaim,
claim: meshClaimToClaim(claim, context) as CddClaim,
});
}
});
Expand Down Expand Up @@ -437,7 +437,7 @@ export class Claims {
if (isMiddlewareAvailable) {
const filters = {
dids: [did],
scope: scope ? await scopeToMiddlewareScope(scope, context) : undefined,
scope: scope ? scopeToMiddlewareScope(scope) : undefined,
includeExpired,
};

Expand Down
Loading

0 comments on commit a1369fc

Please sign in to comment.