Skip to content

Commit

Permalink
feat: add sanity check on provider connect so that users can get clea…
Browse files Browse the repository at this point in the history
…rer error
  • Loading branch information
denniswon committed Oct 30, 2023
1 parent f2717f8 commit ecf61aa
Show file tree
Hide file tree
Showing 8 changed files with 157 additions and 0 deletions.
4 changes: 4 additions & 0 deletions packages/core/src/account/base.ts
Original file line number Diff line number Diff line change
Expand Up @@ -290,6 +290,10 @@ export abstract class BaseSmartContractAccount<
return this.factoryAddress;
}

getEntryPointAddress(): Address {
return this.entryPointAddress;
}

// Extra implementations
async isAccountDeployed(): Promise<boolean> {
return (await this.getDeploymentState()) === DeploymentState.DEPLOYED;
Expand Down
5 changes: 5 additions & 0 deletions packages/core/src/account/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -91,4 +91,9 @@ export interface ISmartContractAccount {
* @returns the address of the factory contract for the smart contract account
*/
getFactoryAddress(): Address;

/**
* @returns the address of the entry point contract for the smart contract account
*/
getEntryPointAddress(): Address;
}
15 changes: 15 additions & 0 deletions packages/core/src/provider/__tests__/base.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -77,6 +77,7 @@ describe("Base Tests", () => {
rpcClient: providerMock.rpcClient,
getAddress: async () => "0xMOCK_ADDRESS",
getFactoryAddress: () => "0xMOCK_FACOTRY_ADDRESS",
getEntryPointAddress: () => entryPointAddress,
getOwner: () => undefined,
} as any;

Expand All @@ -102,6 +103,20 @@ describe("Base Tests", () => {
`);
});

it("should throw error if connected account has different entry point address than the provider", async () => {
const account = {
chain: polygonMumbai,
entryPointAddress: "0xOTHER_ENTRY_POINT_ADDRESS",
rpcClient: providerMock.rpcClient,
getAddress: async () => "0xMOCK_ADDRESS",
getFactoryAddress: () => "0xMOCK_FACOTRY_ADDRESS",
getEntryPointAddress: () => "0xOTHER_ENTRY_POINT_ADDRESS",
getOwner: () => undefined,
} as any;

expect(() => providerMock.connect(() => account)).toThrow();
});

it("should emit disconnected event on disconnect", async () => {
const spy = vi.spyOn(providerMock, "emit");
providerMock.disconnect();
Expand Down
10 changes: 10 additions & 0 deletions packages/core/src/provider/base.ts
Original file line number Diff line number Diff line change
Expand Up @@ -573,6 +573,16 @@ export class SmartAccountProvider<
) => TAccount
): this & { account: TAccount } => {
const account = fn(this.rpcClient);

// sanity check
if (account.getEntryPointAddress() !== this.entryPointAddress) {
throw new Error(
`Account EntryPoint address mismatch for the current provider. account: ${account.getEntryPointAddress()} provider: ${
this.entryPointAddress
}`
);
}

defineReadOnly(this, "account", account);

if (this.rpcClient.transport.type === "http") {
Expand Down
12 changes: 12 additions & 0 deletions site/.vitepress/config.ts
Original file line number Diff line number Diff line change
Expand Up @@ -258,6 +258,10 @@ export default defineConfig({
text: "getNonce",
link: "/getNonce",
},
{
text: "getOwner",
link: "/getOwner",
},
{
text: "getDeploymentState",
link: "/getDeploymentState",
Expand All @@ -266,6 +270,14 @@ export default defineConfig({
text: "isAccountDeployed",
link: "/isAccountDeployed",
},
{
text: "getFactoryAddress",
link: "/getFactoryAddress",
},
{
text: "getEntryPointAddress",
link: "/getEntryPointAddress",
},
],
},
],
Expand Down
37 changes: 37 additions & 0 deletions site/packages/aa-core/accounts/other/getEntryPointAddress.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
---
outline: deep
head:
- - meta
- property: og:title
content: getEntryPointAddress
- - meta
- name: description
content: Overview of the getEntryPointAddress method on BaseSmartContractAccount
- - meta
- property: og:description
content: Overview of the getEntryPointAddress method on BaseSmartContractAccount
---

# getEntryPointAddress

Returns the EntryPoint contract address for the account.

## Usage

::: code-group

```ts [example.ts]
import { provider } from "./provider";
// [!code focus:99]
const entryPointAddress = await provider.getEntryPointAddress();
```

<<< @/snippets/provider.ts

:::

## Returns

### `Address`

The address of the EntryPoint contract
37 changes: 37 additions & 0 deletions site/packages/aa-core/accounts/other/getFactoryAddress.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
---
outline: deep
head:
- - meta
- property: og:title
content: getFactoryAddress
- - meta
- name: description
content: Overview of the getFactoryAddress method on BaseSmartContractAccount
- - meta
- property: og:description
content: Overview of the getFactoryAddress method on BaseSmartContractAccount
---

# getFactoryAddress

Returns the account factory address for the account.

## Usage

::: code-group

```ts [example.ts]
import { provider } from "./provider";
// [!code focus:99]
const factoryAddress = await provider.getFactoryAddress();
```

<<< @/snippets/provider.ts

:::

## Returns

### `Address`

The address of the account factory contract
37 changes: 37 additions & 0 deletions site/packages/aa-core/accounts/other/getOwner.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
---
outline: deep
head:
- - meta
- property: og:title
content: getOwner
- - meta
- name: description
content: Overview of the getOwner method on BaseSmartContractAccount
- - meta
- property: og:description
content: Overview of the getOwner method on BaseSmartContractAccount
---

# getOwner

Returns the `SmartAccountSigner` that repensents the current owner for the account

## Usage

::: code-group

```ts [example.ts]
import { provider } from "./provider";
// [!code focus:99]
const ownerSigner = await provider.getOwner();
```

<<< @/snippets/provider.ts

:::

## Returns

### `SmartAccountSigner | undefined`

The `SmartAccountSigner` object that repensents the current owner for the account

0 comments on commit ecf61aa

Please sign in to comment.