Skip to content

Commit

Permalink
chore: sort test folder
Browse files Browse the repository at this point in the history
  • Loading branch information
joepegler committed Aug 28, 2024
1 parent cd3fe4a commit 45b59cf
Show file tree
Hide file tree
Showing 44 changed files with 4,416 additions and 268 deletions.
Binary file modified bun.lockb
Binary file not shown.
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -96,7 +96,7 @@
"execa": "^9.3.1",
"get-port": "^7.1.0",
"gh-pages": "^6.1.1",
"nexus": "github:bcnmy/nexus#f08ec9f2d6cd2cf0044b901cc4c4a1cb5c527273",
"nexus": "github:bcnmy/nexus#179c0c6ce0c65faf23a40fbf1f9e9dde1ea71bf7",
"prool": "^0.0.16",
"rimraf": "^5.0.5",
"simple-git-hooks": "^2.9.0",
Expand Down
14 changes: 8 additions & 6 deletions scripts/fetch:deployment.ts
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,7 @@ export const getDeployments = async () => {

const tsAbiPath = isForCore
? `${__dirname}/../src/__contracts/abi/${name}Abi.ts`
: `${__dirname}/../tests/__contracts/abi/${name}Abi.ts`
: `${__dirname}/../tests/src/__contracts/abi/${name}Abi.ts`

fs.writeFileSync(tsAbiPath, tsAbiContent)

Expand All @@ -66,14 +66,15 @@ export const getDeployments = async () => {
const abiIndexPath = `${__dirname}/../src/__contracts/abi/index.ts`
fs.writeFileSync(abiIndexPath, abiIndexContent)

const testAbiIndexPath = `${__dirname}/../tests/__contracts/abi/index.ts`
const testAbiIndexPath = `${__dirname}/../tests/src/__contracts/abi/index.ts`
fs.writeFileSync(testAbiIndexPath, testAbiIndexContent)

// Write addresses to src folder
const writeAddressesPath = `${__dirname}/../src/__contracts/addresses.ts`
const writeAddressesPathTest = `${__dirname}/../tests/__contracts/addresses.ts`
const writeAddressesPathTest = `${__dirname}/../tests/src/__contracts/mockAddresses.ts`

const addressesContent = `import type { Hex } from "viem"\nexport const addresses: Record<string, Hex> = ${JSON.stringify(
const addressesContent = `// The contents of this folder is auto-generated. Please do not edit as your changes are likely to be overwritten\n
import type { Hex } from "viem"\nexport const addresses: Record<string, Hex> = ${JSON.stringify(
Object.keys(deployedContracts)
.filter((key) => coreFiles.includes(key))
.reduce((acc, key) => {
Expand All @@ -84,7 +85,8 @@ export const getDeployments = async () => {
2
)} as const;\nexport default addresses\n`

const testAddressesContent = `import type { Hex } from "viem"\nexport const addresses: Record<string, Hex> = ${JSON.stringify(
const testAddressesContent = `// The contents of this folder is auto-generated. Please do not edit as your changes are likely to be overwritten\n
import type { Hex } from "viem"\nexport const mockAddresses: Record<string, Hex> = ${JSON.stringify(
Object.keys(deployedContracts)
.filter((key) => testFiles.includes(key))
.reduce((acc, key) => {
Expand All @@ -93,7 +95,7 @@ export const getDeployments = async () => {
}, {}),
null,
2
)} as const;\nexport default addresses\n`
)} as const;\nexport default mockAddresses\n`

fs.writeFileSync(writeAddressesPath, addressesContent)
fs.writeFileSync(writeAddressesPathTest, testAddressesContent)
Expand Down
1 change: 0 additions & 1 deletion src/__contracts/README.md

This file was deleted.

6 changes: 3 additions & 3 deletions src/__contracts/addresses.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import type { Hex } from "viem"
export const addresses: Record<string, Hex> = {
Nexus: "0x9fE46736679d2D9a65F0992F2272dE9f3c7fa6e0",
K1Validator: "0xDc64a140Aa3E981100a9becA4E685f962f0cF6C9",
K1ValidatorFactory: "0xa513E6E4b8f2a923D98304ec87F64353C4D5C853"
Nexus: "0x776d63154D2aa9256D72C420416c930F3B735464",
K1Validator: "0xd98238BBAeA4f91683d250003799EAd31d7F5c55",
K1ValidatorFactory: "0x8025afaD10209b8bEF3A3C94684AaE4D309c9996"
} as const
export default addresses
99 changes: 52 additions & 47 deletions src/account/NexusSmartAccount.ts
Original file line number Diff line number Diff line change
Expand Up @@ -54,7 +54,7 @@ import {
} from "../paymaster/index.js"
import {
BaseSmartContractAccount,
type DeploymentState
DeploymentState
} from "./BaseSmartContractAccount.js"
import {
Logger,
Expand Down Expand Up @@ -168,16 +168,6 @@ export class NexusSmartAccount extends BaseSmartContractAccount {
this.activeValidationModule =
// biome-ignore lint/style/noNonNullAssertion: <explanation>
nexusSmartAccountConfig.activeValidationModule!

// this.publicClient = createPublicClient({
// chain,
// transport: http(rpcClient)
// })

// this.scanForUpgradedAccountsFromV1 =
// nexusSmartAccountConfig.scanForUpgradedAccountsFromV1 ?? false
// this.maxIndexForScan = nexusSmartAccountConfig.maxIndexForScan ?? 10n
// this.getAddress()
}

/**
Expand Down Expand Up @@ -280,7 +270,9 @@ export class NexusSmartAccount extends BaseSmartContractAccount {
entryPointAddress: defaultedEntryPointAddress
}

return new NexusSmartAccount(config)
const smartAccount = new NexusSmartAccount(config)
await smartAccount.getDeploymentState()
return smartAccount
}

override async getAddress(params?: CounterFactualAddressParam): Promise<Hex> {
Expand Down Expand Up @@ -323,13 +315,13 @@ export class NexusSmartAccount extends BaseSmartContractAccount {
*
* const smartAccount = await createSmartAccountClient({ signer, bundlerUrl, paymasterUrl }); // Retrieve bundler/paymaster url from dashboard
* const encodedCall = encodeFunctionData({
* abi: parseAbi(["function safeMint(address to) public"]),
* functionName: "safeMint",
* abi: CounterAbi,
* functionName: "incrementNumber",
* args: ["0x..."],
* });
*
* const tx = {
* to: nftAddress,
* to: mockAddresses.Counter,
* data: encodedCall
* }
*
Expand Down Expand Up @@ -591,14 +583,17 @@ export class NexusSmartAccount extends BaseSmartContractAccount {
async _getAccountContract(): Promise<
GetContractReturnType<typeof NexusAbi, PublicClient>
> {
if (this.accountContract == null) {
this.accountContract = getContract({
address: await this.getAddress(),
abi: NexusAbi,
client: this.publicClient as PublicClient
})
if (await this.isAccountDeployed()) {
if (!this.accountContract) {
this.accountContract = getContract({
address: await this.getAddress(),
abi: NexusAbi,
client: this.publicClient as PublicClient
})
}
return this.accountContract
}
return this.accountContract
throw new Error(ERROR_MESSAGES.ACCOUNT_NOT_DEPLOYED)
}

isActiveValidationModuleDefined(): boolean {
Expand Down Expand Up @@ -676,10 +671,6 @@ export class NexusSmartAccount extends BaseSmartContractAccount {
return this
}

setDeploymentState(deploymentState: DeploymentState) {
this.deploymentState = deploymentState
}

// async getV1AccountsUpgradedToV2(
// params: QueryParamsForAddressResolver
// ): Promise<Hex> {
Expand Down Expand Up @@ -899,13 +890,13 @@ export class NexusSmartAccount extends BaseSmartContractAccount {
*
* const smartAccount = await createSmartAccountClient({ signer, bundlerUrl }); // Retrieve bundler url from dashboard
* const encodedCall = encodeFunctionData({
* abi: parseAbi(["function safeMint(address to) public"]),
* functionName: "safeMint",
* abi: CounterAbi,
* functionName: "incrementNumber",
* args: ["0x..."],
* });
*
* const transaction = {
* to: nftAddress,
* to: mockAddresses.Counter,
* data: encodedCall
* }
*
Expand Down Expand Up @@ -1028,13 +1019,13 @@ export class NexusSmartAccount extends BaseSmartContractAccount {
*
* const smartAccount = await createSmartAccountClient({ signer, bundlerUrl }); // Retrieve bundler url from dashboard
* const encodedCall = encodeFunctionData({
* abi: parseAbi(["function safeMint(address to) public"]),
* functionName: "safeMint",
* abi: CounterAbi,
* functionName: "incrementNumber",
* args: ["0x..."],
* });
*
* const transaction = {
* to: nftAddress,
* to: mockAddresses.Counter,
* data: encodedCall
* }
*
Expand Down Expand Up @@ -1273,13 +1264,13 @@ export class NexusSmartAccount extends BaseSmartContractAccount {
*
* const smartAccount = await createSmartAccountClient({ signer, bundlerUrl }); // Retrieve bundler url from dashboard
* const encodedCall = encodeFunctionData({
* abi: parseAbi(["function safeMint(address to) public"]),
* functionName: "safeMint",
* abi: CounterAbi,
* functionName: "incrementNumber",
* args: ["0x..."],
* });
*
* const transaction = {
* to: nftAddress,
* to: mockAddresses.Counter,
* data: encodedCall
* }
*
Expand All @@ -1302,13 +1293,13 @@ export class NexusSmartAccount extends BaseSmartContractAccount {
*
* const smartAccount = await createSmartAccountClient({ signer, bundlerUrl }); // Retrieve bundler url from dashboard
* const encodedCall = encodeFunctionData({
* abi: parseAbi(["function safeMint(address to) public"]),
* functionName: "safeMint",
* abi: CounterAbi,
* functionName: "incrementNumber",
* args: ["0x..."],
* });
*
* const transaction = {
* to: nftAddress,
* to: mockAddresses.Counter,
* data: encodedCall
* }
*
Expand All @@ -1331,7 +1322,17 @@ export class NexusSmartAccount extends BaseSmartContractAccount {
: [manyOrOneTransactions],
buildUseropDto
)
return this.sendUserOp(userOp)
const payload = await this.sendUserOp(userOp)
this.setDeploymentState(payload) // Don't wait
return payload
}

private async setDeploymentState({ wait }: UserOpResponse) {
if (this.deploymentState === DeploymentState.DEPLOYED) return
const { success } = await wait()
if (success) {
this.deploymentState = DeploymentState.DEPLOYED
}
}

async sendTransactionWithExecutor(
Expand Down Expand Up @@ -1373,13 +1374,13 @@ export class NexusSmartAccount extends BaseSmartContractAccount {
*
* const smartAccount = await createSmartAccountClient({ signer, bundlerUrl }); // Retrieve bundler url from dashboard
* const encodedCall = encodeFunctionData({
* abi: parseAbi(["function safeMint(address to) public"]),
* functionName: "safeMint",
* abi: CounterAbi,
* functionName: "incrementNumber",
* args: ["0x..."],
* });
*
* const transaction = {
* to: nftAddress,
* to: mockAddresses.Counter,
* data: encodedCall
* }
*
Expand Down Expand Up @@ -1413,12 +1414,13 @@ export class NexusSmartAccount extends BaseSmartContractAccount {
sender: (await this.getAddress()) as Hex,
nonce: nonceFromFetch,
factoryData,
factory: (await this.isAccountDeployed())
? undefined
: this.factoryAddress,
callData
}

if (!(await this.isAccountDeployed())) {
userOp.factory = this.factoryAddress
}

userOp.signature = dummySignature

const gasFeeValues: GetUserOperationGasPriceReturnType | undefined =
Expand Down Expand Up @@ -1722,11 +1724,11 @@ export class NexusSmartAccount extends BaseSmartContractAccount {
async isModuleInstalled(module: Module) {
if (await this.isAccountDeployed()) {
const accountContract = await this._getAccountContract()
return (await accountContract.read.isModuleInstalled([
return await accountContract.read.isModuleInstalled([
BigInt(moduleTypeIds[module.type]),
module.moduleAddress,
module.data ?? "0x"
])) as boolean
])
}
Logger.warn("A module cannot be installed on an undeployed account")
return false
Expand Down Expand Up @@ -2006,7 +2008,10 @@ export class NexusSmartAccount extends BaseSmartContractAccount {
const executors = await this.getInstalledExecutors()
const hook = await this.getActiveHook()
const fallbackHandler = await this.getFallbackBySelector()

return [...validators, ...executors, hook, fallbackHandler]
.flat()
.filter(Boolean)
}

/**
Expand Down
26 changes: 16 additions & 10 deletions src/modules/executors/OwnableExecutor.ts
Original file line number Diff line number Diff line change
Expand Up @@ -11,48 +11,51 @@ import { SENTINEL_ADDRESS } from "../../account"
import type { NexusSmartAccount } from "../../account/NexusSmartAccount"
import type { UserOpReceipt } from "../../bundler"
import { BaseExecutionModule } from "../base/BaseExecutionModule"
import { OWNABLE_EXECUTOR } from "../utils/Constants"
import type { Execution, Module } from "../utils/Types"

export class OwnableExecutorModule extends BaseExecutionModule {
smartAccount!: NexusSmartAccount
public owners: Address[]
private address: Address

public constructor(
module: Module,
smartAccount: NexusSmartAccount,
owners: Address[]
owners: Address[],
address: Address
) {
super(module, smartAccount.getSigner())
this.smartAccount = smartAccount
this.owners = owners
this.data = module.data ?? "0x"
this.address = address
}

public static async create(
smartAccount: NexusSmartAccount,
address: Address,
data?: Hex
): Promise<OwnableExecutorModule> {
const module: Module = {
moduleAddress: OWNABLE_EXECUTOR,
moduleAddress: address,
type: "executor",
data: data ?? "0x",
additionalContext: "0x"
}
const owners = await smartAccount.publicClient.readContract({
address: OWNABLE_EXECUTOR,
address,
abi: parseAbi([
"function getOwners(address account) external view returns (address[])"
]),
functionName: "getOwners",
args: [await smartAccount.getAddress()]
})
const instance = new OwnableExecutorModule(
return new OwnableExecutorModule(
module,
smartAccount,
owners as Address[]
owners as Address[],
address
)
return instance
}

public async execute(
Expand Down Expand Up @@ -140,7 +143,7 @@ export class OwnableExecutorModule extends BaseExecutionModule {
}

public async removeOwner(ownerToRemove: Address): Promise<UserOpReceipt> {
const owners = await this.getOwners()
const owners = await this.getOwners(this.address)
let prevOwner: Address

const currentOwnerIndex = owners.findIndex(
Expand Down Expand Up @@ -175,9 +178,12 @@ export class OwnableExecutorModule extends BaseExecutionModule {
return receipt
}

public async getOwners(accountAddress?: Address): Promise<Address[]> {
public async getOwners(
moduleAddress: Address,
accountAddress?: Address
): Promise<Address[]> {
const owners = await this.smartAccount.publicClient.readContract({
address: OWNABLE_EXECUTOR,
address: moduleAddress,
abi: parseAbi([
"function getOwners(address account) external view returns (address[])"
]),
Expand Down
2 changes: 0 additions & 2 deletions src/modules/utils/Constants.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,3 @@
import type { ModuleVersion } from "./Types.js"

export const OWNABLE_EXECUTOR = "0x"
export const OWNABLE_VALIDATOR = "0x"
export const DEFAULT_MODULE_VERSION: ModuleVersion = "1.0.0-beta"
Loading

0 comments on commit 45b59cf

Please sign in to comment.