Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
Sending IOTA transaction in a single call: API call Send_transfer()
// The snippet is a part of the IOTA Developer Essentials project. You can reach it at https://hribek25.github.io/IOTA101/
// Complete description and story behind the snippet is available at: https://hribek25.github.io/IOTA101/Allchapters_csharp.ipynb.html#00663E550ADF
// Requirement: Tangle.Net library (!nuget install Tangle.Net)
// Requirement: Newtonsoft Json library (!nuget install Newtonsoft.Json)
// Requirement: RestSharp library (Simple REST and HTTP API Client used with Tangle.Net) (!nuget install RestSharp)
#r "Tangle.Net.dll"
#r "Newtonsoft.Json.dll"
#r "RestSharp.dll"
using Tangle.Net.Entity;
using Newtonsoft.Json;
using RestSharp;
using Tangle.Net.Repository;
using Tangle.Net.Utils; //this is needed because of Timestamp function
var MySeed = "HGW9HB9LJPYUGVHNGCPLFKKPNZAIIFHZBDHKSGMQKFMANUBASSMSV9TAJSSMPRZZU9SFZULXKJ9YLAIUA";
var TargetAddress1 = "CXDUYK9XGHC9DTSPDMKGGGXAIARSRVAFGHJOCDDHWADLVBBOEHLICHTMGKVDOGRU9TBESJNHAXYPVJ9R9";
var TargetAddress2 = "CYJV9DRIE9NCQJYLOYOJOGKQGOOELTWXVWUYGQSWCNODHJAHACADUAAHQ9ODUICCESOIVZABA9LTMM9RW";
var NowIs = DateTime.Now.ToShortDateString(); //this is not important - just to have some meaningful message
var pt = new Transfer() // creating a first transfer (a proposed transaction)
{
Address=new Address(TargetAddress1),
ValueToTransfer=0,
Message=TryteString.FromUtf8String("Here comes a first message. Now is " + NowIs),
Tag=new Tag("HRIBEK999IOTA999TUTORIAL"),
Timestamp = Timestamp.UnixSecondsTimestamp // please note, Tangle.Net library does not enter timestamp for you but it is very important since it has impact on bundle hash
};
var pt2 = new Transfer() // creating a second transfer (a proposed transaction)
{
Address = new Address(TargetAddress2),
ValueToTransfer = 0,
Message = TryteString.FromUtf8String("Here comes a second message. Now is " + NowIs),
Tag = new Tag("HRIBEK999IOTA999TUTORIAL"),
Timestamp = Timestamp.UnixSecondsTimestamp // please note, Tangle.Net library does not enter timestamp for you but it is very important since it has impact on bundle hash
};
Console.WriteLine("Preparing/Broadcasting... Wait please...");
var bundle = new Bundle();
bundle.AddTransfer(pt);
bundle.AddTransfer(pt2);
var NodeURL = "https://nodes.thetangle.org:443";
var repo = new RestIotaRepository(new RestClient(NodeURL)); // ctor initialization of the Tangle.Net library
// the whole process initiated in a single call
var sentBundle = repo.SendTransfer(seed: new Seed(MySeed),
bundle: bundle,
securityLevel: 2,
depth: 3,
minWeightMagnitude: 14); //it returns a bundle object
Console.WriteLine("Generated bundle hash: " + sentBundle.Hash.ToString());
Console.WriteLine("Tail Transaction in the Bundle is a transaction #" + sentBundle.TailTransaction.CurrentIndex + ".");
Console.WriteLine("List of all transactions in the bundle:");
foreach (var tx in sentBundle.Transactions)
{
Console.WriteLine(JsonConvert.SerializeObject(tx, Formatting.Indented));
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
You can’t perform that action at this time.