you-solidity 编译工具 youchain-solc

概述:

youchain-solc 是用于编译 you-solidity 文件的 node.js 库和命令行工具

项目地址

https://github.com/youchainhq/youchain-solc

安装:

npm install -g youchain-solc

youchain-solc 在命令行中执行工具

node youchain-solcjs --.sol --bin --abi --optimize -o 

youchain-solcjs 命令详解:

Usage: youchain-solcjs [options] [input_file...]

Options:
  --optimize        Enable bytecode optimizer.                         [boolean]
  --bin             Binary of the contracts in hex.                    [boolean]
  --abi             ABI of the contracts.                              [boolean]
  --standard-json   Turn on Standard JSON Input / Output mode.         [boolean]
  --output-dir, -o  Output directory for the contracts.                 [string]
  --version         Show version number                                [boolean]
  --help            Show help                                          [boolean]

youchain-solcjs API:

youchain-solcjs 提供了 compiler 方法,用于编译 you-solidity 代码。下面是部署合约的一个例子

const YOUChain = require('youchain');
const youchain = new YOUChain('https://test-node.iyouchain.com');
const solc = require('youchain-solc');
const fs = require('fs');
const solc = require('youchain-solc');
const contractWrap = (content) => {
    return {
        language: 'Solidity',
        sources: {
            'Contract.sol': {
                content
            }
        },
        settings: {
            outputSelection: {
                '*': {
                    '*': ['*']
                }
            }
        }
    };
};

s.readFile('Contract.sol', 'utf8', async (error, data) => {
    let output = JSON.parse(solc.compile(JSON.stringify(contractWrap(data))));
    output = output.contracts['CatContract.sol']['CatCoin'];
    const contract = new youchain.you.Contract(output.abi);
    const encodeABI = contract
        .deploy({
            data: output.evm.bytecode.object,
            arguments: []
        })
        .encodeABI();

    const nonce = await youchain.you.getTransactionCount(address);
    let tx = {
        nonce: nonce,
        from: address,
        gas: 4712388,
        gasPrice: 100000000000,
        data: '0x' + encodeABI
    };

    youchain.you.accounts.signTransaction(tx, privateKey).then(async ({rawTransaction}) => {
        console.log('balance ###', await youchain.you.getBalance(address));
        youchain.you
            .sendSignedTransaction(rawTransaction)
            .on('transactionHash', (hash) => {
                console.log('transactionHash', hash);
            })
            .on('receipt', (receipt) => {
                console.log('receipt', receipt);
            })
            .on('error', (error) => {
                console.log('error', error);
            });
    });
});