Hot answers tagged

73

Creating a private testnet for Ethereum is actually pretty easy. In recent versions of geth, you don't even need to create your own genesis block, or any other complicated steps (many of the online tutorials are out of date). First, make sure you have geth installed. On OSX, you would do this by first installing Homebrew, then running: $ brew tap ethereum/...


48

One way is to use Remix, the browser-based Solidity realtime compiler and runtime Solidity (formerly named browser-solidity). Paste the code in Remix. Wrap the function inside a contract if needed. (Example: contract C { ... }.) Click Contract tab and then Create to instantiate the contract. Enter the desired input next to the function. For bytes1 ... ...


36

There are two relevant RPC methods here: evm_increaseTime and evm_setNextBlockTimestamp. In both cases, they affect the next block but don't mine one. evm_increaseTime receives a number of seconds that will be added to the timestamp of the latest block. evm_setNextBlockTimestamp receives an absolute UNIX timestamp (again, in seconds), and so it's not ...


30

Via a Framework There are currently four primary frameworks available that can facilitate writing unit tests for your contracts. Listed in order of Github stars as of 2016-01-25. Disclaimer: I'm the author of the Populus testing library. Embark website: https://iurimatias.github.io/embark-framework/ written in: javascript tests: javascript via Mocha ...


25

As of November 20 2016, the "official" testnet is now Ropsten. Geth 1.5.3 has been released. Simply run geth --testnet: With 1.5.3 --testnet now selects the Ropsten network. If you have a blockchain database for the Morden network, run geth --testnet removedb to remove it There is no fixed schedule when they are reset. Some faucets are: http://...


24

The Remix IDE has a great integrated block-by-block simulator and debugger. Ganache allows fast contract testing without the hassle of running a node. Once the contract is live, the etherscan.io block explorer can be used to view internal state and EVM execution logs.


23

What is Truffle? Truffle is a development environment, testing framework and asset pipeline for Ethereum, aiming to make life as an Ethereum developer easier. It is one of the most widely used IDEs in the Ethereum community. Developers can use it to build and deploy DApps for testing purposes with many features that make it more attractive to users with a ...


22

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 ...


22

I wrote the truffle-assertions package just for this. It has an assertion to check that an event has been emitted. Essentially, it is similar to the answer below, but it does not need to check a specific index of the logs, and it has the option add complex conditions in a straightforward way by passing a filter function. npm install truffle-assertions You ...


21

I would use the analogy of unit testing vs integration testing to distinguish these two methods. In my opinion, a Truffle solidity test can be used to cover a small piece of code, basically you will be able to test every single function in your contract in an isolated way (isolated from Web3 essentially). On the other hand, a Truffle Javascript test (Mocha)...


19

All it needed was to add a package.json file to the project directory with some babel dependencies and doing an 'npm install'. Also, adding a '.babelrc' file to the truffle project directory. Finally, adding some requires to truffle.js file. package.json file { "name": "game-token", "devDependencies": { "babel-preset-es2015": "^6.18.0", "babel-...


19

Try contract('CreeptomaPresale', function(accounts) { describe("adopted over allow quantity", function () { it("test get balance", async function () { let instance = await CreeptomaPresale.deployed(); console.log("deployed address:" + instance.address); let balance = await web3....


18

Just to clarify: You would like to run a geth node starting up with the accounts all locked as per the default You later want to run a 'geth attach' command to unlock one or more accounts for a period of time You can use use the following command to attach to your geth node to unlock an account using the personal.unlockAccount(...) JavaScript API (https://...


18

On top of Rosco's and Nick's answers, it's also helpful to be aware of the --show-events flag for debugging. Use it like this: truffle test --show-events


17

Embark is a development framework. It's used to make development faster and more streamlined by providing a set of tools / functionalities to make you more productive. In the case of embark it makes/will make the process of creating a dapp faster by simplifying the interaction between your app front end and the contract (e.g. running your own blockchain for ...


17

I personally use Dapple for writing Solidity unit tests. It has the advantage of not requiring a Javascript testing framework layer, so you can stay entirely in Solidity. Plus I can use it to test contracts that use imports, which I'm not sure is possible in chriseth's browser-based Solidity. Edit: The accepted answer is definitely a better option for ...


17

You can use OpenZeppelin's expectThrow helper - Source: https://github.com/OpenZeppelin/zeppelin-solidity/blob/master/test/helpers/expectThrow.js export default async promise => { try { await promise; } catch (error) { // TODO: Check jump destination to destinguish between a throw // and an actual invalid jump. ...


16

Truffle is a development environment, testing framework and asset pipeline for Ethereum, aiming to make life as an Ethereum developer easier. With Truffle, you get: Built-in smart contract compilation, linking, deployment and binary management. Automated contract testing with Mocha and Chai. Configurable build pipeline with support for custom build ...


16

If they are internal functions you can inherit them and test them, if they are private functions, I believe the only way to unit test them is to make them public/internal, test them and then change them back to private once the tests are passing. Another way to go about it would be to refactor your code so that the private functions are part of a library ...


15

Those are all good tools, but I have found the logging events is much easier method for debugging, plus if you are doing anything with money you want to keep a record of what happened. In Solidity, you log with Events. You can then listen to all events on your contract with web3.js "allEvents" callback This is how I debug my (currently) 780 lines of ...


15

If you are just testing contracts I would recommend using a lightweight Ethereum node like https://github.com/trufflesuite/ganache-cli. This way getting your network set up is as simple as running: npm install -g ganache-cli Once it is installed, you can start the testnet with: ganache-cli This Ethereum Node simulator will have a few accounts which are ...


15

Some of the main steps involve specifying a networkId, and creating a genesis file (you can give your account a lot of ether!). Rather than repeat what's been written, there are guides here and here.


14

There are a few ways to do testing of Solidity contracts. The easiest, at least in my opinion, is blackbox testing with Truffle. Contracts tend to be relatively small and self-contained, so blackbox testing seems appropriate here. Truffle lets you write unit tests in Javascript using Pudding, an extension of web3, Mocha and Chai. A typical test looks like ...


14

The Solution Set up 2 connected geth instances on your private network and start them mining. There is an admin.addPeer(...) command in geth to add peer nodes to the geth's list of peers, but there does not seem to be any commands to remove these peers. To simulate the fork, block the network ports used by both instances of geth for their peer-to-peer ...


14

Not really an answer to what you're asking, but... Decreasing the size of a uint to less that 256 bits will increase the associated gas costs, not decrease it like you might be expecting. It'll be best to leave it as a uint256. See: Why does uint8 cost more gas than uint256? Edit: Noting that timestamps are the number of seconds since the Unix epoch: ...


14

This can be done with the ganache-cli db command. For example, to run a setup similar to the GUI client, I do ganache-cli --db="./data/save/" -i="5777" -d --mnemonic="YOUR_12_WORDS_HERE" Which will dave the network data to ./data/save directory. If the data is already present in that directory, it will return to the same state (provided deterministic and ...


13

You can write a JavaScript script that controls geth's behavior related to mining. There is a simple script that mines only nonempty blocks on go-ethereum wiki. Another one is mine.js script from Embark Framework. This one is feature-rich and configurable for periodic mining, mining reward cap, number of mining threads and more (via glider). Scripts are ...


13

Add the following to your configurations in launch.json: "configurations": [ { "name": "run tests", "type": "node", "request": "launch", "program": "${workspaceRoot}/node_modules/truffle/build/cli.bundled.js", "args": ["test"], "cwd": "${workspaceRoot}", "outFiles": [...


12

B9lab's IPFS faucet with command line access B9lab have one deployed via IPFS. this is pretty neat. long description below, here is the one-liner for rushed users: curl -X POST -H "Content-Type: application/json" -d '{"toWhom":"0xdcf407eae88d480e280db2d0deaa3a11c82eaa9b"}' https://ropsten.faucet.b9lab.com/tap Replace 0xdcf407... with your testnet ...


12

If you're just looking for a really quick way to test code and play in the browser, you can use https://ethfiddle.com/ (Full disclosure: I'm part of the team who built it.) I found Remix's UI to be complicated and overkill in a lot of cases, when the majority of the time all I wanted to do was 1) quickly run and test some contract code I found online, and ...


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