Skip to content

Instantly share code, notes, and snippets.

@lionello
Last active July 31, 2018 08:07
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save lionello/17602e11a725827df4c0e549ff9b408a to your computer and use it in GitHub Desktop.
Save lionello/17602e11a725827df4c0e549ff9b408a to your computer and use it in GitHub Desktop.
Small JS template for writing Ethereum/Solidity tests
/* Use this with an overmind Procfile like this:
geth: geth --dev --rpc console
jest: pnpx jest --watchAll
*/
const ChildProcess = require('child_process')
const Web3 = require('web3')
const Assert = require('assert')
function addressOf(contract) {
return contract.options.address
}
function compile(filename) {
const solc = ChildProcess.spawnSync('solc', ['--combined-json=abi,bin', filename])
if (solc.status !== 0) throw Error(solc.stderr.toString())
return JSON.parse(solc.stdout)
}
function deploy(contractInfo, from, ...args) {
const abi = JSON.parse(contractInfo.abi)
return new web3.eth.Contract(abi).deploy({data: '0x' + contractInfo.bin, arguments: args}).send({from, gas: 2e6})
}
let web3 = new Web3('http://localhost:8545')
test('Wait for RPC', async () => {
while (true) {
try {
return await web3.eth.getAccounts()
}
catch(err) {
await new Promise(resolve => setTimeout(resolve, 100))
}
}
})
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment