Skip to content

Commit

Permalink
feat: Added getKernelVersion action
Browse files Browse the repository at this point in the history
  • Loading branch information
SahilVasava committed Jan 19, 2025
1 parent 510ecab commit 831b35a
Show file tree
Hide file tree
Showing 5 changed files with 102 additions and 1 deletion.
6 changes: 6 additions & 0 deletions packages/core/CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,5 +1,11 @@
# @zerodev/sdk

## 5.4.14

### Patch Changes

- Added getKernelVersion action

## 5.4.13

### Patch Changes
Expand Down
78 changes: 78 additions & 0 deletions packages/core/actions/public/getKernelVersion.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,78 @@
import { zeroAddress, type Address, type Client } from "viem"
import { getStorageAt } from "viem/actions"
import {
getAction,
getAddress,
isAddressEqual,
isHex,
slice,
toHex
} from "viem/utils"
import {
KERNEL_IMPLEMENTATION_SLOT,
KernelVersionToAddressesMap
} from "../../constants"
import type { KERNEL_VERSION_TYPE } from "../../types"

export type GetKernelVersionParams = {
address: Address
}

/**
* Returns the address of the kernel implementation.
*
* @param client {@link client} that you created using viem's createPublicClient.
* @param args {@link GetKernelVersionParams} address
* @returns KERNEL_VERSION_TYPE
*
* @example
* import { createPublicClient } from "viem"
* import { getKernelImplementationAddress } from "@zerodev/sdk/actions"
*
* const client = createPublicClient({
* chain: goerli,
* transport: http("https://goerli.infura.io/v3/your-infura-key")
* })
*
* const kernelVersion = await getKernelVersion(client, {
* address,
* })
*
* // Return "0.3.1"
*/
export const getKernelVersion = async (
client: Client,
args: GetKernelVersionParams
): Promise<KERNEL_VERSION_TYPE | null> => {
const { address } = args

const storageValue = await getAction(
client,
getStorageAt,
"getStorageAt"
)({
address,
slot: KERNEL_IMPLEMENTATION_SLOT
})
if (!storageValue) {
throw new Error("Kernel version not found")
}
const addressSlice = slice(storageValue, 12)
const addressHex = isHex(addressSlice) ? addressSlice : toHex(addressSlice)
const kernelImplementationAddress = getAddress(addressHex)
if (isAddressEqual(kernelImplementationAddress, zeroAddress)) {
return null
}
const kernelVersion = Object.keys(KernelVersionToAddressesMap).find(
(version) =>
isAddressEqual(
KernelVersionToAddressesMap[version as KERNEL_VERSION_TYPE]
.accountImplementationAddress,
kernelImplementationAddress
)
)
if (!kernelVersion) {
return null
}
return kernelVersion as KERNEL_VERSION_TYPE
}
1 change: 1 addition & 0 deletions packages/core/actions/public/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,4 +9,5 @@ export {
} from "./getSenderAddress.js"
export { isSmartAccountDeployed } from "./isSmartAccountDeployed.js"
export { getKernelImplementationAddress } from "./getKernelImplementationAddress.js"
export { getKernelVersion } from "./getKernelVersion.js"
export { isPluginInstalled } from "./isPluginInstalled.js"
2 changes: 1 addition & 1 deletion packages/core/package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "@zerodev/sdk",
"version": "5.4.13",
"version": "5.4.14",
"author": "ZeroDev",
"main": "./_cjs/index.js",
"module": "./_esm/index.js",
Expand Down
16 changes: 16 additions & 0 deletions packages/test/v0.7/migrationEcdsaKernelAccount.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@ import {
} from "@zerodev/sdk"
import {
getKernelImplementationAddress,
getKernelVersion,
isPluginInstalled
} from "@zerodev/sdk/actions"
import dotenv from "dotenv"
Expand Down Expand Up @@ -171,6 +172,10 @@ describe("ECDSA kernel Account", () => {
}
)
console.log("kernelImplementation", kernelImplementation)
const kernelVersion = await getKernelVersion(publicClient, {
address: kernelAccount.address
})
console.log("kernelVersion", kernelVersion)

const migrationAccount = await createEcdsaKernelMigrationAccount(
publicClient,
Expand Down Expand Up @@ -265,6 +270,10 @@ describe("ECDSA kernel Account", () => {
}
)
console.log("kernelImplementation", kernelImplementation)
const kernelVersion = await getKernelVersion(publicClient, {
address: kernelAccount.address
})
console.log("kernelVersion", kernelVersion)

const migrationAccount = await createEcdsaKernelMigrationAccount(
publicClient,
Expand Down Expand Up @@ -302,6 +311,13 @@ describe("ECDSA kernel Account", () => {
"migrationKernelImplementation",
migrationKernelImplementation
)
const migrationKernelVersion = await getKernelVersion(
publicClient,
{
address: migrationAccount.address
}
)
console.log("migrationKernelVersion", migrationKernelVersion)
const testHash = await migrationKernelClient.sendTransaction({
to: zeroAddress,
value: 0n,
Expand Down

0 comments on commit 831b35a

Please sign in to comment.