Skip to content

Commit

Permalink
Test isContract(address, chainId) (#52)
Browse files Browse the repository at this point in the history
  • Loading branch information
bh2smith authored Sep 17, 2024
1 parent 0451847 commit e39d0c3
Show file tree
Hide file tree
Showing 4 changed files with 22 additions and 13 deletions.
2 changes: 1 addition & 1 deletion examples/send-tx.ts
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ async function main(): Promise<void> {
privateKey: nearAccountPrivateKey,
});
const deployed = await txManager.safeDeployed(chainId);
console.log("Deployed?", deployed)
console.log("Safe Deployed:", deployed);
const transactions = [
// TODO: Replace dummy transaction with real user transaction.
{
Expand Down
6 changes: 1 addition & 5 deletions src/lib/bundler.ts
Original file line number Diff line number Diff line change
Expand Up @@ -25,11 +25,7 @@ export class Erc4337Bundler {
this.entryPointAddress = entryPointAddress;
this.apiKey = apiKey;
this.chainId = chainId;
this.provider = new ethers.JsonRpcProvider(bundlerUrl(chainId, apiKey));
}

client(chainId: number): ethers.JsonRpcProvider {
return new ethers.JsonRpcProvider(bundlerUrl(chainId, this.apiKey));
this.provider = new ethers.JsonRpcProvider(bundlerUrl(chainId, this.apiKey));
}

async getPaymasterData(
Expand Down
17 changes: 10 additions & 7 deletions src/util.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import { Network } from "near-ca";
import { Address, Hex, concatHex, encodePacked, toHex } from "viem";

import { PaymasterData, MetaTransaction } from "./types";
import { Network } from "near-ca";

export const PLACEHOLDER_SIG = encodePacked(["uint48", "uint48"], [0, 0]);

Expand All @@ -25,11 +25,11 @@ export function packPaymasterData(data: PaymasterData): Hex {
return (
data.paymaster
? concatHex([
data.paymaster!,
toHex(BigInt(data.paymasterVerificationGasLimit || 0n), { size: 16 }),
toHex(BigInt(data.paymasterPostOpGasLimit || 0n), { size: 16 }),
data.paymasterData || "0x",
])
data.paymaster!,
toHex(BigInt(data.paymasterVerificationGasLimit || 0n), { size: 16 }),
toHex(BigInt(data.paymasterPostOpGasLimit || 0n), { size: 16 }),
data.paymasterData || "0x",
])
: "0x"
) as Hex;
}
Expand All @@ -38,7 +38,10 @@ export function containsValue(transactions: MetaTransaction[]): boolean {
return transactions.some((tx) => tx.value !== "0");
}

export async function isContract(address: Address, chainId: number): Promise<boolean> {
export async function isContract(
address: Address,
chainId: number
): Promise<boolean> {
const client = Network.fromChainId(chainId).client;
return !!(await client.getCode({ address }));
}
10 changes: 10 additions & 0 deletions tests/utils.spec.ts
Original file line number Diff line number Diff line change
@@ -1,9 +1,11 @@
import { ethers } from "ethers";
import { zeroAddress } from "viem";

import { PaymasterData } from "../src";
import {
PLACEHOLDER_SIG,
containsValue,
isContract,
packGas,
packPaymasterData,
packSignature,
Expand Down Expand Up @@ -65,4 +67,12 @@ describe("Utility Functions (mostly byte packing)", () => {
expect(containsValue([VALUE_TX])).toBe(true);
expect(containsValue([VALUE_TX, NO_VALUE_TX])).toBe(true);
});

it("isContract", () => {
const chainId = 11155111;
expect(isContract(zeroAddress, chainId)).toBe(false);
expect(
isContract("0x9008D19f58AAbD9eD0D60971565AA8510560ab41", chainId)
).toBe(true);
});
});

0 comments on commit e39d0c3

Please sign in to comment.