From fe1fdd1f212d1783d25ed406d2162aa13d9440fe Mon Sep 17 00:00:00 2001 From: Christophe Date: Wed, 23 Oct 2024 12:43:21 +0000 Subject: [PATCH] Bump arbitrum sdk --- src/package.json | 2 +- src/utils/decimals.ts | 55 ++++++++++++++----------------------------- yarn.lock | 8 +++---- 3 files changed, 23 insertions(+), 42 deletions(-) diff --git a/src/package.json b/src/package.json index 3c64c48a..6a277446 100644 --- a/src/package.json +++ b/src/package.json @@ -51,7 +51,7 @@ "viem": "^1.20.0" }, "dependencies": { - "@arbitrum/sdk": "^4.0.0", + "@arbitrum/sdk": "^4.0.2-beta.0", "@arbitrum/token-bridge-contracts": "^1.2.2", "@offchainlabs/fund-distribution-contracts": "^1.0.1", "@safe-global/protocol-kit": "^4.0.2", diff --git a/src/utils/decimals.ts b/src/utils/decimals.ts index fc10fb12..894afb95 100644 --- a/src/utils/decimals.ts +++ b/src/utils/decimals.ts @@ -1,5 +1,8 @@ -import { Address, Chain, PublicClient, Transport, zeroAddress } from 'viem'; -import { fetchDecimals } from './erc20'; +import { Address, Chain, PublicClient, Transport } from 'viem'; +import utils from '@arbitrum/sdk/dist/lib/utils/lib'; +import { publicClientToProvider } from '../ethers-compat/publicClientToProvider'; +import { ArbitrumNetwork } from '@arbitrum/sdk'; +import { BigNumber } from 'ethers'; export async function getNativeTokenDecimals({ publicClient, @@ -8,15 +11,13 @@ export async function getNativeTokenDecimals({ publicClient: PublicClient; nativeTokenAddress: Address; }) { - if (!nativeTokenAddress || nativeTokenAddress === zeroAddress) { - return 18; - } - - try { - return await fetchDecimals({ address: nativeTokenAddress, publicClient }); - } catch { - return 0; - } + const result = await utils.getNativeTokenDecimals({ + childNetwork: { + nativeToken: nativeTokenAddress, + } as ArbitrumNetwork, + parentProvider: publicClientToProvider(publicClient), + }); + return BigInt(result.toString()); } export function scaleToNativeTokenDecimals({ @@ -26,25 +27,9 @@ export function scaleToNativeTokenDecimals({ amount: bigint; decimals: number; }) { - // do nothing for 18 decimals - if (decimals === 18) { - return amount; - } - - const decimalsBigInt = BigInt(decimals); - if (decimals < 18) { - const divisor = 10n ** (18n - decimalsBigInt); - const scaledAmount = amount / divisor; - // round up if necessary - if (scaledAmount * divisor < amount) { - return scaledAmount + 1n; - } - - return scaledAmount; - } - - // decimals > 18 - return amount * 10n ** (decimalsBigInt - 18n); + const amountBigNumber = BigNumber.from(amount); + const result = utils.scaleToNativeTokenDecimals({ amount: amountBigNumber, decimals }); + return BigInt(result.toString()); } export function nativeTokenDecimalsTo18Decimals({ @@ -54,11 +39,7 @@ export function nativeTokenDecimalsTo18Decimals({ amount: bigint; decimals: number; }) { - if (decimals < 18) { - return amount * 10n ** BigInt(18 - decimals); - } else if (decimals > 18) { - return amount / 10n ** BigInt(decimals - 18); - } - - return amount; + const amountBigNumber = BigNumber.from(amount); + const result = utils.nativeTokenDecimalsTo18Decimals({ amount: amountBigNumber, decimals }); + return BigInt(result.toString()); } diff --git a/yarn.lock b/yarn.lock index 20c775ca..77b52ca1 100644 --- a/yarn.lock +++ b/yarn.lock @@ -22,10 +22,10 @@ "@openzeppelin/contracts-upgradeable" "4.5.2" patch-package "^6.4.7" -"@arbitrum/sdk@^4.0.0": - version "4.0.0" - resolved "https://registry.yarnpkg.com/@arbitrum/sdk/-/sdk-4.0.0.tgz#8eb2deed1a438250acb4084a4bb8fc7eae7659b6" - integrity sha512-wNZFRs4yYxd6Pyc6xEjksp3C59PikmmVPIw49ceMoLY5D0e6xJt2nuA7jjsemDgRe7q6EvLFvHAY2At6XUWrkg== +"@arbitrum/sdk@^4.0.2-beta.0": + version "4.0.2-beta.0" + resolved "https://registry.yarnpkg.com/@arbitrum/sdk/-/sdk-4.0.2-beta.0.tgz#864e38797d9f25e0b0d6a5f0147d60bd5b5db3b6" + integrity sha512-Q22DqEYzlkspvx6h9LUgTFgpUr9PNtPiyp4hTaODOdNl+cbpVAz6G9x04iMAnNia9sP0KffhDD5WxS4CzFrFFw== dependencies: "@ethersproject/address" "^5.0.8" "@ethersproject/bignumber" "^5.1.1"