Solidity v^0.5.0 ошибка компилятора [указан неверный обратный вызов]
Я пытаюсь скомпилировать свой контракт, но получаю эту ошибку:
AssertionError [ERR_ASSERTION]: Invalid callback specified.
Одним из ответов было изменение версии компилятора, но моя версия актуальна (0.5.0). Я на самом деле пытаюсь взять старый код (0.4.17) и обновить его. Пробовал в течение 2 дней и просто продолжал терпеть неудачу.
Вот мой контракт:
pragma solidity ^0.5.0;
contract Lottery{
address public manager;
address payable [] public players;
modifier restricted {
require(msg.sender == manager);
_;
}
constructor() public {
manager = msg.sender;
}
function participate() public payable {
require(msg.value > .01 ether);
players.push(msg.sender);
}
function pseudoRandom() private view returns(uint){
return uint(keccak256(abi.encodePacked(block.difficulty, now, players)));
}
function pickWinner() public restricted {
require(msg.sender == manager);
uint index = pseudoRandom() % players.length;
address(players[index]).transfer(address(this).balance);
(players) = new address payable[](0);
}
function getPlayers() public view returns(address payable[] memory){
return players;
}
}
вот мой package.json:
{
"name": "lottery",
"version": "1.0.0",
"description": "lottery contract with Solidity",
"main": "compile.js",
"directories": {
"test": "test"
},
"scripts": {
"test": "mocha"
},
"author": "Torof",
"license": "ISC",
"dependencies": {
"ganache-cli": "^6.2.1",
"mocha": "^5.2.0",
"save": "^2.3.2",
"solc": "^0.5.0",
"tar": "^4.4.8",
"truffle": "^4.1.14",
"truffle-hdwallet-provider": "0.0.6",
"web3": "^1.0.0-beta.36"
}
}
а вот и компилятор:
const path = require('path');
const fs = require('fs');
const solc = require('solc'); //Could the error be here ?
const lotteryPath = path.resolve(__dirname, 'contracts', 'Lottery.sol');
const source = fs.readFileSync( lotteryPath, 'utf8');
module.exports = solc.compile(source, 1).contracts[':Lottery'];
console.log(solc.compile(source, 1));
И, наконец, я нашел это сообщение об ошибке, но не получил его:
[ts]
Could not find a declaration file for module 'solc'.
'/home/torof/desk/coding/Udemy/ETH-stephenGrider/lottery/node_modules/solc/index.js'
implicitly has an 'any' type.
Try `npm install @types/solc` if it exists or add a new declaration (.d.ts) file containing `declare module 'solc';`
2 ответа
Предыдущие версии solc
поддерживается используемый вами стиль компиляции, но похоже, что новые версии поддерживают и поддерживают только стандартный JSON. Вы, вероятно, хотите что-то вроде этого:
console.log(JSON.parse(solc.compile(JSON.stringify({
language: 'Solidity',
sources: {
'lottery.sol': {
content: source,
},
},
settings: {
outputSelection: {
'*': {
'*': ['evm', 'bytecode'],
},
},
},
}))).contracts['lottery.sol'].Lottery);
Вот односторонняя реализация 0.5.X вместе с кодом развертывания: Вот как я компилирую, есть глубокое разрушение вложенности для получения байт-кода.
const path = require('path');
const fs = require('fs');
const solc = require('solc');
const templatePath = path.resolve(__dirname, 'contracts', 'templatename.sol');
const source = fs.readFileSync(templatePath, 'utf8');
const input = {
language: 'Solidity',
sources: {
'yourtemplate.sol': {
content: source
}
},
settings: {
outputSelection: {
'*': {
'*': ['*']
}
}
}
}
const { abi: interface, evm: { bytecode: { object } } } = JSON.parse(solc.compile(JSON.stringify(input))).contracts['yourtemplate.sol'].Templatename; //
module.exports = { interface, object }; // object is the actual name of the bytecode
И код для развертывания:
const ganache = require('ganache-cli');
const Web3 = require('web3');
const web3 = new Web3(ganache.provider());
const { interface, object: bytecode } = require('../compile');
// i've renamed object with bytecode
const accounts = await web3.eth.getAccounts();
templatename = await new web3.eth.Contract(interface)
.deploy({ data: bytecode, arguments: [INPUT_PARAMETER_GOES_HERE] })
.send({ from: accounts[0], gas: '1000000' });