Skip to content

Instantly share code, notes, and snippets.

@adrianmcli
Last active September 29, 2022 14:33
Show Gist options
  • Save adrianmcli/2c9dff19b81a36c52b9284c71fe3a4c0 to your computer and use it in GitHub Desktop.
Save adrianmcli/2c9dff19b81a36c52b9284c71fe3a4c0 to your computer and use it in GitHub Desktop.
Programmatically compile and deploy Solidity smart contracts for your tests
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;
/**
* @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");
});
});
@adrianmcli
Copy link
Author

adrianmcli commented Dec 21, 2018

If you want to also output the JSON artifact into a .json file, then add the following to the end of the compile function (before the return statement):

const artifactFilepath =
  sourcePath.substr(0, sourcePath.lastIndexOf(".")) + `.json`;
const artifactString = JSON.stringify(artifact, null, 4);
fs.writeFileSync(artifactFilepath, artifactString, "utf8");

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment