Skip to content

Commit

Permalink
feat: 🎸 support hex input in internal asBaseAsset method
Browse files Browse the repository at this point in the history
support hex input in a missed internal method
  • Loading branch information
polymath-eric committed Oct 31, 2024
1 parent 447838f commit b55e637
Show file tree
Hide file tree
Showing 4 changed files with 33 additions and 18 deletions.
3 changes: 2 additions & 1 deletion src/api/client/Assets.ts
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,7 @@ import {
import {
asAsset,
assembleAssetQuery,
asUuid,
createProcedureMethod,
getAssetIdForTicker,
getDid,
Expand Down Expand Up @@ -322,7 +323,7 @@ export class Assets {
} else if (ticker) {
assetIdValue = await getAssetIdForTicker(ticker, context);
} else {
assetIdValue = assetId!;
assetIdValue = asUuid(assetId!);
}
return assetIdValue;
}
Expand Down
10 changes: 6 additions & 4 deletions src/api/client/__tests__/Assets.ts
Original file line number Diff line number Diff line change
Expand Up @@ -433,7 +433,8 @@ describe('Assets Class', () => {
const ticker = 'TEST';
it('should return a specific Asset for a specific asset ID', async () => {
const asset = await assets.getFungibleAsset({ assetId });
expect(asset.id).toBe(assetId);

expect(asset.id).toEqual(hexToUuid(assetId));
});

it('should return a specific Asset for a ticker', async () => {
Expand Down Expand Up @@ -463,13 +464,14 @@ describe('Assets Class', () => {

it('should return the collection for a specific asset ID', async () => {
const nftCollection = await assets.getNftCollection({ assetId });
expect(nftCollection.id).toBe(assetId);
expect(nftCollection.id).toEqual(hexToUuid(assetId));
});

it('should return the collection for a specific ticker', async () => {
jest.spyOn(utilsInternalModule, 'getAssetIdForTicker').mockResolvedValue(hexToUuid(assetId));

const nftCollection = await assets.getNftCollection({ ticker });
jest.spyOn(utilsInternalModule, 'getAssetIdForTicker').mockResolvedValue(assetId);
expect(nftCollection.id).toBe(assetId);
expect(nftCollection.id).toEqual(hexToUuid(assetId));
});

it('should throw if the collection does not exist', async () => {
Expand Down
12 changes: 12 additions & 0 deletions src/utils/__tests__/internal.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2907,6 +2907,18 @@ describe('asBaseAsset', () => {
expect(result.ticker).toEqual(ticker);
});

it('should return BaseAsset when asset ID is provided in hex format', async () => {
const baseAsset = '0x12341234123482348234123412341234';
const ticker = 'SOME_TICKER;';

assetIdTickerMock.mockResolvedValue(
dsMockUtils.createMockOption(dsMockUtils.createMockTicker(ticker))
);
const result = await asBaseAsset(baseAsset, context);

expect(result.id).toEqual('12341234-1234-8234-8234-123412341234');
});

it('should return BaseAsset when ticker is provided', async () => {
const assetId = '0x12341234123412341234123412341234';
const ticker = 'SOME_TICKER;';
Expand Down
26 changes: 13 additions & 13 deletions src/utils/internal.ts
Original file line number Diff line number Diff line change
Expand Up @@ -1070,6 +1070,17 @@ export async function getAssetIdAndTicker(
};
}

/**
* @hidden
*/
export function asUuid(id: string): string {
if (isHexUuid(id)) {
return hexToUuid(id);
}

return id;
}

/**
* @hidden
*/
Expand All @@ -1084,9 +1095,9 @@ export async function asBaseAsset(asset: string | BaseAsset, context: Context):
return new BaseAsset({ assetId: asset }, context);
}

if (isUuid(asset)) {
if (isUuid(asset) || isHexUuid(asset)) {
const ticker = await getTickerForAsset(asset, context);
const baseAsset = new BaseAsset({ assetId: asset }, context);
const baseAsset = new BaseAsset({ assetId: asUuid(asset) }, context);
baseAsset.ticker = ticker;
return baseAsset;
} else {
Expand All @@ -1098,17 +1109,6 @@ export async function asBaseAsset(asset: string | BaseAsset, context: Context):
}
}

/**
* @hidden
*/
export function asUuid(id: string): string {
if (isHexUuid(id)) {
return hexToUuid(id);
}

return id;
}

/**
* @hidden
*/
Expand Down

0 comments on commit b55e637

Please sign in to comment.