-
Notifications
You must be signed in to change notification settings - Fork 14
/
Twap.sol
61 lines (50 loc) · 1.9 KB
/
Twap.sol
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
// SPDX-License-Identifier: MIT
pragma solidity ^0.8.13;
import "./interfaces/IUniswapV2Pair.sol";
contract Twap {
/**
* PERFORM A ONE-HOUR AND ONE-DAY TWAP
*
* This contract includes four functions that are called at different intervals: the first two functions are 1 hour apart,
* while the last two are 1 day apart.
*
* The challenge is to calculate the one-hour and one-day TWAP (Time-Weighted Average Price) of the first token in the
* given pool and store the results in the return variable.
*
* Hint: For each time interval, the player needs to take a snapshot in the first function, then calculate the TWAP
* in the second function and divide it by the appropriate time interval.
*
*/
IUniswapV2Pair pool;
// 1HourTWAP storage slots
uint256 public first1HourSnapShot_Price0Cumulative;
uint32 public first1HourSnapShot_TimeStamp;
uint256 public second1HourSnapShot_Price0Cumulative;
uint32 public second1HourSnapShot_TimeStamp;
// 1DayTWAP storage slots
uint256 public first1DaySnapShot_Price0Cumulative;
uint32 public first1DaySnapShot_TimeStamp;
uint256 public second1DaySnapShot_Price0Cumulative;
uint32 public second1DaySnapShot_TimeStamp;
constructor(address _pool) {
pool = IUniswapV2Pair(_pool);
}
//** ONE HOUR TWAP START **//
function first1HourSnapShot() public {
// your code here
}
function second1HourSnapShot() public returns (uint224 oneHourTwap) {
// your code here
return oneHourTwap;
}
//** ONE HOUR TWAP END **//
//** ONE DAY TWAP START **//
function first1DaySnapShot() public {
// your code here
}
function second1DaySnapShot() public returns (uint224 oneDayTwap) {
// your code here
return (oneDayTwap);
}
//** ONE DAY TWAP END **//
}