-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathStrategy.sol
More file actions
120 lines (105 loc) · 3.63 KB
/
Strategy.sol
File metadata and controls
120 lines (105 loc) · 3.63 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
// SPDX-License-Identifier: MIT
pragma solidity 0.8.26;
import {console} from "forge-std/Test.sol";
import {IERC20} from "../../interfaces/IERC20.sol";
import {Math} from "../../lib/Math.sol";
import {Auth} from "../../lib/app/Auth.sol";
import "../../Constants.sol";
import {GmxHelper} from "./GmxHelper.sol";
contract Strategy is Auth, GmxHelper {
IERC20 public constant weth = IERC20(WETH);
constructor(address oracle)
GmxHelper(
GM_TOKEN_ETH_WETH_USDC,
WETH,
USDC,
CHAINLINK_ETH_USD,
CHAINLINK_USDC_USD,
oracle
)
{}
receive() external payable {}
// Task 1: Calculate total vaule managed by this contract in terms of WETH
function totalValueInToken() external view returns (uint256) {
uint256 val = weth.balanceOf(address(this));
int256 remainingCollateral = getPositionWithPnlInToken();
if (remainingCollateral >= 0) {
val += uint256(remainingCollateral);
} else {
val -= Math.min(val, uint256(-remainingCollateral));
}
return val;
}
// Task 2: Create market increase order
function increase(uint256 wethAmount)
external
payable
auth
returns (bytes32 orderKey)
{
orderKey = createIncreaseShortPositionOrder({
executionFee: msg.value,
longTokenAmount: wethAmount
});
}
// Task 3: Create market decrease order
// Function call is from the vault when the callback contract is not address(0).
function decrease(uint256 wethAmount, address callbackContract)
external
payable
auth
returns (bytes32 orderKey)
{
if (callbackContract == address(0)) {
orderKey = createDecreaseShortPositionOrder({
executionFee: msg.value,
longTokenAmount: wethAmount,
receiver: address(this),
callbackContract: address(0),
callbackGasLimit: 0
});
} else {
// Called from the vault
require(
callbackContract.code.length > 0, "callback is not a contract"
);
uint256 maxCallbackGasLimit = getMaxCallbackGasLimit();
require(
msg.value > maxCallbackGasLimit,
"callback gas limit < execution fee"
);
int256 total = getPositionWithPnlInToken();
require(total > 0, "position with pnl <= 0");
orderKey = createDecreaseShortPositionOrder({
executionFee: msg.value,
// Calculate collateral to withdraw
longTokenAmount: getPositionCollateralAmount() * wethAmount
/ uint256(total),
receiver: callbackContract,
callbackContract: callbackContract,
callbackGasLimit: maxCallbackGasLimit
});
}
}
// Task 4: Cancel an order
function cancel(bytes32 orderKey) external payable auth {
cancelOrder(orderKey);
}
// Task 5: Claim funding fees
function claim() external {
claimFundingFees();
}
function transfer(address dst, uint256 amount) external auth {
weth.transfer(dst, amount);
}
function withdraw(address token) external auth {
if (token == address(0)) {
(bool ok,) = msg.sender.call{value: address(this).balance}("");
require(ok, "Send ETH failed");
} else {
IERC20(token).transfer(
msg.sender, IERC20(token).balanceOf(address(this))
);
}
}
}