@@ -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,59 @@ 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
231
241
242
+ srcAmount = isNativeToken ( inputToken )
243
+ ? switchRates . srcAmount
244
+ : switchRates . amountAndCosts . afterNetworkCosts . buyAmount . toString ( ) ;
245
+ destAmount = destAmountWithSlippage ;
246
+ } else if ( limitOrderKind === OrderKind . SELL ) {
247
+ const partnerFee = COW_PARTNER_FEE_IN_DECIMAL ( inputSymbol , outputSymbol ) ;
248
+ const outputAmountBN = valueToBigNumber ( switchRates . destAmount ) ;
249
+ const outputPartnerFeeAmount = outputAmountBN . multipliedBy ( partnerFee ) . toString ( ) || '0' ;
250
+ const buyAmountWithoutPartnerFees =
251
+ outputAmountBN . minus ( outputPartnerFeeAmount ) . decimalPlaces ( 0 ) . toString ( ) || '0' ;
252
+
253
+ console . log ( 'sell order' ) ;
254
+ console . log ( 'output amount' , switchRates . destAmount ) ;
255
+ console . log ( 'output partner fee amount' , outputPartnerFeeAmount . toString ( ) ) ;
256
+ console . log ( 'buy amount without partner fees' , buyAmountWithoutPartnerFees . toString ( ) ) ;
257
+
258
+ srcAmount = switchRates . srcAmount ;
259
+ destAmount = buyAmountWithoutPartnerFees ;
260
+ } else if ( limitOrderKind === OrderKind . BUY ) {
261
+ const partnerFee = COW_PARTNER_FEE_IN_DECIMAL ( inputSymbol , outputSymbol ) ;
262
+ const inputAmountBN = valueToBigNumber ( switchRates . srcAmount ) ;
263
+ const inputPartnerFeeAmount = inputAmountBN . multipliedBy ( partnerFee ) . toString ( ) || '0' ;
264
+ const sellAmountWithoutPartnerFees =
265
+ inputAmountBN . minus ( inputPartnerFeeAmount ) . decimalPlaces ( 0 ) . toString ( ) || '0' ;
266
+
267
+ console . log ( 'input amount' , inputAmountBN . toString ( ) ) ;
268
+ console . log ( 'input partner fee amount' , inputPartnerFeeAmount . toString ( ) ) ;
269
+ console . log ( 'sell amount without partner fees' , sellAmountWithoutPartnerFees . toString ( ) ) ;
270
+
271
+ srcAmount = sellAmountWithoutPartnerFees ;
272
+ destAmount = switchRates . destAmount ;
273
+ } else {
274
+ throw new Error ( 'Invalid order kind' ) ; // This should never happen.
275
+ }
276
+
277
+ try {
232
278
// If srcToken is native, we need to use the eth-flow instead of the orderbook
233
279
if ( isNativeToken ( inputToken ) ) {
234
280
const ethFlowTx = await populateEthFlowTx (
235
- switchRates . srcAmount ,
236
- destAmountWithSlippage . toString ( ) ,
281
+ srcAmount ,
282
+ destAmount ,
237
283
outputToken ,
238
284
user ,
239
285
validTo ,
@@ -261,14 +307,15 @@ export const SwitchActions = ({
261
307
} ) ;
262
308
263
309
const unsignerOrder = await getUnsignerOrder (
264
- switchRates . srcAmount ,
265
- destAmountWithSlippage . toString ( ) ,
310
+ srcAmount ,
311
+ destAmount ,
266
312
outputToken ,
267
313
user ,
268
314
chainId ,
269
315
inputSymbol ,
270
316
outputSymbol ,
271
- validTo
317
+ validTo ,
318
+ limitOrderKind
272
319
) ;
273
320
const calculatedOrderId = await calculateUniqueOrderId ( chainId , unsignerOrder ) ;
274
321
@@ -311,17 +358,15 @@ export const SwitchActions = ({
311
358
tokenDest : outputToken ,
312
359
chainId,
313
360
user,
314
- amount : switchRates . srcAmount ,
315
361
tokenSrc : inputToken ,
316
362
tokenSrcDecimals : switchRates . srcDecimals ,
317
363
tokenDestDecimals : switchRates . destDecimals ,
318
- afterNetworkCostsBuyAmount :
319
- orderKind === 'market'
320
- ? switchRates . amountAndCosts . afterNetworkCosts . buyAmount . toString ( )
321
- : switchRates . destAmount , // TODO: check with cow team
364
+ sellAmount : srcAmount ,
365
+ beforePartnerFeesBuyAmount : destAmount ,
322
366
slippageBps,
323
367
inputSymbol,
324
368
outputSymbol,
369
+ kind : limitOrderKind ,
325
370
validTo,
326
371
quote : switchRates . order ,
327
372
} ) ;
@@ -355,18 +400,16 @@ export const SwitchActions = ({
355
400
tokenDest : outputToken ,
356
401
tokenDestDecimals : switchRates . destDecimals ,
357
402
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
403
+ sellAmount : srcAmount ,
404
+ beforePartnerFeesBuyAmount : destAmount ,
363
405
slippageBps,
364
406
chainId,
365
407
user,
366
408
provider,
367
409
inputSymbol,
368
- validTo,
369
410
outputSymbol,
411
+ validTo,
412
+ kind : limitOrderKind ,
370
413
} ) ;
371
414
setMainTxState ( {
372
415
loading : false ,
0 commit comments