Skip to content

Commit

Permalink
chore(assetUtils): comments and additional tests
Browse files Browse the repository at this point in the history
  • Loading branch information
jaredvu committed Aug 28, 2024
1 parent 48c499d commit e17babd
Show file tree
Hide file tree
Showing 2 changed files with 45 additions and 10 deletions.
44 changes: 34 additions & 10 deletions src/lib/__test__/assetUtils.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,13 +2,11 @@ import { describe, expect, it } from 'vitest';

import { getDisplayableAssetFromBaseAsset, getDisplayableTickerFromMarket } from '../assetUtils';

const ASSET_WITH_DEX_AND_ADDRESS = 'BUFFI,uniswap_v3,0x4c1b1302220d7de5c22b495e78b72f2dd2457d45';

describe('getDisplayableAssetFromBaseAsset', () => {
it('should return the displayable base asset from a dex asset string', () => {
expect(
getDisplayableAssetFromBaseAsset(
'BUFFI,uniswap_v3,0x4c1b1302220d7de5c22b495e78b72f2dd2457d45'
)
).toEqual('BUFFI');
expect(getDisplayableAssetFromBaseAsset(ASSET_WITH_DEX_AND_ADDRESS)).toEqual('BUFFI');
});

it('should handle existing base asset with no dex or address', () => {
Expand All @@ -18,15 +16,41 @@ describe('getDisplayableAssetFromBaseAsset', () => {
it('should handle empty base strings', () => {
expect(getDisplayableTickerFromMarket('')).toEqual('');
});

it('should return dex from asset string', () => {
expect(getDisplayableAssetFromBaseAsset(ASSET_WITH_DEX_AND_ADDRESS, 'dex')).toEqual(
'uniswap_v3'
);
});

it('should return blank if dex is not present', () => {
expect(getDisplayableAssetFromBaseAsset('BUFFI', 'dex')).toEqual('');
});

it('should return address from asset string', () => {
expect(getDisplayableAssetFromBaseAsset(ASSET_WITH_DEX_AND_ADDRESS, 'address')).toEqual(
'0x4c1b1302220d7de5c22b495e78b72f2dd2457d45'
);
});

it('should return blank if address is not present', () => {
expect(getDisplayableAssetFromBaseAsset('BUFFI', 'address')).toEqual('');
});

it('should return blank address if invalid asset is provided', () => {
expect(getDisplayableAssetFromBaseAsset('', 'address')).toEqual('');
});

it('should return blank dex if invalid asset is provided', () => {
expect(getDisplayableAssetFromBaseAsset('', 'dex')).toEqual('');
});
});

describe('getDisplayableTickerFromMarket', () => {
it('should return the base asset from a market string', () => {
expect(
getDisplayableTickerFromMarket(
'BUFFI,uniswap_v3,0x4c1b1302220d7de5c22b495e78b72f2dd2457d45-USD'
)
).toEqual('BUFFI-USD');
expect(getDisplayableTickerFromMarket(`${ASSET_WITH_DEX_AND_ADDRESS}-USD`)).toEqual(
'BUFFI-USD'
);
});

it('should handle existing base asset with no dex or address', () => {
Expand Down
11 changes: 11 additions & 0 deletions src/lib/assetUtils.ts
Original file line number Diff line number Diff line change
@@ -1,3 +1,9 @@
/**
*
* @param fullBaseAsset contains base asset, occassionally accompanied (comma-separated) by dex and address. i.e. 'baseAsset,dex,address'
* @param part
* @returns base asset or dex or address from the full base asset.
*/
export const getDisplayableAssetFromBaseAsset = (
fullBaseAsset: string,
part?: 'base' | 'dex' | 'address'
Expand All @@ -8,6 +14,11 @@ export const getDisplayableAssetFromBaseAsset = (
return base;
};

/**
*
* @param market contains base asset, occassionally accompanied (comma-separated) by dex and address. i.e. 'baseAsset,dex,address-quoteAsset'
* @returns a displayable market id by removing the dex and address data.
*/
export const getDisplayableTickerFromMarket = (market: string): string => {
const [fullBaseAsset, quoteAsset] = market.split('-');
const base = getDisplayableAssetFromBaseAsset(fullBaseAsset);
Expand Down

0 comments on commit e17babd

Please sign in to comment.