61

Script To Find Transactions To/From An Account Usage Find all transaction to/from eth.accounts[0] within the last 1,000 blocks: > getTransactionsByAccount(eth.accounts[0]) Find all transaction to/from The DAO's account 0xbb9bc244d798123fde783fcc1c72d3bb8c189413 between blocks 1,432,400 and 1,432,423: > getTransactionsByAccount("...


36

Do this: You'll need to pull code from web3, and it works best if your frontend is bundled using something like webpack or browserify: var SolidityCoder = require("web3/lib/solidity/coder.js"); var log = receipt.logs[0]; var data = SolidityCoder.decodeParams(["string", "uint"], log.data.replace("0x", "")); In this case, we're decoding log data that ...


35

Warning, I'm not a huge fan of Lisk. This is obviously one side of the story and I'm sure there are more advantages of Lisk than I give them credit for. But I don't know them. A blog post (not by me): Why Lisk is inferior to Ethereum Author's main points regarding Lisk: Lisk "sandbox" cannot be used to run untrusted code Lisk framework provides no ...


29

1. Mine only when there are transactions! var mining_threads = 1 function checkWork() { if (eth.getBlock("pending").transactions.length > 0) { if (eth.mining) return; console.log("== Pending transactions! Mining..."); miner.start(mining_threads); } else { miner.stop(0); // This param means nothing console....


26

Jehan's answer is great, but we need to explain one more thing: Why does sha3(1) in solidity produce b10e2d...fa0cf6? This is because solidity's sha3 function hashes its inputs based on the argument types. Thus the value 1 will generate a different hash if it is stored as bytes8, bytes16, bytes32, etc. Since sha3(1) is being passed 1 as a number literal, it ...


26

I have been running into this issue as well. The network thinks you are trying to replace an existing unmined transaction. There are two ways to avoid this: Adding a gas price %10 higher than the existing unmined transaction's gas price. Increase your nonce to one higher than the unmined transaction. I have also been getting this error: "Transaction was ...


25

Solution: MyContract = new web3.eth.Contract(abi) Docs https://web3js.readthedocs.io/en/1.0/web3-eth-contract.html#eth-contract When using version 1.0.x you need to pass in the Application Binary Interface, i.e. abi. And use the new keyword.


22

@tayvano: Unfortunately, I can't comment directly. I need "50 reputation" for this. Therefore I will write it as a new answer. I don't know as much about Serpent, but it appears to have the same goals and purpose as Solidity, but is meant to be similar to Python (and therefore be great for Python devs.) This, along with the range of clients, also ...


17

Either use the geth console attached to parity or use the Parity UI application Parity/Web3 Console. To attach Geth console to Parity, (on Linux) use: geth attach ~/.local/share/io.parity.ethereum/jsonrpc.ipc On MacOS use: geth attach ~/Library/Application Support/io.parity.ethereum/jsonrpc.ipc To access the Parity/Web3 Console, open the UI, go to ...


17

I've discovered that not all events are displayed in the truffle output window, although they might have fired correctly with the execution of a contract. I believe this to still be an issue After spending hours on this today I have come up with a solution to test that specific events are fired. Here's my example truffle javascript test: it("should do ...


17

You have some errors, so consider reading the solidity docs. Also, remix is a convenient tool for coding and debugging and coding exercises. I modified your code, as follows: pragma solidity ^0.5.0; contract SimpleEnum { enum SomeData {DEFAULT,ONE,TWO} SomeData someData; constructor() public { someData = SomeData.DEFAULT; } function ...


16

This can be accomplished with a simple wrapper: const promisify = (inner) => new Promise((resolve, reject) => inner((err, res) => { if (err) { reject(err) } resolve(res); }) ); To use it, just wrap your web3 calls as follows: const accounts = await promisify(cb => web3.eth.getAccounts(cb)); If you want to promisify ...


15

The one method that is more reliable than the in-page accounts array is web3.eth.getAccounts(accounts => console.log(accounts[0])). This will asynchronously request the accounts array, and call back whenever it's available.


13

Script To Find Non-Zero Transaction Count In A Range Of Blocks (Update 22/04/2016) Here is a script to check the number of transactions between a starting block number and an ending block number: function checkTransactionCount(startBlockNumber, endBlockNumber) { console.log("Searching for non-zero transaction counts between blocks " + startBlockNumber +...


13

Instead of logging, you can assert the events emitted by a smart contract: npm install --save-dev truffle-test-utils At the top of your test: require('truffle-test-utils').init(); When testing that an event occurred: // Regular call thanks to Truffle let result = await testedSmartContract.testedFunction(); // Check event assert.web3Event(result, { ...


11

Tim: Thanks so much for the pointer. You forced me to finally understand some of the internals of web3.js. I found a cleaner way to do this that covers all the corner cases of the actually fairly complicated log message format (e.g. indexing). I just used SolidityEvent from web3 to do the already-tested work for me. Below is the code. I have this code ...


11

Script To Find Mined Blocks And Uncles + List Transactions Here's my scripts to check and print blocks, uncles and transactions. This was originally written as an answer to the question How do I know if a submitted block became an uncle? . I've listed them separately for easier reading. If you intend to use it in geth, you would probably want to ...


11

You need to send JSON-RPC request to testrpc. Here is a helper function to call it from console: truffle(development)> const increaseTime = addSeconds => { web3.currentProvider.send({ jsonrpc: "2.0", method: "evm_increaseTime", params: [addSeconds], id: 0 }) } Now you can use it to adjust time of the next block: ...


10

RPC over HTTP is inherently unsafe, as any browser tab may access your server. Because of this, any functionality that was deemed unsafe for public consumption is not available by default on the HTTP interface. You have two options: you can either force enable the things you're missing via --rpcapi, just make sure you're clear with the security consequences....


10

FAST way to scan blocks for transactions on an account View Node.JS source code or its sample output. This is an asynchronous scanner, it creates 200 threads to scan through looking for any/all transactions related to your account number. You can easily change what happens when your transaction is found, currently it just prints to console so you can see ...


10

This doesn't exist in truffle-contract. Here's a quick way to wrap web3 calls to make them use promises. const promisify = (inner) => new Promise((resolve, reject) => inner((err, res) => { if (err) { reject(err) } resolve(res); }) ); const getBalance = (account, at) => promisify(cb => web3.eth.getBalance(account, at,...


10

The contract constructor will return a transaction hash where the contract is being deployed. The final contract address can be deterministically determined from deployer address and deployer address nonce (see another answer). This information is also available through web3.eth.getTransactionReceipt after the deployment transaction has been mined. Note ...


10

Here's what I use in my Dapp. It seems to work pretty well. function getWeb3(callback) { if (typeof window.web3 === 'undefined') { // no web3, use fallback console.error("Please use a web3 browser"); } else { // window.web3 == web3 most of the time. Don't override the provided, // web3, just wrap it in your Web3. var myWeb3 = new ...


10

Sending ether is a type of transaction. A transaction is a set of instructions for modifying the state of the blockchain (e.g., transferring ether or calling a non-constant function of a contract -- such as a token-transfer function). If we consider only things like token transfers and ether transfers (for the sake of analogy), a "transaction" is like a bank ...


9

Transfer the ENTIRE balance from one account to another /** * Transfer the ENTIRE BALANCE from one account to another. * * Before you call this, you must unlock your account: * personal.unlockAccount(from) * * @see https://github.com/ethereum/go-ethereum/issues/1637 * @see https://github.com/ethereum/go-ethereum/issues/2173 */ function ...


9

Solidity use HEX values internally. > web3.sha3(web3.toHex(1)) "5fe7f977e71dba2ea1a68e21057beebb9be2ac30c6410aa38d4f3fbe41dcffd2"


9

Where did your web3 object come from? It needs to be constructed with a Provider that tells it how to connect to an Ethereum node, like this: var web3 = new Web3(new Web3.providers.HttpProvider('http://localhost:8545'));


9

According to my observation. We need to wait for a while to see the watched event result for first time watch . Solidity contract CrowdFunding { event DeadlineSet( address indexed _from, uint deadline, uint timeNow ); uint deadline; function withdrawal() returns(bool) { DeadlineSet(msg.sender,deadline,...


9

You can install a block filter and only update balances when a new block arrives. const Web3 = require('web3'); const web3 = new Web3(new Web3.providers.HttpProvider("http://localhost:8545")); const address = "0x9b...."; let balance = web3.eth.getBalance(address); const filter = web3.eth.filter('latest'); filter.watch((err, res) => { if (err) { ...


8

Solidity's sha3 function hashes the byte representation of a uint. That is, the number in hex (base 16), padded to 32 bytes. 32 empty bytes in hex representation is 64 zeros. To do this in JS, we can utilize the infamous left-pad package: const jsHashWeb3 = web3.sha3(leftPad((1).toString(16), 64, 0), { encoding: 'hex' }) // ...


Only top voted, non community-wiki answers of a minimum length are eligible