Skip to content

Commit

Permalink
Bump arbitrum sdk
Browse files Browse the repository at this point in the history
  • Loading branch information
chrstph-dvx committed Oct 23, 2024
1 parent c80f0ef commit fe1fdd1
Show file tree
Hide file tree
Showing 3 changed files with 23 additions and 42 deletions.
2 changes: 1 addition & 1 deletion src/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -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",
Expand Down
55 changes: 18 additions & 37 deletions src/utils/decimals.ts
Original file line number Diff line number Diff line change
@@ -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<TChain extends Chain | undefined>({
publicClient,
Expand All @@ -8,15 +11,13 @@ export async function getNativeTokenDecimals<TChain extends Chain | undefined>({
publicClient: PublicClient<Transport, TChain>;
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({
Expand All @@ -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({
Expand All @@ -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());
}
8 changes: 4 additions & 4 deletions yarn.lock
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand Down

0 comments on commit fe1fdd1

Please sign in to comment.