Solidity
無法解構“未定義”或“空”的屬性“介面”。如何解決這個問題?
const assert = require('assert'); const ganache = require('ganache-cli'); const Web3 = require('web3'); const provider = ganache.provider(); const web3 = new Web3(provider); const { interface, bytecode } = require('../compile'); let accounts; let inbox; beforeEach(async () => { // Get a list of all accounts accounts = await web3.eth.getAccounts(); // Use one of those accounts to deploy the contract inbox = await new web3.eth.Contract(JSON.parse(interface)) .deploy({ data: bytecode, arguments: ['Hi there!'] }) .send({ from: accounts[0], gas: '1000000' }); inbox.setProvider(provider); }); describe('inbox', () => { it('deploys a contract', () => { assert.ok(inbox.options.address); }); it('has a default message', async () => { const message = await inbox.methods.message().call(); assert.equal(message, 'Hi there!'); }); });
錯誤在於
require('../compile')
肯定,因為它必須返回空值。嘗試參考下面的程式碼來編譯solidity程式碼。檢查您的 compile.js 文件是否以類似的方式工作。
// Compile the source code const input = fs.readFileSync('Coin.sol'); const output = solc.compile(input.toString(), 1); const bytecode = output.contracts[':Coin'].bytecode; const abi = JSON.parse(output.contracts[':Coin'].interface);
確保你
const abi = JSON.parse(output.contracts[':Coin'].bytecode);
在“compile.js”中得到了“Coin.sol”的“C”,因為這就是它的儲存方式