-
Notifications
You must be signed in to change notification settings - Fork 0
/
Program.cs
242 lines (196 loc) · 7.48 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
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
using System;
using Microsoft.Extensions.Configuration;
using Microsoft.Extensions.DependencyInjection;
using Microsoft.Extensions.Options;
using NLog;
using Coinbase;
using Coinbase.Models;
using System.Net;
using System.Threading;
using System.Threading.Tasks;
namespace CoinbaseBot
{
class Program
{
public static Logger logger = LogManager.GetCurrentClassLogger();
public static IConfigurationRoot Configuration { get; set; }
public static string[] cryptoCoins = { "BTC", "BCH", "ETH", "LTC", "ATOM", "COMP", "BAT", "ALGO", "KNC", "GRT", "BAND" };
static void Main(string[] args)
{
// Logger
logger.Info("");
logger.Info("CoinbaseBot exe started - " + String.Format("{0:MMM d, yyyy h:mm tt}", DateTime.Now));
logger.Info("Loading...");
string devEnvironmentVariable = Environment.GetEnvironmentVariable("NETCORE_ENVIRONMENT");
bool isDevelopment = string.IsNullOrEmpty(devEnvironmentVariable) ||
devEnvironmentVariable.ToLower() == "development";
//Determines the working environment as IHostingEnvironment is unavailable in a console app
ServicePointManager.SecurityProtocol = SecurityProtocolType.Tls12;
IServiceProvider services = ServiceProviderBuilder.GetServiceProvider(args);
IOptions<SecretKeys> options = services.GetRequiredService<IOptions<SecretKeys>>();
logger.Info("BTC Address: " + options.Value.BTCAddress);
logger.Info("Coinbase Api Key: " + options.Value.CoinbaseApiKey);
string strHostName = Dns.GetHostName();
logger.Info("Host name: " + strHostName);
IPHostEntry ipEntry = Dns.GetHostEntry(strHostName);
IPAddress[] addr = ipEntry.AddressList;
for (int i = 0; i < addr.Length; i++)
{
logger.Info("IP Address {0}: {1} ", i, addr[i].ToString());
}
try
{
CheckFiatAccounts(options);
CheckCryptoAccounts(options);
ToInfinityAndBeyond(options);
}
catch (Exception erEx)
{
HandleError(options, erEx);
}
Console.ReadKey();
}
static void ToInfinityAndBeyond(IOptions<SecretKeys> options)
{
//using API Key + Secret authentication
CoinbaseClient coinbaseClient = new CoinbaseClient(new ApiKeyConfig
{
ApiKey = options.Value.CoinbaseApiKey,
ApiSecret = options.Value.CoinbaseApiSecret,
UseTimeApi = true
});
//No authentication
// - Useful only for Data Endpoints that don't require authentication.
//client = new CoinbaseClient();
CancellationTokenSource source = new CancellationTokenSource();
CancellationToken token = source.Token;
Task<Response<Money>> spotPrice = Task.Run(
() =>
{
return coinbaseClient.Data.GetSpotPriceAsync("BTC-USD");
}, token
);
spotPrice.Wait();
logger.Info("BTC is at USD $" + spotPrice.Result.Data.Amount.ToString("N2"));
}
static void CheckFiatAccounts(IOptions<SecretKeys> options)
{
CancellationTokenSource source = new CancellationTokenSource();
CancellationToken token = source.Token;
//using API Key + Secret authentication
CoinbaseClient coinbaseClient = new CoinbaseClient(new ApiKeyConfig
{
ApiKey = options.Value.CoinbaseApiKey,
ApiSecret = options.Value.CoinbaseApiSecret,
UseTimeApi = true
});
Task<PagedResponse<PaymentMethod>> paymentMethod = Task.Run(
() =>
{
return coinbaseClient.PaymentMethods.ListPaymentMethodsAsync();
}, token
);
paymentMethod.Wait();
logger.Info("Check the fiat accounts");
}
static void CheckCryptoAccounts(IOptions<SecretKeys> options)
{
CancellationTokenSource source = new CancellationTokenSource();
CancellationToken token = source.Token;
//using API Key + Secret authentication
CoinbaseClient coinbaseClient = new CoinbaseClient(new ApiKeyConfig
{
ApiKey = options.Value.CoinbaseApiKey,
ApiSecret = options.Value.CoinbaseApiSecret,
UseTimeApi = true
});
Array.ForEach(cryptoCoins, element =>
{
Task<Response<Account>> act = Task.Run(
() =>
{
return coinbaseClient.Accounts.GetAccountAsync(element);
}, token
);
act.Wait();
logger.Info(element + " Balance is at --- " + act.Result.Data.Balance.Amount.ToString());
}
);
}
static void SellCrypto(IOptions<SecretKeys> options, CoinbaseClient coinbaseClient, CancellationToken token, decimal sellPrice, decimal coinageCnt, string fiatAddress, string cryptoAddress, string crypto)
{
try
{
decimal decTotalAmount = sellPrice * coinageCnt;
string totalAmount = decTotalAmount.ToString();
PlaceSell createSell = new PlaceSell
{
Total = totalAmount,
Currency = crypto,
PaymentMethod = fiatAddress,
Commit = true
};
Task<Response<Sell>> sellCoinage = Task.Run(
() =>
{
return coinbaseClient.Sells.PlaceSellOrderAsync(cryptoAddress, createSell);
}, token
);
sellCoinage.Wait();
string emailBody = coinageCnt + " of " + crypto + " was sold at the unit price of $" + sellPrice.ToString("N2") + ", netting a grand total of " + decTotalAmount.ToString("N2");
EmailHelper.SendEmail(options.Value.SmtpEmail, options.Value.SmtpPassword, options.Value.SmtpEmail, "CoinbaseBot Sold " + crypto + " Coins", emailBody, options.Value.SmtpHost, options.Value.SmtpPort);
}
catch(Exception erEx)
{
HandleError(options, erEx);
}
}
static void BuyCrypto(IOptions<SecretKeys> options, CoinbaseClient coinbaseClient, CancellationToken token, decimal dollarDecimal, string fiatAddress, string cryptoAddress, string crypto)
{
try
{
PlaceBuy createBuy = new PlaceBuy
{
Amount = dollarDecimal,
Currency = "USD",
PaymentMethod = fiatAddress,
Commit = true
};
Task<Response<Buy>> buyCoinage = Task.Run(
() =>
{
return coinbaseClient.Buys.PlaceBuyOrderAsync(cryptoAddress, createBuy);
}, token
);
buyCoinage.Wait();
string emailBody = "$" + dollarDecimal.ToString("N2") + " of " + crypto + " was bought!";
EmailHelper.SendEmail(options.Value.SmtpEmail, options.Value.SmtpPassword, options.Value.SmtpEmail, "CoinbaseBot Sold " + crypto + " Coins", emailBody, options.Value.SmtpHost, options.Value.SmtpPort);
}
catch (Exception erEx)
{
HandleError(options, erEx);
}
}
static void HandleError(IOptions<SecretKeys> options, Exception erEx)
{
string emailBody = erEx.Message;
logger.Error(erEx.Message);
if (erEx.InnerException != null)
{
logger.Error(erEx.InnerException.ToString());
emailBody += "<br /><br />" + erEx.InnerException.ToString();
}
if (erEx.StackTrace != null)
{
logger.Error(erEx.StackTrace);
emailBody += "<br /><br />" + erEx.StackTrace;
}
if (erEx.Source != null)
{
logger.Error(erEx.Source);
emailBody += "<br /><br />" + erEx.Source;
}
EmailHelper.SendEmail(options.Value.SmtpEmail, options.Value.SmtpPassword, options.Value.SmtpEmail, "CoinbaseBot Error Message", emailBody, options.Value.SmtpHost, options.Value.SmtpPort);
}
}
}