1
1
import {
2
2
CosignedPriorityOrder ,
3
3
CosignedV2DutchOrder ,
4
+ CosignedV3DutchOrder ,
4
5
DutchOrder ,
5
6
FillInfo ,
6
7
OrderType ,
@@ -25,10 +26,12 @@ export interface FillMetadata {
25
26
export function getSettledAmounts (
26
27
fill : FillInfo ,
27
28
metadata : FillMetadata ,
28
- parsedOrder : DutchOrder | CosignedV2DutchOrder | CosignedPriorityOrder
29
+ parsedOrder : DutchOrder | CosignedV2DutchOrder | CosignedV3DutchOrder | CosignedPriorityOrder
29
30
) {
30
31
if ( parsedOrder instanceof DutchOrder || parsedOrder instanceof CosignedV2DutchOrder ) {
31
32
return getDutchSettledAmounts ( fill , metadata . timestamp , parsedOrder )
33
+ } else if ( parsedOrder instanceof CosignedV3DutchOrder ) {
34
+ return getDutchV3SettledAmounts ( fill , parsedOrder )
32
35
} else if ( parsedOrder instanceof CosignedPriorityOrder ) {
33
36
return getPrioritySettledAmounts ( fill , metadata , parsedOrder )
34
37
} else {
@@ -95,6 +98,73 @@ export function getPrioritySettledAmounts(
95
98
return settledAmounts
96
99
}
97
100
101
+ /**
102
+ * get the ammounts transfered on chain
103
+ * used for logging
104
+ */
105
+ export function getDutchV3SettledAmounts (
106
+ fill : FillInfo ,
107
+ parsedOrder : CosignedV3DutchOrder
108
+ ) : SettledAmount [ ] {
109
+ parsedOrder . resolve ( { currentBlock : fill . blockNumber } )
110
+ const nativeOutputs = parsedOrder . info . outputs . filter ( ( output ) => output . token . toLowerCase ( ) === NATIVE_ADDRESS )
111
+ const settledAmounts : SettledAmount [ ] = [ ]
112
+ let amountIn : string
113
+
114
+ if ( ! parsedOrder . info . input . curve ||
115
+ ( parsedOrder . info . input . curve . relativeAmounts . length === 1 &&
116
+ parsedOrder . info . input . curve . relativeAmounts [ 0 ] === BigInt ( 0 ) ) ) {
117
+ // If the order is EXACT_INPUT then the input will not decay and resolves to the startAmount/endAmount.
118
+ amountIn = parsedOrder . info . input . startAmount . toString ( )
119
+
120
+ // Resolve the native outputs using the fill block number and filler address from the fill log.
121
+ // This will give us a minimum resolved amount for native out swaps.
122
+ const resolvedOrder = parsedOrder . resolve ( { currentBlock : fill . blockNumber , filler : fill . filler } )
123
+ const resolvedNativeOutputs = resolvedOrder . outputs . filter (
124
+ ( output ) => output . token . toLowerCase ( ) === NATIVE_ADDRESS
125
+ )
126
+
127
+ // Add all the resolved native outputs to the settledAmounts as they are not included in the fill logs.
128
+ resolvedNativeOutputs . forEach ( ( resolvedNativeOutput ) => {
129
+ settledAmounts . push ( {
130
+ tokenIn : parsedOrder . info . input . token ,
131
+ amountIn,
132
+ tokenOut : resolvedNativeOutput . token ,
133
+ amountOut : resolvedNativeOutput . amount . toString ( ) ,
134
+ } )
135
+ } )
136
+ } else {
137
+ // If the order is EXACT_OUTPUT we will have all the ERC20 transfers in the fill logs,
138
+ // only log the amountIn that matches the order input token.
139
+
140
+ // eslint-disable-next-line @typescript-eslint/no-non-null-assertion
141
+ const input = fill . inputs . find ( ( input ) => input . token . toLowerCase ( ) === parsedOrder . info . input . token . toLowerCase ( ) ) !
142
+ amountIn = input . amount . toString ( )
143
+
144
+ // Add all the native outputs to the settledAmounts as they are not included in the fill logs.
145
+ // The amount is just the startAmount because the order is EXACT_OUTPUT so there is no decay on the outputs.
146
+ nativeOutputs . forEach ( ( nativeOutput ) => {
147
+ settledAmounts . push ( {
148
+ tokenIn : parsedOrder . info . input . token ,
149
+ amountIn,
150
+ tokenOut : nativeOutput . token ,
151
+ amountOut : nativeOutput . startAmount . toString ( ) ,
152
+ } )
153
+ } )
154
+ }
155
+
156
+ fill . outputs . forEach ( ( output ) => {
157
+ settledAmounts . push ( {
158
+ tokenIn : parsedOrder . info . input . token ,
159
+ amountIn,
160
+ tokenOut : output . token ,
161
+ amountOut : output . amount . toString ( ) ,
162
+ } )
163
+ } )
164
+
165
+ return settledAmounts
166
+ }
167
+
98
168
/**
99
169
* get the ammounts transfered on chain
100
170
* used for logging
0 commit comments