1+ using System ;
2+ using System . Threading . Tasks ;
3+ using Lab2 . SmartContracts ;
4+ using Nethereum . Web3 . Accounts . Managed ;
5+ using Nethereum . Geth ;
6+ using Nethereum . Hex . HexTypes ;
7+
8+ namespace ConsoleApp
9+ {
10+ class Program
11+ {
12+ /// <summary>
13+ /// Simple ConsoleApp to connect to Ethereum Consortium Blockchain on Azure to deploy a contract and run functions and transactions.
14+ /// This project depends on the Solidity project from Lab 2. So make sure to run `npm run build`.
15+ /// </summary>
16+ public static void Main ( string [ ] args )
17+ {
18+ Console . WriteLine ( "Blockchain - Ethereum - ConsoleApp" ) ;
19+
20+ string senderAddress = "0x???" ; // TODO 1
21+ string password = "test" ;
22+
23+ Console . WriteLine ( new string ( '-' , 80 ) ) ;
24+
25+ TestService ( senderAddress , password ) . Wait ( 1000 * 60 * 5 ) ; // Wait max 5 minutes
26+
27+ Console . WriteLine ( new string ( '-' , 80 ) ) ;
28+ }
29+
30+ private static async Task TestService ( string fromAddress , string password )
31+ {
32+ var account = new ManagedAccount ( fromAddress , password ) ;
33+
34+ var web3 = new Web3Geth ( account , "http://???.westeurope.cloudapp.azure.com:8545/" ) ; // TODO 2
35+
36+ bool deployNewContract = true ;
37+ string contractAddress = null ;
38+ if ( deployNewContract )
39+ {
40+ var gasForDeployContract = new HexBigInteger ( 1500000 ) ;
41+ Console . WriteLine ( "Deploying contract (can take some time)" ) ;
42+ contractAddress = await SimpleStorageContractService . DeployContractAsync ( web3 , fromAddress , 1 , "mstack.nl" , null , gasForDeployContract ) . ConfigureAwait ( false ) ;
43+ Console . WriteLine ( $ "Deploying contract done, address = { contractAddress } ") ;
44+ }
45+
46+ // Create an instance from the SimpleStorageContractService service which
47+ // abstracts all calls to the SmartContract.
48+ ISimpleStorageContractService service = new SimpleStorageContractService ( web3 , contractAddress ) ;
49+
50+ bool setNumberResult = await service . ExecuteTransactionAsync ( srv => srv . SetNumberAsync ( fromAddress , 500 ) ) . ConfigureAwait ( false ) ;
51+ Console . WriteLine ( $ "setNumberResult = '{ setNumberResult } '.") ;
52+
53+ var getNumberValue = await service . GetNumberCallAsync ( fromAddress ) . ConfigureAwait ( false ) ;
54+ Console . WriteLine ( $ "The stored number value is '{ getNumberValue } '.") ;
55+
56+ bool setStringResult = await service . ExecuteTransactionAsync ( srv => srv . SetStringAsync ( fromAddress , "mstack.nl test" ) ) ;
57+ Console . WriteLine ( $ "setStringResult = '{ setStringResult } '.") ;
58+
59+ var getStringValue = await service . GetStringCallAsync ( fromAddress ) . ConfigureAwait ( false ) ;
60+ Console . WriteLine ( $ "The stored string value is '{ getStringValue } '.") ;
61+ }
62+ }
63+ }
0 commit comments