Skip to content

Commit

Permalink
Merge branch 'main' into main
Browse files Browse the repository at this point in the history
  • Loading branch information
imelon2 authored Nov 28, 2024
2 parents 06e4d50 + 5ef4430 commit 81df2d6
Show file tree
Hide file tree
Showing 17 changed files with 201 additions and 70 deletions.
12 changes: 11 additions & 1 deletion audit-ci.jsonc
Original file line number Diff line number Diff line change
Expand Up @@ -104,6 +104,16 @@
// Issue with sol2uml library that generates UML diagrams from Solidity code. Only used at build time.
// from: @offchainlabs/l1-l3-teleport-contracts>@arbitrum/nitro-contracts>sol2uml>axios
// from: @offchainlabs/l1-l3-teleport-contracts>@arbitrum/token-bridge-contracts>@arbitrum/nitro-contracts>sol2uml>axios
"GHSA-wf5p-g6vw-rhxx"
"GHSA-wf5p-g6vw-rhxx",
// https://github.com/advisories/GHSA-3xgq-45jj-v275
// cross-spawn command injection vulnerability
// Only used during development via audit-ci, nyc, and patch-package
// from: audit-ci>cross-spawn
// from: nyc>foreground-child>cross-spawn
// from: nyc>spawn-wrap>foreground-child>cross-spawn
// from: @arbitrum/nitro-contracts>patch-package>cross-spawn
// from: @arbitrum/token-bridge-contracts>@arbitrum/nitro-contracts>patch-package>cross-spawn
// from: @offchainlabs/l1-l3-teleport-contracts>@arbitrum/token-bridge-contracts>@arbitrum/nitro-contracts>patch-package>cross-spawn
"GHSA-3xgq-45jj-v275"
]
}
5 changes: 3 additions & 2 deletions package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "@arbitrum/sdk",
"version": "4.0.2-beta.0",
"version": "4.0.2",
"description": "Typescript library client-side interactions with Arbitrum",
"author": "Offchain Labs, Inc.",
"license": "Apache-2.0",
Expand Down Expand Up @@ -96,6 +96,7 @@
"**/hardhat/ws": "7.5.10",
"**/hardhat/@sentry/node/cookie": "0.7.0",
"**/micromatch": "4.0.8",
"**/elliptic": "6.5.7"
"**/elliptic": "6.6.0",
"**/secp256k1": "4.0.4"
}
}
9 changes: 8 additions & 1 deletion src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,10 @@ export {
GetL1L3DepositStatusParams,
} from './lib/assetBridger/l1l3Bridger'
export { EthBridger } from './lib/assetBridger/ethBridger'
export { Erc20Bridger } from './lib/assetBridger/erc20Bridger'
export {
Erc20Bridger,
AdminErc20Bridger,
} from './lib/assetBridger/erc20Bridger'
export {
ChildTransactionReceipt,
ChildContractTransaction,
Expand Down Expand Up @@ -95,3 +98,7 @@ export {
ChildToParentTransactionRequest,
isChildToParentTransactionRequest,
} from './lib/dataEntities/transactionRequest'
export {
scaleFrom18DecimalsToNativeTokenDecimals,
scaleFromNativeTokenDecimalsTo18Decimals,
} from './lib/utils/lib'
9 changes: 5 additions & 4 deletions src/lib/assetBridger/assetBridger.ts
Original file line number Diff line number Diff line change
Expand Up @@ -16,12 +16,13 @@
/* eslint-env node */
'use strict'

import { constants } from 'ethers'

import { ParentContractTransaction } from '../message/ParentTransaction'
import { ChildContractTransaction } from '../message/ChildTransaction'

import { ArbitrumNetwork } from '../dataEntities/networks'
import {
ArbitrumNetwork,
isArbitrumNetworkNativeTokenEther,
} from '../dataEntities/networks'
import {
SignerOrProvider,
SignerProviderUtils,
Expand Down Expand Up @@ -69,7 +70,7 @@ export abstract class AssetBridger<DepositParams, WithdrawParams> {
* @returns {boolean}
*/
protected get nativeTokenIsEth() {
return !this.nativeToken || this.nativeToken === constants.AddressZero
return isArbitrumNetworkNativeTokenEther(this.childNetwork)
}

/**
Expand Down
8 changes: 4 additions & 4 deletions src/lib/assetBridger/erc20Bridger.ts
Original file line number Diff line number Diff line change
Expand Up @@ -79,7 +79,7 @@ import { ParentToChildMessageGasParams } from '../message/ParentToChildMessageCr
import {
getNativeTokenDecimals,
isArbitrumChain,
scaleToNativeTokenDecimals,
scaleFrom18DecimalsToNativeTokenDecimals,
} from '../utils/lib'
import { L2ERC20Gateway__factory } from '../abi/factories/L2ERC20Gateway__factory'
import { getErc20ParentAddressFromParentToChildTxRequest } from '../utils/calldata'
Expand Down Expand Up @@ -626,7 +626,7 @@ export class Erc20Bridger extends AssetBridger<
// callHookData
'0x',
// nativeTokenTotalFee
scaleToNativeTokenDecimals({
scaleFrom18DecimalsToNativeTokenDecimals({
amount: depositParams.gasLimit
.mul(depositParams.maxFeePerGas)
.add(depositParams.maxSubmissionCost), // will be zero
Expand Down Expand Up @@ -1134,11 +1134,11 @@ export class AdminErc20Bridger extends Erc20Bridger {
setTokenGas.gasLimit,
setGatewayGas.gasLimit,
doubleFeePerGas,
scaleToNativeTokenDecimals({
scaleFrom18DecimalsToNativeTokenDecimals({
amount: setTokenDeposit,
decimals: nativeTokenDecimals,
}),
scaleToNativeTokenDecimals({
scaleFrom18DecimalsToNativeTokenDecimals({
amount: setGatewayDeposit,
decimals: nativeTokenDecimals,
}),
Expand Down
11 changes: 6 additions & 5 deletions src/lib/assetBridger/ethBridger.ts
Original file line number Diff line number Diff line change
Expand Up @@ -51,7 +51,7 @@ import { ERC20__factory } from '../abi/factories/ERC20__factory'
import {
getNativeTokenDecimals,
isArbitrumChain,
nativeTokenDecimalsTo18Decimals,
scaleFromNativeTokenDecimalsTo18Decimals,
} from '../utils/lib'

export type ApproveGasTokenParams = {
Expand Down Expand Up @@ -321,10 +321,11 @@ export class EthBridger extends AssetBridger<
childNetwork: this.childNetwork,
})

const amountToBeMintedOnChildChain = nativeTokenDecimalsTo18Decimals({
amount: params.amount,
decimals,
})
const amountToBeMintedOnChildChain =
scaleFromNativeTokenDecimalsTo18Decimals({
amount: params.amount,
decimals,
})

const requestParams = {
...params,
Expand Down
29 changes: 26 additions & 3 deletions src/lib/dataEntities/networks.ts
Original file line number Diff line number Diff line change
Expand Up @@ -17,12 +17,14 @@
;('use strict')

import { Provider } from '@ethersproject/abstract-provider'
import { constants } from 'ethers'

import { SignerOrProvider, SignerProviderUtils } from './signerOrProvider'
import { ArbSdkError } from '../dataEntities/errors'
import { ARB1_NITRO_GENESIS_L2_BLOCK } from './constants'
import { RollupAdminLogic__factory } from '../abi/factories/RollupAdminLogic__factory'
import { Prettify } from '../utils/types'
import { IERC20Bridge__factory } from '../abi/factories/IERC20Bridge__factory'

/**
* Represents an Arbitrum chain, e.g. Arbitrum One, Arbitrum Sepolia, or an L3 chain.
Expand Down Expand Up @@ -113,8 +115,8 @@ export interface TokenBridge {
childWethGateway: string
parentWeth: string
childWeth: string
parentProxyAdmin: string
childProxyAdmin: string
parentProxyAdmin?: string
childProxyAdmin?: string
parentMultiCall: string
childMultiCall: string
}
Expand Down Expand Up @@ -340,6 +342,17 @@ async function getArbitrumNetworkBySignerOrProvider(
return getArbitrumNetworkByChainId(chainId)
}

async function getNativeToken(
bridge: string,
provider: Provider
): Promise<string> {
try {
return await IERC20Bridge__factory.connect(bridge, provider).nativeToken()
} catch (err) {
return constants.AddressZero
}
}

/**
* Returns all Arbitrum networks registered in the SDK, both default and custom.
*/
Expand All @@ -349,7 +362,7 @@ export function getArbitrumNetworks(): ArbitrumNetwork[] {

export type ArbitrumNetworkInformationFromRollup = Pick<
ArbitrumNetwork,
'parentChainId' | 'confirmPeriodBlocks' | 'ethBridge'
'parentChainId' | 'confirmPeriodBlocks' | 'ethBridge' | 'nativeToken'
>

/**
Expand Down Expand Up @@ -388,6 +401,7 @@ export async function getArbitrumNetworkInformationFromRollup(
outbox,
rollup: rollupAddress,
},
nativeToken: await getNativeToken(bridge, parentProvider),
}
}

Expand Down Expand Up @@ -551,6 +565,15 @@ export function assertArbitrumNetworkHasTokenBridge<T extends ArbitrumNetwork>(
}
}

export function isArbitrumNetworkNativeTokenEther(
network: ArbitrumNetwork
): boolean {
return (
typeof network.nativeToken === 'undefined' ||
network.nativeToken === constants.AddressZero
)
}

const { resetNetworksToDefault } = createNetworkStateHandler()

export { resetNetworksToDefault }
7 changes: 5 additions & 2 deletions src/lib/message/ParentToChildMessageCreator.ts
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,10 @@ import {
ParentTransactionReceipt,
} from './ParentTransaction'
import { Inbox__factory } from '../abi/factories/Inbox__factory'
import { getArbitrumNetwork } from '../dataEntities/networks'
import {
getArbitrumNetwork,
isArbitrumNetworkNativeTokenEther,
} from '../dataEntities/networks'
import { ERC20Inbox__factory } from '../abi/factories/ERC20Inbox__factory'
import { PayableOverrides } from '@ethersproject/contracts'
import { SignerProviderUtils } from '../dataEntities/signerOrProvider'
Expand Down Expand Up @@ -156,7 +159,7 @@ export class ParentToChildMessageCreator {
)

const childChain = await getArbitrumNetwork(childProvider)
const nativeTokenIsEth = typeof childChain.nativeToken === 'undefined'
const nativeTokenIsEth = isArbitrumNetworkNativeTokenEther(childChain)

const data = ParentToChildMessageCreator.getTicketCreationRequestCallData(
params,
Expand Down
4 changes: 2 additions & 2 deletions src/lib/message/ParentToChildMessageGasEstimator.ts
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ import {
getBaseFee,
getNativeTokenDecimals,
isDefined,
scaleToNativeTokenDecimals,
scaleFrom18DecimalsToNativeTokenDecimals,
} from '../utils/lib'
import { OmitTyped } from '../utils/types'
import {
Expand Down Expand Up @@ -271,7 +271,7 @@ export class ParentToChildMessageGasEstimator {

const deposit =
options?.deposit?.base ||
scaleToNativeTokenDecimals({
scaleFrom18DecimalsToNativeTokenDecimals({
amount: gasLimit
.mul(maxFeePerGas)
.add(maxSubmissionFee)
Expand Down
4 changes: 2 additions & 2 deletions src/lib/utils/lib.ts
Original file line number Diff line number Diff line change
Expand Up @@ -222,7 +222,7 @@ export async function getNativeTokenDecimals({
}
}

export function scaleToNativeTokenDecimals({
export function scaleFrom18DecimalsToNativeTokenDecimals({
amount,
decimals,
}: {
Expand Down Expand Up @@ -253,7 +253,7 @@ export function scaleToNativeTokenDecimals({
return amount.mul(BigNumber.from(10).pow(BigNumber.from(decimals - 18)))
}

export function nativeTokenDecimalsTo18Decimals({
export function scaleFromNativeTokenDecimalsTo18Decimals({
amount,
decimals,
}: {
Expand Down
7 changes: 5 additions & 2 deletions tests/integration/eth.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,7 @@ import { itOnlyWhenEth } from './custom-fee-token/mochaExtensions'
import { ParentTransactionReceipt } from '../../src'
import {
getNativeTokenDecimals,
scaleToNativeTokenDecimals,
scaleFrom18DecimalsToNativeTokenDecimals,
} from '../../src/lib/utils/lib'
import { parseUnits } from 'ethers/lib/utils'

Expand Down Expand Up @@ -458,7 +458,10 @@ describe('Ether', async () => {
).balanceOf(randomAddress)
: await parentSigner.provider!.getBalance(randomAddress)
expect(finalRandomBalance.toString(), 'L1 final balance').to.eq(
scaleToNativeTokenDecimals({ amount: ethToWithdraw, decimals }).toString()
scaleFrom18DecimalsToNativeTokenDecimals({
amount: ethToWithdraw,
decimals,
}).toString()
)
})
})
48 changes: 45 additions & 3 deletions tests/integration/getArbitrumNetworkInformationFromRollup.test.ts
Original file line number Diff line number Diff line change
@@ -1,21 +1,23 @@
import dotenv from 'dotenv'
import { JsonRpcProvider } from '@ethersproject/providers'
import { constants } from 'ethers'
import { expect } from 'chai'

import {
getArbitrumNetwork,
getArbitrumNetworkInformationFromRollup,
} from '../../src/lib/dataEntities/networks'

dotenv.config()

describe('getArbitrumNetworkInformationFromRollup.test', () => {
describe('getArbitrumNetworkInformationFromRollup', () => {
it('fetches information about arbitrum one', async () => {
const arb1 = await getArbitrumNetwork(42161)
const arb1 = getArbitrumNetwork(42161)
const ethProvider = new JsonRpcProvider(
process.env['MAINNET_RPC'] as string
)

const { parentChainId, confirmPeriodBlocks, ethBridge } =
const { parentChainId, confirmPeriodBlocks, ethBridge, nativeToken } =
await getArbitrumNetworkInformationFromRollup(
arb1.ethBridge.rollup,
ethProvider
Expand All @@ -39,5 +41,45 @@ describe('getArbitrumNetworkInformationFromRollup.test', () => {
)
expect(outbox, 'Outbox contract is not correct').to.eq(arb1EthBridge.outbox)
expect(rollup, 'Rollup contract is not correct').to.eq(arb1EthBridge.rollup)

expect(nativeToken, 'Native token is not correct').to.eq(
constants.AddressZero
)
})

it('fetches information about xai', async () => {
const { parentChainId, confirmPeriodBlocks, ethBridge, nativeToken } =
await getArbitrumNetworkInformationFromRollup(
'0xC47DacFbAa80Bd9D8112F4e8069482c2A3221336',
new JsonRpcProvider('https://arb1.arbitrum.io/rpc')
)

expect(parentChainId, 'parentChainId is not correct').to.eq(42161)

expect(confirmPeriodBlocks, 'confirmPeriodBlocks is not correct').to.eq(
45818
)

const { bridge, inbox, sequencerInbox, outbox, rollup } = ethBridge

expect(bridge, 'Bridge contract is not correct').to.eq(
'0x7dd8A76bdAeBE3BBBaCD7Aa87f1D4FDa1E60f94f'
)
expect(inbox, 'Inbox contract is not correct').to.eq(
'0xaE21fDA3de92dE2FDAF606233b2863782Ba046F9'
)
expect(sequencerInbox, 'SequencerInbox contract is not correct').to.eq(
'0x995a9d3ca121D48d21087eDE20bc8acb2398c8B1'
)
expect(outbox, 'Outbox contract is not correct').to.eq(
'0x1E400568AD4840dbE50FB32f306B842e9ddeF726'
)
expect(rollup, 'Rollup contract is not correct').to.eq(
'0xC47DacFbAa80Bd9D8112F4e8069482c2A3221336'
)

expect(nativeToken, 'Native token is not correct').to.eq(
'0x4Cb9a7AE498CEDcBb5EAe9f25736aE7d428C9D66'
)
})
})
4 changes: 2 additions & 2 deletions tests/integration/retryableData.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@ import { ERC20Inbox__factory } from '../../src/lib/abi/factories/ERC20Inbox__fac
import { isArbitrumNetworkWithCustomFeeToken } from './custom-fee-token/customFeeTokenTestHelpers'
import {
getNativeTokenDecimals,
scaleToNativeTokenDecimals,
scaleFrom18DecimalsToNativeTokenDecimals,
} from '../../src/lib/utils/lib'

describe('RevertData', () => {
Expand All @@ -57,7 +57,7 @@ describe('RevertData', () => {
l2CallValue,
data: hexlify(randomBytes(32)),
maxSubmissionCost: maxSubmissionCost,
value: scaleToNativeTokenDecimals({
value: scaleFrom18DecimalsToNativeTokenDecimals({
amount: l2CallValue
.add(maxSubmissionCost)
.add(RetryableDataTools.ErrorTriggeringParams.gasLimit)
Expand Down
Loading

0 comments on commit 81df2d6

Please sign in to comment.