-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Transaction interpretation helpers (#55)
* Added transaction interpretation helpers * Added transaction interpretation helpers * Added trimUsernameDomain.ts * Refactor * 0.0.0-alpha.12 * 0.0.0-alpha.10 * Refactor * Refactor * Fix
- Loading branch information
1 parent
079d116
commit 29e61db
Showing
61 changed files
with
2,170 additions
and
7,515 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,2 +1,6 @@ | ||
export * from './account'; | ||
export * from './configuration'; | ||
export * from './endpoints'; | ||
export * from './tokens'; | ||
export * from './utils'; | ||
export * from './websocket'; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,60 @@ | ||
import { NFTS_ENDPOINT, TOKENS_ENDPOINT } from 'apiCalls/endpoints'; | ||
import { axiosInstance } from 'apiCalls/utils/axiosInstance'; | ||
import { networkSelector } from 'store/selectors'; | ||
import { getState } from 'store/store'; | ||
import { TokenOptionType, TokenInfoResponse } from 'types/tokens.types'; | ||
import { getIdentifierType } from 'utils/validation/getIdentifierType'; | ||
|
||
export async function getTokenDetails({ | ||
tokenId | ||
}: { | ||
tokenId: string; | ||
}): Promise<TokenOptionType> { | ||
const network = networkSelector(getState()); | ||
const { isNft } = getIdentifierType(tokenId); | ||
|
||
const tokenIdentifier = tokenId; | ||
const tokenEndpoint = isNft ? NFTS_ENDPOINT : TOKENS_ENDPOINT; | ||
|
||
if (!tokenIdentifier) { | ||
return { | ||
tokenDecimals: Number(network.decimals), | ||
tokenLabel: '', | ||
tokenAvatar: '' | ||
}; | ||
} | ||
|
||
try { | ||
const { data: selectedToken } = await axiosInstance.get<TokenInfoResponse>( | ||
`${network.apiAddress}/${tokenEndpoint}/${tokenIdentifier}` | ||
); | ||
|
||
const tokenDecimals = selectedToken | ||
? selectedToken?.decimals | ||
: Number(network.decimals); | ||
const tokenLabel = selectedToken ? selectedToken?.name : ''; | ||
const tokenAvatar = selectedToken | ||
? selectedToken?.assets?.svgUrl ?? selectedToken?.media?.[0]?.thumbnailUrl | ||
: ''; | ||
|
||
return { | ||
tokenDecimals: tokenDecimals, | ||
tokenLabel, | ||
type: selectedToken?.type, | ||
tokenAvatar, | ||
identifier: selectedToken?.identifier, | ||
assets: selectedToken?.assets, | ||
esdtPrice: selectedToken?.price, | ||
ticker: selectedToken?.ticker, | ||
name: selectedToken?.name | ||
}; | ||
} catch (error: any) { | ||
return { | ||
error: `Error fetching token ${tokenIdentifier}: ${error.toString()}`, | ||
tokenDecimals: Number(network.decimals), | ||
tokenLabel: '', | ||
tokenAvatar: '', | ||
identifier: tokenIdentifier | ||
}; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
export * from './getTokenDetails'; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1 +1 @@ | ||
export { formatAmount } from '@multiversx/sdk-dapp-utils/out/helpers/formatAmount'; | ||
export { formatAmount } from '@multiversx/sdk-dapp-utils/out/helpers'; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,2 +1,3 @@ | ||
export * from './fetchAccount'; | ||
export * from './refreshAccount'; | ||
export * from './trimUsernameDomain'; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,12 @@ | ||
export const trimUsernameDomain = (username?: string) => { | ||
if (!username) { | ||
return; | ||
} | ||
|
||
const elrondSuffixExists = username.lastIndexOf('.elrond') > 0; | ||
const trimmedPartBeforeLastDot = elrondSuffixExists | ||
? username.substring(0, username.lastIndexOf('.')) | ||
: username; | ||
|
||
return trimmedPartBeforeLastDot; | ||
}; |
File renamed without changes.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,33 @@ | ||
import { timeRemaining } from '../timeRemaining'; | ||
|
||
describe('timeRemaining tests - short time format', () => { | ||
const entries: [number, string][] = [ | ||
[1076, '17 min'], | ||
[3976, '1 hr'], | ||
[5286, '1 hr'] | ||
]; | ||
|
||
for (let i = 0; i < entries.length; i++) { | ||
const [input, output] = entries[i]; | ||
test(`parse ${input} -> ${output}`, () => { | ||
const result = timeRemaining(input); | ||
expect(result).toStrictEqual(output); | ||
}); | ||
} | ||
}); | ||
|
||
describe('timeRemaining tests - long time format', () => { | ||
const entries: [number, string][] = [ | ||
[1076, '17 min 56 sec'], | ||
[3976, '1 hr 6 min'], | ||
[5286, '1 hr 28 min'] | ||
]; | ||
|
||
for (let i = 0; i < entries.length; i++) { | ||
const [input, output] = entries[i]; | ||
test(`parse ${input} -> ${output}`, () => { | ||
const result = timeRemaining(input, false); | ||
expect(result).toStrictEqual(output); | ||
}); | ||
} | ||
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,88 @@ | ||
export function getRemainingTime(ms: number) { | ||
const days = Math.floor(ms / (24 * 60 * 60 * 1000)); | ||
const daysms = ms % (24 * 60 * 60 * 1000); | ||
const hrs = Math.floor(daysms / (60 * 60 * 1000)); | ||
const hrsms = daysms % (60 * 60 * 1000); | ||
const mins = Math.floor(hrsms / (60 * 1000)); | ||
const minsms = hrsms % (60 * 1000); | ||
const secs = Math.floor(minsms / 1000); | ||
|
||
let secsString = secs + ' sec'; | ||
let minsString = mins + ' min'; | ||
let hrsString = hrs + ' hr'; | ||
let daysString = days + ' day'; | ||
|
||
if (secs > 1) { | ||
secsString = secs + ' sec'; | ||
} | ||
if (mins > 1) { | ||
minsString = mins + ' min'; | ||
} | ||
if (hrs > 1) { | ||
hrsString = hrs + ' hrs'; | ||
} | ||
if (days > 1) { | ||
daysString = days + ' days'; | ||
} | ||
|
||
if (days >= 1) { | ||
return daysString + ' ' + hrsString; | ||
} | ||
if (hrs >= 1) { | ||
const minutesString = mins === 0 ? '' : ' ' + minsString; | ||
return hrsString + minutesString; | ||
} | ||
if (mins >= 1) { | ||
const secString = secs === 0 ? '' : ' ' + secsString; | ||
return minsString + secString; | ||
} | ||
|
||
return secsString; | ||
} | ||
|
||
function getShortDateTimeFormat(datetime: string) { | ||
const parts = datetime.split(' '); | ||
if (parts.length > 1) { | ||
return `${parts[0]} ${parts[1]}`; | ||
} | ||
return datetime; | ||
} | ||
|
||
const getUTCDateNow = (date = new Date(), extendedSeconds = 0) => | ||
Date.UTC( | ||
date.getUTCFullYear(), | ||
date.getUTCMonth(), | ||
date.getUTCDate(), | ||
date.getUTCHours(), | ||
date.getUTCMinutes(), | ||
date.getUTCSeconds() + extendedSeconds, | ||
date.getUTCMilliseconds() | ||
); | ||
|
||
function getUTCdiffInMs(duration: number) { | ||
const date = new Date(); | ||
const startDate = getUTCDateNow(date); | ||
const endDate = getUTCDateNow(date, duration); | ||
const diffInMs = Math.max(endDate - startDate, 0); | ||
return diffInMs; | ||
} | ||
|
||
export function timeRemaining(duration: number, short = true) { | ||
const diffInMs = getUTCdiffInMs(duration); | ||
const remaining = getRemainingTime(diffInMs); | ||
return short ? getShortDateTimeFormat(remaining) : remaining; | ||
} | ||
|
||
function getDifferenceInMs(timestamp: number) { | ||
const dateNow = new Date().getTime(); | ||
const difference = dateNow - timestamp; | ||
const diffInMs = Math.max(difference, 0); | ||
return diffInMs; | ||
} | ||
|
||
export function timeAgo(timestamp: number, short = true) { | ||
const diffInMs = getDifferenceInMs(timestamp); | ||
const remaining = getRemainingTime(diffInMs); | ||
|
||
return short ? getShortDateTimeFormat(remaining) : remaining; | ||
} |
Oops, something went wrong.