ether-camp / ethereum-harmony

DEPRECATED! Ethereum Independent Peer
GNU General Public License v3.0
253 stars 89 forks source link

How consume values of contract ? #83

Open 1jose234 opened 6 years ago

1jose234 commented 6 years ago

Hi I have a private Net in Ethereumj, in this net i deployed a smart contract, so i need from other Web System done in JSF show values processed in the smart contract.

I saw that with web3 its posible do with the net Ethereum for example Testnet. With etherumj is posible to do ? for example Harmony ?

i want to do this example with EthereumJ https://medium.com/@mvmurthy/full-stack-hello-world-voting-ethereum-dapp-tutorial-part-2-30b3d335aa1f

mkalinin commented 6 years ago

Yes, it's possible. web3 can be linked with Harmony JSON-RPC endpoint which is bind to port 8080 by default. That link is what you need to get started

1jose234 commented 6 years ago

i am working with Harmony and with the example in Ethereumj EventListenerSample.java i whant to get the values with web3js in the contract deployed.

but the value in the log of Harmony is Empty.

2018-05-21 17:12:27,780 INFO [pool-15-thread-1] net - submit txs: [TransactionData [hash=6b54582757cd185f023c289ac3a77edb2d61ddde04ec07d196c371ff6105b58f nonce=00, gasPrice=104c533c00, gas=0000000000015f90, receiveAddress=38e4367262a577dffdb7d6df68e201dbcb421084, sendAddress=49912393632feac425cc2aeb8bb0dd10bffa0783, value=, data=623845d80000000000000000000000000000000000000000000000000000000000000016, signatureV=28, signatureR=bd01cb47dbd4457a3ee57d1e17b6b4b7c6cc7246d33be49e0575662ddcf9d5d7, signatureS=34ba3502fef63f0f07b009b3d431a1df11cf0d9b88493117d9883a44d74d66a1]]

this is my code in web3js

<script src="https://ajax.googleapis.com/ajax/libs/jquery/3.3.1/jquery.min.js"> </script>
<script type="text/javascript" src="../dist/web3.js"></script>
<script type="text/javascript">

    var Web3 = require('web3');
    var web3 = new Web3();

   web3.setProvider(new web3.providers.HttpProvider("http://localhost:8080"));    
  web3.eth.defaultAccount = web3.eth.accounts[1];
  web3.personal.unlockAccount("0x49912393632feac425cc2aeb8bb0dd10bffa0783", "pass", null);

var sampleContractABI = [{"constant":false,"inputs":[{"name":"n","type":"int256"}],"name":"inc","outputs":[],"payable":false,"stateMutability":"nonpayable","type":"function"},{"constant":false,"inputs":[],"name":"get","outputs":[{"name":"","type":"int256"}],"payable":false,"stateMutability":"nonpayable","type":"function"},{"anonymous":false,"inputs":[{"indexed":false,"name":"_from","type":"address"},{"indexed":false,"name":"_inc","type":"int256"},{"indexed":false,"name":"_total","type":"int256"}],"name":"Inc","type":"event"}]

var sampleContract = web3.eth.contract(sampleContractABI);
var sampleContractInstance = sampleContract.at('0x38e4367262a577dffdb7d6df68e201dbcb421084');

 console.log(sampleContractInstance);
</script>

i used the console in Chrome this is the metod "inc"

image

this is the methos "get" where we can see that the value is 0 and this is because the transaction send the value empty. image

then the image of log from Harmony

image

some idea of what I can do ?

thanks

zilm13 commented 6 years ago

@1jose234 value is amount of ethereum you send with this tx, as it's just contract call and you are not sending anything, value is empty. Contract call is encoded in data field. 0 encoded is the same as nothing == empty.

1jose234 commented 6 years ago

@zilm13 i don't understand what is the problem to get the value from web3js in the method inc ? could be that i need send a value in the tx ? i am confused because in the tutorial do this using only the call to method.

zilm13 commented 6 years ago

Transaction value is the amount of ethereum (in wei) you send with transaction. Method call inputs are encoded in data field: data=623845d80000000000000000000000000000000000000000000000000000000000000016 here's the call input 623845d8 is call method, encoded, and 16 is your 22 in hex I'm not familiar with web3js, but in EthereumJ to decode it out you will need something like that:

        CallTransaction.Contract contract = new CallTransaction.Contract(abiString);
        CallTransaction.Function inc = contract.getByName("inc");
        Object[] callInputs = inc.decode(tx.getData());
        System.out.println("Function inc called with input: " + ((BigInteger) callInputs[0]));

Do I understand your question correctly? Or I am answering wrong thing?

1jose234 commented 6 years ago

@zilm13 clear but I think my problem is one of definition ... i want to create a Dapp in JSF for example and use a Smart contract. i think that i can do is. 1.- Use Ethereumj bulding a jar, because i need know use all time the source code to create a block chain. 2.- With the jar and the blockchain building, create a project JSF and deploy the smart contract, the set and get of variables could be like the example.

its correct ? with this mode, is not necesary use web3 true ?

analyzing my question arose other... in the application I need to consult a transaction history, is this possible?

zilm13 commented 6 years ago

You could use web3j to be not tied to EthereumJ and change your peer to any other in case of issues. So you could have more diversity with such approach. But I don't know how to decode data using web3j, you'd better learn it from web3j documentation. I'm sure it's possible with it, not a big deal from the implementation level.

in the application I need to consult a transaction history

you will not have fast transaction details for your dapp from any kind of ethereum peer. It's better to add listener for any new imported block, filter transactions that interact with your contract, decode its input and store in separate db. Otherwise you may need to replay all blocks from current to contract deployment.

1jose234 commented 6 years ago

i tried to work with web3j but finally i need that the Dapps will be create with different language and not only Java. In Java i want to create only the blockchain.

so i need consume RPC with web3js but i can't set value to variable and update the value.

mkalinin commented 6 years ago

Actually, there is no way to create Dapps with Java. What variable are you referring to? If it's a Dapp variable then it could be set only via a call message sent to the contract, you must send an Ethereum transaction. To read the value stored in the contract constant functions (pure or view) are usually used, read the docs about that http://solidity.readthedocs.io/en/v0.4.21/contracts.html#functions.

1jose234 commented 6 years ago

@mkalinin i have done the calls with web3js using the documentation, i can get the value of the method get. after send a value as parameter, in the console show that was sent fine however not change the value in the blockchain. the contract is of the example in Ethereumj

to get the value image

to send value to blockchain image

the log of Harmony 2018-05-29 11:27:16,702 INFO [pool-14-thread-1] net - submit txs: [TransactionData [hash=1c9097e6b24f44b60a707f2c6a62425f2e4578dfe9fe308b3d7d72250fbcbedc nonce=00, gasPrice=104c533c00, gas=0f4240, receiveAddress=cbb9c2b539890460e61f5362b96369842e76a464, sendAddress=49912393632feac425cc2aeb8bb0dd10bffa0783, value=, data=623845d80000000000000000000000000000000000000000000000000000000000000001, signatureV=28, signatureR=809bf8c73df9dc8f0bec5019e965adfc69de806bd44e7f551377932587a24973, signatureS=26aaa80f42cfda863d21704220332c5301fa8c4570155b84fd23a851e8d6aff2]]

mkalinin commented 6 years ago

Yep, that way it sends a Tx with call message as I mentioned above. I see that nonce = 0 in your case which is incorrect, such transaction won't be executed. Try to set nonce value to account[1].nonce + 1, it should work

1jose234 commented 6 years ago

@mkalinin i tested setting the nonce but don't update the value and now dont create the hash

[TransactionData [hash= nonce=21, gasPrice=104c533c00, gas=0f4240, receiveAddress=cbb9c2b539890460e61f5362b96369842e76a464, sendAddress=49912393632feac425cc2aeb8bb0dd10bffa0783, value=, data=623845d80000000000000000000000000000000000000000000000000000000000000001, signatureV=28, signatureR=e432017040c3a16cac3e981ccedd9f84e530db3a55aa24af00b81c40dd7c3f82, signatureS=478fb76d0f881ed2fbd2971532661f693b084af36053f43c27f46ffc4e46bffd]]

i think that could be the account, because in harmony i use the account number 2 49912393632feac425cc2aeb8bb0dd10bffa0783, and from the console with Ethereumj when send a value to contract this is sended by other sender but i don't understand if this is not one account

<=== Sending transaction: TransactionData [hash= nonce=34, gasPrice=104c533c00, gas=2dc6c0, receiveAddress=df171f203ad3339cadce5717411dc6fdd4ba1402, sendAddress=31e2e1ed11951c7091dfba62cd4b7145e947219c

mkalinin commented 6 years ago

What is the balance of 49912393632feac425cc2aeb8bb0dd10bffa0783?