1- import {
2- encodeAbiParameters ,
3- erc20Abi ,
4- encodeFunctionData ,
5- keccak256 ,
6- toHex ,
7- } from 'viem' ;
8- import { getPublicClient } from 'wagmi/actions' ;
9-
101import { wagmiConfig } from 'src/ui-config/wagmiConfig' ;
2+ import { encodeAbiParameters , encodeFunctionData , erc20Abi , keccak256 , toHex } from 'viem' ;
3+ import { getPublicClient , getWalletClient } from 'wagmi/actions' ;
114
125const MULTICALL3_ADDRESS = '0xcA11bde05977b3631167028862bE2a173976CA11' ;
136const MULTICALL3_AGGREGATE3_ABI = [
@@ -60,7 +53,21 @@ type OrderSettlementContext = {
6053 buyAmount ?: bigint ;
6154} ;
6255
63- const DEFAULT_BALANCE_SLOT_CANDIDATES = [ 0n , 1n , 2n , 3n , 4n , 5n , 6n , 7n , 8n , 9n , 10n , 11n , 12n ] ;
56+ const DEFAULT_BALANCE_SLOT_CANDIDATES = [
57+ BigInt ( 0 ) ,
58+ BigInt ( 1 ) ,
59+ BigInt ( 2 ) ,
60+ BigInt ( 3 ) ,
61+ BigInt ( 4 ) ,
62+ BigInt ( 5 ) ,
63+ BigInt ( 6 ) ,
64+ BigInt ( 7 ) ,
65+ BigInt ( 8 ) ,
66+ BigInt ( 9 ) ,
67+ BigInt ( 10 ) ,
68+ BigInt ( 11 ) ,
69+ BigInt ( 12 ) ,
70+ ] ;
6471
6572const getBalanceSlotKeys = ( owner : string , slotCandidates = DEFAULT_BALANCE_SLOT_CANDIDATES ) => {
6673 return slotCandidates . map ( ( slot ) => {
@@ -87,10 +94,11 @@ const buildBalanceOverride = async ({
8794 const normalizedRecipient = recipient as `0x${string } ` | undefined ;
8895 if ( ! normalizedToken || ! normalizedRecipient || amount === undefined ) return undefined ;
8996
97+ const walletClient = await getWalletClient ( wagmiConfig , { chainId } ) ;
9098 const publicClient = getPublicClient ( wagmiConfig , { chainId } ) ;
91- if ( ! publicClient ) return undefined ;
99+ if ( ! walletClient || ! publicClient ) return undefined ;
92100
93- let currentBalance = 0n ;
101+ let currentBalance = BigInt ( 0 ) ;
94102 try {
95103 currentBalance = await publicClient . readContract ( {
96104 address : normalizedToken ,
@@ -113,7 +121,9 @@ const buildBalanceOverride = async ({
113121 return { [ normalizedToken ] : { stateDiff } } ;
114122} ;
115123
116- const mergeOverrides = ( ...overrides : ( Record < string , { stateDiff : Record < string , string > } > | undefined ) [ ] ) => {
124+ const mergeOverrides = (
125+ ...overrides : ( Record < string , { stateDiff : Record < string , string > } > | undefined ) [ ]
126+ ) => {
117127 const merged : Record < string , { stateDiff : Record < string , string > } > = { } ;
118128 overrides . forEach ( ( override ) => {
119129 if ( ! override ) return ;
@@ -136,7 +146,7 @@ export const simulateCollateralSwapPreHook = async ({
136146 settlementContext,
137147} : {
138148 chainId : number ;
139- from ?: `0x${ string } ` ;
149+ from ?: string ;
140150 preHook ?: HookDefinition ;
141151 flashloan ?: FlashloanMetadata ;
142152 postHook ?: HookDefinition ;
@@ -149,8 +159,9 @@ export const simulateCollateralSwapPreHook = async ({
149159 return false ;
150160 }
151161
162+ const walletClient = await getWalletClient ( wagmiConfig , { chainId } ) ;
152163 const publicClient = getPublicClient ( wagmiConfig , { chainId } ) ;
153- if ( ! publicClient ) {
164+ if ( ! walletClient || ! publicClient ) {
154165 console . warn ( '[CoW][CollateralSwap] No public client available for simulation' ) ;
155166 return ;
156167 }
@@ -186,7 +197,10 @@ export const simulateCollateralSwapPreHook = async ({
186197 }
187198 : undefined ;
188199
189- const callsSequence = [ { ...encodedPreHook , label : 'preHook' } , ...( encodedPostHook ? [ { ...encodedPostHook , label : 'postHook' } ] : [ ] ) ] ;
200+ const callsSequence = [
201+ { ...encodedPreHook , label : 'preHook' } ,
202+ ...( encodedPostHook ? [ { ...encodedPostHook , label : 'postHook' } ] : [ ] ) ,
203+ ] ;
190204
191205 const aggregateData = encodeFunctionData ( {
192206 abi : MULTICALL3_AGGREGATE3_ABI ,
@@ -195,15 +209,17 @@ export const simulateCollateralSwapPreHook = async ({
195209 callsSequence . map ( ( c ) => ( {
196210 target : c . to as `0x${string } `,
197211 allowFailure : true ,
198- callData : ( c as any ) . input ?? c . data ,
212+ callData : c . input ,
199213 } ) ) ,
200214 ] ,
201215 } ) ;
202216
203217 const stateOverrides = mergeOverrides ( flashloanOverride , settlementOverride ) ;
204218
205219 const blockStateCall = {
206- calls : [ { from, to : MULTICALL3_ADDRESS as `0x${string } `, input : aggregateData , label : 'multicall' } ] ,
220+ calls : [
221+ { from, to : MULTICALL3_ADDRESS as `0x${string } `, input : aggregateData , label : 'multicall' } ,
222+ ] ,
207223 transactions : [ { from, to : MULTICALL3_ADDRESS as `0x${string } `, data : aggregateData } ] ,
208224 ...( stateOverrides ? { stateOverrides } : { } ) ,
209225 } ;
@@ -216,9 +232,9 @@ export const simulateCollateralSwapPreHook = async ({
216232 console . log ( '[CoW][CollateralSwap] PreHook simulation payload' , simulationPayload ) ;
217233
218234 try {
219- const result = await publicClient . request ( {
235+ const result = await publicClient . transport . request ( {
220236 method : 'eth_simulateV1' ,
221- params : [ simulationPayload ] as unknown [ ] ,
237+ params : [ simulationPayload ] ,
222238 } ) ;
223239 console . log ( '[CoW][CollateralSwap] PreHook simulation result' , result ) ;
224240 return true ;
0 commit comments