Skip to content

Commit

Permalink
Merge branch 'develop' into 2599----add-total-balance-to-account-swit…
Browse files Browse the repository at this point in the history
…cher
  • Loading branch information
nicole-obrien authored Jun 11, 2024
2 parents 49f1c67 + cef3664 commit 5bd707f
Show file tree
Hide file tree
Showing 4 changed files with 51 additions and 4 deletions.
2 changes: 1 addition & 1 deletion packages/shared/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@
"@metamask/eth-sig-util": "7.0.1",
"@spruceid/siwe-parser": "2.0.2",
"@sveltejs/svelte-virtual-list": "3.0.1",
"@walletconnect/jsonrpc-types": "1.0.3",
"@walletconnect/jsonrpc-types": "1.0.4",
"@walletconnect/types": "2.11.3",
"@walletconnect/web3wallet": "1.11.2",
"http-status-codes": "2.3.0",
Expand Down
22 changes: 19 additions & 3 deletions packages/shared/src/lib/core/utils/convert.ts
Original file line number Diff line number Diff line change
Expand Up @@ -273,12 +273,28 @@ export class Converter {
* @returns The bytes.
*/
public static bigIntLikeToBigInt(number: BigIntLike | undefined): bigint {
if (number === undefined || number === null || Number.isNaN(number)) {
return BigInt(0)
}

if (ArrayBuffer.isView(number)) {
return bytesToBigInt(number)
} else {
number = number === '0x' ? '0x0' : number
return BigInt(String(number ?? '0'))
}

if (typeof number === 'string') {
if (number === '0x') {
return BigInt('0x0')
}
if (/\d+\.?\d*e[+-]?\d+/i.test(number)) {
number = Number(number)
}
}

if (typeof number === 'number' && !Number.isInteger(number)) {
number = Math.floor(number)
}

return BigInt(number)
}

/**
Expand Down
23 changes: 23 additions & 0 deletions packages/shared/src/lib/core/utils/tests/convert.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -61,7 +61,30 @@ describe('File: convert.ts', () => {
expect(Converter.bigIntLikeToBigInt('0')).toEqual(BigInt(0))
expect(Converter.bigIntLikeToBigInt(100)).toEqual(BigInt(100))
expect(Converter.bigIntLikeToBigInt('123330')).toEqual(BigInt(123330))
})
it('should handle floats by truncating', () => {
expect(Converter.bigIntLikeToBigInt(123.456)).toEqual(BigInt(123))
})
it('should handle scientific notation', () => {
expect(Converter.bigIntLikeToBigInt('1e6')).toEqual(BigInt(1000000))
expect(Converter.bigIntLikeToBigInt('1.23e3')).toEqual(BigInt(1230))
expect(Converter.bigIntLikeToBigInt('-1e6')).toEqual(BigInt(-1000000))
expect(Converter.bigIntLikeToBigInt('1.23e-3')).toEqual(BigInt(0))
})
it('should handle string representations of numbers', () => {
expect(Converter.bigIntLikeToBigInt('123456789012345678901234567890')).toEqual(
BigInt('123456789012345678901234567890')
)
expect(Converter.bigIntLikeToBigInt('-123456789012345678901234567890')).toEqual(
BigInt('-123456789012345678901234567890')
)
})
it('should handle edge cases', () => {
expect(Converter.bigIntLikeToBigInt('')).toEqual(BigInt(0))
expect(Converter.bigIntLikeToBigInt(undefined)).toEqual(BigInt(0))
expect(Converter.bigIntLikeToBigInt(null)).toEqual(BigInt(0))
expect(Converter.bigIntLikeToBigInt(NaN)).toEqual(BigInt(0))
expect(Converter.bigIntLikeToBigInt(false)).toEqual(BigInt(0))
})
})
})
8 changes: 8 additions & 0 deletions yarn.lock
Original file line number Diff line number Diff line change
Expand Up @@ -2840,6 +2840,14 @@
keyvaluestorage-interface "^1.0.0"
tslib "1.14.1"

"@walletconnect/[email protected]":
version "1.0.4"
resolved "https://registry.yarnpkg.com/@walletconnect/jsonrpc-types/-/jsonrpc-types-1.0.4.tgz#ce1a667d79eadf2a2d9d002c152ceb68739c230c"
integrity sha512-P6679fG/M+wuWg9TY8mh6xFSdYnFyFjwFelxyISxMDrlbXokorEVXYOxiqEbrU3x1BmBoCAJJ+vtEaEoMlpCBQ==
dependencies:
events "^3.3.0"
keyvaluestorage-interface "^1.0.0"

"@walletconnect/[email protected]", "@walletconnect/jsonrpc-utils@^1.0.6", "@walletconnect/jsonrpc-utils@^1.0.8":
version "1.0.8"
resolved "https://registry.yarnpkg.com/@walletconnect/jsonrpc-utils/-/jsonrpc-utils-1.0.8.tgz#82d0cc6a5d6ff0ecc277cb35f71402c91ad48d72"
Expand Down

0 comments on commit 5bd707f

Please sign in to comment.