From 9a2b34c7835dc75812a95bb492075989e74d5135 Mon Sep 17 00:00:00 2001 From: Leonardo Zizzamia Date: Sun, 8 Sep 2024 10:46:41 +0800 Subject: [PATCH] feat: added isMainnetOnly to isBase and isEthereum utilities #1167 --- .changeset/cool-walls-dream.md | 5 +++++ CHANGELOG.md | 6 +++--- src/isBase.test.ts | 14 ++++++++++++++ src/isBase.ts | 16 ++++++++++++---- src/isEthereum.test.ts | 12 ++++++++++++ src/isEthereum.ts | 16 ++++++++++++---- src/types.ts | 2 ++ 7 files changed, 60 insertions(+), 11 deletions(-) create mode 100644 .changeset/cool-walls-dream.md diff --git a/.changeset/cool-walls-dream.md b/.changeset/cool-walls-dream.md new file mode 100644 index 0000000000..07f9e6a7d9 --- /dev/null +++ b/.changeset/cool-walls-dream.md @@ -0,0 +1,5 @@ +--- +'@coinbase/onchainkit': patch +--- + +ok diff --git a/CHANGELOG.md b/CHANGELOG.md index 60620dd45b..d733910bb0 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -4,9 +4,9 @@ ### Patch Changes - - **feat**: added support for `EIP-5792` (https://eips.ethereum.org/EIPS/eip-5792) in `OnchainKitProvider`. By @0xAlec #1181 - - **fix**: adjusted hover styling for the `Fund` and `Disconnect` wallet components in mobile view. By @cpcramer #1211 - - **feat** added `walletCapabilities` for atomic batching (`useWriteContracts` vs `useWriteContract`) in `Transaction` component. By @0xAlec #1214 +- **feat**: added support for `EIP-5792` (https://eips.ethereum.org/EIPS/eip-5792) in `OnchainKitProvider`. By@0xAlec #1181 +- **fix**: adjusted hover styling for the `Fund` and `Disconnect` wallet components in mobile view. By@cpcramer #1211 +- **feat** added `walletCapabilities` for atomic batching (`useWriteContracts` vs `useWriteContract`) in`Transaction` component. By @0xAlec #1214 ## 0.31.3 diff --git a/src/isBase.test.ts b/src/isBase.test.ts index bb046efe39..9a071cd3f6 100644 --- a/src/isBase.test.ts +++ b/src/isBase.test.ts @@ -21,4 +21,18 @@ describe('isBase', () => { const result = isBase({ chainId }); expect(result).toEqual(true); }); + + it('should return true when isMainnetOnly is true and chainId is mainnet', () => { + const chainId = base.id; + const isMainnetOnly = true; + const result = isBase({ chainId, isMainnetOnly }); + expect(result).toEqual(true); + }); + + it('should return false when isMainnetOnly is true and chainId is not mainnet', () => { + const chainId = baseSepolia.id; + const isMainnetOnly = true; + const result = isBase({ chainId, isMainnetOnly }); + expect(result).toEqual(false); + }); }); diff --git a/src/isBase.ts b/src/isBase.ts index e92a585254..2922ffda73 100644 --- a/src/isBase.ts +++ b/src/isBase.ts @@ -6,9 +6,17 @@ import type { isBaseOptions } from './types'; * - Checks if the paymaster operations chain id is valid * - Only allows the Base and Base Sepolia chain ids */ -export function isBase({ chainId }: isBaseOptions): boolean { - if (chainId !== baseSepolia.id && chainId !== base.id) { - return false; +export function isBase({ + chainId, + isMainnetOnly = false, +}: isBaseOptions): boolean { + // If only Base mainnet + if (isMainnetOnly && chainId === base.id) { + return true; } - return true; + // If only Base or Base Sepolia + if (!isMainnetOnly && (chainId === baseSepolia.id || chainId === base.id)) { + return true; + } + return false; } diff --git a/src/isEthereum.test.ts b/src/isEthereum.test.ts index fbee8a2e8e..9561350fa7 100644 --- a/src/isEthereum.test.ts +++ b/src/isEthereum.test.ts @@ -13,4 +13,16 @@ describe('isEthereum', () => { it('should return false for other chains for testnet', () => { expect(isEthereum({ chainId: optimism.id })).toBeFalsy(); }); + + it('should return true when isMainnetOnly is true and chainId is mainnet', () => { + expect( + isEthereum({ chainId: mainnet.id, isMainnetOnly: true }), + ).toBeTruthy(); + }); + + it('should return false when isMainnetOnly is true and chainId is not mainnet', () => { + expect( + isEthereum({ chainId: sepolia.id, isMainnetOnly: true }), + ).toBeFalsy(); + }); }); diff --git a/src/isEthereum.ts b/src/isEthereum.ts index 6b44952e8b..6d55a1034e 100644 --- a/src/isEthereum.ts +++ b/src/isEthereum.ts @@ -5,9 +5,17 @@ import type { isEthereumOptions } from './types'; * isEthereum * - Checks if the chain is mainnet or sepolia */ -export function isEthereum({ chainId }: isEthereumOptions): boolean { - if (chainId !== mainnet.id && chainId !== sepolia.id) { - return false; +export function isEthereum({ + chainId, + isMainnetOnly = false, +}: isEthereumOptions): boolean { + // If only ETH mainnet + if (isMainnetOnly && chainId === mainnet.id) { + return true; } - return true; + // If only ETH or ETH Sepolia + if (!isMainnetOnly && (chainId === sepolia.id || chainId === mainnet.id)) { + return true; + } + return false; } diff --git a/src/types.ts b/src/types.ts index bd7ca55585..dc57ce6cfb 100644 --- a/src/types.ts +++ b/src/types.ts @@ -7,6 +7,7 @@ import type { EASSchemaUid } from './identity/types'; */ export type isBaseOptions = { chainId: number; + isMainnetOnly?: boolean; // If the chainId check is only allowed on mainnet }; /** @@ -14,6 +15,7 @@ export type isBaseOptions = { */ export type isEthereumOptions = { chainId: number; + isMainnetOnly?: boolean; // If the chainId check is only allowed on mainnet }; /**