Skip to content

Commit

Permalink
feat: added isMainnetOnly to isBase and isEthereum utilities #1167
Browse files Browse the repository at this point in the history
  • Loading branch information
Zizzamia committed Sep 8, 2024
1 parent 5b98428 commit 9a2b34c
Show file tree
Hide file tree
Showing 7 changed files with 60 additions and 11 deletions.
5 changes: 5 additions & 0 deletions .changeset/cool-walls-dream.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
'@coinbase/onchainkit': patch
---

ok
6 changes: 3 additions & 3 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand Down
14 changes: 14 additions & 0 deletions src/isBase.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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);
});
});
16 changes: 12 additions & 4 deletions src/isBase.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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;
}
12 changes: 12 additions & 0 deletions src/isEthereum.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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();
});
});
16 changes: 12 additions & 4 deletions src/isEthereum.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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;
}
2 changes: 2 additions & 0 deletions src/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,13 +7,15 @@ import type { EASSchemaUid } from './identity/types';
*/
export type isBaseOptions = {
chainId: number;
isMainnetOnly?: boolean; // If the chainId check is only allowed on mainnet
};

/**
* Note: exported as public Type
*/
export type isEthereumOptions = {
chainId: number;
isMainnetOnly?: boolean; // If the chainId check is only allowed on mainnet
};

/**
Expand Down

0 comments on commit 9a2b34c

Please sign in to comment.