Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

chore: removes more ethers utils #459

Draft
wants to merge 7 commits into
base: dl/remove-eth-bytes
Choose a base branch
from
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 2 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -9,9 +9,9 @@ Below is an overview of the Arbitrum SDK functionality. See the [tutorials](http
- ##### Deposit Ether Into Arbitrum

```ts
import { getL2Network, EthBridger } from '@arbitrum/sdk'
import { getArbitrumNetwork, EthBridger } from '@arbitrum/sdk'

const l2Network = await getL2Network(
const l2Network = await getArbitrumNetwork(
l2ChainID /** <-- chain id of target Arbitrum chain */
)
const ethBridger = new EthBridger(l2Network)
Expand Down
12 changes: 6 additions & 6 deletions scripts/deployStandard.ts
Original file line number Diff line number Diff line change
@@ -1,11 +1,11 @@
import { instantiateBridge } from './instantiate_bridge'
import dotenv from 'dotenv'
import args from './getCLargs'
import { constants, BigNumber, utils } from 'ethers'
import { constants, BigNumber } from 'ethers'
import { ZeroAddress, formatEther } from 'ethers-v6'
import { MultiCaller } from '../src'
import axios from 'axios'
import prompt from 'prompts'
import { ZeroAddress } from 'ethers-v6'
dotenv.config()

const privKey = process.env.PRIVKEY as string
Expand Down Expand Up @@ -156,10 +156,10 @@ const main = async () => {
const fee = price.mul(gasNeeded)
if (fee.gt(walletBal)) {
console.log(
`An estimated ${utils.formatEther(
fee
)} ether is needed for deposit; you only have ${utils.formatEther(
walletBal
`An estimated ${formatEther(
fee.toHexString()
)} ether is needed for deposit; you only have ${formatEther(
walletBal.toHexString()
)} ether. Will try depositing anyway:`
)
}
Expand Down
40 changes: 28 additions & 12 deletions scripts/genNetwork.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,10 +2,14 @@ import * as dotenv from 'dotenv'
dotenv.config()
import { execSync } from 'child_process'
import * as fs from 'fs'
import { L2Network } from '../src'
import { ARB_MINIMUM_BLOCK_TIME_IN_SECONDS } from '../src/lib/dataEntities/constants'

import { IERC20Bridge__factory } from '../src/lib/abi/factories/IERC20Bridge__factory'
import { ethers } from 'ethers'
import {
L2Network,
ArbitrumNetwork,
mapL2NetworkToArbitrumNetwork,
} from '../src/lib/dataEntities/networks'

const isTestingOrbitChains = process.env.ORBIT_TEST === '1'

Expand Down Expand Up @@ -39,24 +43,29 @@ async function patchNetworks(
l2Network: L2Network,
l3Network: L2Network | undefined,
l2Provider: ethers.providers.Provider | undefined
) {
l2Network.parentChainId = (l2Network as any).partnerChainID
l2Network.blockTime = ARB_MINIMUM_BLOCK_TIME_IN_SECONDS
): Promise<{
patchedL2Network: ArbitrumNetwork
patchedL3Network?: ArbitrumNetwork
}> {
const patchedL2Network = mapL2NetworkToArbitrumNetwork(l2Network)

// native token for l3
if (l3Network && l2Provider) {
l3Network.parentChainId = (l3Network as any).partnerChainID
l3Network.blockTime = ARB_MINIMUM_BLOCK_TIME_IN_SECONDS
const patchedL3Network = mapL2NetworkToArbitrumNetwork(l3Network)

try {
l3Network.nativeToken = await IERC20Bridge__factory.connect(
patchedL3Network.nativeToken = await IERC20Bridge__factory.connect(
l3Network.ethBridge.bridge,
l2Provider
).nativeToken()
} catch (e) {
// l3 network doesn't have a native token
}

return { patchedL2Network, patchedL3Network }
}

return { patchedL2Network }
}

async function main() {
Expand All @@ -66,17 +75,24 @@ async function main() {

if (isTestingOrbitChains) {
const { l2Network: l3Network } = getLocalNetworksFromContainer('l2l3')
await patchNetworks(
const { patchedL2Network, patchedL3Network } = await patchNetworks(
output.l2Network,
l3Network,
new ethers.providers.JsonRpcProvider(process.env['ARB_URL'])
)

output = {
l1Network: output.l2Network,
l2Network: l3Network,
l1Network: patchedL2Network,
l2Network: patchedL3Network,
}
} else {
await patchNetworks(output.l2Network, undefined, undefined)
const { patchedL2Network } = await patchNetworks(
output.l2Network,
undefined,
undefined
)

output.l2Network = patchedL2Network
}

fs.writeFileSync('localNetwork.json', JSON.stringify(output, null, 2))
Expand Down
32 changes: 7 additions & 25 deletions scripts/instantiate_bridge.ts
Original file line number Diff line number Diff line change
Expand Up @@ -23,14 +23,11 @@ import dotenv from 'dotenv'
import args from './getCLargs'
import { EthBridger, InboxTools, Erc20Bridger } from '../src'
import {
l1Networks,
l2Networks,
L1Network,
L2Network,
ArbitrumNetwork,
getArbitrumNetwork,
} from '../src/lib/dataEntities/networks'
import { Signer } from 'ethers'
import { AdminErc20Bridger } from '../src/lib/assetBridger/erc20Bridger'
import { isDefined } from '../src/lib/utils/lib'

dotenv.config()

Expand All @@ -39,19 +36,18 @@ const ethKey = process.env['ETH_KEY'] as string

const defaultNetworkId = 421614

export const instantiateBridge = (
export const instantiateBridge = async (
l1PkParam?: string,
l2PkParam?: string
): {
l1Network: L1Network
l2Network: L2Network
): Promise<{
l2Network: ArbitrumNetwork
l1Signer: Signer
l2Signer: Signer
erc20Bridger: Erc20Bridger
ethBridger: EthBridger
adminErc20Bridger: AdminErc20Bridger
inboxTools: InboxTools
} => {
}> => {
if (!l1PkParam && !ethKey) {
throw new Error('need ARB_KEY var')
}
Expand All @@ -68,21 +64,8 @@ export const instantiateBridge = (

l2NetworkID = defaultNetworkId
}
const isL1 = isDefined(l1Networks[l2NetworkID])
const isL2 = isDefined(l2Networks[l2NetworkID])
if (!isL1 && !isL2) {
throw new Error(`Unrecognized network ID: ${l2NetworkID}`)
}
if (!isL2) {
throw new Error(`Tests must specify an L2 network ID: ${l2NetworkID}`)
}

const l2Network = l2Networks[l2NetworkID]
const l1Network = l1Networks[l2Network.parentChainId]

if (!l1Network) {
throw new Error(`Unrecognised parent chain id: ${l2Network.parentChainId}`)
}
const l2Network = await getArbitrumNetwork(l2NetworkID)

const l1Rpc = (() => {
if (l2NetworkID === 42161) return process.env['MAINNET_RPC'] as string
Expand Down Expand Up @@ -138,7 +121,6 @@ export const instantiateBridge = (
const inboxTools = new InboxTools(l1Signer, l2Network)

return {
l1Network,
l2Network,
l1Signer,
l2Signer,
Expand Down
4 changes: 2 additions & 2 deletions scripts/sendL2SignedMsg.ts
Original file line number Diff line number Diff line change
Expand Up @@ -18,11 +18,11 @@

import { BigNumber } from 'ethers'
import { InboxTools } from '../src/lib/inbox/inbox'
import { getL2Network } from '../src/lib/dataEntities/networks'
import { getArbitrumNetwork } from '../src/lib/dataEntities/networks'
import { testSetup } from '../scripts/testSetup'
const sendSignedMsg = async () => {
const { l1Deployer, l2Deployer } = await testSetup()
const l2Network = await getL2Network(await l2Deployer.getChainId())
const l2Network = await getArbitrumNetwork(await l2Deployer.getChainId())
const inbox = new InboxTools(l1Deployer, l2Network)
const message = {
to: await l2Deployer.getAddress(),
Expand Down
62 changes: 14 additions & 48 deletions scripts/testSetup.ts
Original file line number Diff line number Diff line change
Expand Up @@ -23,11 +23,11 @@ import dotenv from 'dotenv'

import { EthBridger, InboxTools, Erc20Bridger } from '../src'
import {
L1Network,
L2Network,
ArbitrumNetwork,
getL1Network,
mapL2NetworkToArbitrumNetwork,
getArbitrumNetwork,
addCustomNetwork,
addCustomArbitrumNetwork,
} from '../src/lib/dataEntities/networks'
import { Signer } from 'ethers'
import { AdminErc20Bridger } from '../src/lib/assetBridger/erc20Bridger'
Expand Down Expand Up @@ -72,7 +72,6 @@ export const getSigner = (provider: JsonRpcProvider, key?: string) => {
}

export const testSetup = async (): Promise<{
parentChain: L1Network | ArbitrumNetwork
childChain: ArbitrumNetwork
parentSigner: Signer
childSigner: Signer
Expand All @@ -95,53 +94,18 @@ export const testSetup = async (): Promise<{
const parentSigner = seed.connect(ethProvider)
const childSigner = seed.connect(arbProvider)

let setParentChain: L1Network | ArbitrumNetwork,
setChildChain: ArbitrumNetwork
let setChildChain: ArbitrumNetwork

try {
const l1Network = isTestingOrbitChains
? await getArbitrumNetwork(parentDeployer)
: await getL1Network(parentDeployer)
const l2Network = await getArbitrumNetwork(childDeployer)
setParentChain = l1Network
setChildChain = l2Network
} catch (err) {
// the networks havent been added yet

// check if theres an existing network available
const localNetworkFile = getLocalNetworksFromFile()

const { l1Network: parentChain, l2Network: childChain } = localNetworkFile

if (isTestingOrbitChains) {
const _parentChain = parentChain as ArbitrumNetwork
const ethLocal: L1Network = {
blockTime: 10,
chainID: _parentChain.parentChainId,
isCustom: true,
name: 'EthLocal',
isArbitrum: false,
}

addCustomNetwork({
customL1Network: ethLocal,
customArbitrumNetwork: _parentChain,
})

addCustomNetwork({
customArbitrumNetwork: childChain,
})

setParentChain = parentChain
setChildChain = childChain
} else {
addCustomNetwork({
customL1Network: parentChain as L1Network,
customArbitrumNetwork: childChain,
})

setParentChain = parentChain
setChildChain = childChain
}
const { l2Network: childChain } = getLocalNetworksFromFile()

addCustomArbitrumNetwork(childChain)
setChildChain = childChain
}

const erc20Bridger = new Erc20Bridger(setChildChain)
Expand All @@ -160,7 +124,6 @@ export const testSetup = async (): Promise<{
childSigner,
parentProvider: ethProvider,
childProvider: arbProvider,
parentChain: setParentChain,
childChain: setChildChain,
erc20Bridger,
adminErc20Bridger,
Expand All @@ -172,13 +135,16 @@ export const testSetup = async (): Promise<{
}

export function getLocalNetworksFromFile(): {
l1Network: L1Network | ArbitrumNetwork
l2Network: ArbitrumNetwork
} {
const pathToLocalNetworkFile = path.join(__dirname, '..', 'localNetwork.json')
if (!fs.existsSync(pathToLocalNetworkFile)) {
throw new ArbSdkError('localNetwork.json not found, must gen:network first')
}
const localNetworksFile = fs.readFileSync(pathToLocalNetworkFile, 'utf8')
return JSON.parse(localNetworksFile)
const localL2: L2Network = JSON.parse(localNetworksFile).l2Network

return {
l2Network: mapL2NetworkToArbitrumNetwork(localL2),
}
}
10 changes: 3 additions & 7 deletions src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -43,13 +43,9 @@ export { ParentToChildMessageGasEstimator } from './lib/message/ParentToChildMes
export { argSerializerConstructor } from './lib/utils/byte_serialize_params'
export { CallInput, MultiCaller } from './lib/utils/multicall'
export {
L1Networks,
L2Networks,
L1Network,
L2Network,
getL1Network,
getL2Network,
addCustomNetwork,
ArbitrumNetwork,
getArbitrumNetwork,
addCustomArbitrumNetwork,
addDefaultLocalNetwork,
getChildrenForNetwork,
} from './lib/dataEntities/networks'
Expand Down
19 changes: 6 additions & 13 deletions src/lib/assetBridger/assetBridger.ts
Original file line number Diff line number Diff line change
Expand Up @@ -20,11 +20,7 @@ import { ZeroAddress } from 'ethers-v6'
import { ParentContractTransaction } from '../message/ParentTransaction'
import { ChildContractTransaction } from '../message/ChildTransaction'

import {
L1Network,
ArbitrumNetwork,
getParentForNetwork,
} from '../dataEntities/networks'
import { ArbitrumNetwork } from '../dataEntities/networks'
import {
SignerOrProvider,
SignerProviderUtils,
Expand All @@ -34,11 +30,6 @@ import {
* Base for bridging assets from parent-to-child and back
*/
export abstract class AssetBridger<DepositParams, WithdrawParams> {
/**
* Parent chain for the given Arbitrum chain, can be an L1 or an L2
*/
public readonly parentChain: L1Network | ArbitrumNetwork

/**
* In case of a chain that uses ETH as its native/gas token, this is either `undefined` or the zero address
*
Expand All @@ -47,7 +38,6 @@ export abstract class AssetBridger<DepositParams, WithdrawParams> {
public readonly nativeToken?: string

public constructor(public readonly childChain: ArbitrumNetwork) {
this.parentChain = getParentForNetwork(childChain)
this.nativeToken = childChain.nativeToken
}

Expand All @@ -56,15 +46,18 @@ export abstract class AssetBridger<DepositParams, WithdrawParams> {
* @param sop
*/
protected async checkParentChain(sop: SignerOrProvider): Promise<void> {
await SignerProviderUtils.checkNetworkMatches(sop, this.parentChain.chainID)
await SignerProviderUtils.checkNetworkMatches(
sop,
this.childChain.parentChainId
)
}

/**
* Check the signer/provider matches the childChain, throws if not
* @param sop
*/
protected async checkChildChain(sop: SignerOrProvider): Promise<void> {
await SignerProviderUtils.checkNetworkMatches(sop, this.childChain.chainID)
await SignerProviderUtils.checkNetworkMatches(sop, this.childChain.chainId)
}

/**
Expand Down
Loading
Loading