@@ -3,6 +3,7 @@ import { valueToBigNumber } from '@aave/math-utils';
3
3
import {
4
4
calculateUniqueOrderId ,
5
5
COW_PROTOCOL_VAULT_RELAYER_ADDRESS ,
6
+ OrderKind ,
6
7
SupportedChainId ,
7
8
} from '@cowprotocol/cow-sdk' ;
8
9
import { Trans } from '@lingui/macro' ;
@@ -33,6 +34,7 @@ import { TxActionsWrapper } from '../TxActionsWrapper';
33
34
import { APPROVAL_GAS_LIMIT } from '../utils' ;
34
35
import {
35
36
COW_APP_DATA ,
37
+ COW_PARTNER_FEE_IN_DECIMAL ,
36
38
getPreSignTransaction ,
37
39
getUnsignerOrder ,
38
40
isNativeToken ,
@@ -54,6 +56,7 @@ interface SwitchProps {
54
56
isWrongNetwork : boolean ;
55
57
chainId : number ;
56
58
orderKind : 'market' | 'limit' ;
59
+ limitOrderKind : OrderKind ;
57
60
switchRates ?: SwitchRatesType ;
58
61
inputName : string ;
59
62
outputName : string ;
@@ -79,6 +82,7 @@ export const SwitchActions = ({
79
82
outputSymbol,
80
83
slippage : slippageInPercent = '0' ,
81
84
orderKind,
85
+ limitOrderKind,
82
86
expiry,
83
87
blocked,
84
88
loading,
@@ -146,6 +150,8 @@ export const SwitchActions = ({
146
150
} , [ approvedAmount , inputAmount , isWrongNetwork ] ) ;
147
151
148
152
const action = async ( ) => {
153
+ if ( ! switchRates ) return ;
154
+
149
155
setMainTxState ( { ...mainTxState , loading : true } ) ;
150
156
if ( isParaswapRates ( switchRates ) ) {
151
157
try {
@@ -221,19 +227,58 @@ export const SwitchActions = ({
221
227
} ) ;
222
228
}
223
229
} else if ( isCowProtocolRates ( switchRates ) ) {
230
+ const provider = await getEthersProvider ( wagmiConfig , { chainId } ) ;
224
231
const validTo = Math . floor ( Date . now ( ) / 1000 ) + ( expiry ?? Expiry [ 'Half hour' ] ) ;
225
- try {
226
- const provider = await getEthersProvider ( wagmiConfig , { chainId } ) ;
232
+ const slippageBps = Math . round ( Number ( slippageInPercent ) * 100 * 100 ) ; // percent to bps
233
+
234
+ let srcAmount ;
235
+ let destAmount ;
236
+
237
+ if ( orderKind === 'market' ) {
227
238
const destAmountWithSlippage = valueToBigNumber ( switchRates . destAmount )
228
239
. multipliedBy ( valueToBigNumber ( 1 ) . minus ( valueToBigNumber ( slippageInPercent ) ) )
229
240
. toFixed ( 0 ) ;
230
- const slippageBps = Math . round ( Number ( slippageInPercent ) * 100 * 100 ) ; // percent to bps
241
+
242
+ srcAmount = isNativeToken ( inputToken ) ? switchRates . srcAmount : switchRates . amountAndCosts . afterNetworkCosts . buyAmount . toString ( )
243
+ destAmount = destAmountWithSlippage
244
+ } else if ( limitOrderKind === OrderKind . SELL ) {
245
+
246
+ const partnerFee = COW_PARTNER_FEE_IN_DECIMAL ( inputSymbol , outputSymbol ) ;
247
+ const outputAmountBN = valueToBigNumber ( switchRates . destAmount ) ;
248
+ const outputPartnerFeeAmount = outputAmountBN . multipliedBy ( partnerFee ) . toString ( ) || '0'
249
+ const buyAmountWithoutPartnerFees = outputAmountBN . minus ( outputPartnerFeeAmount ) . decimalPlaces ( 0 ) . toString ( ) || '0'
250
+
251
+ console . log ( "sell order" )
252
+ console . log ( "output amount" , switchRates . destAmount )
253
+ console . log ( "output partner fee amount" , outputPartnerFeeAmount . toString ( ) )
254
+ console . log ( "buy amount without partner fees" , buyAmountWithoutPartnerFees . toString ( ) )
255
+
256
+
257
+ srcAmount = switchRates . srcAmount
258
+ destAmount = buyAmountWithoutPartnerFees
259
+ } else if ( limitOrderKind === OrderKind . BUY ) {
260
+ const partnerFee = COW_PARTNER_FEE_IN_DECIMAL ( inputSymbol , outputSymbol ) ;
261
+ const inputAmountBN = valueToBigNumber ( switchRates . srcAmount ) ;
262
+ const inputPartnerFeeAmount = inputAmountBN . multipliedBy ( partnerFee ) . toString ( ) || '0'
263
+ const sellAmountWithoutPartnerFees = inputAmountBN . minus ( inputPartnerFeeAmount ) . decimalPlaces ( 0 ) . toString ( ) || '0'
264
+
265
+ console . log ( "input amount" , inputAmountBN . toString ( ) )
266
+ console . log ( "input partner fee amount" , inputPartnerFeeAmount . toString ( ) )
267
+ console . log ( "sell amount without partner fees" , sellAmountWithoutPartnerFees . toString ( ) )
268
+
269
+ srcAmount = sellAmountWithoutPartnerFees
270
+ destAmount = switchRates . destAmount
271
+ } else {
272
+ throw new Error ( 'Invalid order kind' ) // This should never happen.
273
+ }
274
+
275
+ try {
231
276
232
277
// If srcToken is native, we need to use the eth-flow instead of the orderbook
233
278
if ( isNativeToken ( inputToken ) ) {
234
279
const ethFlowTx = await populateEthFlowTx (
235
- switchRates . srcAmount ,
236
- destAmountWithSlippage . toString ( ) ,
280
+ srcAmount ,
281
+ destAmount ,
237
282
outputToken ,
238
283
user ,
239
284
validTo ,
@@ -261,14 +306,15 @@ export const SwitchActions = ({
261
306
} ) ;
262
307
263
308
const unsignerOrder = await getUnsignerOrder (
264
- switchRates . srcAmount ,
265
- destAmountWithSlippage . toString ( ) ,
309
+ srcAmount ,
310
+ destAmount ,
266
311
outputToken ,
267
312
user ,
268
313
chainId ,
269
314
inputSymbol ,
270
315
outputSymbol ,
271
- validTo
316
+ validTo ,
317
+ limitOrderKind ,
272
318
) ;
273
319
const calculatedOrderId = await calculateUniqueOrderId ( chainId , unsignerOrder ) ;
274
320
@@ -311,17 +357,15 @@ export const SwitchActions = ({
311
357
tokenDest : outputToken ,
312
358
chainId,
313
359
user,
314
- amount : switchRates . srcAmount ,
315
360
tokenSrc : inputToken ,
316
361
tokenSrcDecimals : switchRates . srcDecimals ,
317
362
tokenDestDecimals : switchRates . destDecimals ,
318
- afterNetworkCostsBuyAmount :
319
- orderKind === 'market'
320
- ? switchRates . amountAndCosts . afterNetworkCosts . buyAmount . toString ( )
321
- : switchRates . destAmount , // TODO: check with cow team
363
+ sellAmount : srcAmount ,
364
+ beforePartnerFeesBuyAmount : destAmount ,
322
365
slippageBps,
323
366
inputSymbol,
324
367
outputSymbol,
368
+ kind : limitOrderKind ,
325
369
validTo,
326
370
quote : switchRates . order ,
327
371
} ) ;
@@ -355,18 +399,16 @@ export const SwitchActions = ({
355
399
tokenDest : outputToken ,
356
400
tokenDestDecimals : switchRates . destDecimals ,
357
401
quote : switchRates . order ,
358
- amount : switchRates . srcAmount ,
359
- afterNetworkCostsBuyAmount :
360
- orderKind === 'market'
361
- ? switchRates . amountAndCosts . afterNetworkCosts . buyAmount . toString ( )
362
- : switchRates . destAmount , // TODO: check with cow team
402
+ sellAmount : srcAmount ,
403
+ beforePartnerFeesBuyAmount : destAmount ,
363
404
slippageBps,
364
405
chainId,
365
406
user,
366
407
provider,
367
408
inputSymbol,
368
- validTo,
369
409
outputSymbol,
410
+ validTo,
411
+ kind : limitOrderKind ,
370
412
} ) ;
371
413
setMainTxState ( {
372
414
loading : false ,
0 commit comments