I have to run node.js to execute smart contracts. My quetsion is about the keys.
I have seen this from the docs –
1. Code to sign 2. Code to execute
But I am unsure how the two are combined. Could someone clarify this?
I have a deployed contract which has this function –
function addBonus( string bonusType, uint bonusTarget, uint bonusEndYear, uint bonusEndMonth, uint bonusEndDay, string bonusToken, uint bonusAmount, string bonusName, uint ineq ) public { // processing }
From the docs –
var Tx = require('ethereumjs-tx'); var privateKey = new Buffer('e331b6d69882b4cb4ea581d88e0b604039a3de5967688d3dcffdd2270c0fd109', 'hex') var rawTx = { nonce: '0x00', gasPrice: '0x09184e72a000', gasLimit: '0x2710', to: '0x0000000000000000000000000000000000000000', value: '0x00', data: '0x7f7465737432000000000000000000000000000000000000000000000000000000600057' } var tx = new Tx(rawTx); tx.sign(privateKey); var serializedTx = tx.serialize(); web3.eth.sendRawTransaction('0x' + serializedTx.toString('hex'), function(err, hash) { if (!err) console.log(hash); // "0x7f9fade1c0d57a7af66ab4ead79fade1c0d57a7af66ab4ead7c2c2eb7b11a91385" });
And this –
// contract abi var abi = [{ name: 'myConstantMethod', type: 'function', constant: true, inputs: [{ name: 'a', type: 'string' }], outputs: [{name: 'd', type: 'string' }] }, { name: 'myStateChangingMethod', type: 'function', constant: false, inputs: [{ name: 'a', type: 'string' }, { name: 'b', type: 'int' }], outputs: [] }, { name: 'myEvent', type: 'event', inputs: [{name: 'a', type: 'int', indexed: true},{name: 'b', type: 'bool', indexed: false}] }]; // creation of contract object var MyContract = web3.eth.contract(abi); // initiate contract for an address var myContractInstance = MyContract.at('0xc4abd0339eb8d57087278718986382264244252f'); // call constant function var result = myContractInstance.myConstantMethod('myParam'); console.log(result) // '0x25434534534' // send a transaction to a function myContractInstance.myStateChangingMethod('someParam1', 23, {value: 200, gas: 2000}); // short hand style web3.eth.contract(abi).at(address).myAwesomeMethod(...); // create filter var filter = myContractInstance.myEvent({a: 5}, function (error, result) { if (!error) console.log(result); /* { address: '0x8718986382264244252fc4abd0339eb8d5708727', topics: "0x12345678901234567890123456789012", "0x0000000000000000000000000000000000000000000000000000000000000005", data: "0x0000000000000000000000000000000000000000000000000000000000000001", ... } */ });
I assume we have to combine these two expressed ideas somehow. I cannot see any private keys in the second example and the first one has no example of running a contract.
Best Answer