Hi @Sarah,
Unfortunately I wasn’t able to reproduce the error. I cut down the sample code so it was just deploying an empty contract and the ERC1820Registry.
I am running on Windows Subsystem for Linux
$ node --version
v10.16.0
$ npm --version
6.13.0
When I ran the script (after running ganache-cli -d
in a separate terminal) I can see the token deployed in ganache-cli
$ node index.js
token deployed at 0x32Cf1f3a98aeAF57b88b3740875D19912A522c1A
Output in ganache-cli
:
Transaction: 0xfefb2da535e927b85fe68eb81cb2e4a5827c905f78381a01ef2322aa9b0aee8e
Contract created: 0x1820a4b7618bde71dce8cdc73aab6c95905fad24
Gas usage: 711453
Block Number: 2
Block Time: Tue Nov 19 2019 14:29:17 GMT+1100 (Australian Eastern Daylight Time)
package.json
{
"name": "lucky",
"version": "1.0.0",
"description": "",
"main": "index.js",
"scripts": {
"test": "echo \"Error: no test specified\" && exit 1"
},
"keywords": [],
"author": "",
"license": "MIT",
"dependencies": {
"@openzeppelin/contracts": "^2.4.0",
"@openzeppelin/test-helpers": "^0.5.4",
"chai": "^4.2.0",
"ganache-core": "^2.8.0",
"solc": "^0.5.12",
"web3": "^1.2.1"
}
}
index.js
const helper = require('./_helper');
const { singletons } = require('@openzeppelin/test-helpers');
(async () => {
var { deploy, accounts, web3 } = await helper.testHelper(`./contracts/`); //
//Deploy token contract
let arr = [accounts[1]]
await singletons.ERC1820Registry(accounts[1]);
let Token = await deploy('Token',{ from: accounts[1], args: ["sahar", "sah", arr]});
console.log('token deployed at', Token._address)
})().catch(e => {
console.log(e)
});
_helper.js
const fs = require ('fs');
const solc= require('solc');
const linker= require('solc/linker');
const Ganache= require('ganache-core');
const Web3= require('web3');
var solcOpts = {
language: 'Solidity',
settings: {
metadata: { useLiteralContent: true },
outputSelection: {
'*': {
'*': ['abi', 'evm.bytecode.object']
}
}
}
}
// Instantiate a web3 instance. Start a node if one is not already running.
async function web3Helper(provider = 'ws://127.0.0.1:8545') {
var web3 = new Web3(provider);
var instance = await server(web3, provider)
return { web3, server: instance }
}
module.exports.web3Helper = web3Helper;
function findImportsPath(prefix) {
return function findImports(path) {
try {
return {
contents: fs.readFileSync(prefix + path).toString()
}
} catch (e) {
return { error: 'File not found' }
}
}
}
async function compile(contractpath,contractName)
{
var web3 = new Web3("ws://"+process.env.GANACHE_HOST+":"+process.env.GANACHE_PORT);
var sources = {
[contractName]: {
content: fs.readFileSync(`${contractpath}/${contractName}.sol`).toString()
}
}
var compileOpts = JSON.stringify({ ...solcOpts, sources })
// Compile the contract using solc
var rawOutput = solc.compileStandardWrapper(
compileOpts,
findImportsPath(contractpath)
)
var output = JSON.parse(rawOutput)
// If there were any compilation errors, throw them
if (output.errors) {
output.errors.forEach(err => {
if (!err.formattedMessage.match(/Warning:/)) {
throw new SyntaxError(err.formattedMessage)
}
})
}
var { abi, evm: { bytecode } } = output.contracts[contractName][
contractName
]
return {abi,web3};
}
module.exports.compile = compile;
async function testHelper(contracts, provider) {
const { web3, server } = await web3Helper(provider)
const accounts = await web3.eth.getAccounts()
async function deploy(contractName, { from, args, log }) {
var sources = {
[contractName]: {
content: fs.readFileSync(`${contracts}/${contractName}.sol`).toString()
}
}
var compileOpts = JSON.stringify({ ...solcOpts, sources })
// Compile the contract using solc
var rawOutput = solc.compileStandardWrapper(
compileOpts,
findImportsPath(contracts)
)
var output = JSON.parse(rawOutput)
// If there were any compilation errors, throw them
if (output.errors) {
output.errors.forEach(err => {
if (!err.formattedMessage.match(/Warning:/)) {
throw new SyntaxError(err.formattedMessage)
}
})
}
var { abi, evm: { bytecode } } = output.contracts[contractName][
contractName
]
// Deploy linked libraries
for (let linkedFile in bytecode.linkReferences) {
for (let linkedLib in bytecode.linkReferences[linkedFile]) {
let libObj = output.contracts[linkedFile][linkedLib]
let LibContract = new web3.eth.Contract(libObj.abi)
var libContract = await LibContract.deploy({
data: libObj.evm.bytecode.object
}).send({
from,
gas: 900000000
})
let libs = { [`${linkedFile}:${linkedLib}`]: libContract._address }
bytecode.object = linker.linkBytecode(bytecode.object, libs)
}
}
if (!bytecode.object) {
throw new Error(
'No Bytecode. Do the method signatures match the interface?'
)
}
if (process.env.BUILD) {
fs.writeFileSync(
__dirname + '/../src/contracts/' + contractName + '.js',
'module.exports = ' +
JSON.stringify(
{
abi,
data: bytecode.object
},
null,
4
)
)
}
// Instantiate the web3 contract using the abi and bytecode output from solc
var Contract = new web3.eth.Contract(abi)
var contract
await new Promise(async resolve => {
var chainId = web3.eth.net.getId()
var data = await Contract.deploy({
data: '0x' + bytecode.object,
arguments: args
}).encodeABI()
web3.eth
.sendTransaction({
data,
from,
value: 0,
gas: 4612388,
chainId
})
.once('transactionHash', hash => {
if (log) {
console.log('Transaction Hash', hash)
}
})
.once('receipt', receipt => {
if (log) {
console.log(
`Deployed ${contractName} to ${receipt.contractAddress} (${
receipt.cumulativeGasUsed
} gas used)`
)
}
})
.catch('error', err => {
console.log('eeeeeeeeeeeeeeeeeeeeeeeeeee', err)
resolve()
})
.then(instance => {
contract = new web3.eth.Contract(abi, instance.contractAddress)
resolve()
})
})
if (contract) {
// Set some default options on the contract
contract.options.gas = 1500000
contract.options.from = from
}
return contract
}
return { web3, accounts, deploy, server}
}
// Start the server if it hasn't been already...
async function server(web3, provider) {
try {
// Hack to prevent "connection not open on send" error when using websockets
web3.setProvider(provider.replace(/^ws/, 'http'))
await web3.eth.net.getId()
web3.setProvider(provider)
return
} catch (e) {
/* Ignore */
}
var port = '7545'
if (String(provider).match(/:([0-9]+)$/)) {
port = provider.match(/:([0-9]+)$/)[1]
}
var server = Ganache.server()
await server.listen(port)
return server
}
module.exports.testHelper = testHelper;
Token.sol
pragma solidity ^0.5.0;
contract Token {
}