【问题标题】:How to compile a solidity file using solc? (Version 0.8.15, node.js)如何使用 solc 编译 Solidity 文件? (版本 0.8.15,node.js)
【发布时间】:2022-07-12 14:01:51
【问题描述】:

我一直在尝试编译我使用 solc 创建的简单合约,但没有成功。

合同。

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


contract basic {


modifier onlyOwner {
      require(msg.sender == owner);
      _;
}

address public owner;
address public peer;

address public lastSender;
int public lastAmaunt;

constructor(address p2){
    owner=msg.sender;
    peer=p2;
}

//see the balance of the contract
function getTotalBalance () public view returns (uint){
    return address(this).balance;
}

//only owner can sent money from contract 
function sentMoneyto (uint to,uint256 _value) public onlyOwner{
        if(to==1){
            (payable(peer)).transfer(_value);
        }else{
            (payable(owner)).transfer(_value);
        }
}
//can get money from evreyone 
function getMoneyFrom() public payable{
        lastSender = msg.sender;
        lastAmaunt =int(msg.value);
}

function cangePeer(address newPeer) public onlyOwner{
    peer=newPeer;
}

}

不是一个复杂的合约,它的目的是作为中间人在两个账户之间转移资金。 编译文件:

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, 'utf8');
 
const input = {
    language: 'Solidity',
    sources: {
        'Inbox.sol': {
            content: source,
        },
    },
    settings: {
        outputSelection: {
            '*': {
                '*': ['*'],
            },
        },
    },
};
 
 
compilesFiles = JSON.parse(solc.compile(JSON.stringify(input))).contracts[
    'Inbox.sol'
].Inbox;

console.log(compilesFiles);

当我运行 compile.js 时,我得到以下输出 ...

my screen

我的意图是编译后将输出保存在文件中。

谢谢。

【问题讨论】:

    标签: node.js compilation solidity


    【解决方案1】:

    可能是因为solc 版本,但问题出在下面一行。

    之前:

    compilesFiles = JSON.parse(solc.compile(JSON.stringify(input))).contracts[
    'Inbox.sol'
    

    ].收件箱;

    之后:

    compilesFiles = JSON.parse(solc.compile(JSON.stringify(input))).contracts[
    'Inbox.sol'
    

    ].基本的;

    下面是一个工作示例!

    const path = require('path');
    const fs = require('fs');
    const solc = require('solc');
    const fsExtra = require('fs-extra')
     
    const inboxPath = path.resolve(__dirname, 'contracts', 'Inbox.sol');
    const source = fs.readFileSync(inboxPath, 'utf8');
     
    const input = {
        language: 'Solidity',
        sources: {
            'Inbox.sol': {
                content: source,
            },
        },
        settings: {
            outputSelection: {
                '*': {
                    '*': ['*'],
                },
            },
        },
    };
    
    /**
     * Writes the contracts from the compiled sources into JSON files, which you will later be able to
     * use in combination with web3.
     * @param compiled - Object containing the compiled contracts.
     * @param buildPath - Path of the build folder.
     */
    function writeOutput(compiled, buildPath) {
        fsExtra.ensureDirSync(buildPath);
    
        for (let contractFileName in compiled.contracts) {
            const contractName = contractFileName.replace('.sol', '');
            console.log('Writing: ', contractName + '.json');
            fsExtra.outputJsonSync(
                path.resolve(buildPath, contractName + '.json'),
                compiled.contracts[contractFileName].basic
            );
        }
    }
     
     
    compilesFiles = JSON.parse(solc.compile(JSON.stringify(input)));
    const buildPath = path.resolve(__dirname, 'build');
    writeOutput(compilesFiles, buildPath);
    

    参考:best reference

    【讨论】:

      猜你喜欢
      • 2019-06-22
      • 2021-03-11
      • 2021-12-16
      • 2019-06-09
      • 2020-03-07
      • 2021-09-10
      • 2022-08-11
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多