|
| 1 | +using IotaWalletNet.Application.AccountContext.Commands.SendAmount; |
| 2 | +using IotaWalletNet.Application.AccountContext.Commands.SyncAccount; |
| 3 | +using IotaWalletNet.Application.AccountContext.Queries.GetBalance; |
| 4 | +using IotaWalletNet.Application.Common.Extensions; |
| 5 | +using IotaWalletNet.Application.Common.Interfaces; |
| 6 | +using IotaWalletNet.Domain.Common.Interfaces; |
| 7 | +using IotaWalletNet.Domain.Common.Models.Coin; |
| 8 | +using IotaWalletNet.Domain.Common.Models.Transaction; |
| 9 | +using Microsoft.Extensions.DependencyInjection; |
| 10 | +using Newtonsoft.Json; |
| 11 | +using static IotaWalletNet.Application.WalletContext.Queries.GetAccount.GetAccountQueryHandler; |
| 12 | +using static IotaWalletNet.Domain.Common.Models.Events.EventTypes; |
| 13 | + |
| 14 | +namespace IotaWalletNet.Main.Examples.Events.WaitForTransactionConfirmation |
| 15 | +{ |
| 16 | + public static class WaitForTransactionConfirmationExample |
| 17 | + { |
| 18 | + public static async Task Run() |
| 19 | + { |
| 20 | + //Register all of the dependencies into a collection of services |
| 21 | + IServiceCollection services = new ServiceCollection().AddIotaWalletServices(); |
| 22 | + |
| 23 | + //Install services to service provider which is used for dependency injection |
| 24 | + IServiceProvider serviceProvider = services.BuildServiceProvider(); |
| 25 | + |
| 26 | + //Use serviceprovider to create a scope, which disposes of all services at end of scope |
| 27 | + using (IServiceScope scope = serviceProvider.CreateScope()) |
| 28 | + { |
| 29 | + //Request IWallet service from service provider |
| 30 | + IWallet wallet = scope.ServiceProvider.GetRequiredService<IWallet>(); |
| 31 | + |
| 32 | + //Build wallet using a fluent-style configuration api |
| 33 | + wallet = wallet |
| 34 | + .ConfigureWalletOptions() |
| 35 | + .SetCoinType(TypeOfCoin.Shimmer) |
| 36 | + .SetStoragePath("./walletdb") |
| 37 | + .Then() |
| 38 | + .ConfigureClientOptions() |
| 39 | + .AddNodeUrl("https://api.testnet.shimmer.network") |
| 40 | + .SetFaucetUrl("https://faucet.testnet.shimmer.network") |
| 41 | + .IsFallbackToLocalPow() |
| 42 | + .IsLocalPow() |
| 43 | + .Then() |
| 44 | + .ConfigureSecretManagerOptions() |
| 45 | + .SetPassword("password") |
| 46 | + .SetSnapshotPath("./mystronghold") |
| 47 | + .Then() |
| 48 | + .Initialize(); |
| 49 | + |
| 50 | + //We can subscrive to all events using WalletEventTypes.AllEvents |
| 51 | + //Howevever for this example, is only focussed on waiting for a transaction to complete. |
| 52 | + //Hence only the TransactionInclusion event is of interest. |
| 53 | + wallet.SubscribeToEvents(WalletEventTypes.TransactionInclusion); |
| 54 | + |
| 55 | + |
| 56 | + //Let's retrieve our cookiemonster account |
| 57 | + (GetAccountResponse accountResponse, IAccount? account) = await wallet.GetAccountAsync("cookiemonster"); |
| 58 | + |
| 59 | + //We can also opt for periodic syncing of our account, |
| 60 | + //so that we don't have to worry about manual syncing |
| 61 | + //Below, we want to sync periodically 30 times. |
| 62 | + //Set count to 0 for forever periodic syncing |
| 63 | + account.EnablePeriodicSyncing(intervalInMilliSeconds: 3000, count: 30); |
| 64 | + |
| 65 | + GetBalanceResponse getBalanceResponse = await account.GetBalanceAsync(); |
| 66 | + Console.WriteLine($"Current balance is : {getBalanceResponse.Payload!.BaseCoin.Total}"); |
| 67 | + |
| 68 | + //Let's send 1 shimmer, which is 1,000,000 Glow |
| 69 | + string receiverAddress = "rms1qz9f7vecqscfynnxacyzefwvpza0wz3r0lnnwrc8r7qhx65s5x7rx2fln5q"; |
| 70 | + |
| 71 | + SendAmountResponse sendAmountResponse = await account.SendAmountUsingBuilder() |
| 72 | + .AddAddressAndAmount(receiverAddress, 1000000) |
| 73 | + .SendAmountAsync(); |
| 74 | + Transaction transaction = sendAmountResponse.Payload!; |
| 75 | + |
| 76 | + //We will setup the event handler for you and let you proceed once we receive |
| 77 | + //confirmation from the node that the transactionid has been confirmed. |
| 78 | + await transaction.WaitForConfirmationAsync(wallet); |
| 79 | + |
| 80 | + getBalanceResponse = await account.GetBalanceAsync(); |
| 81 | + Console.WriteLine($"New balance is : {getBalanceResponse.Payload!.BaseCoin.Total}"); |
| 82 | + |
| 83 | + |
| 84 | + await Task.Delay(200 * 1000); |
| 85 | + } |
| 86 | + |
| 87 | + } |
| 88 | + |
| 89 | + } |
| 90 | +} |
0 commit comments