Skip to content

Commit

Permalink
Add getChain helper (#449)
Browse files Browse the repository at this point in the history
* Add getChain helper

* Update changelog

* export utils
  • Loading branch information
joepegler authored Mar 5, 2024
1 parent 6df7c65 commit 99c65a5
Show file tree
Hide file tree
Showing 6 changed files with 249 additions and 269 deletions.
4 changes: 3 additions & 1 deletion packages/account/CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -5,14 +5,16 @@ See [Conventional Commits](https://conventionalcommits.org) for commit guideline

## 4.1.0 (2023-04-03)

Features
Features:

- Added Speed optimisation, removing redundant gasEstimate call to bundler ([2371b2](https://github.com/bcnmy/biconomy-client-sdk/pull/447/commits/2371b230cd5806ec4c7c95ba604d6f924b4be768))
- Added smartAccount.getBalances() method ([4b8bae](https://github.com/bcnmy/biconomy-client-sdk/pull/447/commits/4b8bae412577b846e700b168976cefa6b0803ff6))
- Added smartAccount.getSupportedTokens() method ([6d2fb27](https://github.com/bcnmy/biconomy-client-sdk/pull/447/commits/6d2fb27d6f9b424e440e45990ea06820a9d16d4b))
- Added smartAccount.deploy() method ([be9dc4](https://github.com/bcnmy/biconomy-client-sdk/pull/447/commits/be9dc4d74a3e5a22e69416983436997cf2ea417c))
- Increased checking of the chainId from the bundler, paymaster and the provider ([5d2f3](https://github.com/bcnmy/biconomy-client-sdk/pull/447/commits/5d2f34d8f0fb4f9ff7c7ddc00336471e57efdcfd))
- Added entity name to Logger calls ([9278ec](https://github.com/bcnmy/biconomy-client-sdk/pull/447/commits/9278ecc21e060ef75ab29a0d054d95d69cd4ae27))
- Export a 'getChain' by id helper, which returns a viem chain ([ab2ba](https://github.com/bcnmy/biconomy-client-sdk/pull/449/commits/ab2ba2c518ce867c52bf90b9018dfc1b4ec3b4d4))
- Add "stateOverride" optional param ([20fd54](https://github.com/bcnmy/biconomy-client-sdk/pull/447/commits/20fd54c817d2dcbc6b7d9a247d890d91b19a9c2f))

Fixes:

Expand Down
1 change: 1 addition & 0 deletions packages/account/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ import { type BiconomySmartAccountV2Config } from "./utils/Types.js";

export * from "./utils/Types.js";
export * from "./utils/Constants.js";
export * from "./utils/Utils.js";
export * from "./BiconomySmartAccountV2.js";

export { WalletClientSigner, LocalAccountSigner, type SmartAccountSigner } from "@alchemy/aa-core";
Expand Down
1 change: 1 addition & 0 deletions packages/account/src/utils/Constants.ts
Original file line number Diff line number Diff line change
Expand Up @@ -63,6 +63,7 @@ export const ERROR_MESSAGES = {
SPENDER_REQUIRED: "spender is required for ERC20 mode",
NO_FEE_QUOTE: "FeeQuote was not provided, please call smartAccount.getTokenFees() to get feeQuote",
FAILED_FEE_QUOTE_FETCH: "Failed to fetch fee quote",
CHAIN_NOT_FOUND: "Chain not found",
};

export const NATIVE_TOKEN_ALIAS = "0xEeeeeEeeeEeEeeEeEeEeeEEEeeeeEeeeeeeeEEeE";
Expand Down
20 changes: 19 additions & 1 deletion packages/account/src/utils/Utils.ts
Original file line number Diff line number Diff line change
@@ -1,9 +1,11 @@
import { encodeAbiParameters, parseAbiParameters, keccak256, Hex } from "viem";
import { encodeAbiParameters, parseAbiParameters, keccak256, Hex, Chain } from "viem";
import type { UserOperationStruct } from "@alchemy/aa-core";
import { SupportedSigner, convertSigner } from "@biconomy/common";
import { extractChainIdFromBundlerUrl } from "@biconomy/bundler";
import { BiconomySmartAccountV2Config } from "./Types";
import { extractChainIdFromPaymasterUrl } from "@biconomy/bundler";
import * as chains from "viem/chains";
import { ERROR_MESSAGES } from "./Constants";

/**
* pack the userOperation
Expand Down Expand Up @@ -85,3 +87,19 @@ export const isValidRpcUrl = (url: string): boolean => {
};

export const addressEquals = (a?: string, b?: string): boolean => !!a && !!b && a?.toLowerCase() === b.toLowerCase();

/**
* Utility method for converting a chainId to a {@link Chain} object
*
* @param chainId
* @returns a {@link Chain} object for the given chainId
* @throws if the chainId is not found
*/
export const getChain = (chainId: number): Chain => {
for (const chain of Object.values(chains)) {
if (chain.id === chainId) {
return chain;
}
}
throw new Error(ERROR_MESSAGES.CHAIN_NOT_FOUND);
};
27 changes: 25 additions & 2 deletions packages/account/tests/utils.spec.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import { BiconomySmartAccountV2Config, createECDSAOwnershipValidationModule } from "../src";
import { BiconomySmartAccountV2Config, ERROR_MESSAGES, createECDSAOwnershipValidationModule } from "../src";
import { TestData } from "../../../tests";
import { compareChainIds } from "../src/utils";
import { compareChainIds, getChain } from "../src/utils";
import { createWalletClient, http } from "viem";
import { bsc } from "viem/chains";

Expand Down Expand Up @@ -104,4 +104,27 @@ describe("Utils tests", () => {

await expect(compareChainIds(walletClient, config, false)).rejects.toThrow();
});

// test chains
it("Should return chain object for chain id 1", () => {
const chainId = 1;
const chain = getChain(chainId);
expect(chain.id).toBe(chainId);
});

// should have correct fields
it("Should have correct fields", () => {
const chainId = 1;
const chain = getChain(chainId);

["blockExplorers", "contracts", "fees", "formatters", "id", "name", "nativeCurrency", "rpcUrls", "serializers"].every((field) => {
expect(chain).toHaveProperty(field);
});
});

// Should throw an error, chain id not found
it("Should throw an error, chain id not found", () => {
const chainId = 0;
expect(() => getChain(chainId)).toThrow(ERROR_MESSAGES.CHAIN_NOT_FOUND);
});
});
Loading

0 comments on commit 99c65a5

Please sign in to comment.