From 62e1acefc4e5f858545ac43443b18373cd3fa91b Mon Sep 17 00:00:00 2001 From: Christophe Deveaux Date: Mon, 24 Jun 2024 18:12:41 +0200 Subject: [PATCH] feat: add isAnyTrust (#130) --- src/index.ts | 3 +++ src/isAnyTrust.ts | 29 +++++++++++++++++++++++++++++ src/isAnyTrust.unit.test.ts | 30 ++++++++++++++++++++++++++++++ 3 files changed, 62 insertions(+) create mode 100644 src/isAnyTrust.ts create mode 100644 src/isAnyTrust.unit.test.ts diff --git a/src/index.ts b/src/index.ts index e53666c0..a23840f4 100644 --- a/src/index.ts +++ b/src/index.ts @@ -106,6 +106,7 @@ import { GetBatchPostersParams, GetBatchPostersReturnType, } from './getBatchPosters'; +import { isAnyTrust } from './isAnyTrust'; export { arbOwnerPublicActions, @@ -195,4 +196,6 @@ export { getBatchPosters, GetBatchPostersParams, GetBatchPostersReturnType, + // + isAnyTrust, }; diff --git a/src/isAnyTrust.ts b/src/isAnyTrust.ts new file mode 100644 index 00000000..24947371 --- /dev/null +++ b/src/isAnyTrust.ts @@ -0,0 +1,29 @@ +import { Address, Chain, PublicClient, Transport, decodeFunctionData, getAbiItem } from 'viem'; +import { createRollupFetchTransactionHash } from './createRollupFetchTransactionHash'; +import { rollupCreator } from './contracts'; + +const createRollupABI = getAbiItem({ abi: rollupCreator.abi, name: 'createRollup' }); +export async function isAnyTrust({ + rollup, + publicClient, +}: { + rollup: Address; + publicClient: PublicClient; +}) { + const createRollupTransactionHash = await createRollupFetchTransactionHash({ + rollup, + publicClient, + }); + + const transaction = await publicClient.getTransaction({ + hash: createRollupTransactionHash, + }); + const { + args: [{ config }], + } = decodeFunctionData({ + abi: [createRollupABI], + data: transaction.input, + }); + const chainConfig = JSON.parse(config.chainConfig); + return chainConfig.arbitrum.DataAvailabilityCommittee; +} diff --git a/src/isAnyTrust.unit.test.ts b/src/isAnyTrust.unit.test.ts new file mode 100644 index 00000000..3ff89023 --- /dev/null +++ b/src/isAnyTrust.unit.test.ts @@ -0,0 +1,30 @@ +import { expect, it } from 'vitest'; +import { isAnyTrust } from './isAnyTrust'; +import { createPublicClient, http } from 'viem'; +import { arbitrumNova, arbitrumSepolia, sepolia } from 'viem/chains'; + +it('should return true for AnyTrust chain', async () => { + const client = createPublicClient({ + chain: arbitrumNova, + transport: http(), + }); + // https://nova.arbiscan.io/tx/0x37be7a29db10d18501dcf4d0243fa6aefeeba21cbc17832ef16ccf288ce58ef2 + const isPlaynanceAnyTrust = await isAnyTrust({ + publicClient: client, + rollup: '0x04ea347cC6A258A7F65D67aFb60B1d487062A1d0', + }); + expect(isPlaynanceAnyTrust).toBeTruthy(); +}); + +it('should return false for non AnyTrust chain', async () => { + const client = createPublicClient({ + chain: arbitrumSepolia, + transport: http(), + }); + // https://sepolia.arbiscan.io/tx/0x0bbb740d8b0286654b3d0f63175ec882dcbb7714cbf5207357a4a72a4d2dc640 + const isAnyTrustChain = await isAnyTrust({ + publicClient: client, + rollup: '0xd0c7b5c4e8f72e0750ed9dc70a10cf6f5afd4787', + }); + expect(isAnyTrustChain).toBeFalsy(); +});