Skip to content

Commit

Permalink
feat: add c# support
Browse files Browse the repository at this point in the history
  • Loading branch information
cryptochassis committed Jul 28, 2023
1 parent a0fb6c1 commit 4c3a4de
Show file tree
Hide file tree
Showing 28 changed files with 449 additions and 115 deletions.
9 changes: 8 additions & 1 deletion binding/csharp/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,13 @@ set(NAME binding_csharp)
project(${NAME})
set(SWIG_TARGET_NAME ccapi_${NAME})

if(NOT DOTNET_BUILD_CONFIGURATION)
set(DOTNET_BUILD_CONFIGURATION "Release" CACHE STRING
"Choose the type of build, options are: Debug, Release."
FORCE)
endif()
message(STATUS "DOTNET_BUILD_CONFIGURATION: ${DOTNET_BUILD_CONFIGURATION}")

# Find dotnet cli
find_program(DOTNET_EXECUTABLE NAMES dotnet REQUIRED)
if(NOT DOTNET_EXECUTABLE)
Expand Down Expand Up @@ -43,7 +50,7 @@ configure_file(
set(CSHARP_PACKAGING_TARGET_NAME csharp_${PACKAGING_DIR})
add_custom_target(${CSHARP_PACKAGING_TARGET_NAME} ALL
COMMAND ${CMAKE_COMMAND} -E copy ${CMAKE_CURRENT_BINARY_DIR}/${SWIG_TARGET_NAME}/*.cs ${SRC_DIR_FULL}
COMMAND ${DOTNET_EXECUTABLE} build -c ${CMAKE_BUILD_TYPE} -o ${PACKAGING_DIR_FULL} ccapi.csproj
COMMAND ${DOTNET_EXECUTABLE} build -c ${DOTNET_BUILD_CONFIGURATION} -o ${PACKAGING_DIR_FULL} ccapi.csproj
COMMAND ${CMAKE_COMMAND} -E copy $<TARGET_FILE:${SWIG_TARGET_NAME}> ${PACKAGING_DIR_FULL}
WORKING_DIRECTORY ${SRC_DIR_FULL}
)
Expand Down
14 changes: 6 additions & 8 deletions binding/csharp/ccapi.csproj.in
Original file line number Diff line number Diff line change
@@ -1,8 +1,6 @@
<Project Sdk="Microsoft.NET.Sdk">

<PropertyGroup>
<TargetFramework>net6.0</TargetFramework>
<Nullable>enable</Nullable>
</PropertyGroup>

</Project>
<Project Sdk="Microsoft.NET.Sdk">
<PropertyGroup>
<TargetFramework>net6.0</TargetFramework>
<Nullable>enable</Nullable>
</PropertyGroup>
</Project>
23 changes: 23 additions & 0 deletions binding/csharp/example/enable_library_logging/MainProgram.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
public class MainProgram {
class MyLogger : ccapi.Logger {
public override void LogMessage(string severity, string threadId, string timeISO, string fileName, string lineNumber, string message) {
lock (this._myLock) {
System.Console.WriteLine(
string.Format("{0}: [{1}] {{{2}:{3}}} {4}{5}{6}", threadId, timeISO, fileName, lineNumber, severity, new string(' ', 8), message));
}
}
private readonly object _myLock = new object();
}
public static void Main(string[] args) {
var myLogger = new MyLogger();
ccapi.Logger.logger = myLogger;
var option = new ccapi.SessionOptions();
var config = new ccapi.SessionConfigs();
var session = new ccapi.Session(option, config);
var subscription = new ccapi.Subscription("okx", "BTC-USDT", "MARKET_DEPTH");
session.Subscribe(subscription);
System.Threading.Thread.Sleep(10000);
session.Stop();
System.Console.WriteLine("Bye");
}
}
14 changes: 14 additions & 0 deletions binding/csharp/example/enable_library_logging/main.csproj
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
<Project Sdk="Microsoft.NET.Sdk">
<PropertyGroup>
<OutputType>Exe</OutputType>
<TargetFramework>net6.0</TargetFramework>
<Nullable>enable</Nullable>
<Configuration>Release</Configuration>
<CcapiLibraryPath>to be provided in command line</CcapiLibraryPath>
</PropertyGroup>
<ItemGroup>
<Reference Include="ccapi">
<HintPath>$(CcapiLibraryPath)</HintPath>
</Reference>
</ItemGroup>
</Project>
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
add_compile_definitions(CCAPI_ENABLE_LOG_TRACE)
# usage: when generating the binding code, do cmake -DCMAKE_PROJECT_INCLUDE=<path-to-this-file> ..., see https://github.com/crypto-chassis/ccapi#non-c
# see https://cmake.org/cmake/help/latest/variable/CMAKE_PROJECT_INCLUDE.html
Original file line number Diff line number Diff line change
@@ -1,38 +1,36 @@
class MainProgram
{
class MyEventHandler : ccapi.EventHandler {
public override bool ProcessEvent(ccapi.Event event_, ccapi.Session session) {
System.Console.WriteLine(System.String.Format("Received an event:\n%s", event_.ToStringPretty(2, 2)));
return true;
}
}
static void Main(string[] args)
{
if(System.Environment.GetEnvironmentVariable("OKX_API_KEY") is null) {
System.Console.Error.WriteLine("Please set environment variable OKX_API_KEY");
return;
}
if(System.Environment.GetEnvironmentVariable("OKX_API_SECRET") is null) {
System.Console.Error.WriteLine("Please set environment variable OKX_API_SECRET");
return;
}
if(System.Environment.GetEnvironmentVariable("OKX_API_PASSPHRASE") is null) {
System.Console.Error.WriteLine("Please set environment variable OKX_API_PASSPHRASE");
return;
}
var eventHandler = new MyEventHandler();
var option = new ccapi.SessionOptions();
var config = new ccapi.SessionConfigs();
var session = new ccapi.Session(option, config, eventHandler);
var request = new ccapi.Request(Request.Operation.CREATE_ORDER, "okx", "BTC-USDT");
var param = new ccapi.MapStringString();
param.Add("SIDE","BUY");
param.Add("QUANTITY","0.0005");
param.Add("LIMIT_PRICE","20000");
request.AppendParam(param);
session.SendRequest(request);
System.Threading.Thread.Sleep(10000);
session.Stop();
System.Console.WriteLine("Bye");
}
}
class MainProgram {
class MyEventHandler : ccapi.EventHandler {
public override bool ProcessEvent(ccapi.Event event_, ccapi.Session session) {
System.Console.WriteLine(string.Format("Received an event:\n{0}", event_.ToStringPretty(2, 2)));
return true;
}
}
static void Main(string[] args) {
if (System.Environment.GetEnvironmentVariable("OKX_API_KEY") is null) {
System.Console.Error.WriteLine("Please set environment variable OKX_API_KEY");
return;
}
if (System.Environment.GetEnvironmentVariable("OKX_API_SECRET") is null) {
System.Console.Error.WriteLine("Please set environment variable OKX_API_SECRET");
return;
}
if (System.Environment.GetEnvironmentVariable("OKX_API_PASSPHRASE") is null) {
System.Console.Error.WriteLine("Please set environment variable OKX_API_PASSPHRASE");
return;
}
var eventHandler = new MyEventHandler();
var option = new ccapi.SessionOptions();
var config = new ccapi.SessionConfigs();
var session = new ccapi.Session(option, config, eventHandler);
var request = new ccapi.Request(ccapi.Request.Operation.CREATE_ORDER, "okx", "BTC-USDT");
var param = new ccapi.MapStringString();
param.Add("SIDE", "BUY");
param.Add("QUANTITY", "0.0005");
param.Add("LIMIT_PRICE", "20000");
request.AppendParam(param);
session.SendRequest(request);
System.Threading.Thread.Sleep(10000);
session.Stop();
System.Console.WriteLine("Bye");
}
}
Original file line number Diff line number Diff line change
@@ -1,13 +1,14 @@
<Project Sdk="Microsoft.NET.Sdk">
<PropertyGroup>
<OutputType>Exe</OutputType>
<TargetFramework>net6.0</TargetFramework>
<Nullable>enable</Nullable>
<CcapiLibraryPath>to be provided in command line</CcapiLibraryPath>
</PropertyGroup>
<ItemGroup>
<Reference Include="ccapi">
<HintPath>$(CcapiLibraryPath)</HintPath>
</Reference>
</ItemGroup>
<PropertyGroup>
<OutputType>Exe</OutputType>
<TargetFramework>net6.0</TargetFramework>
<Nullable>enable</Nullable>
<Configuration>Release</Configuration>
<CcapiLibraryPath>to be provided in command line</CcapiLibraryPath>
</PropertyGroup>
<ItemGroup>
<Reference Include="ccapi">
<HintPath>$(CcapiLibraryPath)</HintPath>
</Reference>
</ItemGroup>
</Project>
Original file line number Diff line number Diff line change
@@ -0,0 +1,46 @@
class MainProgram {
class MyEventHandler : ccapi.EventHandler {
public override bool ProcessEvent(ccapi.Event event_, ccapi.Session session) {
if (event_.GetType_() == ccapi.Event.Type.SUBSCRIPTION_STATUS) {
System.Console.WriteLine(string.Format("Received an event of type SUBSCRIPTION_STATUS:\n{0}", event_.ToStringPretty(2, 2)));
var message = event_.GetMessageList()[0];
if (message.GetType_() == ccapi.Message.Type.SUBSCRIPTION_STARTED) {
var request = new ccapi.Request(ccapi.Request.Operation.CREATE_ORDER, "okx", "BTC-USDT");
var param = new ccapi.MapStringString();
param.Add("SIDE", "BUY");
param.Add("QUANTITY", "0.0005");
param.Add("LIMIT_PRICE", "20000");
request.AppendParam(param);
session.SendRequest(request);
}
} else if (event_.GetType_() == ccapi.Event.Type.SUBSCRIPTION_DATA) {
System.Console.WriteLine(string.Format("Received an event of type SUBSCRIPTION_DATA:\n{0}", event_.ToStringPretty(2, 2)));
}
return true;
}
}
static void Main(string[] args) {
if (System.Environment.GetEnvironmentVariable("OKX_API_KEY") is null) {
System.Console.Error.WriteLine("Please set environment variable OKX_API_KEY");
return;
}
if (System.Environment.GetEnvironmentVariable("OKX_API_SECRET") is null) {
System.Console.Error.WriteLine("Please set environment variable OKX_API_SECRET");
return;
}
if (System.Environment.GetEnvironmentVariable("OKX_API_PASSPHRASE") is null) {
System.Console.Error.WriteLine("Please set environment variable OKX_API_PASSPHRASE");
return;
}
var eventHandler = new MyEventHandler();
var option = new ccapi.SessionOptions();
var config = new ccapi.SessionConfigs();
var session = new ccapi.Session(option, config, eventHandler);
var subscriptionList = new ccapi.SubscriptionList();
subscriptionList.Add(new ccapi.Subscription("okx", "BTC-USDT", "ORDER_UPDATE"));
session.Subscribe(subscriptionList);
System.Threading.Thread.Sleep(10000);
session.Stop();
System.Console.WriteLine("Bye");
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
<Project Sdk="Microsoft.NET.Sdk">
<PropertyGroup>
<OutputType>Exe</OutputType>
<TargetFramework>net6.0</TargetFramework>
<Nullable>enable</Nullable>
<Configuration>Release</Configuration>
<CcapiLibraryPath>to be provided in command line</CcapiLibraryPath>
</PropertyGroup>
<ItemGroup>
<Reference Include="ccapi">
<HintPath>$(CcapiLibraryPath)</HintPath>
</Reference>
</ItemGroup>
</Project>
50 changes: 50 additions & 0 deletions binding/csharp/example/fix_simple/MainProgram.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,50 @@
class MainProgram {
class MyEventHandler : ccapi.EventHandler {
public override bool ProcessEvent(ccapi.Event event_, ccapi.Session session) {
if (event_.GetType_() == ccapi.Event.Type.AUTHORIZATION_STATUS) {
System.Console.WriteLine(string.Format("Received an event of type AUTHORIZATION_STATUS:\n{0}", event_.ToStringPretty(2, 2)));
var message = event_.GetMessageList()[0];
if (message.GetType_() == ccapi.Message.Type.AUTHORIZATION_SUCCESS) {
var request = new ccapi.Request(ccapi.Request.Operation.FIX, "coinbase", "", "same correlation id for subscription and request");
var param = new ccapi.VectorPairIntString();
param.Add(new ccapi.PairIntString(35, "D"));
param.Add(new ccapi.PairIntString(11, "6d4eb0fb-2229-469f-873e-557dd78ac11e"));
param.Add(new ccapi.PairIntString(55, "BTC-USD"));
param.Add(new ccapi.PairIntString(54, "1"));
param.Add(new ccapi.PairIntString(44, "20000"));
param.Add(new ccapi.PairIntString(38, "0.001"));
param.Add(new ccapi.PairIntString(40, "2"));
param.Add(new ccapi.PairIntString(59, "1"));
request.AppendParamFix(param);
session.SendRequest(request);
}
} else if (event_.GetType_() == ccapi.Event.Type.FIX) {
System.Console.WriteLine(string.Format("Received an event of type FIX:\n{0}", event_.ToStringPretty(2, 2)));
}
return true;
}
}
static void Main(string[] args) {
if (System.Environment.GetEnvironmentVariable("COINBASE_API_KEY") is null) {
System.Console.Error.WriteLine("Please set environment variable COINBASE_API_KEY");
return;
}
if (System.Environment.GetEnvironmentVariable("COINBASE_API_SECRET") is null) {
System.Console.Error.WriteLine("Please set environment variable COINBASE_API_SECRET");
return;
}
if (System.Environment.GetEnvironmentVariable("COINBASE_API_PASSPHRASE") is null) {
System.Console.Error.WriteLine("Please set environment variable COINBASE_API_PASSPHRASE");
return;
}
var eventHandler = new MyEventHandler();
var option = new ccapi.SessionOptions();
var config = new ccapi.SessionConfigs();
var session = new ccapi.Session(option, config, eventHandler);
var subscription = new ccapi.Subscription("coinbase", "", "FIX", "", "same correlation id for subscription and request");
session.SubscribeByFix(subscription);
System.Threading.Thread.Sleep(10000);
session.Stop();
System.Console.WriteLine("Bye");
}
}
14 changes: 14 additions & 0 deletions binding/csharp/example/fix_simple/main.csproj
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
<Project Sdk="Microsoft.NET.Sdk">
<PropertyGroup>
<OutputType>Exe</OutputType>
<TargetFramework>net6.0</TargetFramework>
<Nullable>enable</Nullable>
<Configuration>Release</Configuration>
<CcapiLibraryPath>to be provided in command line</CcapiLibraryPath>
</PropertyGroup>
<ItemGroup>
<Reference Include="ccapi">
<HintPath>$(CcapiLibraryPath)</HintPath>
</Reference>
</ItemGroup>
</Project>
25 changes: 25 additions & 0 deletions binding/csharp/example/handle_exception/MainProgram.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
class MainProgram {
class MyEventHandler : ccapi.EventHandler {
public override bool ProcessEvent(ccapi.Event event_, ccapi.Session session) {
try {
throw new System.Exception("oops");
} catch (System.Exception e) {
System.Console.WriteLine(e.ToString());
System.Environment.Exit(0);
}
return true;
}
}
static void Main(string[] args) {
var eventHandler = new MyEventHandler();
var option = new ccapi.SessionOptions();
var config = new ccapi.SessionConfigs();
var session = new ccapi.Session(option, config, eventHandler);
var subscriptionList = new ccapi.SubscriptionList();
subscriptionList.Add(new ccapi.Subscription("okx", "BTC-USDT", "MARKET_DEPTH"));
session.Subscribe(subscriptionList);
System.Threading.Thread.Sleep(10000);
session.Stop();
System.Console.WriteLine("Bye");
}
}
14 changes: 14 additions & 0 deletions binding/csharp/example/handle_exception/main.csproj
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
<Project Sdk="Microsoft.NET.Sdk">
<PropertyGroup>
<OutputType>Exe</OutputType>
<TargetFramework>net6.0</TargetFramework>
<Nullable>enable</Nullable>
<Configuration>Release</Configuration>
<CcapiLibraryPath>to be provided in command line</CcapiLibraryPath>
</PropertyGroup>
<ItemGroup>
<Reference Include="ccapi">
<HintPath>$(CcapiLibraryPath)</HintPath>
</Reference>
</ItemGroup>
</Project>
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
class MainProgram {
class MyEventHandler : ccapi.EventHandler {
public override bool ProcessEvent(ccapi.Event event_, ccapi.Session session) {
if (event_.GetType_() == ccapi.Event.Type.SUBSCRIPTION_DATA) {
foreach (var message in event_.GetMessageList()) {
var correlationId = message.GetCorrelationIdList()[0];
System.Console.WriteLine(string.Format("{0}: Best bid and ask at {1} are:", correlationId, message.GetTimeISO()));
foreach (var element in message.GetElementList()) {
var elementNameValueMap = element.GetNameValueMap();
foreach (var entry in elementNameValueMap) {
var name = entry.Key;
var value = entry.Value;
System.Console.WriteLine(string.Format(" {0} = {1}", name, value));
}
}
}
}
return true;
}
}
static void Main(string[] args) {
var eventHandler = new MyEventHandler();
var option = new ccapi.SessionOptions();
var config = new ccapi.SessionConfigs();
var session = new ccapi.Session(option, config, eventHandler);
var subscriptionList = new ccapi.SubscriptionList();
subscriptionList.Add(new ccapi.Subscription("okx", "BTC-USDT", "MARKET_DEPTH", "", "BTC"));
subscriptionList.Add(new ccapi.Subscription("okx", "ETH-USDT", "MARKET_DEPTH", "", "ETH"));
session.Subscribe(subscriptionList);
System.Threading.Thread.Sleep(10000);
session.Stop();
System.Console.WriteLine("Bye");
}
}

0 comments on commit 4c3a4de

Please sign in to comment.