@web3-onboard/common
Advanced tools
Comparing version 2.4.0-viem.2 to 2.4.0-viem.3
export declare const isAddress: (address: string) => address is `0x${string}`; | ||
export declare const weiHexToEth: (wei: string) => string; | ||
export declare const weiHexToEth: (wei: `0x${string}`) => string; | ||
export declare const weiToEth: (wei: string) => string; | ||
export declare const ethToWeiBigInt: (eth: string | number) => bigint; | ||
export declare const bigIntToHex: (value: bigint) => string; |
@@ -1,9 +0,8 @@ | ||
const addressRegex = /^0x[a-fA-F0-9]{40}$/; | ||
import { formatEther, hexToBigInt, numberToHex, parseEther } from 'viem'; | ||
export const isAddress = (address) => { | ||
return addressRegex.test(address); | ||
return isAddress(address); | ||
}; | ||
export const weiHexToEth = (wei) => { | ||
const weiBigInt = BigInt(parseInt(wei, 16)); | ||
const ethBalance = divideBigIntWithDecimalResolution(weiBigInt, BigInt('1000000000000000000')); | ||
return ethBalance.toString(); | ||
const weiBigInt = hexToBigInt(wei); | ||
return formatEther(weiBigInt); | ||
}; | ||
@@ -14,21 +13,4 @@ export const weiToEth = (wei) => { | ||
const weiBigInt = BigInt(parseInt(wei)); | ||
const ethBalance = divideBigIntWithDecimalResolution(weiBigInt, BigInt('1000000000000000000')); | ||
return ethBalance.toString(); | ||
return formatEther(weiBigInt); | ||
}; | ||
const divideBigIntWithDecimalResolution = (dividend, divisor, decimalPlaces = 8) => { | ||
if (typeof dividend !== 'bigint' || typeof divisor !== 'bigint') { | ||
throw new Error('dividend and divisor must be BigInt values'); | ||
} | ||
if (typeof decimalPlaces !== 'number' || decimalPlaces < 0) { | ||
throw new Error('decimalPlaces must be a non-negative number'); | ||
} | ||
// Multiply the dividend by 10 ** decimalPlaces to add precision | ||
const adjustedDividend = dividend * BigInt(10 ** decimalPlaces); | ||
const quotient = adjustedDividend / divisor; | ||
// Convert the result back to a decimal number | ||
const decimalPart = String(quotient % BigInt(10 ** decimalPlaces)).padStart(decimalPlaces, '0'); | ||
const integerPart = quotient / BigInt(10 ** decimalPlaces); | ||
const result = `${integerPart}.${decimalPart}`; | ||
return parseFloat(result); | ||
}; | ||
export const ethToWeiBigInt = (eth) => { | ||
@@ -39,13 +21,6 @@ if (typeof eth !== 'string' && typeof eth !== 'number') { | ||
const ethString = typeof eth === 'number' ? eth.toString() : eth; | ||
const decimalSplit = ethString.split('.'); | ||
const integerPart = BigInt(decimalSplit[0]); | ||
const decimalPart = decimalSplit.length > 1 ? BigInt(decimalSplit[1]) : BigInt(0); | ||
const decimalLength = decimalSplit.length > 1 ? decimalSplit[1].length : 0; | ||
const weiFactor = BigInt(10 ** (18 - decimalLength)); | ||
// Perform the conversion from Eth to Wei | ||
const weiValue = integerPart * BigInt(10 ** 18) + decimalPart * weiFactor; | ||
return BigInt(weiValue); | ||
return parseEther(ethString); | ||
}; | ||
export const bigIntToHex = (value) => { | ||
return `0x${value.toString(16)}`; | ||
return numberToHex(value); | ||
}; |
{ | ||
"name": "@web3-onboard/common", | ||
"version": "2.4.0-viem.2", | ||
"version": "2.4.0-viem.3", | ||
"description": "Web3-Onboard makes it simple to connect Ethereum hardware and software wallets to your dapp. Features standardised spec compliant web3 providers for all supported wallets, framework agnostic modern javascript UI with code splitting, CSS customization, multi-chain and multi-account support, reactive wallet state subscriptions and real-time transaction state change notifications.", | ||
@@ -78,4 +78,5 @@ "keywords": [ | ||
"dependencies": { | ||
"joi": "17.9.1" | ||
"joi": "17.9.1", | ||
"viem": "2.10.2" | ||
} | ||
} |
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
21752
2
559
+ Addedviem@2.10.2
+ Added@adraffy/ens-normalize@1.10.0(transitive)
+ Added@noble/curves@1.2.0(transitive)
+ Added@noble/hashes@1.3.2(transitive)
+ Added@scure/base@1.1.8(transitive)
+ Added@scure/bip32@1.3.2(transitive)
+ Added@scure/bip39@1.2.1(transitive)
+ Addedabitype@1.0.0(transitive)
+ Addedisows@1.0.3(transitive)
+ Addedviem@2.10.2(transitive)
+ Addedws@8.13.0(transitive)