Skip to content

Commit

Permalink
feat: added tests for ownable executor & cleanup
Browse files Browse the repository at this point in the history
  • Loading branch information
VGabriel45 committed Sep 23, 2024
1 parent c596a02 commit 5531d6b
Show file tree
Hide file tree
Showing 15 changed files with 268 additions and 395 deletions.
8 changes: 4 additions & 4 deletions src/sdk/account/toNexusAccount.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,6 @@ import { toNetwork } from "../../test/testSetup"
import {
fundAndDeployClients,
getTestAccount,
getTestSmartAccountClient,
killNetwork,
toTestClient
} from "../../test/testUtils"
Expand Down Expand Up @@ -78,10 +77,11 @@ describe("nexus.account", async () => {
transport: http()
})

nexusClient = await getTestSmartAccountClient({
account,
nexusClient = await createNexusClient({
signer: account,
chain,
bundlerUrl
transport: http(),
bundlerTransport: http(bundlerUrl)
})

nexusAccount = nexusClient.account
Expand Down
31 changes: 5 additions & 26 deletions src/sdk/account/toNexusAccount.ts
Original file line number Diff line number Diff line change
Expand Up @@ -75,7 +75,7 @@ export type ToNexusSmartAccountParameters = {
/** Optional index for the account */
index?: bigint | undefined
/** Optional active validation module */
activeModule?: BaseValidationModule
activeValidationModule?: BaseValidationModule
/** Optional factory address */
factoryAddress?: Address
/** Optional K1 validator address */
Expand Down Expand Up @@ -114,8 +114,7 @@ export type NexusSmartAccountImplementation = SmartAccountImplementation<
encodeExecuteBatch: (calls: readonly Call[]) => Promise<Hex>
getUserOpHash: (userOp: Partial<UserOperationStruct>) => Promise<Hex>
setActiveValidationModule: (validationModule: BaseValidationModule) => void
getActiveValidationModule: () => BaseValidationModule
getSigner: () => Promise<Signer>
getActiveValidationModule: () => BaseValidationModule,
factoryData: Hex
factoryAddress: Address
}
Expand Down Expand Up @@ -146,7 +145,7 @@ export const toNexusAccount = async (
transport,
signer: _signer,
index = 0n,
activeModule,
activeValidationModule,
factoryAddress = contracts.k1ValidatorFactory.address,
k1ValidatorAddress = contracts.k1Validator.address,
key = "nexus account",
Expand Down Expand Up @@ -182,7 +181,7 @@ export const toNexusAccount = async (
})

let defaultedActiveModule =
activeModule ??
activeValidationModule ??
new K1ValidatorModule(
{
address: k1ValidatorAddress,
Expand All @@ -205,14 +204,6 @@ export const toNexusAccount = async (
return _accountAddress
}

/**
* @description Gets the signer associated with this account
* @returns A Promise that resolves to the signer
*/
const getSigner = async (): Promise<Signer> => {
return signer
}

/**
* @description Gets the counterfactual address of the account
* @returns The counterfactual address
Expand Down Expand Up @@ -364,15 +355,6 @@ export const toNexusAccount = async (
defaultedActiveModule = validationModule;
}

/**
* @description Gets the active validation module for the account
* @returns The active validation module
*/
const getActiveValidationModule = (): BaseValidationModule => {
return defaultedActiveModule;
}


/**
* @description Signs a message
* @param params - The parameters for signing
Expand All @@ -382,9 +364,7 @@ export const toNexusAccount = async (
const signMessage = async ({
message
}: { message: SignableMessage }): Promise<Hex> => {
const tempSignature = await defaultedActiveModule
.getSigner()
.signMessage({ message })
const tempSignature = await defaultedActiveModule.getSigner().signMessage({ message })

const signature = encodePacked(
["address", "bytes"],
Expand Down Expand Up @@ -540,7 +520,6 @@ export const toNexusAccount = async (
getUserOpHash,
setActiveValidationModule,
getActiveValidationModule: () => defaultedActiveModule,
getSigner,
factoryData,
factoryAddress
}
Expand Down
40 changes: 17 additions & 23 deletions src/sdk/clients/createNexusClient.test.ts
Original file line number Diff line number Diff line change
@@ -1,14 +1,8 @@
import { AbiCoder, ParamType } from "ethers/abi"
import { JsonRpcProvider } from "ethers/providers"
import { Wallet } from "ethers/wallet"
import {
http,
type AbiParameter,
type Account,
type Address,
type Chain,
type Hex,
encodeAbiParameters,
encodeFunctionData,
parseEther
} from "viem"
Expand All @@ -19,7 +13,6 @@ import { toNetwork } from "../../test/testSetup"
import {
getBalance,
getTestAccount,
getTestSmartAccountClient,
killNetwork,
toTestClient,
topUp
Expand Down Expand Up @@ -56,10 +49,11 @@ describe("nexus.client", async () => {

testClient = toTestClient(chain, getTestAccount(5))

nexusClient = await getTestSmartAccountClient({
account,
nexusClient = await createNexusClient({
signer: account,
chain,
bundlerUrl
transport: http(),
bundlerTransport: http(bundlerUrl)
})

nexusAccountAddress = await nexusClient.account.getCounterFactualAddress()
Expand Down Expand Up @@ -202,19 +196,19 @@ describe("nexus.client", async () => {
test("should have correct fields", async () => {
const chainId = 1
const chain = getChain(chainId)
;[
"blockExplorers",
"contracts",
"fees",
"formatters",
"id",
"name",
"nativeCurrency",
"rpcUrls",
"serializers"
].every((field) => {
expect(chain).toHaveProperty(field)
})
;[
"blockExplorers",
"contracts",
"fees",
"formatters",
"id",
"name",
"nativeCurrency",
"rpcUrls",
"serializers"
].every((field) => {
expect(chain).toHaveProperty(field)
})
})

test("should throw an error, chain id not found", async () => {
Expand Down
100 changes: 50 additions & 50 deletions src/sdk/clients/createNexusClient.ts
Original file line number Diff line number Diff line change
Expand Up @@ -51,34 +51,34 @@ export type NexusClient<
ClientConfig<transport, chain, account, rpcSchema>,
"cacheTime" | "chain" | "key" | "name" | "pollingInterval" | "rpcSchema"
> &
BundlerActions<NexusAccount> &
Erc7579Actions<NexusAccount> &
SmartAccountActions<chain, NexusAccount> & {
/**
* The Nexus account associated with this client
*/
account: NexusAccount
/**
* Optional client for additional functionality
*/
client?: client | Client | undefined
/**
* Transport configuration for the bundler
*/
bundlerTransport?: BundlerClientConfig["transport"]
/**
* Optional paymaster configuration
*/
paymaster?: BundlerClientConfig["paymaster"] | undefined
/**
* Optional paymaster context
*/
paymasterContext?: BundlerClientConfig["paymasterContext"] | undefined
/**
* Optional user operation configuration
*/
userOperation?: BundlerClientConfig["userOperation"] | undefined
}
BundlerActions<NexusAccount> &
Erc7579Actions<NexusAccount> &
SmartAccountActions<chain, NexusAccount> & {
/**
* The Nexus account associated with this client
*/
account: NexusAccount
/**
* Optional client for additional functionality
*/
client?: client | Client | undefined
/**
* Transport configuration for the bundler
*/
bundlerTransport?: BundlerClientConfig["transport"]
/**
* Optional paymaster configuration
*/
paymaster?: BundlerClientConfig["paymaster"] | undefined
/**
* Optional paymaster context
*/
paymasterContext?: BundlerClientConfig["paymasterContext"] | undefined
/**
* Optional user operation configuration
*/
userOperation?: BundlerClientConfig["userOperation"] | undefined
}
>

/**
Expand Down Expand Up @@ -109,37 +109,37 @@ export type NexusClientConfig<
client?: client | Client | undefined
/** Paymaster configuration. */
paymaster?:
| true
| {
/** Retrieves paymaster-related User Operation properties to be used for sending the User Operation. */
getPaymasterData?: PaymasterActions["getPaymasterData"] | undefined
/** Retrieves paymaster-related User Operation properties to be used for gas estimation. */
getPaymasterStubData?:
| PaymasterActions["getPaymasterStubData"]
| undefined
}
| true
| {
/** Retrieves paymaster-related User Operation properties to be used for sending the User Operation. */
getPaymasterData?: PaymasterActions["getPaymasterData"] | undefined
/** Retrieves paymaster-related User Operation properties to be used for gas estimation. */
getPaymasterStubData?:
| PaymasterActions["getPaymasterStubData"]
| undefined
}
| undefined
/** Paymaster context to pass to `getPaymasterData` and `getPaymasterStubData` calls. */
paymasterContext?: unknown
/** User Operation configuration. */
userOperation?:
| {
/** Prepares fee properties for the User Operation request. */
estimateFeesPerGas?:
| ((parameters: {
account: account | SmartAccount
bundlerClient: Client
userOperation: UserOperationRequest
}) => Promise<EstimateFeesPerGasReturnType<"eip1559">>)
| undefined
}
| {
/** Prepares fee properties for the User Operation request. */
estimateFeesPerGas?:
| ((parameters: {
account: account | SmartAccount
bundlerClient: Client
userOperation: UserOperationRequest
}) => Promise<EstimateFeesPerGasReturnType<"eip1559">>)
| undefined
}
| undefined
/** Owner of the account. */
signer: UnknownSigner
/** Index of the account. */
index?: bigint
/** Active module of the account. */
activeModule?: BaseValidationModule
activeValidationModule?: BaseValidationModule
/** Executor module of the account. */
executorModule?: BaseExecutionModule
/** Factory address of the account. */
Expand Down Expand Up @@ -177,7 +177,7 @@ export async function createNexusClient(
index = 0n,
key = "nexus client",
name = "Nexus Client",
activeModule,
activeValidationModule,
factoryAddress = contracts.k1ValidatorFactory.address,
k1ValidatorAddress = contracts.k1Validator.address,
bundlerTransport,
Expand All @@ -204,7 +204,7 @@ export async function createNexusClient(
chain,
signer,
index,
activeModule,
activeValidationModule,
factoryAddress,
k1ValidatorAddress
})
Expand Down
6 changes: 5 additions & 1 deletion src/sdk/clients/decorators/erc7579/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -51,7 +51,11 @@ import {

export type Erc7579Actions<TSmartAccount extends SmartAccount | undefined> = {
accountId: (args?: GetSmartAccountParameter<TSmartAccount>) => Promise<string>
installModule: (args: InstallModuleParameters<TSmartAccount> & { signatureOverride?: Hex }) => Promise<Hash>
installModule: (
args: InstallModuleParameters<TSmartAccount> & {
signatureOverride?: Hex
}
) => Promise<Hash>
installModules: (
args: InstallModulesParameters<TSmartAccount> & {
signatureOverride?: Hex
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,6 @@ import {
fundAndDeployClients,
getBalance,
getTestAccount,
getTestSmartAccountClient,
killNetwork,
toTestClient
} from "../../../../test/testUtils"
Expand Down Expand Up @@ -38,12 +37,12 @@ describe("account.decorators", async () => {
recipientAddress = recipient.address
testClient = toTestClient(chain, getTestAccount(5))

nexusClient = await getTestSmartAccountClient({
account,
nexusClient = await createNexusClient({
signer: account,
chain,
bundlerUrl
transport: http(),
bundlerTransport: http(bundlerUrl)
})

nexusAccountAddress = await nexusClient.account.getCounterFactualAddress()
await fundAndDeployClients(testClient, [nexusClient])
})
Expand Down
Loading

0 comments on commit 5531d6b

Please sign in to comment.