When watching for events from web3, this is the format of the result you get when one is called:
{ address: '0x9c0ac1e0f0a8e0b01c7b652d5fbe094ddff48b81',
blockNumber: 704227,
transactionHash: '0x5887ba4e15d51e1cfddf626ecf416a0002085a1e0929fffe1f90ad69d5040081',
transactionIndex: 0,
blockHash: '0x5f485133ec662f556d88affccc18a358375de160178c6cf7cc0cec678d833a2a',
logIndex: 0,
removed: false,
event: 'ExampleEvent',
args:
{ argOne: BigNumber { s: 1, e: 0, c: [Array] },
argTwo: '0x374623456fa2' } }
So you can just get the TX from result.transactionHash
Event in contract:
event ExampleEvent(uint argOne, bytes32 argTwo)
Web3 code:
import exampleContractObject from 'path/to/ExampleContract.json'
ExampleContract = web3.eth.contract(exampleContractObject.abi);
contractInstance = ExampleContract.at('0x9c0ac1e0f0a8e0b01c7b652d5fbe094ddff48b81');
exampleEvent = contractInstance.ExampleEvent();
exampleEvent.watch((err, result) => {
// Do something
}
EDIT:
The Transfer event from ERC20 tokens would look like this:
{ address: '<contract_address>',
blockNumber: <block_number>,
transactionHash: '<transaction_hash>',
transactionIndex: <tx_index_in_block,
blockHash: '<block_hash>',
logIndex: <log_index>,
removed: <> ,
event: 'Transfer',
args:
{ _from: <address>,
_two: <address> ,
_value: <uint256>} }
I've used this scripts to deploy a contract using web3 v1.0-beta24
const Web3 = require('web3');
const web3 = new Web3('http://127.0.0.1:8545');
const ownerAddress = "0x....";
const contractAbi = [{"constant":false.... }];
const contractCode = "0x606060...";
const MyContract = new web3.eth.Contract(contractAbi);
MyContract.deploy({
data: contractCode,
})
.send({
from: ownerAddress,
gas: 4000000,
gasPrice: '30000000000000',
})
.then((instance) => {
console.log(`Address: ${instance.options.address}`);
});
And this other script to send a message and make a query
const Web3 = require('web3');
const web3 = new Web3('http://127.0.0.1:8545');
const ownerAddress = "0x....";
const contractAbi = [{"constant":false.... }];
const contractCode = "0x606060...";
const contractAddress = "0x...."; // <----- previous script output
const myContract = new web3.eth.Contract(contractAbi, contractAddress);
const options = {
from: ownerAddress,
gas: 4000000,
gasPrice: '30000000000000',
};
const message = web3.utils.asciiToHex('hola');
myContract.methods.sendMessage(message)
.send(options, (err, hash) => {
if (err) {
console.log(err);
}
console.log(`TxHash: ${hash}`);
})
.then((result) => {
myContract.methods.getMessage().call()
.then((result) => {
console.log(web3.utils.hexToAscii(result));
});
});
The sample contract I've used is very simple
pragma solidity ^0.4.18;
contract Echo {
bytes32 public message;
function sendMessage(bytes32 _message) public {
message = _message;
}
function getMessage() view public returns (bytes32) {
return message;
}
}
Best Answer
There are 2 ways to do it. If you want to use the address with its password, follow the method below.
For this above method, you would need the keystore file to be present within the same Ubuntu machine.
If you want to send with
privateKey
: