Skip to content

Instantly share code, notes, and snippets.

@androolloyd
Last active February 4, 2019 17:05
Show Gist options
  • Save androolloyd/83f753042712b20201a8fbc5ab8336af to your computer and use it in GitHub Desktop.
Save androolloyd/83f753042712b20201a8fbc5ab8336af to your computer and use it in GitHub Desktop.
Trufflesuite Web3js 1.0 Test Helpers
const Web3 = require("web3") // import web3 v1.0 constructor
const getWeb3 = () => {
const myWeb3 = new Web3(web3.currentProvider)
return myWeb3
}
const getContractInstance = (web3) => async (contractName, opts) => {
opts = opts || {
localABI: null,
create: false,
constructorArgs: null,
deployLookup: false,
deployedAddress: null,
};
if (opts.localABI) {
return new web3.eth.Contract(opts.localABI, "0x72fE0d6A3E4CB16918A1c658f0856f3D9c64e3d4");
}
const artifact = artifacts.require(contractName);
artifact.defaults({
gas:8000000
})
let instance = null;
let newArtifact = null;
if (opts.deployedAddress) {
instance = new web3.eth.Contract(artifact.abi, opts.deployedAddress);
} else if (opts.deployLookup) {
instance = new web3.eth.Contract(artifact.abi, artifact.networks[artifact.network_id].address);
} else if (opts.create) {
if (opts.constructorArgs) {
if (Array.isArray(opts.constructorArgs)) {
if(opts.constructorArgs.length === 1) {
newArtifact = await artifact.new(opts.constructorArgs[0]);
} else {
newArtifact = await artifact.new.apply(artifact, opts.constructorArgs);
}
} else {
newArtifact = await artifact.new(opts.constructorArgs);
}
} else {
newArtifact = await artifact.new();
}
instance = new web3.eth.Contract(artifact.abi, newArtifact.address);
}
return instance;
}
const create2Address = (creatorAddress, saltHex, byteCode) => {
return `0x${web3.utils.sha3(`0x${[
'ff',
creatorAddress,
saltHex,
web3.utils.sha3(byteCode)
].map(x => x.replace(/0x/, ''))
.join('')}`).slice(-40)}`.toLowerCase()
}
// converts an int to uint256
const numberToUint256 = (value) => {
const hex = value.toString(16)
return `0x${'0'.repeat(64 - hex.length)}${hex}`
}
// encodes parameter to pass as contract argument
const encodeParam = (dataType, data) => {
return web3.eth.abi.encodeParameter(dataType, data)
}
// returns true if contract is deployed on-chain
const isContract = async (address) => {
const code = await web3.eth.getCode(address)
return code.slice(2).length > 0
}
const getStorageAt = async (address, slot = 0) => {
const storage = await web3.eth.getStoragetAt(address, slot)
return storage;
}
module.exports = {
getWeb3,
getStorageAt,
isContract,
encodeParam,
getContractInstance: getContractInstance,
create2Address: create2Address,
num2uint: numberToUint256
}
const {getContractInstance, create2Address, num2uint} = require("./helpers")
const getInstance = getContractInstance(web3)
contract('YOUR_SMART_CONTRACT', async () => {
...
let localContractVar = await getInstance("ContractName", {create: true});
let localContractVar2 = await getInstance("ContractName", {deployedAddress:"ContractName_DEPLOYED_ADDRESS"});
let localContractVar3 = await getInstance("ContractName", {deployLookup:true});
let contractNameABI = [...ABI...];
let localContractVar4 = await getInstance("ContractName", {localABI: contractNameABI});
let localContractVar5 = await getInstance("ContractName", {create: true, constructorArgs:[arg1, arg2, arg3...]});
let localContractVar6 = await getInstance("ContractName", {create: true, constructorArgs:[arg1, arg2, arg3...]});
});
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment