Last active
July 31, 2018 08:07
-
-
Save lionello/17602e11a725827df4c0e549ff9b408a to your computer and use it in GitHub Desktop.
Small JS template for writing Ethereum/Solidity tests
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/* 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