@@ -11,83 +11,87 @@ import { StrategyType } from 'features/aave/types'
11
11
import { formatPercent } from 'helpers/formatters/format'
12
12
import type { GetTriggersResponse } from 'helpers/lambda/triggers'
13
13
import { nbsp } from 'helpers/nbsp'
14
- import { useMemo } from 'react '
14
+ import { LendingProtocol } from 'lendingProtocols '
15
15
16
- export const mapPartialTakeProfitFromLambda = (
17
- strategyConfig : IStrategyConfig ,
18
- triggers ?: GetTriggersResponse [ 'triggers' ] ,
19
- ) => {
20
- if ( ! triggers ) {
21
- return { }
22
- }
23
- const isShort = strategyConfig . strategyType === StrategyType . Short
16
+ interface MapPartialTakeProfitFromLambdaParams {
17
+ poolId ?: string
18
+ protocol : LendingProtocol
19
+ strategyConfig : IStrategyConfig
20
+ triggers ?: GetTriggersResponse [ 'triggers' ]
21
+ }
24
22
25
- const partialTakeProfitTriggersNames = Object . keys ( triggers ) . filter ( ( triggerName ) =>
26
- triggerName . includes ( 'PartialTakeProfit' ) ,
27
- )
28
- if ( partialTakeProfitTriggersNames . length > 1 ) {
29
- console . warn (
30
- 'Warning: more than one partial take profit trigger found:' ,
31
- partialTakeProfitTriggersNames ,
32
- )
23
+ const getTrigger = ( { protocol, triggers, poolId } : MapPartialTakeProfitFromLambdaParams ) => {
24
+ if ( ! triggers ) return undefined
25
+
26
+ switch ( protocol ) {
27
+ case LendingProtocol . AaveV3 : {
28
+ return triggers . aave3 . partialTakeProfit
29
+ }
30
+ case LendingProtocol . MorphoBlue : {
31
+ if ( `morphoblue-${ poolId } ` in triggers )
32
+ return triggers [ `morphoblue-${ poolId } ` ] . partialTakeProfit
33
+ else return undefined
34
+ }
35
+ case LendingProtocol . SparkV3 : {
36
+ return triggers . spark . partialTakeProfit
37
+ }
38
+ default :
39
+ return undefined
33
40
}
34
- const partialTakeProfitTriggerName = partialTakeProfitTriggersNames [ 0 ] as
35
- | 'sparkPartialTakeProfit'
36
- | 'aavePartialTakeProfit'
37
- const selectedTrigger = triggers [ partialTakeProfitTriggerName ]
41
+ }
38
42
39
- const hasStopLoss = hasActiveStopLossFromTriggers ( { triggers, protocol : strategyConfig . protocol } )
40
- const hasTrailingStopLoss = hasActiveTrailingStopLossFromTriggers ( {
41
- triggers,
42
- protocol : strategyConfig . protocol ,
43
- } )
44
- const currentStopLossLevel = useMemo ( ( ) => {
45
- return mapStopLossFromLambda ( triggers ) . stopLossLevel
46
- } , [ triggers ] )
47
- const trailingStopLossData = mapTrailingStopLossFromLambda ( triggers )
48
- const stopLossTokenLabel = isShort
49
- ? `${ strategyConfig . tokens . debt } /${ strategyConfig . tokens . collateral } `
50
- : `${ strategyConfig . tokens . collateral } /${ strategyConfig . tokens . debt } `
43
+ export const mapPartialTakeProfitFromLambda = ( {
44
+ poolId,
45
+ protocol,
46
+ strategyConfig,
47
+ triggers,
48
+ } : MapPartialTakeProfitFromLambdaParams ) => {
49
+ if ( ! triggers ) return { }
51
50
52
- const triggerLtv = selectedTrigger ?. decodedParams . executionLtv
53
- ? new BigNumber ( Number ( selectedTrigger . decodedParams . executionLtv ) ) . div (
54
- lambdaPercentageDenomination ,
55
- )
56
- : undefined
57
- const startingTakeProfitPriceLong = selectedTrigger ?. decodedParams . executionPrice
58
- ? new BigNumber ( Number ( selectedTrigger . decodedParams . executionPrice ) ) . div (
59
- lambdaPriceDenomination ,
60
- )
61
- : undefined
62
- const startingTakeProfitPriceShort = selectedTrigger ?. decodedParams . executionPrice
63
- ? new BigNumber ( lambdaPriceDenomination ) . div (
64
- new BigNumber ( Number ( selectedTrigger . decodedParams . executionPrice ) ) ,
65
- )
66
- : undefined
67
- const withdrawalLtv =
68
- selectedTrigger ?. decodedParams . targetLtv && selectedTrigger ?. decodedParams . executionLtv
69
- ? new BigNumber ( Number ( selectedTrigger . decodedParams . targetLtv ) )
70
- . minus ( new BigNumber ( Number ( selectedTrigger ?. decodedParams . executionLtv ) ) )
71
- . div ( lambdaPercentageDenomination )
72
- : undefined
73
- const partialTakeProfitToken =
74
- selectedTrigger ?. decodedParams . withdrawToDebt === 'true'
75
- ? ( 'debt' as const )
76
- : ( 'collateral' as const )
51
+ const trigger = getTrigger ( { poolId, protocol, strategyConfig, triggers } )
77
52
78
- return {
79
- triggerId : selectedTrigger ?. triggerId ,
80
- triggerLtv,
81
- startingTakeProfitPrice : isShort ? startingTakeProfitPriceShort : startingTakeProfitPriceLong ,
82
- withdrawalLtv,
83
- partialTakeProfitToken,
84
- hasStopLoss : hasStopLoss || hasTrailingStopLoss ,
85
- currentStopLossLevel,
86
- currentTrailingDistance : trailingStopLossData . trailingDistance ,
87
- stopLossLevelLabel :
88
- hasStopLoss && currentStopLossLevel ? `${ formatPercent ( currentStopLossLevel ) } ` : '' ,
89
- trailingStopLossDistanceLabel : hasTrailingStopLoss
90
- ? `${ trailingStopLossData . trailingDistance } ${ nbsp } ${ stopLossTokenLabel } `
91
- : '' ,
92
- }
53
+ if ( trigger ) {
54
+ const isShort = strategyConfig . strategyType === StrategyType . Short
55
+ const hasStopLoss = hasActiveStopLossFromTriggers ( { triggers, protocol } )
56
+ const hasTrailingStopLoss = hasActiveTrailingStopLossFromTriggers ( { triggers, protocol } )
57
+
58
+ const currentStopLossLevel = mapStopLossFromLambda ( { poolId, protocol, triggers } ) . stopLossLevel
59
+ const trailingStopLossData = mapTrailingStopLossFromLambda ( { poolId, protocol, triggers } )
60
+
61
+ const stopLossTokenLabel = isShort
62
+ ? `${ strategyConfig . tokens . debt } /${ strategyConfig . tokens . collateral } `
63
+ : `${ strategyConfig . tokens . collateral } /${ strategyConfig . tokens . debt } `
64
+
65
+ const triggerLtv = new BigNumber ( Number ( trigger . decodedParams . executionLtv ) ) . div (
66
+ lambdaPercentageDenomination ,
67
+ )
68
+ const startingTakeProfitPriceLong = new BigNumber (
69
+ Number ( trigger . decodedParams . executionPrice ) ,
70
+ ) . div ( lambdaPriceDenomination )
71
+ const startingTakeProfitPriceShort = new BigNumber ( lambdaPriceDenomination ) . div (
72
+ new BigNumber ( Number ( trigger . decodedParams . executionPrice ) ) ,
73
+ )
74
+ const withdrawalLtv = new BigNumber ( Number ( trigger . decodedParams . targetLtv ) )
75
+ . minus ( new BigNumber ( Number ( trigger . decodedParams . executionLtv ) ) )
76
+ . div ( lambdaPercentageDenomination )
77
+
78
+ const partialTakeProfitToken =
79
+ trigger . decodedParams . withdrawToDebt === 'true' ? ( 'debt' as const ) : ( 'collateral' as const )
80
+
81
+ return {
82
+ triggerId : trigger . triggerId ,
83
+ triggerLtv,
84
+ startingTakeProfitPrice : isShort ? startingTakeProfitPriceShort : startingTakeProfitPriceLong ,
85
+ withdrawalLtv,
86
+ partialTakeProfitToken,
87
+ hasStopLoss : hasStopLoss || hasTrailingStopLoss ,
88
+ currentStopLossLevel,
89
+ currentTrailingDistance : trailingStopLossData . trailingDistance ,
90
+ stopLossLevelLabel :
91
+ hasStopLoss && currentStopLossLevel ? `${ formatPercent ( currentStopLossLevel ) } ` : '' ,
92
+ trailingStopLossDistanceLabel : hasTrailingStopLoss
93
+ ? `${ trailingStopLossData . trailingDistance } ${ nbsp } ${ stopLossTokenLabel } `
94
+ : '' ,
95
+ }
96
+ } else return { }
93
97
}
0 commit comments