-
Notifications
You must be signed in to change notification settings - Fork 2
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #276 from IOTA-NET/275-docs-wallet-events-examples
275 docs wallet events examples
- Loading branch information
Showing
6 changed files
with
171 additions
and
3 deletions.
There are no files selected for viewing
82 changes: 82 additions & 0 deletions
82
IotaSDK.NET.Main/Examples/Events/Subscribe To Events/README.md
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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<IWallet>(); | ||
|
||
//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!"); | ||
} | ||
} | ||
``` |
77 changes: 77 additions & 0 deletions
77
IotaSDK.NET.Main/Examples/Events/Subscribe To Events/SubscribeToEventsExample.cs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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<IWallet>(); | ||
|
||
//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!"); | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters