1

Why contract deployed return property contract.options.address null.

Inbox.test.sol

const assert = require('assert');
const ganache = require('ganache-cli');
const { send } = require('process');
const Web3 = require('web3');
const provider = ganache.provider();
const web3 = new Web3(provider);
//var Contract = require('web3-eth-contract');
const { interface, bytecode } = require('../compile');

//let a = web3.transactionConfirmationBlocks;
//let b = web3.eth.transactionConfirmationBlocks;
//let c = web3.shh.transactionConfirmationBlocks; 

//console.log(a);
//console.log(b);
//console.log(c);

class Car {
    park() {
        return 'stopped';
    }
    drive() {
        return 'vroom';
    }
}
let accounts;
let inbox;
beforeEach(async () => {
    accounts = await web3.eth.getAccounts();
    var ad = accounts[0];
    inbox = await new web3.eth.Contract(interface);
    inbox.deploy({data:'0x'+bytecode,arguments:['Hi there!']})
    .send({from:ad,gas:1000000});
    inbox.setProvider(provider);
});

describe('Inbox', () => {
    it('deploy a contract', () => {
        console.log(inbox.options.address);//null
    });
});

package.json

{
  "name": "inbox",
  "version": "1.0.0",
  "description": "",
  "main": "index.js",
  "scripts": {
    "test": "mocha --timeout 10000"
  },
  "author": "",
  "license": "ISC",
  "dependencies": {
    "ganache-cli": "^6.12.2",
    "mocha": "^9.0.3",
    "solc": "^0.8.6",
    "web3": "^1.5.1"
  }
}

Inbox.sol

// SPDX-License-Identifier: GPL-3.0
pragma solidity ^0.8.4;

contract Inbox{
    string public message;
    
    constructor(string memory initialMessage) {
        message = initialMessage;
    }
    
    function setMessage(string memory newMessage) public{
        message = newMessage;
    }
    /*function getMessage() public view returns(string memory){
        return message;
    }*/
}

compile.js

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));
//module.exports = solc.compile(source,1).contracts[':Inbox'];
var input = {
    language: 'Solidity',
    sources: {
        'Inbox.sol' : {
            content: source
        }
    },
    settings: {
        outputSelection: {
            '*': {
                '*': [ '*' ]
            }
        }
    }
};
//console.log(JSON.parse(solc.compile(JSON.stringify(input))));
//console.log(JSON.parse(solc.compile(JSON.stringify(input))).contracts);
//module.exports = JSON.parse(solc.compile(JSON.stringify(input))).contracts['Inbox.sol'].Inbox.evm;
//console.log(module.exports);
//console.log(module.exports.contracts.sources);
//module.exports = solc.compile(JSON.stringify(input));
//console.log(JSON.parse(solc.compile(JSON.stringify(input))));
function findImports (path) {
    if (path === 'Inbox.sol')
        return { contents: source }
    else
        return { error: 'File not found' }
}
//console.log('abi',JSON.parse(solc.compile(JSON.stringify(input))).contracts['Inbox.sol']['Inbox'].abi);
//console.log('object',JSON.parse(solc.compile(JSON.stringify(input))).contracts['Inbox.sol']['Inbox'].evm.bytecode.object);
//const outpucleart = JSON.parse(solc.compile(JSON.stringify(input), findImports));
//console.log(output);
//console.log(JSON.parse(solc.compile(JSON.stringify(input))).contracts);
const val = JSON.parse(solc.compile(JSON.stringify(input))).contracts['Inbox.sol']['Inbox'];
console.log('val',val);
module.exports.interface = val.abi;
//module.exports.interface = source;
module.exports.bytecode = val.evm.bytecode.object;

1 Answer 1

0

I got to deploy my contract, in Inbox.test.sol, miss it pass address to constructor like next

inbox = await new web3.eth.Contract(interface,ad);

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.