Skip to content

Commit

Permalink
fix: remove all references to SimpleSmartAccountOwner (#101)
Browse files Browse the repository at this point in the history
  • Loading branch information
avasisht23 committed Sep 12, 2023
1 parent b887258 commit a8f101d
Show file tree
Hide file tree
Showing 11 changed files with 56 additions and 55 deletions.
8 changes: 4 additions & 4 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -56,7 +56,7 @@ npm i -s @alchemy/aa-ethers
import {
SimpleSmartContractAccount,
SmartAccountProvider,
type SimpleSmartAccountOwner,
type SmartAccountSigner,
LocalAccountSigner,
} from "@alchemy/aa-core";
import { mnemonicToAccount } from "viem/accounts";
Expand All @@ -69,7 +69,7 @@ const SIMPLE_ACCOUNT_FACTORY_ADDRESS =
// 1. define the EOA owner of the Smart Account
// this uses a utility method for creating an account signer using mnemonic
// we also have a utility for creating an account signer from a private key
const owner: SimpleSmartAccountOwner =
const owner: SmartAccountSigner =
LocalAccountSigner.mnemonicToAccountSigner(MNEMONIC);

// 2. initialize the provider and connect it to the account
Expand Down Expand Up @@ -105,7 +105,7 @@ const { hash } = await provider.sendUserOperation({
```ts
import {
SimpleSmartContractAccount,
type SimpleSmartAccountOwner,
type SmartAccountSigner,
LocalAccountSigner,
} from "@alchemy/aa-core";
import { toHex } from "viem";
Expand All @@ -119,7 +119,7 @@ const SIMPLE_ACCOUNT_FACTORY_ADDRESS =
// 1. define the EOA owner of the Smart Account
// this uses a utility method for creating an account signer using mnemonic
// we also have a utility for creating an account signer from a private key
const owner: SimpleSmartAccountOwner =
const owner: SmartAccountSigner =
LocalAccountSigner.mnemonicToAccountSigner(MNEMONIC);

// 2. initialize the provider and connect it to the account
Expand Down
Original file line number Diff line number Diff line change
@@ -1,19 +1,19 @@
import { SimpleSmartAccountOwner } from "@alchemy/aa-core";
import { SignTypedDataParams, SmartAccountSigner } from "@alchemy/aa-core";
import { useCallback } from "react";
import { toHex } from "viem";
import { useWalletClient } from "wagmi";

type SimpleSmartAccountOwnerResult =
type SimpleSmartAccountSignerResult =
| {
isLoading: false;
owner: SimpleSmartAccountOwner;
owner: SmartAccountSigner;
}
| {
isLoading: true;
owner: undefined;
};

export function useSimpleAccountOwner(): SimpleSmartAccountOwnerResult {
export function useSimpleAccountSigner(): SimpleSmartAccountSignerResult {
const walletClientQuery = useWalletClient();
// We need this to by pass a viem bug https://github.com/wagmi-dev/viem/issues/606
const signMessage = useCallback(
Expand All @@ -27,6 +27,17 @@ export function useSimpleAccountOwner(): SimpleSmartAccountOwnerResult {
}),
[walletClientQuery.data]
);
const signTypedData = useCallback(
(data: SignTypedDataParams) =>
walletClientQuery.data!.request({
// ignore the type error here, it's a bug in the viem typing
// @ts-ignore
method: "eth_signTypedData_v4",
// @ts-ignore
params: [toHex(data), walletClientQuery.data.account.address],
}),
[walletClientQuery.data]
);
const getAddress = useCallback(
() => Promise.resolve(walletClientQuery.data!.account.address),
[walletClientQuery.data]
Expand All @@ -37,5 +48,5 @@ export function useSimpleAccountOwner(): SimpleSmartAccountOwnerResult {
owner: undefined,
};
}
return { isLoading: false, owner: { signMessage, getAddress } };
return { isLoading: false, owner: { signMessage, signTypedData, getAddress } };
}
Original file line number Diff line number Diff line change
Expand Up @@ -3,11 +3,11 @@ import {
SimpleSmartContractAccount,
SmartAccountProvider,
createPublicErc4337Client,
type SimpleSmartAccountOwner
type SmartAccountSigner
} from "@alchemy/aa-core";
import { useCallback, useEffect, useMemo, useState } from "react";
import { encodeFunctionData } from "viem";
import { useAccount, useNetwork } from "wagmi";
import { useAccount, useNetwork, type Chain } from "wagmi";
import { localSmartContractStore } from "~/clients/localStorage";
import { NFTContractABI } from "../../clients/nftContract";
import {
Expand Down Expand Up @@ -96,11 +96,13 @@ const onboardingStepHandlers: Record<
if (!context.entrypointAddress) {
throw new Error("No entrypoint address was found");
}

const chain: Chain = context.chain!;
const entryPointAddress = context.entrypointAddress;
let baseSigner = new SmartAccountProvider(
appConfig.rpcUrl,
context.entrypointAddress!,
context.chain!,
entryPointAddress,
chain,
undefined,
{
txMaxRetries: 60,
Expand All @@ -111,7 +113,7 @@ const onboardingStepHandlers: Record<
}
return new SimpleSmartContractAccount({
entryPointAddress,
chain: context.chain!,
chain,
owner: context.owner,
factoryAddress: appConfig.simpleAccountFactoryAddress,
rpcClient: provider,
Expand All @@ -122,7 +124,6 @@ const onboardingStepHandlers: Record<
const smartAccountAddress = await baseSigner.getAddress();
if (context.useGasManager) {
const smartAccountSigner = withAlchemyGasManager(baseSigner, {
provider: baseSigner.rpcClient,
policyId: appConfig.gasManagerPolicyId,
entryPoint: entryPointAddress,
});
Expand Down Expand Up @@ -243,7 +244,7 @@ const onboardingStepHandlers: Record<

export function useOnboardingOrchestrator(
useGasManager: boolean,
owner: SimpleSmartAccountOwner
owner: SmartAccountSigner
) {
// Setup initial data and state
const { address: ownerAddress } = useAccount();
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import {
PublicErc4337Client,
SimpleSmartAccountOwner,
SmartAccountSigner,
SmartAccountProvider,
} from "@alchemy/aa-core";
import { Chain } from "viem";
Expand All @@ -15,8 +15,8 @@ export interface OnboardingContext {
useGasManager: boolean;
client: PublicErc4337Client;
ownerAddress: `0x${string}`;
ownerQuery: UseQueryResult<SimpleSmartAccountOwner>;
owner: SimpleSmartAccountOwner;
ownerQuery: UseQueryResult<SmartAccountSigner>;
owner: SmartAccountSigner;
smartAccountAddress: `0x${string}`;
smartAccountSigner: SmartAccountProvider;
chain: Chain;
Expand Down Expand Up @@ -44,7 +44,7 @@ export enum OnboardingStepIdentifier {
}

export function initialStep(
owner: SimpleSmartAccountOwner,
owner: SmartAccountSigner,
ownerAddress: `0x${string}`,
client: PublicErc4337Client,
chain: Chain,
Expand Down
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
import { SmartAccountSigner } from "@alchemy/aa-core";
import {
Box,
Button,
Expand All @@ -18,26 +19,25 @@ import {
} from "@chakra-ui/react";
import { useMutation } from "@tanstack/react-query";
import { useRouter } from "next/router";
import { useOnboardingOrchestrator } from "./OnboardingController";
import { queryClient } from "../../clients/query";
import { memo, useState } from "react";
import { useAccount } from "wagmi";
import { useSimpleAccountSigner } from "~/clients/simpleAccountSigner";
import { LoadingScreen } from "~/surfaces/shared/LoadingScreen";
import { queryClient } from "../../clients/query";
import { useOnboardingOrchestrator } from "./OnboardingController";
import { OnboardingStepIdentifier } from "./OnboardingDataModels";
import { useSimpleAccountOwner } from "~/clients/simpleAccountOwner";
import { SimpleSmartAccountOwner } from "@alchemy/aa-core";

export function OnboardingPage() {
const { isConnected } = useAccount();
const ownerResult = useSimpleAccountOwner();
const ownerResult = useSimpleAccountSigner();
if (isConnected && !ownerResult.isLoading) {
return <Onboarding owner={ownerResult.owner} />;
} else {
return <LoadingScreen />;
}
}

function UnmemoOnboarding({ owner }: { owner: SimpleSmartAccountOwner }) {
function UnmemoOnboarding({ owner }: { owner: SmartAccountSigner }) {
const router = useRouter();
const [gasManagerChecked, setGasManagerChecked] = useState(false);
const { go, reset, currentStep } = useOnboardingOrchestrator(
Expand Down
1 change: 0 additions & 1 deletion packages/alchemy/e2e-tests/constants.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,3 @@
export const RPC_URL = process.env.RPC_URL;
export const API_KEY = process.env.API_KEY;
export const OWNER_MNEMONIC = process.env.OWNER_MNEMONIC!;
export const PAYMASTER_POLICY_ID = process.env.PAYMASTER_POLICY_ID!;
21 changes: 7 additions & 14 deletions packages/alchemy/e2e-tests/simple-account.test.ts
Original file line number Diff line number Diff line change
@@ -1,35 +1,30 @@
import {
SimpleSmartContractAccount,
type SimpleSmartAccountOwner,
type SmartAccountSigner,
} from "@alchemy/aa-core";
import { toHex, type Hash } from "viem";
import { mnemonicToAccount } from "viem/accounts";
import { polygonMumbai } from "viem/chains";
import { AlchemyProvider } from "../src/provider.js";
import {
RPC_URL,
API_KEY,
OWNER_MNEMONIC,
PAYMASTER_POLICY_ID,
} from "./constants.js";
import { API_KEY, OWNER_MNEMONIC, PAYMASTER_POLICY_ID } from "./constants.js";

const ENTRYPOINT_ADDRESS = "0x5FF137D4b0FDCD49DcA30c7CF57E578a026d2789";
const SIMPLE_ACCOUNT_FACTORY_ADDRESS =
"0x9406Cc6185a346906296840746125a0E44976454";

describe("Simple Account Tests", () => {
const ownerAccount = mnemonicToAccount(OWNER_MNEMONIC);
const owner: SimpleSmartAccountOwner = {
const owner: SmartAccountSigner = {
signMessage: async (msg) =>
ownerAccount.signMessage({
message: { raw: toHex(msg) },
}),
signTypedData: async () => "0xHash",
getAddress: async () => ownerAccount.address,
};
const chain = polygonMumbai;
const signer = new AlchemyProvider({
apiKey: API_KEY,
rpcUrl: RPC_URL,
apiKey: API_KEY!,
chain,
entryPointAddress: ENTRYPOINT_ADDRESS,
}).connect(
Expand Down Expand Up @@ -62,8 +57,7 @@ describe("Simple Account Tests", () => {
it("should fail to execute if account address is not deployed and not correct", async () => {
const accountAddress = "0xc33AbD9621834CA7c6Fc9f9CC3c47b9c17B03f9F";
const newSigner = new AlchemyProvider({
apiKey: API_KEY,
rpcUrl: RPC_URL,
apiKey: API_KEY!,
chain,
entryPointAddress: ENTRYPOINT_ADDRESS,
}).connect(
Expand Down Expand Up @@ -124,8 +118,7 @@ describe("Simple Account Tests", () => {

it("should successfully use paymaster with fee opts", async () => {
const newSigner = new AlchemyProvider({
apiKey: API_KEY,
rpcUrl: RPC_URL,
apiKey: API_KEY!,
chain,
entryPointAddress: ENTRYPOINT_ADDRESS,
feeOpts: {
Expand Down
5 changes: 3 additions & 2 deletions packages/alchemy/src/__tests__/provider.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ import * as AACoreModule from "@alchemy/aa-core";
import {
SimpleSmartContractAccount,
type BatchUserOperationCallData,
type SimpleSmartAccountOwner,
type SmartAccountSigner,
} from "@alchemy/aa-core";
import { toHex } from "viem";
import { mnemonicToAccount } from "viem/accounts";
Expand All @@ -13,11 +13,12 @@ describe("Alchemy Provider Tests", () => {
const dummyMnemonic =
"test test test test test test test test test test test test";
const ownerAccount = mnemonicToAccount(dummyMnemonic);
const owner: SimpleSmartAccountOwner = {
const owner: SmartAccountSigner = {
signMessage: async (msg) =>
ownerAccount.signMessage({
message: { raw: toHex(msg) },
}),
signTypedData: async () => "0xHash",
getAddress: async () => ownerAccount.address,
};
const chain = polygonMumbai;
Expand Down
10 changes: 4 additions & 6 deletions packages/core/e2e-tests/simple-account.test.ts
Original file line number Diff line number Diff line change
@@ -1,20 +1,18 @@
import { isAddress, type Hash } from "viem";
import { generatePrivateKey } from "viem/accounts";
import { polygonMumbai } from "viem/chains";
import {
SimpleSmartContractAccount,
type SimpleSmartAccountOwner,
} from "../src/account/simple.js";
import { SimpleSmartContractAccount } from "../src/account/simple.js";
import { type SmartAccountSigner } from "../src/index.js";
import { SmartAccountProvider } from "../src/provider/base.js";
import { LocalAccountSigner } from "../src/signer/local-account.js";
import { RPC_URL, API_KEY, OWNER_MNEMONIC } from "./constants.js";
import { API_KEY, OWNER_MNEMONIC, RPC_URL } from "./constants.js";

const ENTRYPOINT_ADDRESS = "0x5FF137D4b0FDCD49DcA30c7CF57E578a026d2789";
const SIMPLE_ACCOUNT_FACTORY_ADDRESS =
"0x9406Cc6185a346906296840746125a0E44976454";

describe("Simple Account Tests", () => {
const owner: SimpleSmartAccountOwner =
const owner: SmartAccountSigner =
LocalAccountSigner.mnemonicToAccountSigner(OWNER_MNEMONIC);
const chain = polygonMumbai;
const signer = new SmartAccountProvider(
Expand Down
8 changes: 3 additions & 5 deletions packages/core/src/account/__tests__/simple.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,15 +3,13 @@ import { describe, it } from "vitest";
import { SmartAccountProvider } from "../../provider/base.js";
import { LocalAccountSigner } from "../../signer/local-account.js";
import type { BatchUserOperationCallData } from "../../types.js";
import {
SimpleSmartContractAccount,
type SimpleSmartAccountOwner,
} from "../simple.js";
import { SimpleSmartContractAccount } from "../simple.js";
import { type SmartAccountSigner } from "../../signer/types.js";

describe("Account Simple Tests", () => {
const dummyMnemonic =
"test test test test test test test test test test test test";
const owner: SimpleSmartAccountOwner =
const owner: SmartAccountSigner =
LocalAccountSigner.mnemonicToAccountSigner(dummyMnemonic);
const chain = polygonMumbai;
const signer = new SmartAccountProvider(
Expand Down
2 changes: 1 addition & 1 deletion packages/core/src/provider/__tests__/base.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,7 @@ describe("Base Tests", () => {
expectedMockCalls: number,
getUserOperationReceiptMock: SpyInstance<
[hash: `0x${string}`],
Promise<UserOperationReceipt>
Promise<UserOperationReceipt | null>
>
) => {
expect(retryMsDelays).toEqual(expectedRetryMsDelays);
Expand Down

0 comments on commit a8f101d

Please sign in to comment.