1- using IotaWalletNet . Application . AccountContext . Commands . SendAmount ;
1+ using IotaWalletNet . Application . AccountContext . Commands . MintNfts ;
2+ using IotaWalletNet . Application . AccountContext . Commands . SendAmount ;
23using IotaWalletNet . Application . AccountContext . Commands . SyncAccount ;
34using IotaWalletNet . Application . AccountContext . Queries . GetBalance ;
45using IotaWalletNet . Application . Common . Extensions ;
56using IotaWalletNet . Application . Common . Interfaces ;
7+ using IotaWalletNet . Domain . Common . Extensions ;
68using IotaWalletNet . Domain . Common . Interfaces ;
79using IotaWalletNet . Domain . Common . Models . Coin ;
10+ using IotaWalletNet . Domain . Common . Models . Nft ;
11+ using IotaWalletNet . Domain . Common . Models . Output ;
812using IotaWalletNet . Domain . Common . Models . Transaction ;
913using Microsoft . Extensions . DependencyInjection ;
14+ using MimeMapping ;
1015using Newtonsoft . Json ;
1116using static IotaWalletNet . Application . WalletContext . Queries . GetAccount . GetAccountQueryHandler ;
1217using static IotaWalletNet . Domain . Common . Models . Events . EventTypes ;
@@ -24,66 +29,70 @@ public static async Task Run()
2429 IServiceProvider serviceProvider = services . BuildServiceProvider ( ) ;
2530
2631 //Use serviceprovider to create a scope, which disposes of all services at end of scope
27- using ( IServiceScope scope = serviceProvider . CreateScope ( ) )
32+ using IServiceScope serviceScope = serviceProvider . CreateScope ( ) ;
33+
34+ //Request IWallet service from service provider
35+ IWallet wallet = serviceScope . ServiceProvider . GetRequiredService < IWallet > ( ) ;
36+
37+ //Build wallet using a fluent-style configuration api
38+ wallet = wallet
39+ . ConfigureClientOptions ( )
40+ . AddNodeUrl ( "https://api.testnet.shimmer.network" )
41+ . SetFaucetUrl ( "https://faucet.testnet.shimmer.network" )
42+ . IsFallbackToLocalPow ( )
43+ . Then ( )
44+ . ConfigureSecretManagerOptions ( )
45+ . SetSnapshotPath ( "./mystronghold" )
46+ . SetPassword ( "password" )
47+ . Then ( )
48+ . ConfigureWalletOptions ( )
49+ . SetStoragePath ( "./walletdb" )
50+ . SetCoinType ( TypeOfCoin . Shimmer )
51+ . Then ( )
52+ . Initialize ( ) ;
53+
54+ //Subscrive to events, particularly transaction inclusion
55+ //For simplicity, we included all events
56+ wallet . SubscribeToEvents ( WalletEventTypes . AllEvents ) ;
57+
58+ //Let's retrieve our cookiemonster account
59+ ( _ , IAccount ? account ) = await wallet . GetAccountAsync ( "cookiemonster" ) ;
60+
61+ //Let's enable periodic syncing every 5 esconds
62+ //We can cancel the periodic syncing with the tokenSource
63+ CancellationTokenSource tokenSource = new CancellationTokenSource ( ) ;
64+ account . EnablePeriodicSyncing ( intervalInMilliSeconds : 5000 , tokenSource . Token ) ;
65+
66+ //Let's create a simple random nft that just contains textual data
67+ NftIrc27 metadata = new NftIrc27 ( KnownMimeTypes . Text , "Random NFT" , "www.random.com" ) ;
68+ List < NftOptions > nftOptions = new List < NftOptions > ( )
2869 {
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-
70+ new NftOptions ( ) { ImmutableMetadata = JsonConvert . SerializeObject ( metadata ) . ToHexString ( ) }
71+ } ;
72+ MintNftsResponse mintNftsResponse = await account . MintNftsAsync ( nftOptions ) ;
73+
74+ //Now lets obtain the transaction object and wait for its confirmation as well as the new outputs to be
75+ //added to our wallet.
76+ //The below could only be achieved if you have subscribed to the transaction inclusion event.
77+ //You also have to sync in order to receive updates. Thus, we have enabled periodic syncing to help us.
78+ Transaction transaction = mintNftsResponse . Payload ! ;
79+ string transactionId = transaction . TransactionId ;
80+ Task waitConfirmationTask = transaction . WaitForConfirmationAsync ( account ) ;
81+ Task waitNewOutputTask = transaction . WaitForNewOutputAsync ( account ) ;
82+ await Task . WhenAll ( waitConfirmationTask , waitNewOutputTask ) ;
83+
84+ //Success,the transaction have been confirmed and new outputs have arrived to our wallet!
85+ Console . WriteLine ( "Finished waiting" ) ;
86+
87+ //Let's stop our periodic syncing
88+ tokenSource . Cancel ( ) ;
89+
90+ await account . SyncAccountAsync ( ) ;
91+
92+ List < OutputData > outputs = ( await account . GetUnspentOutputsAsync ( ) ) . Payload ! ;
93+ var newOutputs = outputs . Where ( x => x . Metadata . TransactionId == transactionId && x . Output . Type == 6 ) ;
94+ Console . WriteLine ( newOutputs . Count ( ) ) ;
95+ await Task . Delay ( 200 * 1000 ) ;
8796 }
8897
8998 }
0 commit comments