Skip to content

Commit

Permalink
adding kernel account and service to project (#49)
Browse files Browse the repository at this point in the history
* adding kernel account and service to project
adding permissionless and updating viem
changing balance indicator to use kernel account

* replacing SimpleAccountClientService with KernelAccountClientService everywhere
adding execute call oto kernelaccount

* use external RPC URLs

* getting kernel fulfill working

* fixing tests

* comment

---------

Co-authored-by: Carlos Febres <cfebres@eco.com>
  • Loading branch information
StoyanD and carlosfebres authored Oct 24, 2024
1 parent 73ea0cd commit b3f9c03
Show file tree
Hide file tree
Showing 29 changed files with 662 additions and 194 deletions.
3 changes: 2 additions & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -41,11 +41,12 @@
"lodash": "^4.17.21",
"mongoose": "^8.5.0",
"nestjs-pino": "^4.1.0",
"permissionless": "^0.2.11",
"pino-http": "^10.2.0",
"redlock": "^5.0.0-beta.2",
"reflect-metadata": "^0.2.0",
"rxjs": "^7.8.1",
"viem": "^2.21.2"
"viem": "^2.21.32"
},
"devDependencies": {
"@golevelup/ts-jest": "^0.5.0",
Expand Down
9 changes: 5 additions & 4 deletions src/balance/balance.service.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,9 +4,10 @@ import { Solver } from '../eco-configs/eco-config.types'
import { getDestinationNetworkAddressKey } from '../common/utils/strings'
import { EcoLogMessage } from '../common/logging/eco-log-message'
import { erc20Abi, Hex } from 'viem'
import { SimpleAccountClientService } from '../transaction/simple-account-client.service'
import { ViemEventLog } from '../common/events/viem'
import { decodeTransferLog, isSupportedTokenType } from '../contracts'
import { KernelAccountClientService } from '../transaction/smart-wallets/kernel/kernel-account-client.service'

type TokenBalance = { decimals: bigint; balance: bigint }

/**
Expand All @@ -20,7 +21,7 @@ export class BalanceService implements OnApplicationBootstrap {

constructor(
private readonly ecoConfig: EcoConfigService,
private readonly simpleAccountClientService: SimpleAccountClientService,
private readonly kernelAccountClientService: KernelAccountClientService,
) {}

async onApplicationBootstrap() {
Expand Down Expand Up @@ -90,7 +91,7 @@ export class BalanceService implements OnApplicationBootstrap {
): Promise<TokenBalance | undefined> {
const key = getDestinationNetworkAddressKey(chainID, tokenAddress)
if (!this.tokenBalances.has(key)) {
const client = await this.simpleAccountClientService.getClient(chainID)
const client = await this.kernelAccountClientService.getClient(chainID)
const erc20 = {
address: tokenAddress,
abi: erc20Abi,
Expand All @@ -101,7 +102,7 @@ export class BalanceService implements OnApplicationBootstrap {
{
...erc20,
functionName: 'balanceOf',
args: [client.simpleAccountAddress],
args: [client.kernelAccount.address],
},
{
...erc20,
Expand Down
8 changes: 4 additions & 4 deletions src/balance/balance.ws.service.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,10 +6,10 @@ import { QUEUES } from '../common/redis/constants'
import { InjectQueue } from '@nestjs/bullmq'
import { ViemEventLog } from '../common/events/viem'
import { erc20Abi, Hex, WatchContractEventReturnType, zeroHash } from 'viem'
import { SimpleAccountClientService } from '../transaction/simple-account-client.service'
import { convertBigIntsToStrings } from '../common/viem/utils'
import { EcoLogMessage } from '../common/logging/eco-log-message'
import { getIntentJobId } from '../common/utils/strings'
import { KernelAccountClientService } from '../transaction/smart-wallets/kernel/kernel-account-client.service'

@Injectable()
export class BalanceWebsocketService implements OnApplicationBootstrap, OnModuleDestroy {
Expand All @@ -19,7 +19,7 @@ export class BalanceWebsocketService implements OnApplicationBootstrap, OnModule

constructor(
@InjectQueue(QUEUES.ETH_SOCKET.queue) private readonly ethQueue: Queue,
private readonly simpleAccountClientService: SimpleAccountClientService,
private readonly kernelAccountClientService: KernelAccountClientService,
private readonly ecoConfigService: EcoConfigService,
) {}

Expand All @@ -37,7 +37,7 @@ export class BalanceWebsocketService implements OnApplicationBootstrap, OnModule

const websocketTasks = Object.entries(this.ecoConfigService.getSolvers()).map(
async ([, solver]) => {
const client = await this.simpleAccountClientService.getClient(solver.chainID)
const client = await this.kernelAccountClientService.getClient(solver.chainID)
// const instanceAddress = this.alchemyService.getWallet(solver.network).address

Object.entries(solver.targets).forEach(([address, source]) => {
Expand All @@ -48,7 +48,7 @@ export class BalanceWebsocketService implements OnApplicationBootstrap, OnModule
abi: erc20Abi,
eventName: 'Transfer',
// restrict transfers from anyone to the simple account address
args: { to: client.simpleAccountAddress },
args: { to: client.kernelAccount.address },
onLogs: this.addJob(solver.network, solver.chainID) as any,
})
}
Expand Down
6 changes: 3 additions & 3 deletions src/chain-monitor/chain-sync.service.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,8 +8,8 @@ import { SourceIntent } from '../eco-configs/eco-config.types'
import { IntentCreatedLog, IntentSourceAbi } from '../contracts'
import { entries } from 'lodash'
import { BlockTag } from 'viem'
import { SimpleAccountClientService } from '../transaction/simple-account-client.service'
import { WatchIntentService } from '../intent/watch-intent.service'
import { KernelAccountClientService } from '../transaction/smart-wallets/kernel/kernel-account-client.service'

/**
* Service class for syncing any missing transactions for all the source intent contracts.
Expand All @@ -23,7 +23,7 @@ export class ChainSyncService implements OnApplicationBootstrap {

constructor(
@InjectModel(SourceIntentModel.name) private intentModel: Model<SourceIntentModel>,
private readonly simpleAccountClientService: SimpleAccountClientService,
private readonly kernelAccountClientService: KernelAccountClientService,
private readonly watchIntentService: WatchIntentService,
private ecoConfigService: EcoConfigService,
) {}
Expand Down Expand Up @@ -72,7 +72,7 @@ export class ChainSyncService implements OnApplicationBootstrap {
* @returns
*/
async getMissingTxs(source: SourceIntent): Promise<IntentCreatedLog[]> {
const client = await this.simpleAccountClientService.getClient(source.chainID)
const client = await this.kernelAccountClientService.getClient(source.chainID)
const solverSupportedChains = entries(this.ecoConfigService.getSolvers()).map(([chainID]) =>
BigInt(chainID),
)
Expand Down
10 changes: 5 additions & 5 deletions src/chain-monitor/tests/chain-sync.service.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,11 +9,11 @@ import { Model } from 'mongoose'
import { Solver, SourceIntent } from '../../eco-configs/eco-config.types'
import { IntentSourceAbi } from '../../contracts'
import { entries } from 'lodash'
import { SimpleAccountClientService } from '../../transaction/simple-account-client.service'
import { KernelAccountClientService } from '../../transaction/smart-wallets/kernel/kernel-account-client.service'

describe('ChainSyncService', () => {
let chainSyncService: ChainSyncService
let accountService: DeepMocked<SimpleAccountClientService>
let accountService: DeepMocked<KernelAccountClientService>
let watchIntentService: DeepMocked<WatchIntentService>
let ecoConfigService: DeepMocked<EcoConfigService>

Expand All @@ -22,8 +22,8 @@ describe('ChainSyncService', () => {
providers: [
ChainSyncService,
{
provide: SimpleAccountClientService,
useValue: createMock<SimpleAccountClientService>(),
provide: KernelAccountClientService,
useValue: createMock<KernelAccountClientService>(),
},
{ provide: WatchIntentService, useValue: createMock<WatchIntentService>() },
{ provide: EcoConfigService, useValue: createMock<EcoConfigService>() },
Expand All @@ -35,7 +35,7 @@ describe('ChainSyncService', () => {
}).compile()

chainSyncService = chainMod.get(ChainSyncService)
accountService = chainMod.get(SimpleAccountClientService)
accountService = chainMod.get(KernelAccountClientService)
watchIntentService = chainMod.get(WatchIntentService)
ecoConfigService = chainMod.get(EcoConfigService) as DeepMocked<EcoConfigService>
})
Expand Down
2 changes: 1 addition & 1 deletion src/common/chains/transport.ts
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ import { getRpcUrl } from '../viem/utils'
*/
export function getTransport(
chain: Chain,
apiKey: string,
apiKey?: string,
websocketEnabled: boolean = false,
): WebSocketTransport | HttpTransport {
const { url, isWebsocket } = getRpcUrl(chain, apiKey, websocketEnabled)
Expand Down
6 changes: 5 additions & 1 deletion src/common/errors/eco-error.ts
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,11 @@ export class EcoError extends Error {
}

static InvalidSimpleAccountConfig() {
return new EcoError(`The simple account is invalid`)
return new EcoError(`The simple account config is invalid`)
}

static InvalidKernelAccountConfig() {
return new EcoError(`The kernel account config is invalid`)
}

static FeasableIntentNoTransactionError = new Error('No transaction data found')
Expand Down
24 changes: 24 additions & 0 deletions src/contracts/Inbox.contract.ts
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,17 @@ export const InboxAbi = [
name: 'CallToMailbox',
type: 'error',
},
{
inputs: [
{
internalType: 'uint256',
name: '_requiredFee',
type: 'uint256',
},
],
name: 'InsufficientFee',
type: 'error',
},
{
inputs: [
{
Expand Down Expand Up @@ -322,6 +333,19 @@ export const InboxAbi = [
stateMutability: 'nonpayable',
type: 'function',
},
{
inputs: [
{
internalType: 'address',
name: '_destination',
type: 'address',
},
],
name: 'drain',
outputs: [],
stateMutability: 'nonpayable',
type: 'function',
},
{
inputs: [
{
Expand Down
87 changes: 87 additions & 0 deletions src/contracts/KernelAccount.abi.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,87 @@
/**
* The exeute abi, used to execute a transaction on the kernel smart account
*/
export const KernelExecuteAbi = [
{
inputs: [
{
internalType: 'address',
name: 'to',
type: 'address',
},
{
internalType: 'uint256',
name: 'value',
type: 'uint256',
},
{
internalType: 'bytes',
name: 'data',
type: 'bytes',
},
{
internalType: 'enum Operation',
name: '',
type: 'uint8',
},
],
name: 'execute',
outputs: [],
stateMutability: 'payable',
type: 'function',
},
{
inputs: [
{
components: [
{
internalType: 'address',
name: 'to',
type: 'address',
},
{
internalType: 'uint256',
name: 'value',
type: 'uint256',
},
{
internalType: 'bytes',
name: 'data',
type: 'bytes',
},
],
internalType: 'struct Call[]',
name: 'calls',
type: 'tuple[]',
},
],
name: 'executeBatch',
outputs: [],
stateMutability: 'payable',
type: 'function',
},
] as const

/**
* The init abi, used to initialise kernel account
*/
export const KernelInitAbi = [
{
inputs: [
{
internalType: 'contract IKernelValidator',
name: '_defaultValidator',
type: 'address',
},
{
internalType: 'bytes',
name: '_data',
type: 'bytes',
},
],
name: 'initialize',
outputs: [],
stateMutability: 'payable',
type: 'function',
},
] as const
Loading

0 comments on commit b3f9c03

Please sign in to comment.