Skip to content

Commit 7f907ea

Browse files
committed
Lab 2 + updated code generation (Event)
1 parent 0a6f424 commit 7f907ea

File tree

4 files changed

+33
-33
lines changed

4 files changed

+33
-33
lines changed

Common/Solidity/templates/cs-service-interface.ejs

+9-18
Original file line numberDiff line numberDiff line change
@@ -1,18 +1,21 @@
11
using System;
2-
using System.Threading.Tasks;
32
using System.Numerics;
4-
using Nethereum.Hex.HexTypes;
3+
using System.Threading.Tasks;
54
using Nethereum.Contracts;
5+
using Nethereum.Hex.HexTypes;
6+
using Nethereum.RPC.Eth.DTOs;
67

78
// Created with 'Solidity to C# generator' by [email protected] (mStack B.V.)
89
// Based on abi-code-gen (https://github.com/Nethereum/abi-code-gen)
910
namespace <%= locals.namespace ? namespace : 'DefaultNamespace' %>
1011
{
1112
public interface I<%= capitalizeFirstLetter(getContractName()) %>Service
1213
{
13-
Task<bool> ExecuteTransactionAsync(Func<I<%= capitalizeFirstLetter(getContractName()) %>Service, Task<string>> func, int timeoutInSeconds = 120);
14+
Task<TransactionReceipt> ExecuteTransactionAsync(Func<I<%= capitalizeFirstLetter(getContractName()) %>Service, Task<string>> func, int timeoutInSeconds = 120);
15+
16+
Task<BlockWithTransactions> GetBlockWithTransactionsAsync(string blockhash);
1417

15-
<%= abi.forEach(generateGetFunction) %>
18+
<%#= abi.forEach(generateGetFunction) %>
1619
<%= abi.forEach(generateGetEvent) %>
1720
<%= abi.forEach(generateFunctionCall) %>
1821
<%= abi.forEach(generateFunctionTransaction) %>
@@ -229,7 +232,8 @@ _%>
229232
_%>
230233
<%_ function generateGetEvent(item) { _%>
231234
<%_ if (item.type === 'event') { _%>
232-
Event Get<%= capitalizeFirstLetter(item.name) %>();
235+
<%_ var eventName = capitalizeFirstLetter(item.name); _%>
236+
Event<<%=eventName%>> Get<%=eventName%>();
233237
<%_ } }
234238
_%>
235239
<%_ function generateFunctionCall_Original(item) { _%>
@@ -263,12 +267,6 @@ _%>
263267
[Parameter("<%=param.type%>", "<%=param.name%>", <%=index + 1%>)]
264268
public <%-getTypeMap(param.type)-%> <%-capitalizeFirstLetter(getParamName(param.name, index))-%> {get; set;}
265269
266-
<%_ }
267-
_%>
268-
<%_ function generateEventDTOProperty(param, index) { _%>
269-
[Parameter("<%=param.type%>", "<%=param.name%>", <%=index + 1%>, <%= param.indexed ? 'true' : 'false'%>)]
270-
public <%-getTypeMap(param.type)-%> <%-capitalizeFirstLetter(getParamName(param.name, index))-%> {get; set;}
271-
272270
<%_ }
273271
_%>
274272
<%_ function generateFunctionOutputDTO(item) { _%>
@@ -280,11 +278,4 @@ _%>
280278
}
281279
<%_ } }
282280
_%>
283-
<%_ function generateEventDTO(item) { _%>
284-
<%_ if (item.type === 'event' && item.inputs !== null && item.inputs.length > 1 ) { _%>
285-
public class <%=capitalizeFirstLetter(item.name)%>Event
286-
{
287-
<%- item.inputs.forEach(generateEventDTOProperty) -%>
288-
}
289-
<%_ } }
290281
_%>

Common/Solidity/templates/cs-service.ejs

+18-9
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,11 @@
11
using System;
2+
using System.Numerics;
23
using System.Threading;
34
using System.Threading.Tasks;
4-
using System.Numerics;
5-
using Nethereum.Hex.HexTypes;
6-
using Nethereum.ABI.FunctionEncoding.Attributes;
75
using Nethereum.Contracts;
6+
using Nethereum.ABI.FunctionEncoding.Attributes;
7+
using Nethereum.Hex.HexTypes;
8+
using Nethereum.RPC.Eth.DTOs;
89
using Nethereum.Web3;
910

1011
// Created with 'Solidity to C# generator' by [email protected] (mStack B.V.)
@@ -38,7 +39,7 @@ namespace <%= locals.namespace ? namespace : 'DefaultNamespace' %>
3839
_contract = _web3.Eth.GetContract(ABI, address);
3940
}
4041

41-
public async Task<bool> ExecuteTransactionAsync(Func<I<%= capitalizeFirstLetter(getContractName()) %>Service, Task<string>> func, int timeoutInSeconds)
42+
public async Task<TransactionReceipt> ExecuteTransactionAsync(Func<I<%= capitalizeFirstLetter(getContractName()) %>Service, Task<string>> func, int timeoutInSeconds)
4243
{
4344
var cts = new CancellationTokenSource(TimeSpan.FromSeconds(timeoutInSeconds));
4445

@@ -51,7 +52,12 @@ namespace <%= locals.namespace ? namespace : 'DefaultNamespace' %>
5152
receipt = await _web3.Eth.Transactions.GetTransactionReceipt.SendRequestAsync(transaction).ConfigureAwait(false);
5253
}
5354

54-
return receipt != null;
55+
return receipt;
56+
}
57+
58+
public Task<BlockWithTransactions> GetBlockWithTransactionsAsync(string blockhash)
59+
{
60+
return _web3.Eth.Blocks.GetBlockWithTransactionsByHash.SendRequestAsync(blockhash);
5561
}
5662

5763
<%= abi.forEach(generateGetFunction) %>
@@ -270,7 +276,7 @@ function getTypeMap(typeName) {
270276
_%>
271277
<%_ function generateGetFunction(item) { _%>
272278
<%_ if (item.type === 'function') { _%>
273-
public Function GetFunction<%= capitalizeFirstLetter(item.name) %>()
279+
private Function GetFunction<%= capitalizeFirstLetter(item.name) %>()
274280
{
275281
return _contract.GetFunction("<%=item.name%>");
276282
}
@@ -279,9 +285,10 @@ _%>
279285
_%>
280286
<%_ function generateGetEvent(item) { _%>
281287
<%_ if (item.type === 'event') { _%>
282-
public Event Get<%= capitalizeFirstLetter(item.name) %>()
288+
<%_ var eventName = capitalizeFirstLetter(item.name); _%>
289+
public Event<<%=eventName%>> Get<%=eventName%>()
283290
{
284-
return _contract.GetEvent("<%=item.name%>");
291+
return _contract.GetEvent<<%=eventName%>>("<%=item.name%>");
285292
}
286293
287294
<%_ } }
@@ -393,7 +400,9 @@ _%>
393400
_%>
394401
<%_ function generateEventDTO(item) { _%>
395402
<%_ if (item.type === 'event' && item.inputs !== null && item.inputs.length > 1 ) { _%>
396-
public class <%=capitalizeFirstLetter(item.name)%>
403+
<%_ var eventName = capitalizeFirstLetter(item.name); _%>
404+
[Event("<%=eventName%>")]
405+
public class <%=eventName%> : IEventDTO
397406
{
398407
<%- item.inputs.forEach(generateEventDTOProperty) -%>
399408
}

Lab 2 - Completed/ConsoleApp/Program.cs

+3-3
Original file line numberDiff line numberDiff line change
@@ -43,20 +43,20 @@ private static async Task TestService(string fromAddress, string password)
4343
if (deployNewContract)
4444
{
4545
var gasForDeployContract = new HexBigInteger(3000000);
46-
Console.WriteLine("Deploying contract (can take some time)");
46+
Console.WriteLine("Deploying contract");
4747
contractAddress = await SimpleStorageContractService.DeployContractAsync(web3, fromAddress, 1, "mstack.nl", null, gasForDeployContract);
4848
Console.WriteLine($"Deploying contract done, address = {contractAddress}");
4949
}
5050

5151
// Create an instance from the SimpleStorageContractService service which abstracts all calls to the SmartContract.
5252
ISimpleStorageContractService service = new SimpleStorageContractService(web3, contractAddress);
5353

54-
bool setNumberResult = await service.ExecuteTransactionAsync(srv => srv.SetNumberAsync(fromAddress, 500));
54+
var setNumberResult = await service.ExecuteTransactionAsync(srv => srv.SetNumberAsync(fromAddress, 500));
5555

5656
var getNumberValue = await service.GetNumberCallAsync(fromAddress);
5757
Console.WriteLine($"The stored number value is '{getNumberValue}'.");
5858

59-
bool setStringResult = await service.ExecuteTransactionAsync(srv => srv.SetStringAsync(fromAddress, "mstack.nl test"));
59+
var setStringResult = await service.ExecuteTransactionAsync(srv => srv.SetStringAsync(fromAddress, "mstack.nl test"));
6060

6161
var getStringValue = await service.GetStringCallAsync(fromAddress);
6262
Console.WriteLine($"The stored string value is '{getStringValue}'.");

Lab 2/ConsoleApp/Program.cs

+3-3
Original file line numberDiff line numberDiff line change
@@ -43,7 +43,7 @@ private static async Task TestService(string fromAddress, string password)
4343
if (deployNewContract)
4444
{
4545
var gasForDeployContract = new HexBigInteger(3000000);
46-
Console.WriteLine("Deploying contract (can take some time)");
46+
Console.WriteLine("Deploying contract");
4747
contractAddress = await SimpleStorageContractService.DeployContractAsync(web3, fromAddress, 1, "mstack.nl", null, gasForDeployContract);
4848
Console.WriteLine($"Deploying contract done, address = {contractAddress}");
4949
}
@@ -53,14 +53,14 @@ private static async Task TestService(string fromAddress, string password)
5353
ISimpleStorageContractService service = new SimpleStorageContractService(web3, contractAddress);
5454

5555
// TODO 1:
56-
// bool setNumberResult = await service.ExecuteTransactionAsync(srv => srv.SetNumberAsync(fromAddress, 500));
56+
// var setNumberResult = await service.ExecuteTransactionAsync(srv => srv.SetNumberAsync(fromAddress, 500));
5757

5858
// TODO 2:
5959
// var getNumberValue = await service.GetNumberCallAsync(fromAddress);
6060
// Console.WriteLine($"The stored number value is '{getNumberValue}'.");
6161

6262
// TODO 3:
63-
// bool setStringResult = await service.ExecuteTransactionAsync(srv => srv.SetStringAsync(fromAddress, "mstack.nl test"));
63+
// var setStringResult = await service.ExecuteTransactionAsync(srv => srv.SetStringAsync(fromAddress, "mstack.nl test"));
6464

6565
// TODO 4:
6666
// var getStringValue = await service.GetStringCallAsync(fromAddress);

0 commit comments

Comments
 (0)