From e17babdbf7463c14412a3c0bfa75d141aa5806e6 Mon Sep 17 00:00:00 2001 From: jaredvu Date: Tue, 27 Aug 2024 20:39:30 -0700 Subject: [PATCH] chore(assetUtils): comments and additional tests --- src/lib/__test__/assetUtils.spec.ts | 44 ++++++++++++++++++++++------- src/lib/assetUtils.ts | 11 ++++++++ 2 files changed, 45 insertions(+), 10 deletions(-) diff --git a/src/lib/__test__/assetUtils.spec.ts b/src/lib/__test__/assetUtils.spec.ts index 4106ff23d..a28e808cd 100644 --- a/src/lib/__test__/assetUtils.spec.ts +++ b/src/lib/__test__/assetUtils.spec.ts @@ -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', () => { @@ -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', () => { diff --git a/src/lib/assetUtils.ts b/src/lib/assetUtils.ts index 6ae472394..d258bd17f 100644 --- a/src/lib/assetUtils.ts +++ b/src/lib/assetUtils.ts @@ -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' @@ -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);