Sending transactions

In the previous chapter we have learned about seeds and addresses. In order to send a transaction, we are going to need both. According to the official docs: a transaction is a single operation that you can send to a node. Transactions can withdraw/deposit IOTA tokens or send/receive data. To send a node one or more transactions, you must package them in a bundle.

Below we can see an example of how to send a data only (0 token value) transaction.

var repository = IotaRepositoryFactory.Create("https://nodes.devnet.thetangle.org");
var senderSeed = new Seed("KIHCI9AIOLGZQCCPLFYNHPVHEQZTJTOYCGXYCNQWKADKAYAYXMJZTCG9HKM9VAWTWJNY99DYZLEOBAMHI");
var recipientSeed = new Seed("HBXSBCAYLXTNKDNOGPUDJDHDPHDMJBWO9HMYLAQLKQNSKKYYDSCKVQHHOGWFGCASUFUUUUF9FKQGAJQWE");

//Generate one new recipient address                
var recipientAddress = repository.GetNewAddresses(recipientSeed, 0, 1, SecurityLevel.Medium);

//Prepare transaction
var transfer1 = new Transfer(
    address: recipientAddress[0].Value, 
    tag: "PATRIQTUTORIALS", 
    message: "PatriQ tutorials testing https://patriq.gitbook.io/iota/", 
    timestamp: DateTime.Now, 
    value: 0);

//Create a bundle
var bundle = new Bundle();
bundle.AddTransfer(transfer1);

//Send the bundle
var result = repository.SendTransfer(senderSeed, bundle, SecurityLevel.Medium, 4);

//Check the result in Tangle Explorer
Console.WriteLine("Link to Tangle Explorer: {0}{1}", "https://devnet.thetangle.org/bundle/", result.Hash);

If we want to do a value transfer, then we just have to increment the ValueToTransfer property to something larger than 0. In the case that we do not have enough balance on the seed we are sending from, the SendTransfer method is going to throw an exception. Because of this, it is recommended to put it in a try-catch block.

We can also send multiple transactions within one bundle. Example:

var repository = IotaRepositoryFactory.Create("https://nodes.devnet.thetangle.org");
var senderSeed = new Seed("NYLBCMVKSWQJCIVWUSHLOYRAGGJQTMUOUXJKQTT9LYIV9ZWTYANOWRFTUKNVPVEJGHUXNPAPNCSKLFSAC");
var recipientSeed = new Seed("VJEFRYYWLYMKGGQPERQISOAIJRCLKDJKDYFPQYTAEKQVLEFTEYKYKVSEFBWBCLVNBUPNYCUDNXSJQLKWV");

//Generate one new recipient address                
var recipientAddress = repository.GetNewAddresses(recipientSeed, 0, 1, SecurityLevel.Medium);

//Prepare first transaction
var transfer1 = new Transfer(
    address: recipientAddress[0].Value, 
    tag: "PATRIQTUTORIALS", 
    message: "PatriQ tutorials testing https://patriq.gitbook.io/iota/ first transaction", 
    timestamp: DateTime.Now, 
    value: 0);
    
//Prepare second transaction
var transfer2 = new Transfer(
    address: recipientAddress[0].Value, 
    tag: "PATRIQTUTORIALS", 
    message: "PatriQ tutorials testing https://patriq.gitbook.io/iota/ second transaction", 
    timestamp: DateTime.Now, 
    value: 0);

//Create a bundle
var bundle = new Bundle();
bundle.AddTransfer(transfer1);
bundle.AddTransfer(transfer2);

//Send the bundle
var result = repository.SendTransfer(senderSeed, bundle, SecurityLevel.Medium, 4);

//Check the result in Tangle Explorer
Console.WriteLine("Link to Tangle Explorer: {0}{1}", "https://devnet.thetangle.org/bundle/", result.Hash);

Last updated