Last active
September 24, 2019 10:30
-
-
Save Hribek25/003f8113be6152319ca75c71ef394b92 to your computer and use it in GitHub Desktop.
Sending IOTA transaction in more granular way: API calls Prepare_transfer() and Send_transfer()
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// 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#07CFD43B146C | |
// 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; | |
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 | |
}; | |
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 | |
Console.WriteLine("Almost prepared bundle - tips and POW are still missing"); | |
var preparedBundle = repo.PrepareTransfer(seed: new Seed(MySeed), | |
bundle: bundle, | |
securityLevel: 2); // please note, depth and MNWM are not needed since neither GTTA nor ATT is performed | |
foreach (var tx in preparedBundle.ToTrytes()) | |
{ | |
Console.WriteLine(tx.ToString()); | |
} | |
Console.WriteLine("Searching for tips and performing POW... Wait please..."); | |
var sentBundle = repo.SendTrytes(transactions: preparedBundle.Transactions, | |
depth: 3, | |
minWeightMagnitude: 14); | |
Console.WriteLine("Bundle was broadcasted."); | |
Console.WriteLine("Final transactions were returned - including nonce(POW)"); | |
foreach (var tx in sentBundle) | |
{ | |
Console.WriteLine(tx.ToString()); | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment