smartcontractkit / full-blockchain-solidity-course-js

Learn Blockchain, Solidity, and Full Stack Web3 Development with Javascript
11.94k stars 2.89k forks source link

How to fix `Invalid callback object specfied` error when deploying a smart contract? #6229

Open JYOBALA opened 8 months ago

JYOBALA commented 8 months ago

Lesson

Other (please describe)

Could you please leave a link to the timestamp in the video where this error occurs? (You can right click a video and "copy video URL at current time")

No response

Operating System

Windows

Describe the bug

// SPDX-License-Identifier: MIT 
pragma solidity 0.8.19;

contract Inbox {

    string public message;

    constructor (string memory initiialMessage) {
        message = intialMessage;
    }
}

My smart contract

JYOBALA commented 8 months ago
// compile code will go here
const path = require('path');
const fs = require('fs');
const solc = require('solc');

const inboxPath = path.resolve(__dirname, 'contracts', 'Inbox.sol');
const source = fs.readFileSync(inboxPath, 'utf-8');

console.log(solc.compile(source, 1))

{
  "name": "inbox",
  "version": "1.0.0",
  "description": "",
  "main": "index.js",
  "scripts": {
    "test": "mocha"
  },
  "author": "",
  "license": "ISC",
  "dependencies": {
    "@truffle/hdwallet-provider": "^2.1.12",
    "ganache": "^7.8.0",
    "ganache-cli": "^6.12.2",
    "mocha": "^10.2.0",
    "solc": "0.8.19",
    "web3": "^4.1.2"
  }
}

// contract test code will go here
const assert = require('assert');
const ganache = require('ganache-cli');
const { Web3 } = require('web3');
const web3 = new Web3(ganache.provider());
const {abi, evm} = require('../compile');

let accounts;
let inbox;

beforeEach(async () => {
    // Get a list of accounts 
    accounts = await web3.eth.getAccounts();

    // Use one of the accounts to deploy contract 
    inbox = await new web3.eth.Contract(abi)
    .deploy({
        data: evm.bytecode.object, 
        arguments: ['Halo']
    })
    .send({from: accounts[0], gas: '1000000'});
})

describe('Inbox', () => {
    it('deploy contract', () => {
        console.log(inbox);
    })
})

My test

JYOBALA commented 8 months ago
C:\Users\ASUS ROG\Downloads\inbox>npm run test

> inbox@1.0.0 test
> mocha

AssertionError [ERR_ASSERTION]: Invalid callback object specified.
    at runWithCallbacks (C:\Users\ASUS ROG\Downloads\inbox\node_modules\solc\bindings\compile.js:120:30)
    at Object.boundFunctionStandard [as compileStandard] (C:\Users\ASUS ROG\Downloads\inbox\node_modules\solc\bindings\compile.js:83:20)
    at compileStandardWrapper (C:\Users\ASUS ROG\Downloads\inbox\node_modules\solc\wrapper.js:66:24)
    at Object.<anonymous> (C:\Users\ASUS ROG\Downloads\inbox\compile.js:9:18)
    at Module._compile (node:internal/modules/cjs/loader:1256:14)
    at Object.Module._extensions..js (node:internal/modules/cjs/loader:1310:10)
    at Module.load (node:internal/modules/cjs/loader:1119:32)
    at Function.Module._load (node:internal/modules/cjs/loader:960:12)
    at Module.require (node:internal/modules/cjs/loader:1143:19)
    at require (node:internal/modules/cjs/helpers:119:18)
    at Object.<anonymous> (C:\Users\ASUS ROG\Downloads\inbox\test\Inbox.test.js:6:20)
    at Module._compile (node:internal/modules/cjs/loader:1256:14)
    at Object.Module._extensions..js (node:internal/modules/cjs/loader:1310:10)
    at Module.load (node:internal/modules/cjs/loader:1119:32)
    at Function.Module._load (node:internal/modules/cjs/loader:960:12)
    at ModuleWrap.<anonymous> (node:internal/modules/esm/translators:169:29)
    at ModuleJob.run (node:internal/modules/esm/module_job:194:25)

And this is the error