Skip to content

Instantly share code, notes, and snippets.

@jcalixv
Created May 30, 2017 16:21
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save jcalixv/883596974dfd05a6358251a381fa94fe to your computer and use it in GitHub Desktop.
Save jcalixv/883596974dfd05a6358251a381fa94fe to your computer and use it in GitHub Desktop.
Ethereum Geth, Nethereum contract transaction, with multiples paramaters.
/*I found the below contract example: https://medium.com/@robhitchens/solidity-crud-part-1-824ffa69509a */
/*I use the Ethereum wallet to publish the contract 0.8.10 */
/*Im running a private dev Node, with the below command */
/*geth --datadir "L:\chains\usd" --rpc --rpcport 8545 --rpcaddr 192.168.100.13 --rpcapi "eth,web3,personal,net,miner,admin,debug" --dev console 2>> L:\chains\usd.log*/
/*the problem, when i test the contract from within the wallet, the transactions and events shows as expected.*/
/* when I send the transaction from .NET 4.6.1 TestProject the test goes green,
but the contract within the wallet never shows the new record added or the event fired*/
/* I made a few tests, for example: if I call the function insertUser, the above problem. But*/
/* If I call the insertUserTest function, the event gets fired.*/
/*Im thinking the problem arise when I want to save to the ethereum storage.*/
/*please help me out*/
/*Solidity Contract code*/
pragma solidity ^0.4.11;
contract UserCrud {
struct UserStruct {
bytes32 userEmail;
uint userAge;
uint index;
}
mapping(address => UserStruct) private userStructs;
address[] private userIndex;
event LogNewUser (address indexed userAddress, uint index, bytes32 userEmail, uint userAge);
event LogUpdateUser(address indexed userAddress, uint index, bytes32 userEmail, uint userAge);
function isUser(address userAddress)
public
constant
returns(bool isIndeed)
{
if(userIndex.length == 0) return false;
return (userIndex[userStructs[userAddress].index] == userAddress);
}
function insertUser(
address userAddress,
bytes32 userEmail,
uint userAge)
public
returns(uint index)
{
if(isUser(userAddress)) throw;
userStructs[userAddress].userEmail = userEmail;
userStructs[userAddress].userAge = userAge;
userStructs[userAddress].index = userIndex.push(userAddress)-1;
LogNewUser(
userAddress,
userStructs[userAddress].index,
userEmail,
userAge);
return userIndex.length-1;
}
function insertUserTest(
address userAddress,
bytes32 userEmail,
uint userAge)
public
returns(uint index)
{
LogNewUser(
userAddress,
userStructs[userAddress].index,
userEmail,
userAge);
return 1;
}
function getUser(address userAddress)
public
constant
returns(bytes32 userEmail, uint userAge, uint index)
{
if(!isUser(userAddress)) throw;
return(
userStructs[userAddress].userEmail,
userStructs[userAddress].userAge,
userStructs[userAddress].index);
}
function updateUserEmail(address userAddress, bytes32 userEmail)
public
returns(bool success)
{
if(!isUser(userAddress)) throw;
userStructs[userAddress].userEmail = userEmail;
LogUpdateUser(
userAddress,
userStructs[userAddress].index,
userEmail,
userStructs[userAddress].userAge);
return true;
}
function updateUserAge(address userAddress, uint userAge)
public
returns(bool success)
{
if(!isUser(userAddress)) throw;
userStructs[userAddress].userAge = userAge;
LogUpdateUser(
userAddress,
userStructs[userAddress].index,
userStructs[userAddress].userEmail,
userAge);
return true;
}
function getUserCount()
public
constant
returns(uint count)
{
return userIndex.length;
}
function getUserAtIndex(uint index)
public
constant
returns(address userAddress)
{
return userIndex[index];
}
}
/*c# contract caller code. .NET 4.6.1 UnitTestProject*/
[TestMethod]
public async Task TestMethodAddTx2()
{
var abi = @"[ { ""constant"": false, ""inputs"": [ { ""name"": ""userAddress"", ""type"": ""address"" }, { ""name"": ""userEmail"", ""type"": ""bytes32"" }, { ""name"": ""userAge"", ""type"": ""uint256"" } ], ""name"": ""insertUser"", ""outputs"": [ { ""name"": ""index"", ""type"": ""uint256"" } ], ""payable"": false, ""type"": ""function"" }, { ""constant"": false, ""inputs"": [ { ""name"": ""userAddress"", ""type"": ""address"" }, { ""name"": ""userAge"", ""type"": ""uint256"" } ], ""name"": ""updateUserAge"", ""outputs"": [ { ""name"": ""success"", ""type"": ""bool"" } ], ""payable"": false, ""type"": ""function"" }, { ""constant"": true, ""inputs"": [ { ""name"": ""userAddress"", ""type"": ""address"" } ], ""name"": ""isUser"", ""outputs"": [ { ""name"": ""isIndeed"", ""type"": ""bool"", ""value"": false } ], ""payable"": false, ""type"": ""function"" }, { ""constant"": true, ""inputs"": [ { ""name"": ""userAddress"", ""type"": ""address"" } ], ""name"": ""getUser"", ""outputs"": [ { ""name"": ""userEmail"", ""type"": ""bytes32"", ""value"": ""0x"" }, { ""name"": ""userAge"", ""type"": ""uint256"", ""value"": ""0"" }, { ""name"": ""index"", ""type"": ""uint256"", ""value"": ""0"" } ], ""payable"": false, ""type"": ""function"" }, { ""constant"": false, ""inputs"": [ { ""name"": ""userAddress"", ""type"": ""address"" }, { ""name"": ""userEmail"", ""type"": ""bytes32"" } ], ""name"": ""updateUserEmail"", ""outputs"": [ { ""name"": ""success"", ""type"": ""bool"" } ], ""payable"": false, ""type"": ""function"" }, { ""constant"": false, ""inputs"": [ { ""name"": ""userAddress"", ""type"": ""address"" }, { ""name"": ""userEmail"", ""type"": ""bytes32"" }, { ""name"": ""userAge"", ""type"": ""uint256"" } ], ""name"": ""insertUserTest"", ""outputs"": [ { ""name"": ""index"", ""type"": ""uint256"" } ], ""payable"": false, ""type"": ""function"" }, { ""constant"": true, ""inputs"": [], ""name"": ""getUserCount"", ""outputs"": [ { ""name"": ""count"", ""type"": ""uint256"", ""value"": ""0"" } ], ""payable"": false, ""type"": ""function"" }, { ""constant"": true, ""inputs"": [ { ""name"": ""index"", ""type"": ""uint256"" } ], ""name"": ""getUserAtIndex"", ""outputs"": [ { ""name"": ""userAddress"", ""type"": ""address"", ""value"": ""0x"" } ], ""payable"": false, ""type"": ""function"" }, { ""anonymous"": false, ""inputs"": [ { ""indexed"": true, ""name"": ""userAddress"", ""type"": ""address"" }, { ""indexed"": false, ""name"": ""index"", ""type"": ""uint256"" }, { ""indexed"": false, ""name"": ""userEmail"", ""type"": ""bytes32"" }, { ""indexed"": false, ""name"": ""userAge"", ""type"": ""uint256"" } ], ""name"": ""LogNewUser"", ""type"": ""event"" }, { ""anonymous"": false, ""inputs"": [ { ""indexed"": true, ""name"": ""userAddress"", ""type"": ""address"" }, { ""indexed"": false, ""name"": ""index"", ""type"": ""uint256"" }, { ""indexed"": false, ""name"": ""userEmail"", ""type"": ""bytes32"" }, { ""indexed"": false, ""name"": ""userAge"", ""type"": ""uint256"" } ], ""name"": ""LogUpdateUser"", ""type"": ""event"" } ]";
var contractAddress = "0x3B795b5caF0274086784aC9e5aD74AD651D0Ce75";
var web3 = new Web3(@"http://192.168.100.13:8545");
var web3Geth = new Web3Geth(web3.Client);
var unlockResult = await web3.Personal.UnlockAccount.SendRequestAsync(_mainAccount, _password, 60);
Assert.IsTrue(unlockResult);
var contract = web3.Eth.GetContract(abi, contractAddress);
var addTxFunction = contract.GetFunction("insertUser");
var transactionHash = await addTxFunction.SendTransactionAsync(_mainAccount
, _adminAccount, "Hola".ToHexUTF8().HexToByteArray(), 10);
Console.WriteLine(transactionHash);
var receipt = await MineAndGetReceiptAsync(web3Geth, transactionHash);
Console.WriteLine(JsonConvert.SerializeObject(receipt));
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment