Created
February 7, 2020 09:48
-
-
Save Dave-Whiffin/61edf8a5b6ccaef92243d441f95cdc0e to your computer and use it in GitHub Desktop.
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
using Nethereum.ABI.FunctionEncoding.Attributes; | |
using Nethereum.Contracts; | |
using Nethereum.Web3; | |
using Nethereum.Web3.Accounts; | |
using Newtonsoft.Json; | |
using System; | |
using System.Numerics; | |
using System.Threading.Tasks; | |
namespace NethereumSamples | |
{ | |
public static class ProductHistorySamplev2 | |
{ | |
/* | |
pragma solidity ^0.6.1; | |
pragma experimental ABIEncoderV2; | |
contract ProductTransHistory_2 { | |
struct Transaction { | |
string text; | |
address FromAddress; | |
address ToAddress; | |
uint256 Price; | |
} | |
struct Product { | |
string text; | |
mapping(uint256 => Transaction) TransactionStructs; | |
uint256 TransactionCount; | |
} | |
mapping(string => Product) ProductStructs; | |
string[] ProductList; | |
function newProduct(string memory ProductKey, string memory text) public returns (bool success) { | |
ProductStructs[ProductKey].text = text; | |
ProductList.push(ProductKey); | |
return true; | |
} | |
function getProduct(string memory ProductKey) public view returns (string memory wording, uint256 TransactionCount) { | |
return (ProductStructs[ProductKey].text, ProductStructs[ProductKey].TransactionCount); | |
} | |
function addTransaction( | |
string memory ProductKey, string memory _transactionText, address _fromAddress, address _toAddress, uint256 _Price) | |
public returns (bool success) | |
{ | |
uint256 _TransacitonCount = ProductStructs[ProductKey].TransactionCount + 1; | |
ProductStructs[ProductKey].TransactionStructs[_TransacitonCount].text = _transactionText; | |
ProductStructs[ProductKey].TransactionStructs[_TransacitonCount].FromAddress = _fromAddress; | |
ProductStructs[ProductKey].TransactionStructs[_TransacitonCount].ToAddress = _toAddress; | |
ProductStructs[ProductKey].TransactionStructs[_TransacitonCount].Price = _Price; | |
ProductStructs[ProductKey].TransactionCount = _TransacitonCount; | |
return true; | |
} | |
function getProductTransaction(string memory ProductKey, uint256 TransactionNo) | |
public | |
view | |
returns (string memory ProductText, string memory TransactionText, address FromAddress, address ToAddress, uint256 TransacitonCost) | |
{ | |
return ( | |
ProductStructs[ProductKey].text, | |
ProductStructs[ProductKey].TransactionStructs[TransactionNo].text, | |
ProductStructs[ProductKey].TransactionStructs[TransactionNo].FromAddress, | |
ProductStructs[ProductKey].TransactionStructs[TransactionNo].ToAddress, | |
ProductStructs[ProductKey].TransactionStructs[TransactionNo].Price | |
); | |
} | |
function getTransactionCount() public view returns (uint256 ProductCount) { | |
return ProductList.length; | |
} | |
function getProductAtIndex(uint256 row) public view returns (string memory productkey) { | |
return ProductList[row]; | |
} | |
function getProductTransactionCount(string memory ProductKey) public view returns (uint256 transactionCount) { | |
return ProductStructs[ProductKey].TransactionCount; | |
} | |
} | |
*/ | |
public static async Task Main(string[] args) | |
{ | |
// contract deployment starts here // | |
var url = "http://testchain.nethereum.com:8545"; | |
var privateKey = "0x7580e7fb49df1c861f0050fae31c2224c6aba908e116b8da44ee8cd927b990b0"; | |
var account = new Account(privateKey); | |
var web3 = new Web3(account, url); | |
Console.WriteLine($"Deploying Contract"); | |
var deploymentReceipt = await web3.Eth.GetContractDeploymentHandler<ProductTransHistory_2Deployment>().SendRequestAndWaitForReceiptAsync(); | |
Console.WriteLine($"Contract Address: {deploymentReceipt.ContractAddress}"); | |
// contract deployment ends here // | |
var contractHandler = web3.Eth.GetContractHandler(deploymentReceipt.ContractAddress); | |
// create product | |
Console.WriteLine("New Product"); | |
var newProductFunctionArgs = new NewProductFunction { ProductKey = "p1", Text = "Product 1" }; | |
var receipt = await contractHandler.SendRequestAndWaitForReceiptAsync(newProductFunctionArgs); | |
Console.WriteLine($"Transaction Complete. Tx Hash: {receipt.TransactionHash}, Success: {receipt.Status}"); | |
Console.WriteLine("Add Transaction"); | |
var newTranArgs = new AddTransactionFunction { | |
ProductKey = "p1", | |
TransactionText = "tran 1", | |
FromAddress = account.Address, | |
ToAddress = account.Address, | |
Price = new BigInteger(99) }; | |
var addTranReceipt = await contractHandler.SendRequestAndWaitForReceiptAsync<AddTransactionFunction>(newTranArgs); | |
Console.WriteLine($"Transaction Complete. Tx Hash: {addTranReceipt.TransactionHash}, Success: {addTranReceipt.Status}"); | |
// query | |
Console.WriteLine("Query/Get Product"); | |
var getProductArgs = new GetProductFunction { ProductKey ="p1" }; | |
var productText = await contractHandler.QueryAsync<GetProductFunction, string>(getProductArgs); | |
Console.WriteLine($"Product Text. {productText}"); | |
Console.WriteLine("Query/Get Product"); | |
var getProductTranArgs = new GetProductTransactionFunction { ProductKey = "p1", TransactionNo = 1 }; | |
var productTransactionDto = await contractHandler.QueryDeserializingToObjectAsync<GetProductTransactionFunction, GetProductTransactionOutputDTO>(getProductTranArgs); | |
Console.WriteLine($"Product Trans. {JsonConvert.SerializeObject(productTransactionDto)}"); | |
} | |
/* | |
pragma solidity ^0.6.1; | |
pragma experimental ABIEncoderV2; | |
contract ProductHistory { | |
mapping(bytes32 => string) public productText; | |
bytes32[] public productList; | |
constructor() public {} | |
function newProduct(bytes32 productKey, string memory text) public payable returns (bool) { | |
productText[productKey] = text; | |
productList.push(productKey); | |
return true; | |
} | |
function getProductText(bytes32 productKey) public view returns(string memory){ | |
return productText[productKey]; | |
} | |
} | |
*/ | |
// Generated code starts here | |
public partial class ProductTransHistory_2Deployment : ProductTransHistory_2DeploymentBase | |
{ | |
public ProductTransHistory_2Deployment() : base(BYTECODE) { } | |
public ProductTransHistory_2Deployment(string byteCode) : base(byteCode) { } | |
} | |
public class ProductTransHistory_2DeploymentBase : ContractDeploymentMessage | |
{ | |
public static string BYTECODE = "608060405234801561001057600080fd5b50610b28806100206000396000f3fe608060405234801561001057600080fd5b506004361061007d5760003560e01c80639e7ab8ec1161005b5780639e7ab8ec146100e1578063ad4d978614610105578063c8f0532a14610125578063d54630e3146101385761007d565b80632e7700f01461008257806368111cce146100a0578063776059ec146100c1575b600080fd5b61008a61014b565b6040516100979190610ab9565b60405180910390f35b6100b36100ae36600461085d565b610152565b604051610097929190610a97565b6100d46100cf3660046109c8565b610227565b6040516100979190610a33565b6100f46100ef366004610985565b6102d0565b604051610097959493929190610a4d565b610118610113366004610898565b6104f4565b6040516100979190610a28565b61008a61013336600461085d565b61057a565b6101186101463660046108f9565b6105a4565b6001545b90565b6060600080836040516101659190610a0c565b90815260405190819003602001812090600090610183908690610a0c565b90815260408051602092819003830181206002908101548554600181161561010002600019011691909104601f810185900485028301850190935282825292909184918301828280156102175780601f106101ec57610100808354040283529160200191610217565b820191906000526020600020905b8154815290600101906020018083116101fa57829003601f168201915b5050505050915091509150915091565b60606001828154811061023657fe5b600091825260209182902001805460408051601f60026000196101006001871615020190941693909304928301859004850281018501909152818152928301828280156102c45780601f10610299576101008083540402835291602001916102c4565b820191906000526020600020905b8154815290600101906020018083116102a757829003601f168201915b50505050509050919050565b606080600080600080876040516102e79190610a0c565b90815260405190819003602001812090600090610305908a90610a0c565b90815260408051918290036020908101832060008b8152600190910190915290812091610333908b90610a0c565b90815260408051918290036020908101832060008c81526001918201909252918120909101546001600160a01b03169161036e908c90610a0c565b90815260408051918290036020908101832060008d81526001909101909152908120600201546001600160a01b0316916103a9908d90610a0c565b908152604080516020928190038301812060008e81526001918201855283902060030154885460029281161561010002600019011691909104601f8101859004850283018501909352828252929091879183018282801561044b5780601f106104205761010080835404028352916020019161044b565b820191906000526020600020905b81548152906001019060200180831161042e57829003601f168201915b5050875460408051602060026001851615610100026000190190941693909304601f8101849004840282018401909252818152959a50899450925084019050828280156104d95780601f106104ae576101008083540402835291602001916104d9565b820191906000526020600020905b8154815290600101906020018083116104bc57829003601f168201915b50505050509350945094509450945094509295509295909350565b6000816000846040516105079190610a0c565b9081526020016040518091039020600001908051906020019061052b929190610730565b50600180548082018255600091909152835161056e917fb10e2d527612073b26eecdfd717e6a320cf44b4afac2b0732d9fcbe2b7fa0cf601906020860190610730565b50600190505b92915050565b6000808260405161058b9190610a0c565b9081526020016040518091039020600201549050919050565b6000806000876040516105b79190610a0c565b9081526020016040518091039020600201546001019050856000886040516105df9190610a0c565b908152604080516020928190038301902060008581526001909101835220825161060f9391929190910190610730565b50846000886040516106219190610a0c565b9081526020016040518091039020600101600083815260200190815260200160002060010160006101000a8154816001600160a01b0302191690836001600160a01b031602179055508360008860405161067b9190610a0c565b9081526020016040518091039020600101600083815260200190815260200160002060020160006101000a8154816001600160a01b0302191690836001600160a01b03160217905550826000886040516106d59190610a0c565b90815260200160405180910390206001016000838152602001908152602001600020600301819055508060008860405161070f9190610a0c565b90815260405190819003602001902060020155506001905095945050505050565b828054600181600116156101000203166002900490600052602060002090601f016020900481019282601f1061077157805160ff191683800117855561079e565b8280016001018555821561079e579182015b8281111561079e578251825591602001919060010190610783565b506107aa9291506107ae565b5090565b61014f91905b808211156107aa57600081556001016107b4565b80356001600160a01b038116811461057457600080fd5b600082601f8301126107ef578081fd5b813567ffffffffffffffff80821115610806578283fd5b604051601f8301601f191681016020018281118282101715610826578485fd5b60405282815292508284830160200186101561084157600080fd5b8260208601602083013760006020848301015250505092915050565b60006020828403121561086e578081fd5b813567ffffffffffffffff811115610884578182fd5b610890848285016107df565b949350505050565b600080604083850312156108aa578081fd5b823567ffffffffffffffff808211156108c1578283fd5b6108cd868387016107df565b935060208501359150808211156108e2578283fd5b506108ef858286016107df565b9150509250929050565b600080600080600060a08688031215610910578081fd5b853567ffffffffffffffff80821115610927578283fd5b61093389838a016107df565b96506020880135915080821115610948578283fd5b50610955888289016107df565b94505061096587604088016107c8565b925061097487606088016107c8565b949793965091946080013592915050565b60008060408385031215610997578182fd5b823567ffffffffffffffff8111156109ad578283fd5b6109b9858286016107df565b95602094909401359450505050565b6000602082840312156109d9578081fd5b5035919050565b600081518084526109f8816020860160208601610ac2565b601f01601f19169290920160200192915050565b60008251610a1e818460208701610ac2565b9190910192915050565b901515815260200190565b600060208252610a4660208301846109e0565b9392505050565b600060a08252610a6060a08301886109e0565b8281036020840152610a7281886109e0565b6001600160a01b03968716604085015294909516606083015250608001529392505050565b600060408252610aaa60408301856109e0565b90508260208301529392505050565b90815260200190565b60005b83811015610add578181015183820152602001610ac5565b83811115610aec576000848401525b5050505056fea264697066735822122018c74a190666b217b1957cbcc2432e9f9b8ba37096ebc4893970a453ea739bf064736f6c63430006010033"; | |
public ProductTransHistory_2DeploymentBase() : base(BYTECODE) { } | |
public ProductTransHistory_2DeploymentBase(string byteCode) : base(byteCode) { } | |
} | |
public partial class AddTransactionFunction : AddTransactionFunctionBase { } | |
[Function("addTransaction", "bool")] | |
public class AddTransactionFunctionBase : FunctionMessage | |
{ | |
[Parameter("string", "ProductKey", 1)] | |
public virtual string ProductKey { get; set; } | |
[Parameter("string", "_transactionText", 2)] | |
public virtual string TransactionText { get; set; } | |
[Parameter("address", "_fromAddress", 3)] | |
public virtual string FromAddress { get; set; } | |
[Parameter("address", "_toAddress", 4)] | |
public virtual string ToAddress { get; set; } | |
[Parameter("uint256", "_Price", 5)] | |
public virtual BigInteger Price { get; set; } | |
} | |
public partial class GetProductFunction : GetProductFunctionBase { } | |
[Function("getProduct", typeof(GetProductOutputDTO))] | |
public class GetProductFunctionBase : FunctionMessage | |
{ | |
[Parameter("string", "ProductKey", 1)] | |
public virtual string ProductKey { get; set; } | |
} | |
public partial class GetProductAtIndexFunction : GetProductAtIndexFunctionBase { } | |
[Function("getProductAtIndex", "string")] | |
public class GetProductAtIndexFunctionBase : FunctionMessage | |
{ | |
[Parameter("uint256", "row", 1)] | |
public virtual BigInteger Row { get; set; } | |
} | |
public partial class GetProductTransactionFunction : GetProductTransactionFunctionBase { } | |
[Function("getProductTransaction", typeof(GetProductTransactionOutputDTO))] | |
public class GetProductTransactionFunctionBase : FunctionMessage | |
{ | |
[Parameter("string", "ProductKey", 1)] | |
public virtual string ProductKey { get; set; } | |
[Parameter("uint256", "TransactionNo", 2)] | |
public virtual BigInteger TransactionNo { get; set; } | |
} | |
public partial class GetProductTransactionCountFunction : GetProductTransactionCountFunctionBase { } | |
[Function("getProductTransactionCount", "uint256")] | |
public class GetProductTransactionCountFunctionBase : FunctionMessage | |
{ | |
[Parameter("string", "ProductKey", 1)] | |
public virtual string ProductKey { get; set; } | |
} | |
public partial class GetTransactionCountFunction : GetTransactionCountFunctionBase { } | |
[Function("getTransactionCount", "uint256")] | |
public class GetTransactionCountFunctionBase : FunctionMessage | |
{ | |
} | |
public partial class NewProductFunction : NewProductFunctionBase { } | |
[Function("newProduct", "bool")] | |
public class NewProductFunctionBase : FunctionMessage | |
{ | |
[Parameter("string", "ProductKey", 1)] | |
public virtual string ProductKey { get; set; } | |
[Parameter("string", "text", 2)] | |
public virtual string Text { get; set; } | |
} | |
public partial class GetProductOutputDTO : GetProductOutputDTOBase { } | |
[FunctionOutput] | |
public class GetProductOutputDTOBase : IFunctionOutputDTO | |
{ | |
[Parameter("string", "wording", 1)] | |
public virtual string Wording { get; set; } | |
[Parameter("uint256", "TransactionCount", 2)] | |
public virtual BigInteger TransactionCount { get; set; } | |
} | |
public partial class GetProductAtIndexOutputDTO : GetProductAtIndexOutputDTOBase { } | |
[FunctionOutput] | |
public class GetProductAtIndexOutputDTOBase : IFunctionOutputDTO | |
{ | |
[Parameter("string", "productkey", 1)] | |
public virtual string Productkey { get; set; } | |
} | |
public partial class GetProductTransactionOutputDTO : GetProductTransactionOutputDTOBase { } | |
[FunctionOutput] | |
public class GetProductTransactionOutputDTOBase : IFunctionOutputDTO | |
{ | |
[Parameter("string", "ProductText", 1)] | |
public virtual string ProductText { get; set; } | |
[Parameter("string", "TransactionText", 2)] | |
public virtual string TransactionText { get; set; } | |
[Parameter("address", "FromAddress", 3)] | |
public virtual string FromAddress { get; set; } | |
[Parameter("address", "ToAddress", 4)] | |
public virtual string ToAddress { get; set; } | |
[Parameter("uint256", "TransacitonCost", 5)] | |
public virtual BigInteger TransacitonCost { get; set; } | |
} | |
public partial class GetProductTransactionCountOutputDTO : GetProductTransactionCountOutputDTOBase { } | |
[FunctionOutput] | |
public class GetProductTransactionCountOutputDTOBase : IFunctionOutputDTO | |
{ | |
[Parameter("uint256", "transactionCount", 1)] | |
public virtual BigInteger TransactionCount { get; set; } | |
} | |
public partial class GetTransactionCountOutputDTO : GetTransactionCountOutputDTOBase { } | |
[FunctionOutput] | |
public class GetTransactionCountOutputDTOBase : IFunctionOutputDTO | |
{ | |
[Parameter("uint256", "ProductCount", 1)] | |
public virtual BigInteger ProductCount { get; set; } | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment