Last active
September 29, 2022 14:33
-
-
Save adrianmcli/2c9dff19b81a36c52b9284c71fe3a4c0 to your computer and use it in GitHub Desktop.
Programmatically compile and deploy Solidity smart contracts for your tests
This file contains 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
const path = require("path"); | |
const fs = require("fs"); | |
const TruffleCompile = require("truffle-compile"); | |
// Promisify truffle-compile | |
const truffleCompile = (...args) => | |
new Promise(resolve => TruffleCompile(...args, (_, data) => resolve(data))); | |
const compile = async filename => { | |
const sourcePath = path.join(__dirname, "../contracts", filename); | |
const sources = { | |
[sourcePath]: fs.readFileSync(sourcePath, { encoding: "utf8" }), | |
}; | |
const options = { | |
contracts_directory: path.join(__dirname, "../contracts"), | |
compilers: { | |
solc: { | |
version: "0.5.2", | |
settings: { | |
optimizer: { | |
enabled: false, | |
runs: 200, | |
}, | |
evmVersion: "byzantium", | |
}, | |
}, | |
}, | |
}; | |
const artifact = await truffleCompile(sources, options); | |
return artifact; | |
}; | |
module.exports = compile; |
This file contains 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
/** | |
* @jest-environment node | |
*/ | |
const Ganache = require("ganache-core"); | |
const Web3 = require("web3"); | |
const compile = require("./compile"); | |
jest.setTimeout(20000); // provide enough time to download compiler if not cached | |
describe("test stuff", () => { | |
let contractInstance; | |
let accounts; | |
beforeAll(async () => { | |
// 1. Compile contract artifact | |
const { SimpleStorage } = await compile("SimpleStorage.sol"); | |
// 2. Spawn Ganache test blockchain | |
const provider = Ganache.provider({ seed: "drizzle-utils" }); | |
const web3 = new Web3(provider); | |
accounts = await web3.eth.getAccounts(); | |
// 3. Create initial contract instance | |
const instance = new web3.eth.Contract(SimpleStorage.abi); | |
// 4. Deploy contract and get new deployed Instance | |
const deployedInstance = await instance | |
.deploy({ data: SimpleStorage.bytecode }) | |
.send({ from: accounts[0], gas: 150000 }); | |
// Note: deployed address located at `deployedInstance._address` | |
contractInstance = deployedInstance; | |
}); | |
test("ensure test suite setup works", async () => { | |
// Get old value, set a new value, check to see if it was set correctly | |
const oldVal = await contractInstance.methods.get().call(); | |
await contractInstance.methods.set(5).send({ from: accounts[0] }); | |
const newVal = await contractInstance.methods.get().call(); | |
expect(oldVal).toBe("0"); | |
expect(newVal).toBe("5"); | |
}); | |
}); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
If you want to also output the JSON artifact into a
.json
file, then add the following to the end of thecompile
function (before thereturn
statement):