eco-solver 0.0.1-security → 1.5.2
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Potentially problematic release.
This version of eco-solver might be problematic. Click here for more details.
- package/.eslintignore +8 -0
- package/.eslintrc.js +24 -0
- package/.github/workflows/ci.yaml +38 -0
- package/.nvmrc +1 -0
- package/.prettierignore +3 -0
- package/.prettierrc +8 -0
- package/Dockerfile +11 -0
- package/LICENSE +21 -0
- package/README.md +29 -5
- package/config/default.ts +135 -0
- package/config/development.ts +95 -0
- package/config/preproduction.ts +17 -0
- package/config/production.ts +17 -0
- package/config/staging.ts +17 -0
- package/config/test.ts +7 -0
- package/index.js +43 -0
- package/jest.config.ts +14 -0
- package/nest-cli.json +8 -0
- package/package.json +117 -6
- package/src/api/api.module.ts +27 -0
- package/src/api/balance.controller.ts +41 -0
- package/src/api/quote.controller.ts +54 -0
- package/src/api/tests/balance.controller.spec.ts +113 -0
- package/src/api/tests/quote.controller.spec.ts +83 -0
- package/src/app.module.ts +74 -0
- package/src/balance/balance.module.ts +14 -0
- package/src/balance/balance.service.ts +230 -0
- package/src/balance/balance.ws.service.ts +104 -0
- package/src/balance/types.ts +16 -0
- package/src/bullmq/bullmq.helper.ts +41 -0
- package/src/bullmq/processors/eth-ws.processor.ts +47 -0
- package/src/bullmq/processors/inbox.processor.ts +55 -0
- package/src/bullmq/processors/interval.processor.ts +54 -0
- package/src/bullmq/processors/processor.module.ts +14 -0
- package/src/bullmq/processors/signer.processor.ts +41 -0
- package/src/bullmq/processors/solve-intent.processor.ts +73 -0
- package/src/bullmq/processors/tests/solve-intent.processor.spec.ts +3 -0
- package/src/bullmq/utils/queue.ts +22 -0
- package/src/chain-monitor/chain-monitor.module.ts +12 -0
- package/src/chain-monitor/chain-sync.service.ts +134 -0
- package/src/chain-monitor/tests/chain-sync.service.spec.ts +190 -0
- package/src/commander/.eslintrc.js +6 -0
- package/src/commander/balance/balance-command.module.ts +12 -0
- package/src/commander/balance/balance.command.ts +73 -0
- package/src/commander/command-main.ts +15 -0
- package/src/commander/commander-app.module.ts +31 -0
- package/src/commander/eco-config.command.ts +20 -0
- package/src/commander/safe/safe-command.module.ts +11 -0
- package/src/commander/safe/safe.command.ts +70 -0
- package/src/commander/transfer/client.command.ts +24 -0
- package/src/commander/transfer/transfer-command.module.ts +26 -0
- package/src/commander/transfer/transfer.command.ts +138 -0
- package/src/commander/utils.ts +8 -0
- package/src/common/chains/definitions/arbitrum.ts +12 -0
- package/src/common/chains/definitions/base.ts +21 -0
- package/src/common/chains/definitions/eco.ts +54 -0
- package/src/common/chains/definitions/ethereum.ts +22 -0
- package/src/common/chains/definitions/helix.ts +53 -0
- package/src/common/chains/definitions/mantle.ts +12 -0
- package/src/common/chains/definitions/optimism.ts +22 -0
- package/src/common/chains/definitions/polygon.ts +12 -0
- package/src/common/chains/supported.ts +26 -0
- package/src/common/chains/transport.ts +19 -0
- package/src/common/errors/eco-error.ts +155 -0
- package/src/common/events/constants.ts +3 -0
- package/src/common/events/viem.ts +22 -0
- package/src/common/logging/eco-log-message.ts +74 -0
- package/src/common/redis/constants.ts +55 -0
- package/src/common/redis/redis-connection-utils.ts +106 -0
- package/src/common/routes/constants.ts +3 -0
- package/src/common/utils/objects.ts +34 -0
- package/src/common/utils/strings.ts +49 -0
- package/src/common/utils/tests/objects.spec.ts +23 -0
- package/src/common/utils/tests/strings.spec.ts +22 -0
- package/src/common/viem/contracts.ts +25 -0
- package/src/common/viem/tests/utils.spec.ts +115 -0
- package/src/common/viem/utils.ts +78 -0
- package/src/contracts/ERC20.contract.ts +389 -0
- package/src/contracts/EntryPoint.V6.contract.ts +1309 -0
- package/src/contracts/KernelAccount.abi.ts +87 -0
- package/src/contracts/OwnableExecutor.abi.ts +128 -0
- package/src/contracts/SimpleAccount.contract.ts +524 -0
- package/src/contracts/inbox.ts +8 -0
- package/src/contracts/index.ts +9 -0
- package/src/contracts/intent-source.ts +55 -0
- package/src/contracts/interfaces/index.ts +1 -0
- package/src/contracts/interfaces/prover.interface.ts +22 -0
- package/src/contracts/prover.ts +9 -0
- package/src/contracts/tests/erc20.contract.spec.ts +59 -0
- package/src/contracts/utils.ts +31 -0
- package/src/decoder/decoder.interface.ts +3 -0
- package/src/decoder/tests/utils.spec.ts +36 -0
- package/src/decoder/utils.ts +24 -0
- package/src/decorators/cacheable.decorator.ts +48 -0
- package/src/eco-configs/aws-config.service.ts +75 -0
- package/src/eco-configs/eco-config.module.ts +44 -0
- package/src/eco-configs/eco-config.service.ts +220 -0
- package/src/eco-configs/eco-config.types.ts +278 -0
- package/src/eco-configs/interfaces/config-source.interface.ts +3 -0
- package/src/eco-configs/tests/aws-config.service.spec.ts +52 -0
- package/src/eco-configs/tests/eco-config.service.spec.ts +137 -0
- package/src/eco-configs/tests/utils.spec.ts +84 -0
- package/src/eco-configs/utils.ts +49 -0
- package/src/fee/fee.module.ts +10 -0
- package/src/fee/fee.service.ts +467 -0
- package/src/fee/tests/fee.service.spec.ts +909 -0
- package/src/fee/tests/utils.spec.ts +49 -0
- package/src/fee/types.ts +44 -0
- package/src/fee/utils.ts +23 -0
- package/src/flags/flags.module.ts +10 -0
- package/src/flags/flags.service.ts +112 -0
- package/src/flags/tests/flags.service.spec.ts +68 -0
- package/src/flags/utils.ts +22 -0
- package/src/health/constants.ts +1 -0
- package/src/health/health.controller.ts +23 -0
- package/src/health/health.module.ts +25 -0
- package/src/health/health.service.ts +40 -0
- package/src/health/indicators/balance.indicator.ts +196 -0
- package/src/health/indicators/eco-redis.indicator.ts +23 -0
- package/src/health/indicators/git-commit.indicator.ts +67 -0
- package/src/health/indicators/mongodb.indicator.ts +11 -0
- package/src/health/indicators/permission.indicator.ts +64 -0
- package/src/intent/create-intent.service.ts +129 -0
- package/src/intent/feasable-intent.service.ts +80 -0
- package/src/intent/fulfill-intent.service.ts +318 -0
- package/src/intent/intent.controller.ts +199 -0
- package/src/intent/intent.module.ts +49 -0
- package/src/intent/schemas/intent-call-data.schema.ts +16 -0
- package/src/intent/schemas/intent-data.schema.ts +114 -0
- package/src/intent/schemas/intent-source.schema.ts +33 -0
- package/src/intent/schemas/intent-token-amount.schema.ts +14 -0
- package/src/intent/schemas/reward-data.schema.ts +48 -0
- package/src/intent/schemas/route-data.schema.ts +52 -0
- package/src/intent/schemas/watch-event.schema.ts +32 -0
- package/src/intent/tests/create-intent.service.spec.ts +215 -0
- package/src/intent/tests/feasable-intent.service.spec.ts +155 -0
- package/src/intent/tests/fulfill-intent.service.spec.ts +564 -0
- package/src/intent/tests/utils-intent.service.spec.ts +308 -0
- package/src/intent/tests/utils.spec.ts +62 -0
- package/src/intent/tests/validate-intent.service.spec.ts +297 -0
- package/src/intent/tests/validation.service.spec.ts +337 -0
- package/src/intent/utils-intent.service.ts +168 -0
- package/src/intent/utils.ts +37 -0
- package/src/intent/validate-intent.service.ts +176 -0
- package/src/intent/validation.sevice.ts +223 -0
- package/src/interceptors/big-int.interceptor.ts +30 -0
- package/src/intervals/interval.module.ts +18 -0
- package/src/intervals/retry-infeasable-intents.service.ts +89 -0
- package/src/intervals/tests/retry-infeasable-intents.service.spec.ts +167 -0
- package/src/kms/errors.ts +0 -0
- package/src/kms/kms.module.ts +12 -0
- package/src/kms/kms.service.ts +65 -0
- package/src/kms/tests/kms.service.spec.ts +60 -0
- package/src/liquidity-manager/jobs/check-balances-cron.job.ts +229 -0
- package/src/liquidity-manager/jobs/liquidity-manager.job.ts +52 -0
- package/src/liquidity-manager/jobs/rebalance.job.ts +61 -0
- package/src/liquidity-manager/liquidity-manager.module.ts +29 -0
- package/src/liquidity-manager/processors/base.processor.ts +117 -0
- package/src/liquidity-manager/processors/eco-protocol-intents.processor.ts +34 -0
- package/src/liquidity-manager/processors/grouped-jobs.processor.ts +103 -0
- package/src/liquidity-manager/queues/liquidity-manager.queue.ts +48 -0
- package/src/liquidity-manager/schemas/rebalance-token.schema.ts +32 -0
- package/src/liquidity-manager/schemas/rebalance.schema.ts +32 -0
- package/src/liquidity-manager/services/liquidity-manager.service.ts +188 -0
- package/src/liquidity-manager/services/liquidity-provider.service.ts +25 -0
- package/src/liquidity-manager/services/liquidity-providers/LiFi/lifi-provider.service.spec.ts +125 -0
- package/src/liquidity-manager/services/liquidity-providers/LiFi/lifi-provider.service.ts +117 -0
- package/src/liquidity-manager/services/liquidity-providers/LiFi/utils/get-transaction-hashes.ts +16 -0
- package/src/liquidity-manager/tests/liquidity-manager.service.spec.ts +142 -0
- package/src/liquidity-manager/types/token-state.enum.ts +5 -0
- package/src/liquidity-manager/types/types.d.ts +52 -0
- package/src/liquidity-manager/utils/address.ts +5 -0
- package/src/liquidity-manager/utils/math.ts +9 -0
- package/src/liquidity-manager/utils/serialize.spec.ts +24 -0
- package/src/liquidity-manager/utils/serialize.ts +47 -0
- package/src/liquidity-manager/utils/token.ts +91 -0
- package/src/main.ts +63 -0
- package/src/nest-redlock/nest-redlock.config.ts +14 -0
- package/src/nest-redlock/nest-redlock.interface.ts +5 -0
- package/src/nest-redlock/nest-redlock.module.ts +64 -0
- package/src/nest-redlock/nest-redlock.service.ts +59 -0
- package/src/prover/proof.service.ts +184 -0
- package/src/prover/prover.module.ts +10 -0
- package/src/prover/tests/proof.service.spec.ts +154 -0
- package/src/quote/dto/quote.intent.data.dto.ts +35 -0
- package/src/quote/dto/quote.reward.data.dto.ts +67 -0
- package/src/quote/dto/quote.route.data.dto.ts +71 -0
- package/src/quote/dto/types.ts +18 -0
- package/src/quote/errors.ts +215 -0
- package/src/quote/quote.module.ts +17 -0
- package/src/quote/quote.service.ts +299 -0
- package/src/quote/schemas/quote-call.schema.ts +16 -0
- package/src/quote/schemas/quote-intent.schema.ts +27 -0
- package/src/quote/schemas/quote-reward.schema.ts +24 -0
- package/src/quote/schemas/quote-route.schema.ts +30 -0
- package/src/quote/schemas/quote-token.schema.ts +14 -0
- package/src/quote/tests/quote.service.spec.ts +444 -0
- package/src/sign/atomic-signer.service.ts +24 -0
- package/src/sign/atomic.nonce.service.ts +114 -0
- package/src/sign/kms-account/kmsToAccount.ts +73 -0
- package/src/sign/kms-account/signKms.ts +30 -0
- package/src/sign/kms-account/signKmsTransaction.ts +37 -0
- package/src/sign/kms-account/signKmsTypedData.ts +21 -0
- package/src/sign/nonce.service.ts +89 -0
- package/src/sign/schemas/nonce.schema.ts +36 -0
- package/src/sign/sign.controller.ts +52 -0
- package/src/sign/sign.helper.ts +23 -0
- package/src/sign/sign.module.ts +27 -0
- package/src/sign/signer-kms.service.ts +27 -0
- package/src/sign/signer.service.ts +26 -0
- package/src/solver/filters/tests/valid-smart-wallet.service.spec.ts +87 -0
- package/src/solver/filters/valid-smart-wallet.service.ts +58 -0
- package/src/solver/solver.module.ts +10 -0
- package/src/transaction/multichain-public-client.service.ts +15 -0
- package/src/transaction/smart-wallets/kernel/actions/encodeData.kernel.ts +57 -0
- package/src/transaction/smart-wallets/kernel/create-kernel-client-v2.account.ts +183 -0
- package/src/transaction/smart-wallets/kernel/create.kernel.account.ts +270 -0
- package/src/transaction/smart-wallets/kernel/index.ts +2 -0
- package/src/transaction/smart-wallets/kernel/kernel-account-client-v2.service.ts +90 -0
- package/src/transaction/smart-wallets/kernel/kernel-account-client.service.ts +107 -0
- package/src/transaction/smart-wallets/kernel/kernel-account.client.ts +105 -0
- package/src/transaction/smart-wallets/kernel/kernel-account.config.ts +34 -0
- package/src/transaction/smart-wallets/simple-account/create.simple.account.ts +19 -0
- package/src/transaction/smart-wallets/simple-account/index.ts +2 -0
- package/src/transaction/smart-wallets/simple-account/simple-account-client.service.ts +42 -0
- package/src/transaction/smart-wallets/simple-account/simple-account.client.ts +83 -0
- package/src/transaction/smart-wallets/simple-account/simple-account.config.ts +5 -0
- package/src/transaction/smart-wallets/smart-wallet.types.ts +38 -0
- package/src/transaction/smart-wallets/utils.ts +14 -0
- package/src/transaction/transaction.module.ts +25 -0
- package/src/transaction/viem_multichain_client.service.ts +100 -0
- package/src/transforms/viem-address.decorator.ts +14 -0
- package/src/utils/bigint.ts +44 -0
- package/src/utils/types.ts +18 -0
- package/src/watch/intent/tests/watch-create-intent.service.spec.ts +257 -0
- package/src/watch/intent/tests/watch-fulfillment.service.spec.ts +141 -0
- package/src/watch/intent/watch-create-intent.service.ts +106 -0
- package/src/watch/intent/watch-event.service.ts +133 -0
- package/src/watch/intent/watch-fulfillment.service.ts +115 -0
- package/src/watch/watch.module.ts +13 -0
- package/test/app.e2e-spec.ts +21 -0
- package/test/jest-e2e.json +9 -0
- package/tsconfig.build.json +4 -0
- package/tsconfig.json +29 -0
@@ -0,0 +1,105 @@
|
|
1
|
+
import {
|
2
|
+
Account,
|
3
|
+
Chain,
|
4
|
+
createPublicClient,
|
5
|
+
Hex,
|
6
|
+
http,
|
7
|
+
Prettify,
|
8
|
+
RpcSchema,
|
9
|
+
Transport,
|
10
|
+
WalletRpcSchema,
|
11
|
+
} from 'viem'
|
12
|
+
import { ExecuteSmartWalletArgs, SmartWalletClient } from '../smart-wallet.types'
|
13
|
+
import { ToEcdsaKernelSmartAccountReturnType } from 'permissionless/accounts'
|
14
|
+
import { KernelWalletActions } from './kernel-account.config'
|
15
|
+
import { encodeKernelExecuteCallData } from './actions/encodeData.kernel'
|
16
|
+
|
17
|
+
export type DeployFactoryArgs = {
|
18
|
+
factory?: Hex | undefined
|
19
|
+
factoryData?: Hex | undefined
|
20
|
+
deployReceipt?: Hex | undefined
|
21
|
+
chainID?: number
|
22
|
+
}
|
23
|
+
|
24
|
+
export type KernelAccountClient<
|
25
|
+
entryPointVersion extends '0.6' | '0.7',
|
26
|
+
transport extends Transport = Transport,
|
27
|
+
chain extends Chain | undefined = Chain | undefined,
|
28
|
+
account extends Account | undefined = Account | undefined,
|
29
|
+
rpcSchema extends RpcSchema | undefined = undefined,
|
30
|
+
> = Prettify<
|
31
|
+
SmartWalletClient<
|
32
|
+
transport,
|
33
|
+
chain,
|
34
|
+
account,
|
35
|
+
rpcSchema extends RpcSchema ? [...WalletRpcSchema, ...rpcSchema] : WalletRpcSchema
|
36
|
+
> & {
|
37
|
+
kernelAccount: ToEcdsaKernelSmartAccountReturnType<entryPointVersion>
|
38
|
+
kernelAccountAddress: Hex
|
39
|
+
}
|
40
|
+
>
|
41
|
+
|
42
|
+
export function KernelAccountActions<
|
43
|
+
entryPointVersion extends '0.6' | '0.7',
|
44
|
+
transport extends Transport,
|
45
|
+
chain extends Chain | undefined = Chain | undefined,
|
46
|
+
account extends Account | undefined = Account | undefined,
|
47
|
+
>(client: KernelAccountClient<entryPointVersion, transport, chain, account>): KernelWalletActions {
|
48
|
+
return {
|
49
|
+
execute: (args) => execute(client, args),
|
50
|
+
deployKernelAccount: () => deployKernelAccount(client),
|
51
|
+
}
|
52
|
+
}
|
53
|
+
|
54
|
+
async function execute<
|
55
|
+
entryPointVersion extends '0.6' | '0.7',
|
56
|
+
chain extends Chain | undefined,
|
57
|
+
account extends Account | undefined,
|
58
|
+
>(
|
59
|
+
client: KernelAccountClient<entryPointVersion, Transport, chain, account>,
|
60
|
+
transactions: ExecuteSmartWalletArgs,
|
61
|
+
): Promise<Hex> {
|
62
|
+
const calls = transactions.map((tx) => ({ to: tx.to, data: tx.data, value: tx.value }))
|
63
|
+
const kernelVersion = client.kernelAccount.entryPoint.version == '0.6' ? '0.2.4' : '0.3.1'
|
64
|
+
const data = encodeKernelExecuteCallData({ calls, kernelVersion })
|
65
|
+
return client.sendTransaction({
|
66
|
+
data: data,
|
67
|
+
kzg: undefined,
|
68
|
+
to: client.kernelAccount.address,
|
69
|
+
chain: client.chain as Chain,
|
70
|
+
account: client.account as Account,
|
71
|
+
})
|
72
|
+
}
|
73
|
+
|
74
|
+
async function deployKernelAccount<
|
75
|
+
entryPointVersion extends '0.6' | '0.7',
|
76
|
+
transport extends Transport,
|
77
|
+
chain extends Chain | undefined = Chain | undefined,
|
78
|
+
account extends Account | undefined = Account | undefined,
|
79
|
+
>(
|
80
|
+
client: KernelAccountClient<entryPointVersion, transport, chain, account>,
|
81
|
+
): Promise<DeployFactoryArgs> {
|
82
|
+
const args: DeployFactoryArgs = {}
|
83
|
+
|
84
|
+
const publicClient = createPublicClient({
|
85
|
+
chain: client.chain,
|
86
|
+
transport: http(),
|
87
|
+
})
|
88
|
+
const code = await publicClient.getCode({ address: client.kernelAccount.address })
|
89
|
+
|
90
|
+
if (!code) {
|
91
|
+
const fa = await client.kernelAccount.getFactoryArgs()
|
92
|
+
args.factory = fa.factory
|
93
|
+
args.factoryData = fa.factoryData
|
94
|
+
args.chainID = client.chain?.id
|
95
|
+
args.deployReceipt = await client.sendTransaction({
|
96
|
+
data: args.factoryData,
|
97
|
+
kzg: undefined,
|
98
|
+
to: args.factory,
|
99
|
+
chain: client.chain as Chain,
|
100
|
+
account: client.account as Account,
|
101
|
+
})
|
102
|
+
await client.waitForTransactionReceipt({ hash: args.deployReceipt, confirmations: 5 })
|
103
|
+
}
|
104
|
+
return args
|
105
|
+
}
|
@@ -0,0 +1,34 @@
|
|
1
|
+
import { KernelVersion, ToEcdsaKernelSmartAccountParameters } from 'permissionless/accounts'
|
2
|
+
import {
|
3
|
+
Account,
|
4
|
+
Chain,
|
5
|
+
LocalAccount,
|
6
|
+
OneOf,
|
7
|
+
Prettify,
|
8
|
+
Transport,
|
9
|
+
WalletClient,
|
10
|
+
WalletClientConfig,
|
11
|
+
} from 'viem'
|
12
|
+
import { SmartWalletActions } from '../smart-wallet.types'
|
13
|
+
import { DeployFactoryArgs } from './kernel-account.client'
|
14
|
+
import { EthereumProvider } from 'permissionless/utils/toOwner'
|
15
|
+
|
16
|
+
export type KernelAccountClientConfig<
|
17
|
+
entryPointVersion extends '0.6' | '0.7',
|
18
|
+
kernelVersion extends KernelVersion<entryPointVersion>,
|
19
|
+
owner extends OneOf<
|
20
|
+
EthereumProvider | WalletClient<Transport, Chain | undefined, Account> | LocalAccount
|
21
|
+
>,
|
22
|
+
> = WalletClientConfig &
|
23
|
+
ToEcdsaKernelSmartAccountParameters<entryPointVersion, kernelVersion, owner>
|
24
|
+
|
25
|
+
export type KernelWalletActions = Prettify<
|
26
|
+
SmartWalletActions & {
|
27
|
+
deployKernelAccount: () => Promise<DeployFactoryArgs>
|
28
|
+
}
|
29
|
+
>
|
30
|
+
|
31
|
+
export const isKernelV2 = (version: KernelVersion<'0.6' | '0.7'>): boolean => {
|
32
|
+
const regex = /0\.2\.\d+/
|
33
|
+
return regex.test(version)
|
34
|
+
}
|
@@ -0,0 +1,19 @@
|
|
1
|
+
import { createWalletClient, publicActions } from 'viem'
|
2
|
+
import { SimpleAccountActions, SimpleAccountClient } from './simple-account.client'
|
3
|
+
import { SimpleAccountClientConfig } from './simple-account.config'
|
4
|
+
|
5
|
+
export function createSimpleAccountClient(
|
6
|
+
parameters: SimpleAccountClientConfig,
|
7
|
+
): SimpleAccountClient {
|
8
|
+
const { key = 'simpleAccountClient', name = 'Simple Account Client', transport } = parameters
|
9
|
+
|
10
|
+
let client = createWalletClient({
|
11
|
+
...parameters,
|
12
|
+
key,
|
13
|
+
name,
|
14
|
+
transport,
|
15
|
+
}) as SimpleAccountClient
|
16
|
+
client.simpleAccountAddress = parameters.simpleAccountAddress
|
17
|
+
client = client.extend(SimpleAccountActions).extend(publicActions) as any
|
18
|
+
return client
|
19
|
+
}
|
@@ -0,0 +1,42 @@
|
|
1
|
+
import { Injectable } from '@nestjs/common'
|
2
|
+
import { EcoConfigService } from '../../../eco-configs/eco-config.service'
|
3
|
+
import { ViemMultichainClientService } from '../../viem_multichain_client.service'
|
4
|
+
import { SimpleAccountClient, SimpleAccountClientConfig } from '.'
|
5
|
+
import { EcoError } from '../../../common/errors/eco-error'
|
6
|
+
import { createSimpleAccountClient } from './create.simple.account'
|
7
|
+
import { SignerService } from '../../../sign/signer.service'
|
8
|
+
import { Chain } from 'viem'
|
9
|
+
|
10
|
+
@Injectable()
|
11
|
+
export class SimpleAccountClientService extends ViemMultichainClientService<
|
12
|
+
SimpleAccountClient,
|
13
|
+
SimpleAccountClientConfig
|
14
|
+
> {
|
15
|
+
constructor(
|
16
|
+
readonly ecoConfigService: EcoConfigService,
|
17
|
+
private readonly signerService: SignerService,
|
18
|
+
) {
|
19
|
+
super(ecoConfigService)
|
20
|
+
}
|
21
|
+
|
22
|
+
protected override async createInstanceClient(
|
23
|
+
configs: SimpleAccountClientConfig,
|
24
|
+
): Promise<SimpleAccountClient> {
|
25
|
+
return createSimpleAccountClient(configs)
|
26
|
+
}
|
27
|
+
|
28
|
+
protected override async buildChainConfig(chain: Chain): Promise<SimpleAccountClientConfig> {
|
29
|
+
const base = await super.buildChainConfig(chain)
|
30
|
+
const simpleAccountConfig = this.ecoConfigService.getEth().simpleAccount
|
31
|
+
|
32
|
+
if (!simpleAccountConfig) {
|
33
|
+
throw EcoError.InvalidSimpleAccountConfig()
|
34
|
+
}
|
35
|
+
|
36
|
+
return {
|
37
|
+
...base,
|
38
|
+
simpleAccountAddress: simpleAccountConfig.walletAddr,
|
39
|
+
account: this.signerService.getAccount(),
|
40
|
+
}
|
41
|
+
}
|
42
|
+
}
|
@@ -0,0 +1,83 @@
|
|
1
|
+
import {
|
2
|
+
encodeFunctionData,
|
3
|
+
Hex,
|
4
|
+
Transport,
|
5
|
+
Chain,
|
6
|
+
Account,
|
7
|
+
RpcSchema,
|
8
|
+
Prettify,
|
9
|
+
WalletRpcSchema,
|
10
|
+
} from 'viem'
|
11
|
+
import { SimpleAccountAbi } from '../../../contracts'
|
12
|
+
import {
|
13
|
+
ExecuteSmartWalletArgs,
|
14
|
+
SmartWalletActions,
|
15
|
+
SmartWalletClient,
|
16
|
+
} from '../smart-wallet.types'
|
17
|
+
import { throwIfValueSendInBatch } from '../utils'
|
18
|
+
|
19
|
+
export type SimpleAccountClient<
|
20
|
+
transport extends Transport = Transport,
|
21
|
+
chain extends Chain | undefined = Chain | undefined,
|
22
|
+
account extends Account | undefined = Account | undefined,
|
23
|
+
rpcSchema extends RpcSchema | undefined = undefined,
|
24
|
+
> = Prettify<
|
25
|
+
SmartWalletClient<
|
26
|
+
transport,
|
27
|
+
chain,
|
28
|
+
account,
|
29
|
+
rpcSchema extends RpcSchema ? [...WalletRpcSchema, ...rpcSchema] : WalletRpcSchema
|
30
|
+
> & {
|
31
|
+
simpleAccountAddress: Hex
|
32
|
+
}
|
33
|
+
>
|
34
|
+
|
35
|
+
export function SimpleAccountActions<
|
36
|
+
transport extends Transport,
|
37
|
+
chain extends Chain | undefined = Chain | undefined,
|
38
|
+
account extends Account | undefined = Account | undefined,
|
39
|
+
>(
|
40
|
+
client: SimpleAccountClient<transport, chain, account>,
|
41
|
+
): Omit<SmartWalletActions, 'deployKernelAccount'> {
|
42
|
+
return {
|
43
|
+
execute: (args) => execute(client, args),
|
44
|
+
}
|
45
|
+
}
|
46
|
+
|
47
|
+
async function execute<chain extends Chain | undefined, account extends Account | undefined>(
|
48
|
+
client: SimpleAccountClient<Transport, chain, account>,
|
49
|
+
transactions: ExecuteSmartWalletArgs,
|
50
|
+
): Promise<Hex> {
|
51
|
+
if (transactions.length === 1) {
|
52
|
+
const [tx] = transactions
|
53
|
+
|
54
|
+
const data = encodeFunctionData({
|
55
|
+
abi: SimpleAccountAbi,
|
56
|
+
functionName: 'execute',
|
57
|
+
args: [tx.to, tx.value || 0n, tx.data!],
|
58
|
+
})
|
59
|
+
|
60
|
+
return client.sendTransaction({
|
61
|
+
data: data,
|
62
|
+
kzg: undefined,
|
63
|
+
to: client.simpleAccountAddress,
|
64
|
+
chain: client.chain as Chain,
|
65
|
+
account: client.account as Account,
|
66
|
+
})
|
67
|
+
}
|
68
|
+
throwIfValueSendInBatch(transactions)
|
69
|
+
|
70
|
+
const data = encodeFunctionData({
|
71
|
+
abi: SimpleAccountAbi,
|
72
|
+
functionName: 'executeBatch',
|
73
|
+
args: [transactions.map((tx) => tx.to), transactions.map((tx) => tx.data!)],
|
74
|
+
})
|
75
|
+
|
76
|
+
return client.sendTransaction({
|
77
|
+
data: data,
|
78
|
+
kzg: undefined,
|
79
|
+
to: client.simpleAccountAddress,
|
80
|
+
chain: client.chain as Chain,
|
81
|
+
account: client.account as Account,
|
82
|
+
})
|
83
|
+
}
|
@@ -0,0 +1,38 @@
|
|
1
|
+
import {
|
2
|
+
WalletClient,
|
3
|
+
Hex,
|
4
|
+
Transport,
|
5
|
+
Chain,
|
6
|
+
Account,
|
7
|
+
RpcSchema,
|
8
|
+
Prettify,
|
9
|
+
WalletRpcSchema,
|
10
|
+
PublicActions,
|
11
|
+
} from 'viem'
|
12
|
+
import { DeployFactoryArgs } from './kernel'
|
13
|
+
|
14
|
+
// The type that simple account executes arrays of
|
15
|
+
export type ExecuteSmartWalletArg = { to: Hex; data?: Hex; value?: bigint }
|
16
|
+
|
17
|
+
export type ExecuteSmartWalletArgs = ExecuteSmartWalletArg[]
|
18
|
+
|
19
|
+
export type SmartWalletActions = {
|
20
|
+
execute: (args: ExecuteSmartWalletArgs) => Promise<Hex>
|
21
|
+
deployKernelAccount: () => Promise<DeployFactoryArgs>
|
22
|
+
}
|
23
|
+
|
24
|
+
export type SmartWalletClient<
|
25
|
+
transport extends Transport = Transport,
|
26
|
+
chain extends Chain | undefined = Chain | undefined,
|
27
|
+
account extends Account | undefined = Account | undefined,
|
28
|
+
rpcSchema extends RpcSchema | undefined = undefined,
|
29
|
+
> = Prettify<
|
30
|
+
WalletClient<
|
31
|
+
transport,
|
32
|
+
chain,
|
33
|
+
account,
|
34
|
+
rpcSchema extends RpcSchema ? [...WalletRpcSchema, ...rpcSchema] : WalletRpcSchema
|
35
|
+
> &
|
36
|
+
PublicActions<transport, chain> &
|
37
|
+
SmartWalletActions
|
38
|
+
>
|
@@ -0,0 +1,14 @@
|
|
1
|
+
import { ExecuteSmartWalletArgs } from './smart-wallet.types'
|
2
|
+
|
3
|
+
/**
|
4
|
+
* Throws if we don`t support value send in batch transactions, {@link SimpleAccountClient}
|
5
|
+
* @param transactions the transactions to execute
|
6
|
+
*/
|
7
|
+
export function throwIfValueSendInBatch(transactions: ExecuteSmartWalletArgs) {
|
8
|
+
if (
|
9
|
+
transactions.length > 1 &&
|
10
|
+
transactions.some((tx) => tx.value !== undefined || tx.value !== 0n)
|
11
|
+
) {
|
12
|
+
throw new Error('Value send is not support value in batch transactions')
|
13
|
+
}
|
14
|
+
}
|
@@ -0,0 +1,25 @@
|
|
1
|
+
import { Module } from '@nestjs/common'
|
2
|
+
import { SignModule } from '../sign/sign.module'
|
3
|
+
import { SimpleAccountClientService } from './smart-wallets/simple-account/simple-account-client.service'
|
4
|
+
import { MultichainPublicClientService } from './multichain-public-client.service'
|
5
|
+
import { ViemMultichainClientService } from './viem_multichain_client.service'
|
6
|
+
import { KernelAccountClientService } from './smart-wallets/kernel/kernel-account-client.service'
|
7
|
+
import { KernelAccountClientV2Service } from '@/transaction/smart-wallets/kernel/kernel-account-client-v2.service'
|
8
|
+
|
9
|
+
@Module({
|
10
|
+
imports: [SignModule],
|
11
|
+
providers: [
|
12
|
+
SimpleAccountClientService,
|
13
|
+
MultichainPublicClientService,
|
14
|
+
ViemMultichainClientService,
|
15
|
+
KernelAccountClientService,
|
16
|
+
KernelAccountClientV2Service,
|
17
|
+
],
|
18
|
+
exports: [
|
19
|
+
SimpleAccountClientService,
|
20
|
+
MultichainPublicClientService,
|
21
|
+
KernelAccountClientService,
|
22
|
+
KernelAccountClientV2Service,
|
23
|
+
],
|
24
|
+
})
|
25
|
+
export class TransactionModule {}
|
@@ -0,0 +1,100 @@
|
|
1
|
+
import { Injectable, OnModuleInit } from '@nestjs/common'
|
2
|
+
import { EcoConfigService } from '../eco-configs/eco-config.service'
|
3
|
+
import { Chain, Client, ClientConfig, createClient, extractChain, Hex, zeroAddress } from 'viem'
|
4
|
+
import { EcoError } from '../common/errors/eco-error'
|
5
|
+
import { ChainsSupported } from '../common/chains/supported'
|
6
|
+
import { getTransport } from '../common/chains/transport'
|
7
|
+
|
8
|
+
@Injectable()
|
9
|
+
export class ViemMultichainClientService<T extends Client, V extends ClientConfig>
|
10
|
+
implements OnModuleInit
|
11
|
+
{
|
12
|
+
readonly instances: Map<number, T> = new Map()
|
13
|
+
|
14
|
+
protected supportedAlchemyChainIds: number[] = []
|
15
|
+
protected apiKey: string
|
16
|
+
protected pollingInterval: number
|
17
|
+
|
18
|
+
constructor(readonly ecoConfigService: EcoConfigService) {}
|
19
|
+
|
20
|
+
onModuleInit() {
|
21
|
+
this.setChainConfigs()
|
22
|
+
}
|
23
|
+
|
24
|
+
async getClient(id: number): Promise<T> {
|
25
|
+
if (!this.isSupportedNetwork(id)) {
|
26
|
+
throw EcoError.AlchemyUnsupportedNetworkIDError(id)
|
27
|
+
}
|
28
|
+
return this.loadInstance(id)
|
29
|
+
}
|
30
|
+
|
31
|
+
private setChainConfigs() {
|
32
|
+
const alchemyConfigs = this.ecoConfigService.getAlchemy()
|
33
|
+
this.supportedAlchemyChainIds = alchemyConfigs.networks.map((n) => n.id)
|
34
|
+
this.apiKey = alchemyConfigs.apiKey
|
35
|
+
this.pollingInterval = this.ecoConfigService.getEth().pollingInterval
|
36
|
+
}
|
37
|
+
|
38
|
+
private async loadInstance(chainID: number): Promise<T> {
|
39
|
+
if (!this.instances.has(chainID)) {
|
40
|
+
const client = await this.createInstanceClient(await this.getChainConfig(chainID))
|
41
|
+
this.instances.set(chainID, client)
|
42
|
+
}
|
43
|
+
return this.instances.get(chainID)!
|
44
|
+
}
|
45
|
+
|
46
|
+
protected async createInstanceClient(configs: V): Promise<T> {
|
47
|
+
//@ts-expect-error client mismatch on property definition
|
48
|
+
return createClient(configs)
|
49
|
+
}
|
50
|
+
|
51
|
+
/**
|
52
|
+
* Use overrides if they exist -- otherwise use the default settings.
|
53
|
+
* @param chainID
|
54
|
+
* @returns
|
55
|
+
*/
|
56
|
+
public async getChainConfig(chainID: number): Promise<V> {
|
57
|
+
const chain = extractChain({
|
58
|
+
chains: ChainsSupported,
|
59
|
+
id: chainID,
|
60
|
+
})
|
61
|
+
|
62
|
+
if (chain) {
|
63
|
+
return this.buildChainConfig(chain)
|
64
|
+
} else {
|
65
|
+
throw EcoError.UnsupportedChainError(chain[0])
|
66
|
+
}
|
67
|
+
}
|
68
|
+
|
69
|
+
protected async buildChainConfig(chain: Chain): Promise<V> {
|
70
|
+
//only pass api key if chain is supported by alchemy, otherwise it'll be incorrectly added to other rpcs
|
71
|
+
const apiKey = this.supportedAlchemyChainIds.includes(chain.id) ? this.apiKey : undefined
|
72
|
+
const rpcTransport = getTransport(chain, apiKey)
|
73
|
+
return {
|
74
|
+
transport: rpcTransport,
|
75
|
+
chain: chain,
|
76
|
+
pollingInterval: this.pollingInterval,
|
77
|
+
} as V
|
78
|
+
}
|
79
|
+
|
80
|
+
/**
|
81
|
+
* Returns the address of the wallet for the first solver in the config.
|
82
|
+
* @returns
|
83
|
+
*/
|
84
|
+
protected async getAddress(): Promise<Hex> {
|
85
|
+
const solvers = this.ecoConfigService.getSolvers()
|
86
|
+
if (!solvers || Object.values(solvers).length == 0) {
|
87
|
+
return zeroAddress
|
88
|
+
}
|
89
|
+
|
90
|
+
const wallet = await this.getClient(Object.values(solvers)[0].chainID)
|
91
|
+
return wallet.account?.address || zeroAddress
|
92
|
+
}
|
93
|
+
|
94
|
+
private isSupportedNetwork(chainID: number): boolean {
|
95
|
+
return (
|
96
|
+
this.supportedAlchemyChainIds.includes(chainID) ||
|
97
|
+
ChainsSupported.some((chain) => chain.id === chainID)
|
98
|
+
)
|
99
|
+
}
|
100
|
+
}
|
@@ -0,0 +1,14 @@
|
|
1
|
+
import { Transform } from 'class-transformer'
|
2
|
+
import { getAddress } from 'viem'
|
3
|
+
|
4
|
+
/**
|
5
|
+
* Decorator that transforms dtos to chechsum addresses or throws
|
6
|
+
*/
|
7
|
+
export function ViemAddressTransform() {
|
8
|
+
return Transform(({ value }) => {
|
9
|
+
if (typeof value === 'string') {
|
10
|
+
return getAddress(value) // validate and checksum
|
11
|
+
}
|
12
|
+
return value // Return as-is if not a string
|
13
|
+
})
|
14
|
+
}
|
@@ -0,0 +1,44 @@
|
|
1
|
+
/**
|
2
|
+
* Mathb is a utility class for bigint operations.
|
3
|
+
*/
|
4
|
+
export class Mathb {
|
5
|
+
/**
|
6
|
+
* Get the absolute value of a bigint.
|
7
|
+
* @param x the number
|
8
|
+
* @returns
|
9
|
+
*/
|
10
|
+
static abs(x: bigint): bigint {
|
11
|
+
return x < 0 ? -x : x
|
12
|
+
}
|
13
|
+
|
14
|
+
/**
|
15
|
+
* Get the minimum of two bigints.
|
16
|
+
* @param x first bigint
|
17
|
+
* @param y second bigint
|
18
|
+
* @returns
|
19
|
+
*/
|
20
|
+
static min(x: bigint, y: bigint): bigint {
|
21
|
+
return x < y ? x : y
|
22
|
+
}
|
23
|
+
|
24
|
+
/**
|
25
|
+
* Get the maximum of two bigints.
|
26
|
+
* @param x first bigint
|
27
|
+
* @param y second bigint
|
28
|
+
* @returns
|
29
|
+
*/
|
30
|
+
static max(x: bigint, y: bigint): bigint {
|
31
|
+
return x > y ? x : y
|
32
|
+
}
|
33
|
+
|
34
|
+
/**
|
35
|
+
* Compares two bigints. Returns 0 if they are equal, 1 if x is greater than y, and -1 if x is less than y.
|
36
|
+
* Usefull for sorting.
|
37
|
+
* @param x first bigint
|
38
|
+
* @param y second bigint
|
39
|
+
* @returns
|
40
|
+
*/
|
41
|
+
static compare(x: bigint, y: bigint): number {
|
42
|
+
return x === y ? 0 : x > y ? 1 : -1
|
43
|
+
}
|
44
|
+
}
|
@@ -0,0 +1,18 @@
|
|
1
|
+
/**
|
2
|
+
* The type of an array
|
3
|
+
*/
|
4
|
+
export type GetElementType<T> = T extends (infer U)[] ? U : never
|
5
|
+
|
6
|
+
/**
|
7
|
+
* Removes the readonly modifier entire object
|
8
|
+
*/
|
9
|
+
export type Mutable<T> = {
|
10
|
+
-readonly [K in keyof T]: T[K]
|
11
|
+
}
|
12
|
+
|
13
|
+
/**
|
14
|
+
* Removes the readonly modifier from a field
|
15
|
+
*/
|
16
|
+
export type MutableField<T, K extends keyof T> = Omit<T, K> & {
|
17
|
+
-readonly [P in K]: T[P]
|
18
|
+
}
|