-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathProgram.cs
150 lines (129 loc) · 5.2 KB
/
Program.cs
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
using System;
using System.Configuration;
using System.Diagnostics;
using System.Reflection;
using System.Timers;
using GraphQL.Client.Http;
using GraphQL.Client.Serializer.SystemTextJson;
using log4net;
namespace Uniswap.Client
{
public class Program
{
static readonly Stopwatch StopWatch = new Stopwatch();
const string UniswapUrl = "https://api.thegraph.com/subgraphs/name/uniswap/uniswap-v3";
static readonly ILog SLogger = LogManager.GetLogger(
MethodBase.GetCurrentMethod()?.DeclaringType);
static void Main(string[] args)
{
bool validInput = true;
if (!int.TryParse(ConfigurationManager.AppSettings["PoolId"], out var poolId))
{
Console.WriteLine(
"Configuration for key 'PoolId' is not in correct integer format.");
validInput = false;
}
if (!int.TryParse(
ConfigurationManager.AppSettings["PrecisionForPosition0"],
out var precisionForPosition0))
{
Console.WriteLine(
"Configuration for key 'PrecisionForPosition0' is not in correct integer format.");
validInput = false;
}
if (!int.TryParse(
ConfigurationManager.AppSettings["PrecisionForPosition1"],
out var precisionForPosition1))
{
Console.WriteLine(
"Configuration for key 'PrecisionForPosition1' is not in correct integer format.");
validInput = false;
}
if (!int.TryParse(
ConfigurationManager.AppSettings["PrecisionForCurrentPrice"],
out var precisionForCurrentPrice))
{
Console.WriteLine(
"Configuration for key 'PrecisionForCurrentPrice' is not in correct integer format.");
validInput = false;
}
if (!int.TryParse(
ConfigurationManager.AppSettings["IntervalInSeconds"],
out var interval))
{
Console.WriteLine(
"Configuration for key 'IntervalInSeconds' is not in correct integer format.");
validInput = false;
}
if (!validInput)
{
return;
}
var graphQlOptions = new GraphQLHttpClientOptions
{
EndPoint = new Uri(UniswapUrl)
};
var graphQlClient = new GraphQLHttpClient(
graphQlOptions,
new SystemTextJsonSerializer());
var unisawp = new GraphQL.Uniswap(graphQlClient);
int retry = 0;
int sum = 0;
do
{
retry++;
sum += 5 * retry;
} while (sum < interval);
var t = new Timer();
t.Elapsed += (sender, e) => Callback(unisawp,
poolId,
retry - 1,
precisionForPosition0 == 0 ? null : (int?)precisionForPosition0,
precisionForPosition1 == 0 ? null : (int?)precisionForPosition1,
precisionForCurrentPrice == 0 ? null : (int?)precisionForCurrentPrice);
t.Interval = TimeSpan.FromSeconds(interval).TotalMilliseconds;
t.Enabled = true;
Console.WriteLine("Press \'q\' to quit the sample.");
Console.WriteLine();
Console.WriteLine($"*********** Liquidity position for pool id : {poolId} ***********");
Console.WriteLine("***************************************************************");
while (Console.Read() != 'q') { };
}
static async void Callback(
GraphQL.Uniswap uniswapGraphQl,
int poolId,
int maxRetryCount,
int? precisionForPosition0,
int? precisionForPosition1,
int? precisionForCurrentPrice)
{
StopWatch.Restart();
string msg = string.Empty;
try
{
var result = await uniswapGraphQl.GetLiquidityPosition(
poolId,
maxRetryCount,
precisionForPosition0,
precisionForPosition1,
precisionForCurrentPrice);
msg = $"[{DateTime.Now}] " +
$"{result.Position.Token0.Symbol} position {result.Position.Position0Amount}, " +
$"{result.Position.Token1.Symbol} position {result.Position.Position1Amount}, " +
$"Current Price {result.Position.Price0Amount} ({result.Position.Token0.Symbol} per {result.Position.Token1.Symbol}) " +
$"[Time taken: {StopWatch.ElapsedMilliseconds} ms]";
}
catch(Exception ex)
{
SLogger.Error(ex);
msg = $"[{DateTime.Now}] Unable to retrieve the data from server after {maxRetryCount} retries.";
}
finally
{
SLogger.Info(msg);
Console.WriteLine(msg);
}
StopWatch.Stop();
}
}
}