From 3bf3eba1e14004074b7ab584b0c5b46509c36dea Mon Sep 17 00:00:00 2001 From: wireless90 <12537739+wireless90@users.noreply.github.com> Date: Thu, 1 Feb 2024 21:22:49 +0800 Subject: [PATCH 1/3] Added events subscription example --- .../SubscribeToEventsExample.cs | 77 +++++++++++++++++++ IotaSDK.NET.Main/Program.cs | 3 +- IotaSDK.NET/Common/Interfaces/IWallet.cs | 9 ++- IotaSDK.NET/Wallet.cs | 2 +- 4 files changed, 88 insertions(+), 3 deletions(-) create mode 100644 IotaSDK.NET.Main/Examples/Events/Subscribe To Events/SubscribeToEventsExample.cs diff --git a/IotaSDK.NET.Main/Examples/Events/Subscribe To Events/SubscribeToEventsExample.cs b/IotaSDK.NET.Main/Examples/Events/Subscribe To Events/SubscribeToEventsExample.cs new file mode 100644 index 0000000..625a218 --- /dev/null +++ b/IotaSDK.NET.Main/Examples/Events/Subscribe To Events/SubscribeToEventsExample.cs @@ -0,0 +1,77 @@ +using IotaSDK.NET.Common.Extensions; +using IotaSDK.NET.Common.Interfaces; +using IotaSDK.NET.Domain.Events; +using IotaSDK.NET.Domain.Tokens; + +using Microsoft.Extensions.DependencyInjection; + +namespace IotaSDK.NET.Main.Examples.Native_Tokens.Burn_Native_Tokens +{ + public static class SubscribeToEventsExample + { + public static async Task Run() + { + //Register all of the dependencies into a collection of services + IServiceCollection services = new ServiceCollection().AddIotaSDKServices(); + + //Install services to service provider which is used for dependency injection + IServiceProvider serviceProvider = services.BuildServiceProvider(); + + //Use serviceprovider to create a scope, which disposes of all services at end of scope + using (IServiceScope scope = serviceProvider.CreateScope()) + { + //Request IWallet service from service provider + IWallet wallet = scope.ServiceProvider.GetRequiredService(); + + //Build wallet using a fluent-style configuration api + wallet = await wallet + .ConfigureWalletOptions() + .SetCoinType(TypeOfCoin.Shimmer) + .SetStoragePath("./walletdb") + .Then() + .ConfigureClientOptions() + .AddNodeUrl("https://api.testnet.shimmer.network") + .SetFaucetUrl("https://faucet.testnet.shimmer.network/api/enqueue") + .IsLocalPow() + .Then() + .ConfigureSecretManagerOptions() + .SetPassword("password") + .SetSnapshotPath("./mystronghold") + .Then() + .InitializeAsync(); + + + //Let's proceed to retrieve our previously created"spending" account. + IAccount spendingAccount = (await wallet.GetAccountAsync("spending")).Payload; + await spendingAccount.SyncAcountAsync(); + + /* + * Available events are: ConsolidationRequired | NewOutput | SpentOutput | TransactionInclusion | TransactionProgress | LedgerAddressGeneration + * Let's get automatically notified when our transaction succeeds. + * Hence we need to subscribe to TransactionInclusion event + * */ + + await wallet.SubscribeToEventsAsync(WalletEventType.TransactionInclusion); + + wallet.OnTransactionInclusion += Wallet_OnTransactionInclusion; + + //We put our wallet into periodic sync mode, + //If it notices our tx getting confirmed, it would alert us + await wallet.StartBackgroundSyncAsync(intervalInMilliseconds: 5000); + + //Lets send a tx to ourselves! + string spendingAddress = (await spendingAccount.GetAddressesAsync()).Payload.First().Address; + await spendingAccount.SendBaseCoinAsync(amount: 1000000, spendingAddress); + + //Just to not let program exit + Console.ReadLine(); + + } + } + private static void Wallet_OnTransactionInclusion(object? sender, WalletEventNotification e) + { + string transactionId = (e.Event as TransactionInclusionWalletEvent)!.TransactionId; + Console.WriteLine($"Tx ID: {transactionId} just got confirmed!"); + } + } +} diff --git a/IotaSDK.NET.Main/Program.cs b/IotaSDK.NET.Main/Program.cs index 1265624..1186921 100644 --- a/IotaSDK.NET.Main/Program.cs +++ b/IotaSDK.NET.Main/Program.cs @@ -25,7 +25,8 @@ static async Task Main(string[] args) //await SendNativeTokensExample.Run(); //await MeltNativeTokensExample.Run(); //await GetFoundryImmutableMetadataExample.Run(); - await BurnNativeTokensExample.Run(); + //await BurnNativeTokensExample.Run(); + await SubscribeToEventsExample.Run(); } } diff --git a/IotaSDK.NET/Common/Interfaces/IWallet.cs b/IotaSDK.NET/Common/Interfaces/IWallet.cs index 3ad9fcc..0a0492a 100644 --- a/IotaSDK.NET/Common/Interfaces/IWallet.cs +++ b/IotaSDK.NET/Common/Interfaces/IWallet.cs @@ -58,6 +58,13 @@ public interface IWallet : IDisposable Task> GenerateEd25519AddressCommandAsync(int accountIndex, int addressIndex, AddressGenerationOptions? addressGenerationOptions = null, HumanReadablePart? bech32Hrp = null); - Task> SubscribeToEventsAsync(WalletEventType walletEventTypes, WalletEventHandler callback); + Task> SubscribeToEventsAsync(WalletEventType walletEventTypes); + + public event EventHandler OnConsolidationRequired; + public event EventHandler OnLedgerAddressGeneration; + public event EventHandler OnNewOutput; + public event EventHandler OnSpentOutput; + public event EventHandler OnTransactionInclusion; + public event EventHandler OnTransactionProgress; } } diff --git a/IotaSDK.NET/Wallet.cs b/IotaSDK.NET/Wallet.cs index fd15569..6ab3272 100644 --- a/IotaSDK.NET/Wallet.cs +++ b/IotaSDK.NET/Wallet.cs @@ -216,7 +216,7 @@ public async Task> GenerateEd25519AddressCommandAsync(in return await _mediator.Send(new GenerateEd25519AddressCommand(_walletHandle, accountIndex, addressIndex, addressGenerationOptions, bech32Hrp)); } - public async Task> SubscribeToEventsAsync(WalletEventType walletEventTypes, WalletEventHandler callback) + public async Task> SubscribeToEventsAsync(WalletEventType walletEventTypes) { return await _mediator.Send(new SubscribeToEventsCommand(_walletHandle, walletEventTypes, WalletEventCallback)); } From b69748c9394c7b4faae8e0aa849fd99331ff8ab6 Mon Sep 17 00:00:00 2001 From: wireless90 <12537739+wireless90@users.noreply.github.com> Date: Thu, 1 Feb 2024 21:25:31 +0800 Subject: [PATCH 2/3] Create README.md --- .../Events/Subscribe To Events/README.md | 82 +++++++++++++++++++ 1 file changed, 82 insertions(+) create mode 100644 IotaSDK.NET.Main/Examples/Events/Subscribe To Events/README.md diff --git a/IotaSDK.NET.Main/Examples/Events/Subscribe To Events/README.md b/IotaSDK.NET.Main/Examples/Events/Subscribe To Events/README.md new file mode 100644 index 0000000..6fbd970 --- /dev/null +++ b/IotaSDK.NET.Main/Examples/Events/Subscribe To Events/README.md @@ -0,0 +1,82 @@ +# Let's subscribe to some Wallet Events! +## Code Example + +The following example will: + +1. Create a wallet +2. Retrieve your spending account +3. Sync account +4. Subscribe to TransactionInclusion Event +5. Register a callback for the TransactionInclusion Event +6. Enable Background Syncing + + +```cs +public static class SubscribeToEventsExample +{ + public static async Task Run() + { + //Register all of the dependencies into a collection of services + IServiceCollection services = new ServiceCollection().AddIotaSDKServices(); + + //Install services to service provider which is used for dependency injection + IServiceProvider serviceProvider = services.BuildServiceProvider(); + + //Use serviceprovider to create a scope, which disposes of all services at end of scope + using (IServiceScope scope = serviceProvider.CreateScope()) + { + //Request IWallet service from service provider + IWallet wallet = scope.ServiceProvider.GetRequiredService(); + + //Build wallet using a fluent-style configuration api + wallet = await wallet + .ConfigureWalletOptions() + .SetCoinType(TypeOfCoin.Shimmer) + .SetStoragePath("./walletdb") + .Then() + .ConfigureClientOptions() + .AddNodeUrl("https://api.testnet.shimmer.network") + .SetFaucetUrl("https://faucet.testnet.shimmer.network/api/enqueue") + .IsLocalPow() + .Then() + .ConfigureSecretManagerOptions() + .SetPassword("password") + .SetSnapshotPath("./mystronghold") + .Then() + .InitializeAsync(); + + + //Let's proceed to retrieve our previously created"spending" account. + IAccount spendingAccount = (await wallet.GetAccountAsync("spending")).Payload; + await spendingAccount.SyncAcountAsync(); + + /* + * Available events are: ConsolidationRequired | NewOutput | SpentOutput | TransactionInclusion | TransactionProgress | LedgerAddressGeneration + * Let's get automatically notified when our transaction succeeds. + * Hence we need to subscribe to TransactionInclusion event + * */ + + await wallet.SubscribeToEventsAsync(WalletEventType.TransactionInclusion); + + wallet.OnTransactionInclusion += Wallet_OnTransactionInclusion; + + //We put our wallet into periodic sync mode, + //If it notices our tx getting confirmed, it would alert us + await wallet.StartBackgroundSyncAsync(intervalInMilliseconds: 5000); + + //Lets send a tx to ourselves! + string spendingAddress = (await spendingAccount.GetAddressesAsync()).Payload.First().Address; + await spendingAccount.SendBaseCoinAsync(amount: 1000000, spendingAddress); + + //Just to not let program exit + Console.ReadLine(); + + } + } + private static void Wallet_OnTransactionInclusion(object? sender, WalletEventNotification e) + { + string transactionId = (e.Event as TransactionInclusionWalletEvent)!.TransactionId; + Console.WriteLine($"Tx ID: {transactionId} just got confirmed!"); + } +} +``` \ No newline at end of file From 99edc5cc7d8dfac610e20301544732a6ca684eab Mon Sep 17 00:00:00 2001 From: wireless90 <12537739+wireless90@users.noreply.github.com> Date: Thu, 1 Feb 2024 21:26:24 +0800 Subject: [PATCH 3/3] Update README.md --- README.md | 1 + 1 file changed, 1 insertion(+) diff --git a/README.md b/README.md index 184639e..ed7d987 100644 --- a/README.md +++ b/README.md @@ -62,6 +62,7 @@ To install,
  • StartBackgroundSync
  • StopBackgroundSync
  • StoreMnemonic
  • +
  • SubscribeToEvents