Web3js

為什麼部署合約返回屬性contract.options.address null

  • August 21, 2021

為什麼部署合約返回屬性contract.options.address null。

收件箱.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
   });
});

包.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"
 }
}

收件箱.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;
   }*/
}

編譯.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;

我必須在 Inbox.test.sol 中部署我的合約,錯過它將地址傳遞給建構子,如下一個

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

引用自:https://ethereum.stackexchange.com/questions/107040