Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Test isContract(address, chainId) #52

Merged
merged 2 commits into from
Sep 17, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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);
});
});