Skip to content

Commit c4c9c2a

Browse files
committed
Lab 3
1 parent fdc6d45 commit c4c9c2a

File tree

4 files changed

+172
-0
lines changed

4 files changed

+172
-0
lines changed

Lab 3/ConsoleApp/ConsoleApp.csproj

+21
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
<Project Sdk="Microsoft.NET.Sdk">
2+
3+
<PropertyGroup>
4+
<OutputType>Exe</OutputType>
5+
<TargetFramework>netcoreapp2.0</TargetFramework>
6+
<AssemblyName>ConsoleApp.Lab3</AssemblyName>
7+
<StartupObject>ConsoleApp.Program</StartupObject>
8+
</PropertyGroup>
9+
10+
<ItemGroup>
11+
<Compile Include="..\..\Lab 2\Solidity\contracts\build\ISimpleStorageContractService.Generated.cs" Link="ISimpleStorageContractService.Generated.cs" />
12+
<Compile Include="..\..\Lab 2\Solidity\contracts\build\SimpleStorageContractService.Generated.cs" Link="SimpleStorageContractService.Generated.cs" />
13+
</ItemGroup>
14+
15+
<ItemGroup>
16+
<PackageReference Include="Nethereum.Geth" Version="2.2.3" />
17+
<PackageReference Include="Nethereum.Web3" Version="2.2.3" />
18+
<PackageReference Include="Newtonsoft.Json" Version="10.0.3" />
19+
</ItemGroup>
20+
21+
</Project>

Lab 3/ConsoleApp/ConsoleApp.sln

+25
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
2+
Microsoft Visual Studio Solution File, Format Version 12.00
3+
# Visual Studio 15
4+
VisualStudioVersion = 15.0.27130.2036
5+
MinimumVisualStudioVersion = 10.0.40219.1
6+
Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "ConsoleApp", "ConsoleApp.csproj", "{E41E4230-826C-4B8C-BBB4-2B73732D4364}"
7+
EndProject
8+
Global
9+
GlobalSection(SolutionConfigurationPlatforms) = preSolution
10+
Debug|Any CPU = Debug|Any CPU
11+
Release|Any CPU = Release|Any CPU
12+
EndGlobalSection
13+
GlobalSection(ProjectConfigurationPlatforms) = postSolution
14+
{E41E4230-826C-4B8C-BBB4-2B73732D4364}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
15+
{E41E4230-826C-4B8C-BBB4-2B73732D4364}.Debug|Any CPU.Build.0 = Debug|Any CPU
16+
{E41E4230-826C-4B8C-BBB4-2B73732D4364}.Release|Any CPU.ActiveCfg = Release|Any CPU
17+
{E41E4230-826C-4B8C-BBB4-2B73732D4364}.Release|Any CPU.Build.0 = Release|Any CPU
18+
EndGlobalSection
19+
GlobalSection(SolutionProperties) = preSolution
20+
HideSolutionNode = FALSE
21+
EndGlobalSection
22+
GlobalSection(ExtensibilityGlobals) = postSolution
23+
SolutionGuid = {96E9B6A0-ACA9-4777-9DCD-3DAF4EE96B16}
24+
EndGlobalSection
25+
EndGlobal

Lab 3/ConsoleApp/Program.cs

+63
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,63 @@
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+
}

Lab 3/README.md

+63
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,63 @@
1+
# Lab 3
2+
3+
## Info
4+
5+
This lab (continues on Lab 2) and will teach you how to:
6+
7+
- deploy the SmartContract using a console application to Ethereum Consortium Blockchain on Azure
8+
- execute functions on the SmartContract which is deployed to Ethereum Consortium Blockchain on Azure
9+
10+
If you have some generic or detailed questions, please feel free to ask these at mStack colleagues.
11+
12+
## Folder structure
13+
14+
- `ConsoleApp`: folder for the ConsoleApp (C#).
15+
16+
## Details
17+
18+
This lab contains one simple SmartContract:
19+
20+
- `SimpleStorageContract` this contract can store and retrieve number and string values
21+
22+
## Steps
23+
24+
Execute the following steps in order to follow this Lab.
25+
26+
### Step 1
27+
28+
Now open a new command terminal in Visual Code, and type:
29+
30+
- `cd Lab 3`
31+
- `cd ConsoleApp`
32+
- `dotnet build` (if all is fine, a restore should be done and the build should complete with 0 errors.)
33+
34+
### Step 2
35+
36+
Open the file `Lab 3\ConsoleApp\Program.cs` in Visual Code.
37+
38+
- `TODO 1` : make sure to fill in your assigned address. Note that the password is always `test`.
39+
- `TODO 2` : replace `http://???.westeurope.cloudapp.azure.com:8545/` by the correct URL provided by mStack.
40+
- Run the ConsoleApp `dotnet run` and the output should look like this:
41+
42+
``` x
43+
Blockchain - Ethereum - ConsoleApp
44+
--------------------------------------------------------------------------------
45+
Deploying contract (can take some time)
46+
Deploying contract done, address = 0x97da25343187cbc1d31d98b1fd244cec5fc77f86
47+
setNumberResult = 'True'.
48+
The stored number value is '10'.
49+
setStringResult = 'True'.
50+
The stored string value is 'mstack.nl test'.
51+
```
52+
53+
### Step 3
54+
55+
You can experiment by calling some other functions or providing other values.
56+
57+
### Step 4
58+
59+
Each time you run the full program, a new contract is deployed, if you want to keep using the same contract, just remember the contract address and use that one. (See also step 11 from Lab 2)
60+
61+
### Step 5
62+
63+
Done

0 commit comments

Comments
 (0)