Skip to content

Commit

Permalink
feat: fetch native token information (#544)
Browse files Browse the repository at this point in the history
  • Loading branch information
spsjvc authored Oct 31, 2024
1 parent f7d4c50 commit 4ba8468
Show file tree
Hide file tree
Showing 2 changed files with 59 additions and 4 deletions.
15 changes: 14 additions & 1 deletion src/lib/dataEntities/networks.ts
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@ 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 @@ -341,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 @@ -350,7 +362,7 @@ export function getArbitrumNetworks(): ArbitrumNetwork[] {

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

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

Expand Down
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'
)
})
})

0 comments on commit 4ba8468

Please sign in to comment.