-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathTakeProfitAndStopLoss.sol
More file actions
164 lines (151 loc) · 6.51 KB
/
TakeProfitAndStopLoss.sol
File metadata and controls
164 lines (151 loc) · 6.51 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
// SPDX-License-Identifier: MIT
pragma solidity 0.8.26;
import {console} from "forge-std/Test.sol";
import {IERC20} from "../interfaces/IERC20.sol";
import {IExchangeRouter} from "../interfaces/IExchangeRouter.sol";
import {Order} from "../types/Order.sol";
import {IBaseOrderUtils} from "../types/IBaseOrderUtils.sol";
import {Oracle} from "../lib/Oracle.sol";
import "../Constants.sol";
contract TakeProfitAndStopLoss {
IERC20 constant weth = IERC20(WETH);
IERC20 constant usdc = IERC20(USDC);
IExchangeRouter constant exchangeRouter = IExchangeRouter(EXCHANGE_ROUTER);
Oracle immutable oracle;
constructor(address _oracle) {
oracle = Oracle(_oracle);
}
// Task 1 - Receive execution fee refund from GMX
receive() external payable {}
// Task 2 - Create orders to
// 1. Long ETH with USDC collateral
// 2. Stop loss for ETH price below 90% of current price
// 3. Take profit for ETH price above 110% of current price
function createTakeProfitAndStopLossOrders(
uint256 leverage,
uint256 usdcAmount
) external payable returns (bytes32[] memory keys) {
uint256 executionFee = 0.1 * 1e18;
usdc.transferFrom(msg.sender, address(this), usdcAmount);
keys = new bytes32[](3);
// Task 2.1 - Send execution fee to the order vault
exchangeRouter.sendWnt{value: executionFee}({
receiver: ORDER_VAULT,
amount: executionFee
});
// Task 2.2 - Send USDC to the order vault
usdc.approve(ROUTER, usdcAmount);
exchangeRouter.sendTokens({
token: USDC,
receiver: ORDER_VAULT,
amount: usdcAmount
});
// Task 2.3 - Create a long order to long ETH with USDC collateral
// 1 USD = 1e8
uint256 usdcPrice = oracle.getPrice(CHAINLINK_USDC_USD);
uint256 ethPrice = oracle.getPrice(CHAINLINK_ETH_USD);
// 1 USD = 1e30
uint256 sizeDeltaUsd = leverage * usdcAmount * usdcPrice * 1e16;
keys[0] = exchangeRouter.createOrder(
IBaseOrderUtils.CreateOrderParams({
addresses: IBaseOrderUtils.CreateOrderParamsAddresses({
receiver: address(this),
cancellationReceiver: address(0),
callbackContract: address(0),
uiFeeReceiver: address(0),
market: GM_TOKEN_ETH_WETH_USDC,
initialCollateralToken: USDC,
swapPath: new address[](0)
}),
numbers: IBaseOrderUtils.CreateOrderParamsNumbers({
sizeDeltaUsd: sizeDeltaUsd,
initialCollateralDeltaAmount: 0,
triggerPrice: 0,
acceptablePrice: ethPrice * 1e4 * 101 / 100,
executionFee: executionFee,
callbackGasLimit: 0,
minOutputAmount: 0,
validFromTime: 0
}),
orderType: Order.OrderType.MarketIncrease,
decreasePositionSwapType: Order.DecreasePositionSwapType.NoSwap,
isLong: true,
shouldUnwrapNativeToken: false,
autoCancel: false,
referralCode: bytes32(uint256(0))
})
);
// Task 2.4 - Send execution fee to the order vault
exchangeRouter.sendWnt{value: executionFee}({
receiver: ORDER_VAULT,
amount: executionFee
});
// Task 2.5 - Create a stop loss for 90% of current ETH price
keys[1] = exchangeRouter.createOrder(
IBaseOrderUtils.CreateOrderParams({
addresses: IBaseOrderUtils.CreateOrderParamsAddresses({
receiver: address(this),
cancellationReceiver: address(0),
callbackContract: address(0),
uiFeeReceiver: address(0),
market: GM_TOKEN_ETH_WETH_USDC,
initialCollateralToken: USDC,
swapPath: new address[](0)
}),
numbers: IBaseOrderUtils.CreateOrderParamsNumbers({
sizeDeltaUsd: sizeDeltaUsd,
initialCollateralDeltaAmount: usdcAmount,
triggerPrice: ethPrice * 1e4 * 90 / 100,
acceptablePrice: 0,
executionFee: executionFee,
callbackGasLimit: 0,
minOutputAmount: 0,
validFromTime: block.timestamp
}),
orderType: Order.OrderType.StopLossDecrease,
decreasePositionSwapType: Order.DecreasePositionSwapType.NoSwap,
isLong: true,
shouldUnwrapNativeToken: false,
autoCancel: true,
referralCode: bytes32(uint256(0))
})
);
// Task 2.6 - Send execution fee to the order vault
exchangeRouter.sendWnt{value: executionFee}({
receiver: ORDER_VAULT,
amount: executionFee
});
// Task 2.7 - Create an order to take profit above 110% of current price
keys[2] = exchangeRouter.createOrder(
IBaseOrderUtils.CreateOrderParams({
addresses: IBaseOrderUtils.CreateOrderParamsAddresses({
receiver: address(this),
cancellationReceiver: address(0),
callbackContract: address(0),
uiFeeReceiver: address(0),
market: GM_TOKEN_ETH_WETH_USDC,
initialCollateralToken: USDC,
swapPath: new address[](0)
}),
numbers: IBaseOrderUtils.CreateOrderParamsNumbers({
sizeDeltaUsd: sizeDeltaUsd,
initialCollateralDeltaAmount: usdcAmount,
triggerPrice: ethPrice * 1e4 * 110 / 100,
acceptablePrice: ethPrice * 1e4 * 99 / 100,
executionFee: executionFee,
callbackGasLimit: 0,
minOutputAmount: 0,
validFromTime: block.timestamp
}),
orderType: Order.OrderType.LimitDecrease,
decreasePositionSwapType: Order
.DecreasePositionSwapType
.SwapPnlTokenToCollateralToken,
isLong: true,
shouldUnwrapNativeToken: false,
autoCancel: true,
referralCode: bytes32(uint256(0))
})
);
}
}