Skip to content

Commit

Permalink
Merge pull request #7 from stojce/master
Browse files Browse the repository at this point in the history
C# BASED CLIENT FOR THE NEW SOCKET-BASED API
  • Loading branch information
zackcoburn committed Dec 12, 2017
2 parents 3bc9411 + f937ada commit cd13ff4
Show file tree
Hide file tree
Showing 19 changed files with 1,166 additions and 0 deletions.
9 changes: 9 additions & 0 deletions dotnet/.gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
*.userprefs
.vscode/*
!.vscode/settings.json
!.vscode/tasks.json
!.vscode/launch.json
!.vscode/extensions.json
bin/
obj/
.vs
48 changes: 48 additions & 0 deletions dotnet/.vscode/launch.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,48 @@
{
// Use IntelliSense to find out which attributes exist for C# debugging
// Use hover for the description of the existing attributes
// For further information visit https://github.com/OmniSharp/omnisharp-vscode/blob/master/debugger-launchjson.md
"version": "0.2.0",
"configurations": [
{
"name": "Taker",
"type": "coreclr",
"request": "launch",
"preLaunchTask": "build",
// If you have changed target frameworks, make sure to update the program path.
"program": "${workspaceFolder}/bin/Debug/netcoreapp2.0/EhterDelta.Bots.Dontnet.dll",
"args": [
"taker",
"-v"
],
"cwd": "${workspaceFolder}",
// For more information about the 'console' field, see https://github.com/OmniSharp/omnisharp-vscode/blob/master/debugger-launchjson.md#console-terminal-window
"console": "internalConsole",
"stopAtEntry": false,
"internalConsoleOptions": "openOnSessionStart"
},
{
"name": "Maker",
"type": "coreclr",
"request": "launch",
"preLaunchTask": "build",
// If you have changed target frameworks, make sure to update the program path.
"program": "${workspaceFolder}/bin/Debug/netcoreapp2.0/EhterDelta.Bots.Dontnet.dll",
"args": [
"maker",
"-v"
],
"cwd": "${workspaceFolder}",
// For more information about the 'console' field, see https://github.com/OmniSharp/omnisharp-vscode/blob/master/debugger-launchjson.md#console-terminal-window
"console": "internalConsole",
"stopAtEntry": false,
"internalConsoleOptions": "openOnSessionStart"
},
{
"name": ".NET Core Attach",
"type": "coreclr",
"request": "attach",
"processId": "${command:pickProcess}"
}
]
}
3 changes: 3 additions & 0 deletions dotnet/.vscode/settings.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
{
"editor.tabSize": 4
}
15 changes: 15 additions & 0 deletions dotnet/.vscode/tasks.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
{
"version": "2.0.0",
"tasks": [
{
"label": "build",
"command": "dotnet",
"type": "process",
"args": [
"build",
"${workspaceFolder}/EtherDeltaClient.csproj"
],
"problemMatcher": "$msCompile"
}
]
}
16 changes: 16 additions & 0 deletions dotnet/App.config
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
<?xml version="1.0" encoding="utf-8" ?>
<configuration>
<appSettings>
<add key="SocketUrl" value="wss://socket.etherdelta.com/socket.io/?transport=websocket" />
<add key="Provider" value="https://mainnet.infura.io/Ky03pelFIxoZdAUsr82w"/>
<add key="AddressEtherDelta" value="0x8d12a197cb00d4747a1fe03395095ce2a5cc6819" />
<add key="AbiFile" value="../contracts/etherdelta.json" />
<add key="TokenFile" value="../contracts/token.json" />
<add key="Token" value="0x8f3470a7388c05ee4e7af3d01d8c722b0ff52374" />
<add key="User" value="0x1a96f3bc6cd0e9db43e89f983d4227ec5f707539" />
<add key="PrivateKey" value="1234567891234567891234567891234567891234567891234567891234567890" />
<add key="UnitDecimals" value="18" />
<add key="GasPrice" value="1000000000" />
<add key="GasLimit" value="250000" />
</appSettings>
</configuration>
194 changes: 194 additions & 0 deletions dotnet/BaseBot.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,194 @@
using Nethereum.Util;
using System;
using System.Linq;
using System.Numerics;
using System.Threading.Tasks;

namespace EhterDelta.Bots.Dontnet
{
public abstract class BaseBot
{
protected Service Service { get; set; }

protected BigInteger EtherDeltaETH { get; set; }
protected BigInteger WalletETH { get; set; }
protected BigInteger EtherDeltaToken { get; set; }
protected BigInteger WalletToken { get; set; }

public BaseBot(EtherDeltaConfiguration config, ILogger logger = null)
{
Console.Clear();
Console.ResetColor();
Service = new Service(config, logger);

Task[] tasks = new[] {
GetMarket(),
GetBalanceAsync("ETH", config.User),
GetBalanceAsync(config.Token, config.User),
GetEtherDeltaBalance("ETH", config.User),
GetEtherDeltaBalance(config.Token, config.User)
};

Task.WaitAll(tasks);

PrintOrders();
PrintTrades();
PrintWallet();

Console.WriteLine();
}

private async Task<BigInteger> GetEtherDeltaBalance(string token, string user)
{
BigInteger balance = 0;
try
{
balance = await Service.GetEtherDeltaBalance(token, user);
}
catch (TimeoutException)
{
Console.WriteLine("Could not get balance");
}

if (token == "ETH")
{
EtherDeltaETH = balance;
}
else
{
EtherDeltaToken = balance;
}
return balance;
}

private async Task<BigInteger> GetBalanceAsync(string token, string user)
{
BigInteger balance = 0;

try
{
balance = await Service.GetBalance(token, user);
}
catch (TimeoutException)
{
Console.WriteLine("Could not get balance");
}

if (token == "ETH")
{
WalletETH = balance;
}
else
{
WalletToken = balance;
}
return balance;
}

private void PrintTrades()
{
Console.WriteLine();
Console.WriteLine("Recent trades");
Console.WriteLine("====================================");
int numTrades = 10;

if (Service.Trades != null)
{
var trades = Service.Trades.Take(numTrades);
foreach (var trade in trades)
{
Console.ForegroundColor = trade.Side == "sell" ? ConsoleColor.Red : ConsoleColor.Green;
Console.WriteLine($"{trade.Date.ToLocalTime()} {trade.Side} {trade.Amount.ToString("N3")} @ {trade.Price.ToString("N9")}");
}
}

Console.ResetColor();
}

private void PrintOrders()
{
Console.WriteLine();
Console.WriteLine("Order book");
Console.WriteLine("====================================");
int ordersPerSide = 10;

if (Service.Orders.Sells.Count() == 0 && Service.Orders.Buys.Count() == 0)
{
Console.WriteLine("No sell or buy orders");
return;
}

var sells = Service.Orders.Sells.Take(ordersPerSide).Reverse();
var buys = Service.Orders.Buys.Take(ordersPerSide);

Console.ForegroundColor = ConsoleColor.Red;
foreach (var order in sells)
{
Console.WriteLine(FormatOrder(order));
}
Console.ResetColor();

if (buys.Count() > 0 && sells.Count() > 0)
{
var salesPrice = sells.Last().Price;
var buysPrice = buys.Last().Price;
Console.WriteLine($"---- Spread ({(salesPrice - buysPrice).ToString("N9")}) ----");
}
else
{
Console.WriteLine("--------");
}

Console.ForegroundColor = ConsoleColor.Green;

if (buys != null)
{
foreach (var order in buys)
{
Console.WriteLine(FormatOrder(order));
}
}

Console.ResetColor();
}

private void PrintWallet()
{
var uc = new UnitConversion();
Console.WriteLine();
Console.WriteLine("Account balances");
Console.WriteLine("====================================");
Console.WriteLine($"Wallet ETH balance: {uc.FromWei(WalletETH).ToString("N18")}");
Console.WriteLine($"EtherDelta ETH balance: {uc.FromWei(EtherDeltaETH).ToString("N18")}");
Console.WriteLine($"Wallet token balance: {uc.FromWei(WalletToken).ToString("N18")}");
Console.WriteLine($"EtherDelta token balance: {uc.FromWei(EtherDeltaToken).ToString("N18")}");
}

private string FormatOrder(Order order)
{
return $"{order.Price.ToString("N9")} {order.EthAvailableVolume.ToString("N3"),20}";
}

private async Task GetMarket()
{
try
{
await Service.WaitForMarket();
}
catch (TimeoutException)
{
Console.ForegroundColor = ConsoleColor.Red;
Console.WriteLine("Could not get Market!");
Console.ResetColor();
}
}

~BaseBot()
{
if (Service != null)
{
Service.Close();
}
}
}
}
19 changes: 19 additions & 0 deletions dotnet/EtherDeltaClient.csproj
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
<Project Sdk="Microsoft.NET.Sdk">

<PropertyGroup>
<AssemblyTitle>EhterDelta.Bots.Dontnet</AssemblyTitle>
<Version>$(NethereumVersion)</Version>
<Authors>Stojce Slavkovski</Authors>
<AssemblyName>EhterDelta.Bots.Dontnet</AssemblyName>
<OutputType>Exe</OutputType>
<TargetFramework>netcoreapp2.0</TargetFramework>
</PropertyGroup>

<ItemGroup>
<PackageReference Include="Nethereum.Web3" Version="2.1.0" />
<PackageReference Include="Newtonsoft.Json" Version="10.0.3" />
<PackageReference Include="System.Configuration.ConfigurationManager" Version="4.4.0" />
<PackageReference Include="WebSocket4Net" Version="0.15.1" />
</ItemGroup>

</Project>
17 changes: 17 additions & 0 deletions dotnet/EtherDeltaClient.sln
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@

Microsoft Visual Studio Solution File, Format Version 14.00
# Visual Studio 2017
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "EtherDeltaClient", "EtherDeltaClient.csproj", "{2E39944F-2564-4DF5-A46D-011F59CD704B}"
EndProject
Global
GlobalSection(SolutionConfigurationPlatforms) = preSolution
Debug|Any CPU = Debug|Any CPU
Release|Any CPU = Release|Any CPU
EndGlobalSection
GlobalSection(ProjectConfigurationPlatforms) = postSolution
{2E39944F-2564-4DF5-A46D-011F59CD704B}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
{2E39944F-2564-4DF5-A46D-011F59CD704B}.Debug|Any CPU.Build.0 = Debug|Any CPU
{2E39944F-2564-4DF5-A46D-011F59CD704B}.Release|Any CPU.ActiveCfg = Release|Any CPU
{2E39944F-2564-4DF5-A46D-011F59CD704B}.Release|Any CPU.Build.0 = Release|Any CPU
EndGlobalSection
EndGlobal
19 changes: 19 additions & 0 deletions dotnet/EtherDeltaConfiguration.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
using System.Numerics;

namespace EhterDelta.Bots.Dontnet
{
public class EtherDeltaConfiguration
{
public string AddressEtherDelta { get; set; }
public string Provider { get; set; }
public string SocketUrl { get; set; }
public string AbiFile { get; internal set; }
public string TokenFile { get; internal set; }
public string Token { get; internal set; }
public string User { get; internal set; }
public string PrivateKey { get; internal set; }
public int UnitDecimals { get; internal set; }
public BigInteger GasLimit { get; internal set; }
public BigInteger GasPrice { get; internal set; }
}
}
7 changes: 7 additions & 0 deletions dotnet/ILogger.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
namespace EhterDelta.Bots.Dontnet
{
public interface ILogger
{
void Log(string message);
}
}
Loading

0 comments on commit cd13ff4

Please sign in to comment.