Skip to content

Commit

Permalink
chore(helper): Clean up bnHelper
Browse files Browse the repository at this point in the history
  • Loading branch information
DeFiFoFum committed Jun 19, 2024
1 parent 3e3fe71 commit a7e082d
Show file tree
Hide file tree
Showing 2 changed files with 64 additions and 28 deletions.
64 changes: 41 additions & 23 deletions lib/bignumber/bnHelper.ts
Original file line number Diff line number Diff line change
@@ -1,38 +1,56 @@
import { BigNumber } from "ethers";
import { BigNumber } from 'ethers'

/**
* Pass BN object, BN, or string returned from a smart contract and convert all BN values to strings to easily read them.
*
* @param {*} bigNumberObject BN, Object of BNs or string
*
* @param {*} value BN, Object of BNs or string
* @param {Set<any>} [seen] A set to track seen objects to avoid circular references
* @returns All values are converted to a string
*/
export function formatBNValueToString(value: any) {
if (typeof value === 'string' || typeof value == 'number' || (value as BigNumber)._isBigNumber) {
return value.toString();
} else if (typeof value === 'object') {
// Functions with multiple returns can't be updated. A new object is used instead.
const replacementValue: any = {};
Object.keys(value).forEach(key => {
replacementValue[key] = formatBNValueToString(value[key]);
})
return replacementValue;
export function formatBNValueToString(value: any, seen: Set<any> = new Set()) {
// Explicitly handle undefined and null values
if (value === undefined || value === null) {
return value
}

// Check if the value is a string, number, or BigNumber and convert to string
if (typeof value === 'string' || typeof value === 'number' || (value as BigNumber)?._isBigNumber) {
return value.toString()
}

// Check if the value is a non-null object
if (typeof value === 'object') {
// Detect circular references
if (seen.has(value)) {
return '[Circular]'
}
return value;
seen.add(value)

// Functions with multiple returns can't be updated. A new object is used instead.
const replacementValue: any = {}
Object.keys(value).forEach((key) => {
replacementValue[key] = formatBNValueToString(value[key], seen)
})
return replacementValue
}

// Return the value as is if it doesn't match any of the above conditions
return value
}

export function addDecimals(value: BigNumber | string, decimals: number | string): BigNumber {
const multiplier = BigNumber.from(10).pow(decimals);
return BigNumber.from(value).mul(multiplier);
const multiplier = BigNumber.from(10).pow(decimals)
return BigNumber.from(value).mul(multiplier)
}

export function removeDecimals(value: BigNumber | string, decimals: number | string): BigNumber {
const multiplier = BigNumber.from(10).pow(decimals);
return BigNumber.from(value).div(multiplier);
const multiplier = BigNumber.from(10).pow(decimals)
return BigNumber.from(value).div(multiplier)
}

export function normalizeDecimals(value: BigNumber | string, decimals: number | string): BigNumber {
const currentMultiplier = BigNumber.from(10).pow(decimals);
const targetMultiplier = BigNumber.from(10).pow(18);
const normalizedValue = BigNumber.from(value).mul(targetMultiplier).div(currentMultiplier);
return normalizedValue;
}
const currentMultiplier = BigNumber.from(10).pow(decimals)
const targetMultiplier = BigNumber.from(10).pow(18)
const normalizedValue = BigNumber.from(value).mul(targetMultiplier).div(currentMultiplier)
return normalizedValue
}
28 changes: 23 additions & 5 deletions test/utils/bnHelper.ts
Original file line number Diff line number Diff line change
Expand Up @@ -23,20 +23,38 @@ export function divBNStr(a: BigNumberish, b: BigNumberish) {
/**
* Pass BN object, BN, or string returned from a smart contract and convert all BN values to strings to easily read them.
*
* @param {*} bigNumberObject BN, Object of BNs or string
* @param {*} value BN, Object of BNs or string
* @param {Set<any>} [seen] A set to track seen objects to avoid circular references
* @returns All values are converted to a string
*/
export function formatBNValueToString(value: any) {
if (typeof value === 'string' || typeof value == 'number' || (value as BigNumber)?._isBigNumber) {
export function formatBNValueToString(value: any, seen: Set<any> = new Set()) {
// Explicitly handle undefined and null values
if (value === undefined || value === null) {
return value
}

// Check if the value is a string, number, or BigNumber and convert to string
if (typeof value === 'string' || typeof value === 'number' || (value as BigNumber)?._isBigNumber) {
return value.toString()
} else if (typeof value === 'object') {
}

// Check if the value is a non-null object
if (typeof value === 'object') {
// Detect circular references
if (seen.has(value)) {
return '[Circular]'
}
seen.add(value)

// Functions with multiple returns can't be updated. A new object is used instead.
const replacementValue: any = {}
Object.keys(value).forEach((key) => {
replacementValue[key] = formatBNValueToString(value[key])
replacementValue[key] = formatBNValueToString(value[key], seen)
})
return replacementValue
}

// Return the value as is if it doesn't match any of the above conditions
return value
}

Expand Down

0 comments on commit a7e082d

Please sign in to comment.