Created
February 7, 2020 09:18
-
-
Save Dave-Whiffin/f96c2dc1e4b95cc854aab1a31dcfafec 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.Encoders; | |
using Nethereum.ABI.FunctionEncoding.Attributes; | |
using Nethereum.Contracts; | |
using Nethereum.Web3; | |
using Nethereum.Web3.Accounts; | |
using System; | |
using System.Numerics; | |
using System.Threading.Tasks; | |
namespace NethereumSamples | |
{ | |
/* | |
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]; | |
} | |
} | |
*/ | |
public static class ProductHistorySample | |
{ | |
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<ProductHistoryDeployment>().SendRequestAndWaitForReceiptAsync(); | |
Console.WriteLine($"Contract Address: {deploymentReceipt.ContractAddress}"); | |
// contract deployment ends here // | |
var bytes32Encoder = new Bytes32TypeEncoder(); | |
var contractHandler = web3.Eth.GetContractHandler(deploymentReceipt.ContractAddress); | |
// create product | |
Console.WriteLine("New Product"); | |
var newProductFunctionArgs = new NewProductFunction { ProductKey = bytes32Encoder.Encode("p1"), Text = "Product 1" }; | |
var receipt = await contractHandler.SendRequestAndWaitForReceiptAsync(newProductFunctionArgs); | |
Console.WriteLine($"Transaction Complete. Tx Hash: {receipt.TransactionHash}, Success: {receipt.Status}"); | |
// query | |
Console.WriteLine("Query/Get Product"); | |
var getProductArgs = new GetProductTextFunction { ProductKey = bytes32Encoder.Encode("p1") }; | |
var productText = await contractHandler.QueryAsync<GetProductTextFunction, string>(getProductArgs); | |
Console.WriteLine($"Product Text. {productText}"); | |
} | |
// Generated code starts here | |
public partial class ProductHistoryDeployment : ProductHistoryDeploymentBase | |
{ | |
public ProductHistoryDeployment() : base(BYTECODE) { } | |
public ProductHistoryDeployment(string byteCode) : base(byteCode) { } | |
} | |
public class ProductHistoryDeploymentBase : ContractDeploymentMessage | |
{ | |
public static string BYTECODE = "608060405234801561001057600080fd5b5061049b806100206000396000f3fe60806040526004361061003f5760003560e01c8063528c87d2146100445780637b5bc6b61461007a5780638d18c3581461009a5780639f8242b6146100c7575b600080fd5b34801561005057600080fd5b5061006461005f36600461032f565b6100e7565b6040516100719190610442565b60405180910390f35b61008d610088366004610347565b610105565b6040516100719190610437565b3480156100a657600080fd5b506100ba6100b536600461032f565b61015b565b604051610071919061044b565b3480156100d357600080fd5b506100ba6100e236600461032f565b6101f5565b600181815481106100f457fe5b600091825260209091200154905081565b6000828152602081815260408220835161012192850190610294565b505060018054808201825560008290527fb10e2d527612073b26eecdfd717e6a320cf44b4afac2b0732d9fcbe2b7fa0cf601929092555090565b600060208181529181526040908190208054825160026001831615610100026000190190921691909104601f8101859004850282018501909352828152929091908301828280156101ed5780601f106101c2576101008083540402835291602001916101ed565b820191906000526020600020905b8154815290600101906020018083116101d057829003601f168201915b505050505081565b6000818152602081815260409182902080548351601f60026000196101006001861615020190931692909204918201849004840281018401909452808452606093928301828280156102885780601f1061025d57610100808354040283529160200191610288565b820191906000526020600020905b81548152906001019060200180831161026b57829003601f168201915b50505050509050919050565b828054600181600116156101000203166002900490600052602060002090601f016020900481019282601f106102d557805160ff1916838001178555610302565b82800160010185558215610302579182015b828111156103025782518255916020019190600101906102e7565b5061030e929150610312565b5090565b61032c91905b8082111561030e5760008155600101610318565b90565b600060208284031215610340578081fd5b5035919050565b60008060408385031215610359578081fd5b8235915060208084013567ffffffffffffffff80821115610378578384fd5b81860187601f820112610389578485fd5b8035925081831115610399578485fd5b604051601f8401601f19168101850183811182821017156103b8578687fd5b60405283815281840185018910156103ce578586fd5b83858301868301378585858301015280955050505050509250929050565b60008151808452815b81811015610411576020818501810151868301820152016103f5565b818111156104225782602083870101525b50601f01601f19169290920160200192915050565b901515815260200190565b90815260200190565b60006020825261045e60208301846103ec565b939250505056fea264697066735822122072f27a441d33a383ad0c06d730fa91cd253815cc10923e8729b80178c0667d0164736f6c63430006010033"; | |
public ProductHistoryDeploymentBase() : base(BYTECODE) { } | |
public ProductHistoryDeploymentBase(string byteCode) : base(byteCode) { } | |
} | |
public partial class GetProductTextFunction : GetProductTextFunctionBase { } | |
[Function("getProductText", "string")] | |
public class GetProductTextFunctionBase : FunctionMessage | |
{ | |
[Parameter("bytes32", "productKey", 1)] | |
public virtual byte[] ProductKey { get; set; } | |
} | |
public partial class NewProductFunction : NewProductFunctionBase { } | |
[Function("newProduct", "bool")] | |
public class NewProductFunctionBase : FunctionMessage | |
{ | |
[Parameter("bytes32", "productKey", 1)] | |
public virtual byte[] ProductKey { get; set; } | |
[Parameter("string", "text", 2)] | |
public virtual string Text { get; set; } | |
} | |
public partial class ProductListFunction : ProductListFunctionBase { } | |
[Function("productList", "bytes32")] | |
public class ProductListFunctionBase : FunctionMessage | |
{ | |
[Parameter("uint256", "", 1)] | |
public virtual BigInteger ReturnValue1 { get; set; } | |
} | |
public partial class ProductTextFunction : ProductTextFunctionBase { } | |
[Function("productText", "string")] | |
public class ProductTextFunctionBase : FunctionMessage | |
{ | |
[Parameter("bytes32", "", 1)] | |
public virtual byte[] ReturnValue1 { get; set; } | |
} | |
public partial class GetProductTextOutputDTO : GetProductTextOutputDTOBase { } | |
[FunctionOutput] | |
public class GetProductTextOutputDTOBase : IFunctionOutputDTO | |
{ | |
[Parameter("string", "", 1)] | |
public virtual string ReturnValue1 { get; set; } | |
} | |
public partial class ProductListOutputDTO : ProductListOutputDTOBase { } | |
[FunctionOutput] | |
public class ProductListOutputDTOBase : IFunctionOutputDTO | |
{ | |
[Parameter("bytes32", "", 1)] | |
public virtual byte[] ReturnValue1 { get; set; } | |
} | |
public partial class ProductTextOutputDTO : ProductTextOutputDTOBase { } | |
[FunctionOutput] | |
public class ProductTextOutputDTOBase : IFunctionOutputDTO | |
{ | |
[Parameter("string", "", 1)] | |
public virtual string ReturnValue1 { get; set; } | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment