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 =newSeed("KIHCI9AIOLGZQCCPLFYNHPVHEQZTJTOYCGXYCNQWKADKAYAYXMJZTCG9HKM9VAWTWJNY99DYZLEOBAMHI");var recipientSeed =newSeed("HBXSBCAYLXTNKDNOGPUDJDHDPHDMJBWO9HMYLAQLKQNSKKYYDSCKVQHHOGWFGCASUFUUUUF9FKQGAJQWE");//Generate one new recipient address var recipientAddress =repository.GetNewAddresses(recipientSeed,0,1,SecurityLevel.Medium);//Prepare transactionvar transfer1 =newTransfer( address:recipientAddress[0].Value, tag:"PATRIQTUTORIALS", message:"PatriQ tutorials testing https://patriq.gitbook.io/iota/", timestamp:DateTime.Now, value:0);//Create a bundlevar bundle =newBundle();bundle.AddTransfer(transfer1);//Send the bundlevar result =repository.SendTransfer(senderSeed, bundle,SecurityLevel.Medium,4);//Check the result in Tangle ExplorerConsole.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 =newSeed("NYLBCMVKSWQJCIVWUSHLOYRAGGJQTMUOUXJKQTT9LYIV9ZWTYANOWRFTUKNVPVEJGHUXNPAPNCSKLFSAC");var recipientSeed =newSeed("VJEFRYYWLYMKGGQPERQISOAIJRCLKDJKDYFPQYTAEKQVLEFTEYKYKVSEFBWBCLVNBUPNYCUDNXSJQLKWV");//Generate one new recipient address var recipientAddress =repository.GetNewAddresses(recipientSeed,0,1,SecurityLevel.Medium);//Prepare first transactionvar transfer1 =newTransfer( address:recipientAddress[0].Value, tag:"PATRIQTUTORIALS", message:"PatriQ tutorials testing https://patriq.gitbook.io/iota/ first transaction", timestamp:DateTime.Now, value:0);//Prepare second transactionvar transfer2 =newTransfer( address:recipientAddress[0].Value, tag:"PATRIQTUTORIALS", message:"PatriQ tutorials testing https://patriq.gitbook.io/iota/ second transaction", timestamp:DateTime.Now, value:0);//Create a bundlevar bundle =newBundle();bundle.AddTransfer(transfer1);bundle.AddTransfer(transfer2);//Send the bundlevar result =repository.SendTransfer(senderSeed, bundle,SecurityLevel.Medium,4);//Check the result in Tangle ExplorerConsole.WriteLine("Link to Tangle Explorer: {0}{1}","https://devnet.thetangle.org/bundle/",result.Hash);