Skip to content

Instantly share code, notes, and snippets.

@Hribek25
Last active September 24, 2019 10:30
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save Hribek25/c19ac43192dd1d6228996e94caa2c9d0 to your computer and use it in GitHub Desktop.
Save Hribek25/c19ac43192dd1d6228996e94caa2c9d0 to your computer and use it in GitHub Desktop.
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