Run code when creating an account
Sometimes, you might want to execute some custom code when you create an account. One common use case is when you want to emit an event whenever a user creates an account.
Running custom code during initialization can be done via plugins. Specifically you can follow this process:
- Write a custom validator that executes your custom code during initialization.
- Install the validator to your account during account initialization.
Developing a custom validator
Kernel implements the ERC-7579 interface, so you can write a 7579 validator.
Here's an example of a validator that simply emits an event. Note that the onInstall
function accepts an initialization data, which you can use to run customized code per account.
Once you have developed the custom validator, deploy it using CREATE2
so that it has the same address across all chains.
Install the validator
Once you have deployed the validator, the easiest way to install it is like this:
const account = await createKernelAccount(publicClient, {
plugins: {
sudo: ecdsaValidator,
},
entryPoint,
kernelVersion,
pluginMigrations: [
{
// This is the deployed validator address
address: "0x43C757131417c5a245a99c4D5B7722ec20Cb0b31",
type: PLUGIN_TYPE.VALIDATOR,
// this is the data passed to `onInstall`
data: "0x",
},
],
})
Now, your validator's onInstall
function will run whenever an account is created.