|
| 1 | +using IotaWalletNet.Application.AccountContext.Commands.SendMicroAmount; |
| 2 | +using IotaWalletNet.Application.Common.Extensions; |
| 3 | +using IotaWalletNet.Application.Common.Interfaces; |
| 4 | +using IotaWalletNet.Domain.Common.Models.Coin; |
| 5 | +using Microsoft.Extensions.DependencyInjection; |
| 6 | +using static IotaWalletNet.Application.WalletContext.Queries.GetAccount.GetAccountQueryHandler; |
| 7 | + |
| 8 | +namespace IotaWalletNet.Main.Examples.Outputs_and_Transactions.Send_a_Transaction |
| 9 | +{ |
| 10 | + public static class SendMicroTransactionExample |
| 11 | + { |
| 12 | + public static async Task Run() |
| 13 | + { |
| 14 | + //Register all of the dependencies into a collection of services |
| 15 | + IServiceCollection services = new ServiceCollection().AddIotaWalletServices(); |
| 16 | + |
| 17 | + //Install services to service provider which is used for dependency injection |
| 18 | + IServiceProvider serviceProvider = services.BuildServiceProvider(); |
| 19 | + |
| 20 | + //Use serviceprovider to create a scope, which disposes of all services at end of scope |
| 21 | + using (IServiceScope scope = serviceProvider.CreateScope()) |
| 22 | + { |
| 23 | + //Request IWallet service from service provider |
| 24 | + IWallet wallet = scope.ServiceProvider.GetRequiredService<IWallet>(); |
| 25 | + |
| 26 | + //Build wallet using a fluent-style configuration api |
| 27 | + wallet = wallet |
| 28 | + .ConfigureWalletOptions() |
| 29 | + .SetCoinType(TypeOfCoin.Shimmer) |
| 30 | + .SetStoragePath("./walletdb") |
| 31 | + .Then() |
| 32 | + .ConfigureClientOptions() |
| 33 | + .AddNodeUrl("https://api.testnet.shimmer.network") |
| 34 | + .SetFaucetUrl("https://faucet.testnet.shimmer.network") |
| 35 | + .IsFallbackToLocalPow() |
| 36 | + .IsLocalPow() |
| 37 | + .Then() |
| 38 | + .ConfigureSecretManagerOptions() |
| 39 | + .SetPassword("password") |
| 40 | + .SetSnapshotPath("./mystronghold") |
| 41 | + .Then() |
| 42 | + .Initialize(); |
| 43 | + |
| 44 | + |
| 45 | + //Let's retrieve our cookiemonster account |
| 46 | + (GetAccountResponse accountResponse, IAccount? account) = await wallet.GetAccountAsync("cookiemonster"); |
| 47 | + Console.WriteLine($"GetAccountAsync: {accountResponse}"); |
| 48 | + |
| 49 | + if (account == null) |
| 50 | + { |
| 51 | + Console.WriteLine("There was a problem retreiving the account."); |
| 52 | + return; |
| 53 | + } |
| 54 | + |
| 55 | + await account.SyncAccountAsync(); |
| 56 | + |
| 57 | + //Let's send 1 Glow, followed by 2 glow, via a single transaction |
| 58 | + //The below creates 2 outputs to the receiver address and 1 more output for your balance. |
| 59 | + //Since a micro transaction creates dust for the receiver, the sender first pays a temporary storage deposit along with the sending micro amount. |
| 60 | + // The receiver now bears the burden of whether to accept this transaction, as if he accepts it he needs to pay storage deposit and the sender's storage deposit |
| 61 | + // would be returned back to the sender. |
| 62 | + //Thus the receiver is given a choice of whether to acccept this transaction within the stipulated time as indicated |
| 63 | + //by [expirationInSeconds]. |
| 64 | + //[1]If it expired, both the amount sent and storage deposit sent is returned back to the sender. |
| 65 | + //[2]If receiver rejects the transaction, both the amount sent and storage deposit sent is returned back to the sender. |
| 66 | + //[3]If the receiver accepts the transaction, the sender's storage deposit is returned. In turn, the receiver now has to put in the storage deposit |
| 67 | + // to claim the micro amount. |
| 68 | + string receiverAddress = "rms1qp8rknypruss89dkqnnuedm87y7xmnmdj2tk3rrpcy3sw3ev52q0vzl42tr"; |
| 69 | + |
| 70 | + SendMicroAmountResponse sendMicroAmountResponse = await account.SendMicroAmountUsingBuilder() |
| 71 | + .AddAddressAndAmount(receiverAddress, 1, expirationInSeconds:120) |
| 72 | + .AddAddressAndAmount(receiverAddress, 2, expirationInSeconds:60) |
| 73 | + .SendMicroAmountAsync(); |
| 74 | + |
| 75 | + |
| 76 | + Console.WriteLine($"SendMicroAmountAsync: {sendMicroAmountResponse}"); |
| 77 | + |
| 78 | + } |
| 79 | + } |
| 80 | + |
| 81 | + |
| 82 | + } |
| 83 | +} |
0 commit comments