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

feat: add prepareArbitrumNetwork #221

Merged
merged 12 commits into from
Oct 31, 2024
17 changes: 13 additions & 4 deletions src/createRollupFetchTransactionHash.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ export type CreateRollupFetchTransactionHashParams<TChain extends Chain | undefi
fromBlock?: bigint;
};

const RollupInitializedEventAbi: AbiEvent = {
const RollupInitializedEventAbi = {
anonymous: false,
inputs: [
{
Expand All @@ -28,9 +28,9 @@ const RollupInitializedEventAbi: AbiEvent = {
],
name: 'RollupInitialized',
type: 'event',
};
} as const satisfies AbiEvent;

export async function createRollupFetchTransactionHash<TChain extends Chain | undefined>({
export async function getRollupInitializedEvents<TChain extends Chain | undefined>({
rollup,
publicClient,
fromBlock,
Expand All @@ -48,8 +48,17 @@ export async function createRollupFetchTransactionHash<TChain extends Chain | un
);
}

return rollupInitializedEvents;
}

export async function createRollupFetchTransactionHash<TChain extends Chain | undefined>({
rollup,
publicClient,
fromBlock,
}: CreateRollupFetchTransactionHashParams<TChain>) {
// Get the transaction hash that emitted that event
const transactionHash = rollupInitializedEvents[0].transactionHash;
const transactionHash = (await getRollupInitializedEvents({ rollup, publicClient, fromBlock }))[0]
.transactionHash;

if (!transactionHash) {
throw new Error(
Expand Down
3 changes: 3 additions & 0 deletions src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -165,6 +165,7 @@ import {
fetchDecimals,
FetchDecimalsProps,
} from './utils/erc20';
import { prepareArbitrumNetwork } from './utils/registerNewNetwork';

export {
arbOwnerPublicActions,
Expand Down Expand Up @@ -301,4 +302,6 @@ export {
FetchAllowanceProps,
fetchDecimals,
FetchDecimalsProps,
//
prepareArbitrumNetwork,
};
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.2-beta.1",
"@arbitrum/sdk": "^4.0.2-beta.2",
"@arbitrum/token-bridge-contracts": "^1.2.2",
"@offchainlabs/fund-distribution-contracts": "^1.0.1",
"@safe-global/protocol-kit": "^4.0.2",
Expand Down
67 changes: 67 additions & 0 deletions src/utils/__snapshots__/registerNewNetwork.unit.test.ts.snap
Original file line number Diff line number Diff line change
@@ -0,0 +1,67 @@
// Vitest Snapshot v1, https://vitest.dev/guide/snapshot.html

exports[`prepareArbitrumNetwork > should create orbit chain network object for ETH gas token chain (Proof of Play Apex) 1`] = `
{
"chainId": 70700,
"confirmPeriodBlocks": 40320,
"ethBridge": {
"bridge": "0x074fFD20C6D8865752C997f4980Cf70F2a3Fbac6",
"inbox": "0xC3874bE54E3f25BBC6B4fB582654fd9294f485a1",
"outbox": "0x0cD85675897B7020d7121e63AB250d3F47ff3Ff2",
"rollup": "0x65AD139061B3f6DDb16170a07b925337ddf42407",
"sequencerInbox": "0xa58F38102579dAE7C584850780dDA55744f67DF1",
},
"isCustom": true,
"isTestnet": false,
"name": "70700-arbitrum-network",
"nativeToken": "0x0000000000000000000000000000000000000000",
"parentChainId": 42161,
"tokenBridge": {
"childCustomGateway": "0x1a4ba648Ddc0E726085A847178eBff204411EB1A",
"childErc20Gateway": "0x7aEdD5a2F3bBd4841711D017Edf90d611aD96a9e",
"childGatewayRouter": "0x33e59640CD7E5C5E8D43fd46d995efDdDd0Fc930",
"childMultiCall": "0xEB4150a4F26Cf3563B3a86965E269C8873D48527",
"childWeth": "0x77684A04145a5924eFCE0D92A7c4a2A2E8C359de",
"childWethGateway": "0x6e965dd667cb08f09DE8285317f012Ac889507b4",
"parentCustomGateway": "0x653f8D34a86207569069164d45a031eE552A4729",
"parentErc20Gateway": "0x298eb8d9f2F046AC60c01535fad40320CCdeB7c0",
"parentGatewayRouter": "0x2f883c5997Cf60B4d52a2fD4039918E1f9D1147c",
"parentMultiCall": "0x90B02D9F861017844F30dFbdF725b6aa84E63822",
"parentWeth": "0x82aF49447D8a07e3bd95BD0d56f35241523fBab1",
"parentWethGateway": "0xEB2Ae03709f63CEa9E5eC6ab25C1838c4A5634BA",
},
}
`;

exports[`prepareArbitrumNetwork > should create orbit chain network object for custom gas token chain (Xai) 1`] = `
{
"chainId": 660279,
"confirmPeriodBlocks": 45818,
"ethBridge": {
"bridge": "0x7dd8A76bdAeBE3BBBaCD7Aa87f1D4FDa1E60f94f",
"inbox": "0xaE21fDA3de92dE2FDAF606233b2863782Ba046F9",
"outbox": "0x1E400568AD4840dbE50FB32f306B842e9ddeF726",
"rollup": "0xc47dacfbaa80bd9d8112f4e8069482c2a3221336",
"sequencerInbox": "0x995a9d3ca121D48d21087eDE20bc8acb2398c8B1",
},
"isCustom": true,
"isTestnet": false,
"name": "660279-arbitrum-network",
"nativeToken": "0x4Cb9a7AE498CEDcBb5EAe9f25736aE7d428C9D66",
"parentChainId": 42161,
"tokenBridge": {
"childCustomGateway": "0x96551194230725c72ACF8E9573B1382CCBC70635",
"childErc20Gateway": "0x0c71417917D24F4A6A6A55559B98c5cCEcb33F7a",
"childGatewayRouter": "0xd096e8dE90D34de758B0E0bA4a796eA2e1e272cF",
"childMultiCall": "0xEEC168551A85911Ec3A905e0561b656979f3ea67",
"childWeth": "0x0000000000000000000000000000000000000000",
"childWethGateway": "0x0000000000000000000000000000000000000000",
"parentCustomGateway": "0xb15A0826d65bE4c2fDd961b72636168ee70Af030",
"parentErc20Gateway": "0xb591cE747CF19cF30e11d656EB94134F523A9e77",
"parentGatewayRouter": "0x22CCA5Dc96a4Ac1EC32c9c7C5ad4D66254a24C35",
"parentMultiCall": "0x90B02D9F861017844F30dFbdF725b6aa84E63822",
"parentWeth": "0x0000000000000000000000000000000000000000",
"parentWethGateway": "0x0000000000000000000000000000000000000000",
},
}
`;
60 changes: 60 additions & 0 deletions src/utils/registerNewNetwork.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,12 +4,72 @@ import {
getArbitrumNetworkInformationFromRollup,
registerCustomArbitrumNetwork,
} from '@arbitrum/sdk';
import { Address, Chain, PublicClient, Transport } from 'viem';

import { testnets } from '../chains';
import { getRollupInitializedEvents } from '../createRollupFetchTransactionHash';
import { publicClientToProvider } from '../ethers-compat/publicClientToProvider';
import { createTokenBridgeFetchTokenBridgeContracts } from '../createTokenBridgeFetchTokenBridgeContracts';

const isTestnet = (parentChainId: number) => {
return testnets.some((testnet) => testnet.id === parentChainId);
};

export async function prepareArbitrumNetwork<TChain extends Chain | undefined>(
parentChainPublicClient: PublicClient<Transport, TChain>,
{ rollup }: { rollup: Address },
): Promise<ArbitrumNetwork> {
const rollupInitializedEvent = await getRollupInitializedEvents({
rollup,
publicClient: parentChainPublicClient,
});

// Fetch orbit chain chainId
const { chainId } = rollupInitializedEvent[0].args;

if (!chainId) {
throw new Error("RollupInitialized event doesn't contain chainId");
}

const { parentChainId, ethBridge, confirmPeriodBlocks, nativeToken } =
await getArbitrumNetworkInformationFromRollup(
rollup,
publicClientToProvider(parentChainPublicClient),
);

const { parentChainContracts, orbitChainContracts } =
await createTokenBridgeFetchTokenBridgeContracts({
inbox: ethBridge.inbox as Address,
parentChainPublicClient,
});

return {
name: String(`${chainId}-arbitrum-network`),
chainId: Number(chainId),
parentChainId,
confirmPeriodBlocks,
ethBridge,
isCustom: true,
isTestnet: isTestnet(parentChainId),
nativeToken,
tokenBridge: {
parentGatewayRouter: parentChainContracts.router,
parentErc20Gateway: parentChainContracts.standardGateway,
parentCustomGateway: parentChainContracts.customGateway,
parentWeth: parentChainContracts.weth,
parentWethGateway: parentChainContracts.wethGateway,
parentMultiCall: parentChainContracts.multicall,

childGatewayRouter: orbitChainContracts.router,
childErc20Gateway: orbitChainContracts.standardGateway,
childCustomGateway: orbitChainContracts.customGateway,
childWeth: orbitChainContracts.weth,
childWethGateway: orbitChainContracts.wethGateway,
childMultiCall: orbitChainContracts.multicall,
},
} satisfies ArbitrumNetwork;
}

export const registerNewNetwork = async (
parentProvider: JsonRpcProvider,
childProvider: JsonRpcProvider,
Expand Down
26 changes: 26 additions & 0 deletions src/utils/registerNewNetwork.unit.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
import { describe, expect, it } from 'vitest';
import { createPublicClient, http } from 'viem';

import { prepareArbitrumNetwork } from './registerNewNetwork';
import { arbitrum } from 'viem/chains';

const client = createPublicClient({
chain: arbitrum,
transport: http(),
});

describe('prepareArbitrumNetwork', () => {
it(`should create orbit chain network object for custom gas token chain (Xai)`, async () => {
const network = await prepareArbitrumNetwork(client, {
rollup: '0xc47dacfbaa80bd9d8112f4e8069482c2a3221336',
});
expect(network).toMatchSnapshot();
});

it(`should create orbit chain network object for ETH gas token chain (Proof of Play Apex)`, async () => {
const network = await prepareArbitrumNetwork(client, {
rollup: '0x65AD139061B3f6DDb16170a07b925337ddf42407',
});
expect(network).toMatchSnapshot();
});
});
38 changes: 28 additions & 10 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.2-beta.1":
version "4.0.2-beta.1"
resolved "https://registry.yarnpkg.com/@arbitrum/sdk/-/sdk-4.0.2-beta.1.tgz#c6c6c5784a8eaa6a4edab3600f765e9f82ce5988"
integrity sha512-a5DG6+Ld3X4L8H3CTUl3gc27Bj/Pqjo+5UZiCI97SM2mHtHKWF1BgQ+FCk/8bgFUP5nS9dXBl5tPJRtCFfOe0A==
"@arbitrum/sdk@^4.0.2-beta.2":
version "4.0.2-beta.2"
resolved "https://registry.yarnpkg.com/@arbitrum/sdk/-/sdk-4.0.2-beta.2.tgz#4da9cf686ea93b31ca2c6a47bb26bdeca43dc9f9"
integrity sha512-ABEkDexf0U6jYbXS9yyuNDqthyzSlH2yZDwYTC/kKqcrvbSOwQI+ahfpiBx79WR05X7gaRRFHHGMEVhZ0Zha2Q==
dependencies:
"@ethersproject/address" "^5.0.8"
"@ethersproject/bignumber" "^5.1.1"
Expand Down Expand Up @@ -1596,7 +1596,20 @@ duplexer@^0.1.1, duplexer@~0.1.1:
resolved "https://registry.yarnpkg.com/duplexer/-/duplexer-0.1.2.tgz#3abe43aef3835f8ae077d136ddce0f276b0400e6"
integrity sha512-jtD6YG370ZCIi/9GTaJKQxWTZD045+4R4hTk/x1UyoqadyJ9x9CgSi1RlVDQF8U2sxLLSnFkCaMihqljHIWgMg==

[email protected], [email protected], elliptic@^6.5.4:
[email protected]:
version "6.5.4"
resolved "https://registry.yarnpkg.com/elliptic/-/elliptic-6.5.4.tgz#da37cebd31e79a1367e941b592ed1fbebd58abbb"
integrity sha512-iLhC6ULemrljPZb+QutR5TQGB+pdW6KGD5RSegS+8sorOZT+rdQFbsQFJgvN3eRqNALqJer4oQ16YvJHlU8hzQ==
dependencies:
bn.js "^4.11.9"
brorand "^1.1.0"
hash.js "^1.0.0"
hmac-drbg "^1.0.1"
inherits "^2.0.4"
minimalistic-assert "^1.0.1"
minimalistic-crypto-utils "^1.0.1"

elliptic@^6.5.4:
version "6.5.7"
resolved "https://registry.yarnpkg.com/elliptic/-/elliptic-6.5.7.tgz#8ec4da2cb2939926a1b9a73619d768207e647c8b"
integrity sha512-ESVCtTwiA+XhY3wyh24QqRGBoP3rEdDUl3EDUUo9tft074fi19IrdpH7hLCMMP3CIj7jb3W96rn8lt/BqIlt5Q==
Expand Down Expand Up @@ -3703,12 +3716,17 @@ wrappy@1:
resolved "https://registry.yarnpkg.com/wrappy/-/wrappy-1.0.2.tgz#b5243d8f3ec1aa35f1364605bc0d1036e30ab69f"
integrity sha512-l4Sp/DRseor9wL6EvV2+TuQn63dMkPjZ/sp9XkghTEbV9KlPS1xUsZ3u7/IQO4wxtcFB4bgpQPRcR3QCvezPcQ==

[email protected], [email protected]:
version "7.5.10"
resolved "https://registry.yarnpkg.com/ws/-/ws-7.5.10.tgz#58b5c20dc281633f6c19113f39b349bd8bd558d9"
integrity sha512-+dbF1tHwZpXcbOJdVOkzLDxZP1ailvSxM6ZweXTegylPny803bFhA+vqBYw4s31NSAk4S2Qz+AKXK9a4wkdjcQ==
[email protected]:
version "7.4.6"
resolved "https://registry.yarnpkg.com/ws/-/ws-7.4.6.tgz#5654ca8ecdeee47c33a9a4bf6d28e2be2980377c"
integrity sha512-YmhHDO4MzaDLB+M9ym/mDA5z0naX8j7SIlT8f8z+I0VtzsRbekxEutHSme7NPS2qE8StCYQNUnfWdXta/Yu85A==

[email protected]:
version "8.13.0"
resolved "https://registry.yarnpkg.com/ws/-/ws-8.13.0.tgz#9a9fb92f93cf41512a0735c8f4dd09b8a1211cd0"
integrity sha512-x9vcZYTrFPC7aSIbj7sRCYo7L/Xb8Iy+pW0ng0wt2vCJv7M9HOMy0UoN3rr+IFC7hb7vXoqS+P9ktyLLLhO+LA==

ws@8.13.0, ws@8.17.1:
[email protected]:
version "8.17.1"
resolved "https://registry.yarnpkg.com/ws/-/ws-8.17.1.tgz#9293da530bb548febc95371d90f9c878727d919b"
integrity sha512-6XQFvXTkbfUOZOKKILFG1PDK2NDQs4azKQl26T0YS5CxqWLgXajbPZ+h4gZekJyRqFU8pvnbAbbs/3TgRPy+GQ==
Expand Down
Loading