-
Notifications
You must be signed in to change notification settings - Fork 36
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
510ecab
commit 831b35a
Showing
5 changed files
with
102 additions
and
1 deletion.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters