I'm looking for any kind of advice, like when or where to do exception handling, the overall library structure/layout, usage of classes/partial classes, code efficiency, naming conventions, and so on and so forth.
Note: Knowledge of cryptocurrency technology would be an advantage.
The following code is the section that covers this API call. The whole wrapper can be found here. I will post code for one specific operation but would love if someone would be willing to do a full review.
Connecting to NIS:
using System;
namespace NemApi
{
public static class Connection
{
public static string partialUri { get; set; }
static Connection()
{
NisConnect();
}
public static void NisConnect(String nodeIP, int port)
{
partialUri = String.Format("http://{0}:{1}", nodeIP, port);
}
public static void NisConnect()
{
partialUri = String.Format("http://{0}:{1}", "127.0.0.1", 7890);
}
}
}
When making any request, I need to work with JSON. For example, when sending a transaction, I populate a JSON data transfer object and then serialize it into a JSON string:
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace NemApi.DTOs.AccountTransactionObjects
{
public class TransactionVersionOne
{
public class Message
{
public string payload { get; set; }
public int type { get; set; }
}
public class Transaction
{
public long timeStamp { get; set; }
public long amount { get; set; }
public long fee { get; set; }
public string recipient { get; set; }
public int type { get; set; }
public long deadline { get; set; }
public Message message { get; set; }
public int version { get; set; }
public string signer { get; set; }
}
public class RootObject
{
public Transaction transaction { get; set; }
public string privateKey { get; set; }
}
}
}
Populating the DTO and post the request:
using NemApi;
using NemApi.DTOs.AccountTransactionObjects;
using NemApi.DTOs.NisStatusObjects;
using Newtonsoft.Json;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Net.Http;
using System.Text;
using System.Threading.Tasks;
namespace NemApi
{
public partial class PostTransaction
{
static HttpClient client = new HttpClient();
public static async Task<GetPrepareAnnounce.Response> VersionOne(
string privKey,
long amount,
long fee,
int deadline,
string recipient,
string signer,
string messagePayload = "",
bool encrypted = false)
{
Nis.NetworkTime time = await GetNis.NetworkTime();
TransactionVersionOne.Transaction Tx = new TransactionVersionOne.Transaction();
Tx.amount = 10000000;
Tx.fee = fee * 1000000;
Tx.deadline = (time.receiveTimeStamp / 1000) + deadline;
Tx.recipient = recipient;
Tx.signer = signer;
Tx.timeStamp = time.receiveTimeStamp / 1000;
Tx.type = 257;
Tx.version = 1744830465;
TransactionVersionOne.Message Msg = new TransactionVersionOne.Message();
Msg.payload = messagePayload;
Msg.type = encrypted ? 2 : 1;
Tx.message = Msg;
TransactionVersionOne.RootObject RPA = new TransactionVersionOne.RootObject();
RPA.privateKey = privKey;
RPA.transaction = Tx;
string uri = String.Concat(Connection.partialUri, "/transaction/prepare-announce");
var response = await client.PostAsync(uri, new StringContent(JsonConvert.SerializeObject(RPA).ToString(), Encoding.UTF8, "application/json"));
var content = response.Content;
{
var json = await content.ReadAsStringAsync();
var model = JsonConvert.DeserializeObject<GetPrepareAnnounce.Response>(json);
return model;
}
}
}
}
This function is then called as follows:
public static async void SendTransactionV1()
{
GetPrepareAnnounce.Response a = await PostTransaction.VersionOne(
"<privat key>", //priv key
1000000, // amount
10, // fee
1000, // deadline
"<address>", // recipient
"<sender public key>" // sender pubkey
);
Console.WriteLine(a.message);
}