@@ -14,7 +14,7 @@ import (
1414 "github.com/ethereum/go-ethereum/common"
1515)
1616
17- type MultiCallProcessorv3 struct {
17+ type MultiCallProcessorv3Unit struct {
1818 // borrower string
1919 running bool // is the multicall running
2020 nonMultiCallEvents []* schemas.AccountOperation
@@ -23,7 +23,7 @@ type MultiCallProcessorv3 struct {
2323}
2424
2525// edge case it adds non multicall addCollateral for open credit account
26- func (p * MultiCallProcessorv3 ) AddMulticallEvent (operation * schemas.AccountOperation ) {
26+ func (p * MultiCallProcessorv3Unit ) AddMulticallEvent (operation * schemas.AccountOperation ) {
2727 lastMainAction := p .lastMainAction ()
2828 //
2929 if ! p .running { // non multicall
@@ -33,7 +33,7 @@ func (p *MultiCallProcessorv3) AddMulticallEvent(operation *schemas.AccountOpera
3333 }
3434}
3535
36- func (p * MultiCallProcessorv3 ) AddOpenEvent (openEvent * schemas.AccountOperation ) {
36+ func (p * MultiCallProcessorv3Unit ) AddOpenEvent (openEvent * schemas.AccountOperation ) {
3737 if p .noOfOpens > 0 {
3838 log .Fatal ("2 opencreditaccount event are in same txhash" , utils .ToJson (p .facadeActions ), utils .ToJson (openEvent ))
3939 }
@@ -44,14 +44,14 @@ func (p *MultiCallProcessorv3) AddOpenEvent(openEvent *schemas.AccountOperation)
4444 p .noOfOpens ++
4545}
4646
47- func (p * MultiCallProcessorv3 ) lastMainAction () * FacadeAccountAction {
47+ func (p * MultiCallProcessorv3Unit ) lastMainAction () * FacadeAccountAction {
4848 if len (p .facadeActions ) > 0 {
4949 return p .facadeActions [len (p .facadeActions )- 1 ]
5050 }
5151 return nil
5252}
5353
54- func (p * MultiCallProcessorv3 ) Start (txHash string , startEvent * schemas.AccountOperation ) {
54+ func (p * MultiCallProcessorv3Unit ) Start (txHash string , startEvent * schemas.AccountOperation ) {
5555 lastMainAction := p .lastMainAction ()
5656 if p .running {
5757 log .Fatalf ("Previously started multicall(%s) is not ended for txHash(%s)" ,
@@ -66,22 +66,22 @@ func (p *MultiCallProcessorv3) Start(txHash string, startEvent *schemas.AccountO
6666 p .running = true
6767}
6868
69- func (p * MultiCallProcessorv3 ) AddCloseEvent (event * schemas.AccountOperation ) {
69+ func (p * MultiCallProcessorv3Unit ) AddCloseEvent (event * schemas.AccountOperation ) {
7070 p .facadeActions = append (p .facadeActions , & FacadeAccountAction {
7171 Data : event ,
7272 ended : true ,
7373 Type : GBFacadeCloseEvent ,
7474 })
7575}
76- func (p * MultiCallProcessorv3 ) AddLiquidateEvent (event * schemas.AccountOperation ) {
76+ func (p * MultiCallProcessorv3Unit ) AddLiquidateEvent (event * schemas.AccountOperation ) {
7777 p .facadeActions = append (p .facadeActions , & FacadeAccountAction {
7878 Data : event ,
7979 Type : GBFacadev3LiqUpdateEvent ,
8080 ended : true ,
8181 })
8282}
8383
84- func (p * MultiCallProcessorv3 ) End (logId uint , debts []pool_v3.ManageDebt , underlying string ) {
84+ func (p * MultiCallProcessorv3Unit ) End (logId uint , debts []pool_v3.ManageDebt , underlying string ) {
8585 if ! p .running {
8686 log .Fatal ("Multicall end called though multicall not running" )
8787 }
@@ -133,7 +133,7 @@ func AddManageDebtsToMain(lastMainAction *schemas.AccountOperation, debts []pool
133133// - open call without multicalls
134134// open call have the multicalls in them
135135// liquidated, closed and directly multicalls are separated entries
136- func (p * MultiCallProcessorv3 ) PopMainActions (txHash string , mgr * ds.AccountQuotaMgr ) (facadeActions , openEventWithoutMulticall []* FacadeAccountAction ) {
136+ func (p * MultiCallProcessorv3Unit ) popMainActions (txHash string , quotas [] * ds. UpdateQuotaEvent , mgr * ds.AccountQuotaMgr ) (facadeActions , openEventWithoutMulticall []* FacadeAccountAction ) {
137137 defer func () { p .facadeActions = nil }()
138138
139139 p .noOfOpens = 0
@@ -142,7 +142,6 @@ func (p *MultiCallProcessorv3) PopMainActions(txHash string, mgr *ds.AccountQuot
142142 }
143143
144144 {
145- quotas := mgr .GetUpdateQuotaEventForAccount (common .HexToHash (txHash ))
146145 // update quota with session based on accountquotamgr
147146 for i := len (facadeActions ) - 1 ; i >= 0 ; i -- {
148147 facade := facadeActions [i ]
@@ -164,7 +163,7 @@ func (p *MultiCallProcessorv3) PopMainActions(txHash string, mgr *ds.AccountQuot
164163 return
165164}
166165
167- func (p * MultiCallProcessorv3 ) PopNonMulticallEvents () []* schemas.AccountOperation {
166+ func (p * MultiCallProcessorv3Unit ) PopNonMulticallEvents () []* schemas.AccountOperation {
168167 calls := p .nonMultiCallEvents
169168 p .nonMultiCallEvents = nil
170169 return calls
@@ -191,3 +190,105 @@ func addQuotasToFacade(action *FacadeAccountAction, quotaEvents []*ds.UpdateQuot
191190 return action .Data .MultiCall [i ].LogId < action .Data .MultiCall [j ].LogId
192191 })
193192}
193+
194+ type MultiCallProcessorv3 struct {
195+ units map [string ]* MultiCallProcessorv3Unit
196+ facadeAddrToSession map [string ]string
197+ }
198+
199+ func NewMultiCallProcessorv3 () * MultiCallProcessorv3 {
200+ return & MultiCallProcessorv3 {
201+ units : make (map [string ]* MultiCallProcessorv3Unit ),
202+ facadeAddrToSession : map [string ]string {},
203+ }
204+ }
205+
206+ func (p * MultiCallProcessorv3 ) check (operation * schemas.AccountOperation ) {
207+ if operation .SessionId == "" {
208+ log .Fatal ("Multicall event without sessionid" , utils .ToJson (operation ))
209+ }
210+ }
211+ func (p * MultiCallProcessorv3 ) AddMulticallEvent (operation * schemas.AccountOperation ) {
212+ p .check (operation )
213+ unit , exists := p .units [operation .SessionId ]
214+ if ! exists {
215+ unit = & MultiCallProcessorv3Unit {}
216+ p .units [operation .SessionId ] = unit
217+ }
218+ unit .AddMulticallEvent (operation )
219+ }
220+ func (p * MultiCallProcessorv3 ) Start (txHash string , startEvent * schemas.AccountOperation , facadeAddr string ) {
221+ p .check (startEvent )
222+ unit , exists := p .units [startEvent .SessionId ]
223+ if ! exists {
224+ unit = & MultiCallProcessorv3Unit {}
225+ p .units [startEvent .SessionId ] = unit
226+ }
227+ if _ , ok := p .facadeAddrToSession [facadeAddr ]; ok {
228+ log .Fatal ("Two multicalls started for same facade in same txhash without ending previous one" , facadeAddr , p .facadeAddrToSession [facadeAddr ], startEvent .SessionId , txHash )
229+ } else {
230+ p .facadeAddrToSession [facadeAddr ] = startEvent .SessionId
231+ }
232+ unit .Start (txHash , startEvent )
233+ }
234+ func (p * MultiCallProcessorv3 ) AddOpenEvent (openEvent * schemas.AccountOperation ) {
235+ p .check (openEvent )
236+ unit , exists := p .units [openEvent .SessionId ]
237+ if ! exists {
238+ unit = & MultiCallProcessorv3Unit {}
239+ p .units [openEvent .SessionId ] = unit
240+ }
241+ unit .AddOpenEvent (openEvent )
242+ }
243+ func (p * MultiCallProcessorv3 ) AddCloseEvent (event * schemas.AccountOperation ) {
244+ p .check (event )
245+ unit , exists := p .units [event .SessionId ]
246+ if ! exists {
247+ unit = & MultiCallProcessorv3Unit {}
248+ p .units [event .SessionId ] = unit
249+ }
250+ unit .AddCloseEvent (event )
251+ }
252+ func (p * MultiCallProcessorv3 ) AddLiquidateEvent (event * schemas.AccountOperation ) {
253+ p .check (event )
254+ unit , exists := p .units [event .SessionId ]
255+ if ! exists {
256+ unit = & MultiCallProcessorv3Unit {}
257+ p .units [event .SessionId ] = unit
258+ }
259+ unit .AddLiquidateEvent (event )
260+ }
261+ func (p * MultiCallProcessorv3 ) PopNonMulticallEvents () []* schemas.AccountOperation {
262+ var calls []* schemas.AccountOperation
263+ for _ , unit := range p .units {
264+ calls = append (calls , unit .PopNonMulticallEvents ()... )
265+ }
266+ return calls
267+ }
268+ func (p * MultiCallProcessorv3 ) PopMainActions (txHash string , mgr * ds.AccountQuotaMgr ) (facadeActions , openEventWithoutMulticall []* FacadeAccountAction ) {
269+ quotas := mgr .GetUpdateQuotaEventForAccount (common .HexToHash (txHash ))
270+ for sessionId , unit := range p .units {
271+ facade , openWithoutMC := unit .popMainActions (txHash , quotas [common .HexToAddress (strings .Split (sessionId , "_" )[0 ])], mgr )
272+ facadeActions = append (facadeActions , facade ... )
273+ openEventWithoutMulticall = append (openEventWithoutMulticall , openWithoutMC ... )
274+ }
275+ p .units = make (map [string ]* MultiCallProcessorv3Unit )
276+ if len (p .facadeAddrToSession ) != 0 {
277+ log .Fatal ("facadeAddrToSession not empty after popping main actions" , utils .ToJson (p .facadeAddrToSession ))
278+ }
279+ return
280+
281+ }
282+ func (p * MultiCallProcessorv3 ) End (logId uint , debts []pool_v3.ManageDebt , underlyingtoken string , facadeAddr string ) {
283+ sessionId , ok := p .facadeAddrToSession [facadeAddr ]
284+ if ! ok {
285+ log .Fatal ("Multicall end called though no multicall was started for facadeAddr" , facadeAddr )
286+ }
287+ delete (p .facadeAddrToSession , facadeAddr )
288+ //
289+ unit , exists := p .units [sessionId ]
290+ if ! exists {
291+ log .Fatal ("Multicall end called though no multicall was started for sessionid" , sessionId )
292+ }
293+ unit .End (logId , debts , underlyingtoken )
294+ }
0 commit comments