This page was exported from David Olliver [ https://www.davidolliver.com ]
Export date: Wed Mar 20 1:30:28 2019 / +0000 GMT

C#: Transaction Basics - Sending a transaction to the XRP Ledger





In this article, I will cover the process of  sending a transaction to the XRP Ledger. You will learn how to build your own code to interact with XRP Ledger.   






A Transaction is the only way to modify the XRP Ledger. Transactions are only final if signed, submitted, and accepted into a validated ledger version following the consensus process.


XRP Ledger Dev Portal




Learn more about Signing and Submitting Transactions





I'll try to buy time and provide key details below.





The following is an example of what a simple transaction looks like:


[cc lang="javascript"]
{
"TransactionType": "Payment",
"Account": "rMmTCjGFRWPz8S2zAUUoNVSQHxtRQD4eCx",
"Destination": "r3kmLJN5D28dHuH8vZNUZpMC43pEHpaocV",
"Memos": [
{
"Memo": {
"MemoType": "687474703a2f2f6578616d706c652e636f6d2f6d656d6f2f67656e65726963",
"MemoData": "72656e74"
}
}
],
"Amount": "1"
}
[/cc]



Learn more about Transaction Common Fields





1. Generate C# classes from JSON





[cc lang="csharp"]
public class Memos
{
public Memos()
{
Memo = new Memo();
}
[JsonProperty("Memo")]
public Memo Memo { get; set; }

}

public class Memo
{

public string MemoData { get; set; }

public string MemoType { get; set; }

}

public class RootObject
{
public string TransactionType { get; set; }
public long Flags { get; set; }
public int Sequence { get; set; }
public string Amount { get; set; }
public string Fee { get; set; }
public string Account { get; set; }
public string Destination { get; set; }
public List Memos { get; set; }
}
[/cc]





2. String to Hex conversion for Memos


[cc lang="csharp"]
string msg = "testing";
byte[] utfBytes = System.Text.Encoding.UTF8.GetBytes(msg);
string hex = BitConverter.ToString(utfBytes).Replace("-", "");
[/cc]





3. Build the JSON



[cc lang="csharp"]
RootObject obj = new RootObject();
List memos = new List();
Memos mm = new Memos();
obj.TransactionType = "Payment";
obj.Flags = 2147483648;
obj.Sequence = 81;
obj.Amount = "18000000";
obj.Fee = "10";
obj.Account = "rp5EciKXsqUb6ZWkFQBGzeGACTsgfkLp6E";
obj.Destination = "rJAGYcpj2TGePnEmKfqmNAfjQFhrBZdzRr";
mm.Memo.MemoData = hex;
mm.Memo.MemoType = hex;
memos.Add(mm);
obj.Memos = memos;
string json = JsonConvert.SerializeObject(obj, Formatting.Indented);
[/cc]





4. Connect IRippleClient


[cc lang="csharp"]
IRippleClient client = new RippleClient("wss://s.altnet.rippletest.net:51233");
client.Connect();

var signed = TxSigner.SignJson(JObject.Parse(json), secret);
SubmitBlobRequest request = new SubmitBlobRequest();
request.TransactionBlob = signed.TxBlob;

Submit result = await client.SubmitTransactionBlob(request);
client.Disconnect();
[/cc]





5. How to use Ripple Dot Net


[cc lang="csharp"]
using Newtonsoft.Json;
using Newtonsoft.Json.Linq;
using RippleDotNet;
using RippleDotNet.Json.Converters;
using RippleDotNet.Model;
using RippleDotNet.Exceptions;
using RippleDotNet.Model.Account;
using RippleDotNet.Model.Ledger;
using RippleDotNet.Model.Server;
using RippleDotNet.Model.Transaction.TransactionTypes;
using RippleDotNet.Requests;
using RippleDotNet.Requests.Account;
using RippleDotNet.Requests.Ledger;
using RippleDotNet.Requests.Transaction;
using RippleDotNet.Responses;
using RippleDotNet.Responses.Transaction;
using RippleDotNet.Responses.Transaction.Interfaces;
using RippleDotNet.Responses.Transaction.TransactionTypes;
using BookOffers = RippleDotNet.Model.Transaction.BookOffers;
using ChannelAuthorize = RippleDotNet.Model.Transaction.ChannelAuthorize;
using ChannelVerify = RippleDotNet.Model.Transaction.ChannelVerify;
using Submit = RippleDotNet.Model.Transaction.Submit;
using Currency = RippleDotNet.Model.Currency;
using RippleDotNet.Model.Transaction.Interfaces;
[/cc]





Please, note that you don't need to use all of them. All above information is listed for information purpose only





Tips


You can set the transaction fee this way:


[cc lang="csharp"]
Fee = new Currency {Value = "10"};
[/cc]


and to use it you need the following piece of code:


[cc lang="csharp"]obj.Fee = new Currency{Value = "10"};[/cc]


How to deal with Sequence?


Do the following:


[cc lang="csharp"]
AccountInfo accountInfo = await client.AccountInfo("rp5EciKXsqUb6ZWkFQBGzeGACTsgfkLp6E");
[/cc]


and then



[cc lang="csharp"]
obj.Sequence = accountInfo.AccountData.Sequence;
[/cc]





Variable SECRET


Don't forget to declare a variable for your secret key


[cc lang="csharp"]
var secret = "XXXXXXXX";
[/cc]


which later will be used here:


[cc lang="csharp"]
var signed = TxSigner.SignJson(JObject.Parse(json), secret);[/cc]





Conclusion


Next chapter will cover Escrow Transactions


Post date: 2018-12-21 02:20:29
Post date GMT: 2018-12-21 02:20:29
Post modified date: 2018-12-21 14:32:22
Post modified date GMT: 2018-12-21 14:32:22
Powered by [ Universal Post Manager ] plugin. HTML saving format developed by gVectors Team www.gVectors.com