Skip to content

Latest commit

 

History

History
599 lines (509 loc) · 14.4 KB

Documentation.md

File metadata and controls

599 lines (509 loc) · 14.4 KB

BinanceChain .NET/C# SDK Documentation

Contents

Getting Started

BinanceChain .NET is available on NuGet. To install, go to Visual Studio > Tools > NuGet Package Manager > Package Manager Console.

PM> Install-Package BinanceChain.NET

Configuring Environment

Binance Chain .NET includes two build in environments by default - TestNet and MainNet. If you would like to configure your own environment, you can do so as follow.

// MainNet
EnvironmentInfo mainnetEnv = EnvironmentInfo.PROD;
var mainnetAPI = BinanceApiFactory.CreateApiClient(mainnetEnv);
// TestNet
EnvironmentInfo testnetEnv = EnvironmentInfo.TESTNET;
var testnetAPI = BinanceApiFactory.CreateApiClient(testnetEnv);
// Custom
EnvironmentInfo customEnv = new EnvironmentInfo("Local", "http://localhost/api", "ws://localhost/api", "cbnb");
var customAPI = BinanceApiFactory.CreateApiClient(customEnv);

HTTP API Requests

To interact with the Binance Chain HTTP API, include the BinanceChain .NET API namespaces in your class.

using BinanceChain.NET.Api;
using BinanceChain.NET.Api.Domain.Requests;

Official HTTP Api documentation: https://docs.binance.org/api-reference/dex-api/paths.html

NodeInfo

var api = BinanceApiFactory.CreateApiClient(EnvironmentInfo.TESTNET);
var result = api.GetNodeInfo();

Validators

var api = BinanceApiFactory.CreateApiClient(EnvironmentInfo.TESTNET);
var result = api.GetValidators();

Peers

var api = BinanceApiFactory.CreateApiClient(EnvironmentInfo.TESTNET);
var result = api.GetPeers();

Fees

var api = BinanceApiFactory.CreateApiClient(EnvironmentInfo.TESTNET);
var result = api.GetFees();

Account

string address = "{address}";
var api = BinanceApiFactory.CreateApiClient(EnvironmentInfo.TESTNET);
var result = api.GetAccount(address);

Account Sequence

string address = "{address}";
var api = BinanceApiFactory.CreateApiClient(EnvironmentInfo.TESTNET);
var result = api.GetAccountSequence(address);

Transaction Metadata

string hash = "E81BAB8E555819E4211D62E2E536B6D5812D3D91C105F998F5C6EB3AB8136482";
var api = BinanceApiFactory.CreateApiClient(EnvironmentInfo.TESTNET);
var result = api.GetTransactionMetadata(hash);

Tokens

var api = BinanceApiFactory.CreateApiClient(EnvironmentInfo.TESTNET);
var result = api.GetNodeInfo();

Markets

MarketRequest request = new MarketRequest()
{
    Limit = null,
    Offset = null
};

var api = BinanceApiFactory.CreateApiClient(EnvironmentInfo.TESTNET);
var result = api.GetMarkets(request);

Order Book

OrderBookRequest request = new OrderBookRequest()
{
    Symbol = "NNB-0AD_BNB",
    Limit = null
};

var api = BinanceApiFactory.CreateApiClient(EnvironmentInfo.TESTNET);
var result = api.GetOrderBook(request);

Candlestick Bars

CandlestickBars request = new CandlestickBars()
{
    Symbol = "NNB-338_BNB",
    Interval = Api.Domain.Intervals.FiveMinute,
    Limit = null,
    StartTime = null,
    EndTime = null
};

var api = BinanceApiFactory.CreateApiClient(EnvironmentInfo.TESTNET);
var result = api.GetCandlestickBars(request);

Open Orders

OpenOrdersRequest request = new OpenOrdersRequest()
{
    Address = "{address}",
    Limit = null,
    Offset = null,
    Symbol = null,
    Total = null
};

var api = BinanceApiFactory.CreateApiClient(EnvironmentInfo.TESTNET);
var result = api.GetOpenOrders(request);

Closed Orders

ClosedOrdersRequest request = new ClosedOrdersRequest()
{
    Address = "{address}",
    End = null,
    Limit = null,
    Offset = null,
    Side = null,
    Start = null,
    Symbol = null,
    Total = null
};
request.Status.Add(Api.Domain.OrderStatuses.Canceled);

var api = BinanceApiFactory.CreateApiClient(EnvironmentInfo.TESTNET);
var result = api.GetClosedOrders(request);

Order

var api = BinanceApiFactory.CreateApiClient(EnvironmentInfo.TESTNET);
var result = api.GetOrder("{orderid}");

24Hr Price Statistics

var api = BinanceApiFactory.CreateApiClient(EnvironmentInfo.TESTNET);
var result = api.Get24HrPriceStatistics();

Trades

var api = BinanceApiFactory.CreateApiClient(EnvironmentInfo.TESTNET);
var result = api.GetTrades();

Transactions

TransactionsRequest request = new TransactionsRequest()
{
    Address = "{address}",
    BlockHeight = null,
    EndTime = null,
    Limit = null,
    Offset = null,
    Side = null,
    StartTime = null,
    TxAsset = null,
    TxType = Api.Domain.TransactionTypes.TRANSFER
};

var api = BinanceApiFactory.CreateApiClient(EnvironmentInfo.TESTNET);
var result = api.GetTransactions(request);

Web Socket Streams

OrdersStream

string address = "{address}";

var stream = BinanceWebSocketFactory.CreateOrdersStream(EnvironmentInfo.TESTNET);
stream.Subscribe(address);
stream.OnDataReceived += (List<WebSocket.Domain.OrdersData> message) =>
{
    Console.WriteLine(message);
};

AccountStream

string address = "{address}";

var stream = BinanceWebSocketFactory.CreateAccountStream(EnvironmentInfo.TESTNET);
stream.Subscribe(address);
stream.OnDataReceived += (List<WebSocket.Domain.AccountData> message) =>
{
    Console.WriteLine(message);
};

TransferStream

string address = "{address}";

var stream = BinanceWebSocketFactory.CreateTransferStream(EnvironmentInfo.TESTNET);
stream.Subscribe(address);
stream.OnDataReceived += (WebSocket.Domain.TransferData message) =>
{
    Console.WriteLine(message);
};

TradesStream

string address = "{address}";

var stream = BinanceWebSocketFactory.CreateTradesStream(EnvironmentInfo.TESTNET);
stream.Subscribe(address);
stream.OnDataReceived += (List<WebSocket.Domain.TradesData> message) =>
{
    Console.WriteLine(message);
};

DiffDepthStream

List<string> symbols = new List<string>();
symbols.Add("BNB_BTC");

var stream = BinanceWebSocketFactory.CreateDiffDepthStream(EnvironmentInfo.TESTNET);
stream.Subscribe(symbols);
stream.OnDataReceived += (WebSocket.Domain.MarketDiffData message) =>
{
    Console.WriteLine(message);
};

BookDepthStream

List<string> symbols = new List<string>();
symbols.Add("BNB_BTC");

var stream = BinanceWebSocketFactory.CreateBookDepthStream(EnvironmentInfo.TESTNET);
stream.Subscribe(symbols);
stream.OnDataReceived += (WebSocket.Domain.MarketDepthData message) =>
{
    Console.WriteLine(message);
};

CandlestickStream

List<string> symbols = new List<string>();
symbols.Add("BNB_BTC");

var stream = BinanceWebSocketFactory.CreateCandlestickStream(EnvironmentInfo.TESTNET);
stream.Subscribe(symbols, WebSocket.Streams.CandlestickStream.KlineIntervals.FiveMinute);
stream.OnDataReceived += (List<WebSocket.Domain.TradesData> message) =>
{
    Console.WriteLine(message);
};

TickerStream

List<string> symbols = new List<string>();
symbols.Add("BNB_BTC");

var stream = BinanceWebSocketFactory.CreateTickerStream(EnvironmentInfo.TESTNET);
stream.Subscribe(symbols);
stream.OnDataReceived += (WebSocket.Domain.TickerData message) =>
{
    Console.WriteLine(message);
};

AllSymbolsTickerStream

var stream = BinanceWebSocketFactory.CreateAllSymbolsTickerStream(EnvironmentInfo.TESTNET);
stream.Subscribe();
stream.OnDataReceived += (List<WebSocket.Domain.TickerData> message) =>
{
    Console.WriteLine(message);
};

MiniTickerStream

List<string> symbols = new List<string>();
symbols.Add("BNB_BTC");

var stream = BinanceWebSocketFactory.CreateMiniTickerStream(EnvironmentInfo.TESTNET);
stream.Subscribe(symbols);
stream.OnDataReceived += (WebSocket.Domain.MiniTickerData message) =>
{
    Console.WriteLine(message);
};

AllSymbolsMiniTickerStream

var stream = BinanceWebSocketFactory.CreateAllSymbolsMiniTickerStream(EnvironmentInfo.TESTNET);
stream.Subscribe();
stream.OnDataReceived += (List<WebSocket.Domain.MiniTickerData> message) =>
{
    Console.WriteLine(message);
};

Node RPC Requests

To interact with the Binance Chain Node RPC, include the BinanceChain .NET NodeRPC namespaces in your class.

using BinanceChain.NET.NodeRPC;
using BinanceChain.NET.NodeRPC.Domain.Requests;

Official Node RPC documentation: https://docs.binance.org/api-reference/node-rpc.html

NodeStatus Query

var api = BinanceNodeFactory.CreateNodeClient(EnvironmentInfo.TESTNET_NODE);
var result = api.GetNodeStatus();

ABCIInfo Query

var api = BinanceNodeFactory.CreateNodeClient(EnvironmentInfo.TESTNET_NODE);
var result = api.GetABCIInfo();

ConsensusState Query

var api = BinanceNodeFactory.CreateNodeClient(EnvironmentInfo.TESTNET_NODE);
var result = api.GetConsensusState();

DumpConsensusState Query

var api = BinanceNodeFactory.CreateNodeClient(EnvironmentInfo.TESTNET_NODE);
var result = api.GetDumpConsensusState();

NetInfo Query

var api = BinanceNodeFactory.CreateNodeClient(EnvironmentInfo.TESTNET_NODE);
var result = api.GetNetInfo();

NumUnconfirmedTxs Query

var api = BinanceNodeFactory.CreateNodeClient(EnvironmentInfo.TESTNET_NODE);
var result = api.GetNumUnconfirmedTxs();

ABCIQuery Query

ABCIQueryRequest request = new ABCIQueryRequest()
{
    Path = "/store/acc/key",
    Data = "6163636F756E743A89F856CB39D25C1BDDAAEC74A381577CA8E2F886"
};

var api = BinanceNodeFactory.CreateNodeClient(EnvironmentInfo.TESTNET_NODE);
var result = api.GetABCIQuery(request);

Wallet

Creating a New Wallet

Wallet wallet = Wallet.NewWallet("testpassword", EnvironmentInfo.TESTNET);

Opening a Wallet

Opening a wallet by mnemonic words

Wallet wallet = Wallet.Open("{mnemonicWords}", "{password}", EnvironmentInfo.TESTNET);

Opening a wallet by private key

Wallet wallet = Wallet.Open("{privateKey}", EnvironmentInfo.TESTNET);

Transaction Broadcast

All transactions are signed with your wallet upon broadcast. To broadcast transactions, include the following namespaces in your class.

using BinanceChain.NET.Api.Domain;
using BinanceChain.NET.Api.Domain.Broadcast;

NewOrder

Wallet wallet = Wallet.Open("{privateKey}", EnvironmentInfo.TESTNET);

NewOrder newOrder = new NewOrder()
{
    Symbol = "BNB_BTC",
    OrderType = OrderTypes.LimitOrder,
    Side  = OrderSides.Buy,
    Price  = "10",
    Quantity = "1",
    TimeInForce = TimeInForce.GoodTillExpire
};

TransactionOption option = new TransactionOption()
{
    Memo = null,
    Source = 1,
    Data = null
};

var api = BinanceApiFactory.CreateApiClient(EnvironmentInfo.TESTNET);
var result = api.NewOrder(newOrder, wallet, option);

Vote

Wallet wallet = Wallet.Open("{privateKey}", EnvironmentInfo.TESTNET);

Vote vote = new Vote()
{
    ProposalId = 1,
    Option = 1
};

TransactionOption option = new TransactionOption()
{
    Memo = null,
    Source = 1,
    Data = null
};

var api = BinanceApiFactory.CreateApiClient(EnvironmentInfo.TESTNET);
var result = api.Vote(vote, wallet, option);

CancelOrder

Wallet wallet = Wallet.Open("{privateKey}", EnvironmentInfo.TESTNET);

CancelOrder cancelOrder = new CancelOrder()
{
    Symbol = null,
    RefId = null
};

TransactionOption option = new TransactionOption()
{
    Memo = null,
    Source = 1,
    Data = null
};

var api = BinanceApiFactory.CreateApiClient(EnvironmentInfo.TESTNET);
var result = api.CancelOrder(cancelOrder, wallet, option);

Transfer

Wallet wallet = Wallet.Open("{privateKey}", EnvironmentInfo.TESTNET);

Transfer transfer = new Transfer()
{
    FromAddress = null,
    ToAddress = null,
    Coin = null,
    Amount = null
};

TransactionOption option = new TransactionOption()
{
    Memo = null,
    Source = 1,
    Data = null
};

var api = BinanceApiFactory.CreateApiClient(EnvironmentInfo.TESTNET);
var result = api.Transfer(transfer, wallet, option);

Freeze

Wallet wallet = Wallet.Open("{privateKey}", EnvironmentInfo.TESTNET);

TokenFreeze tokenFreeze = new TokenFreeze()
{
    Symbol = null,
    Amount = null
};

TransactionOption option = new TransactionOption()
{
    Memo = null,
    Source = 1,
    Data = null
};

var api = BinanceApiFactory.CreateApiClient(EnvironmentInfo.TESTNET);
var result = api.Freeze(tokenFreeze, wallet, option);

Unfreeze

Wallet wallet = Wallet.Open("{privateKey}", EnvironmentInfo.TESTNET);

TokenUnfreeze tokenUnfreeze = new TokenUnfreeze()
{
    Symbol = null,
    Amount = null
};

TransactionOption option = new TransactionOption()
{
    Memo = null,
    Source = 1,
    Data = null
};

var api = BinanceApiFactory.CreateApiClient(EnvironmentInfo.TESTNET);
var result = api.Unfreeze(tokenUnfreeze, wallet, option);