繁体   English   中英

仅使用 EVM API 在区块链上部署智能合约,而不使用 geth 或 tuffle 或 ganache?

[英]Deploy smart contract on blockchain only using EVM API without using geth or tuffle or ganache?

我已经使用 node js 为 IoT 用例实现了我的个人区块链,所以我现在想在不使用 geth 或 tuffle 或 ganache OR Remix 或 web3.js 的情况下将智能合约代码部署到这个区块链中,所以我的目标是使用EVM API来编译和执行智能合约。 又怎样?

您可以查看合同 ABI 规范

关注这里

合约应用二进制接口 (ABI) 是在以太坊生态系统中与合约交互的标准方式,既可以从区块链外部进行,也可以用于合约间交互。 数据根据其类型进行编码,如本规范中所述。 编码不是自我描述的,因此需要一个模式才能解码。

还,

要与以太坊集成,需要启用 RPC / IPC 的以太坊客户端节点,例如Geth, parityquorum等。

如果您只是对从合约中检索现有数据或从以太坊公共链发送离线签名交易感兴趣,您可以使用像Infura这样的公共节点,而无需安装像 Parity 或 Geth 这样的本地客户端。

对于智能合约开发,您还可以安装Test RPC ,它将在 memory 中创建一个没有松露或 ganache 的网络。

testrpc 是一个基于 Node.js 的以太坊客户端,用于测试和开发。 它使用 ethereumjs 来模拟完整的客户端行为并使开发以太坊应用程序的速度更快。 它还包括所有流行的 RPC 功能和特性(如事件),并且可以确定性地运行以使开发变得轻而易举。

有关 testrpc 的更多详细信息,请点击此处

安装 testrpc

npm install -g ethereumjs-testrpc

笔记

Ganache CLI 是最新版本的 TestRPC:一个快速且可定制的区块链模拟器。 它允许您调用区块链,而无需运行实际的以太坊节点。

安装 ganache-cli

npm install -g ganache-cli

脚步:-

  1. 为物联网应用创建智能合约。
  2. 使用 nodejs 的 solc solidity 编译器编译合约
  3. 从编译好的JS合约object得到接口object

    字节码字符串,它是合约的二进制表示,基本上是一组(某种)汇编指令,供以太坊虚拟机执行

  4. 将合约部署到 testrpc 网络

是不使用松露或甘纳许的详细解决方案

仅使用字节码进行部署

另请参阅仅使用字节码进行部署: 此处此处以及黄皮书

编译你的合约(结果文件是 /build/contracts/YourContract.json),然后在没有松露的情况下进行部署:

// Dependencies
const Web3 = require('web3');
const fs = require("fs");

// Load Parameter
let PROVIDER = ...
let FROM_ACCOUNT = ...
let FROM_PRIVATE_KEY = ...

// Create web3 instance
const web3 = new Web3(new Web3.providers.HttpProvider(PROVIDER), null, {transactionConfirmationBlocks: 1});

// Deploy
web3.eth.getTransactionCount(FROM_ACCOUNT)
    .catch(e => {
        console.log(e);
    })
    .then(txCount => {

        // construct the transaction data
        const txData = {
            nonce: web3.utils.toHex(txCount),
            from: FROM_ACCOUNT,
            data: JSON.parse(fs.readFileSync('./build/contracts/YourContract.json', 'utf8')).bytecode,
            gasLimit: web3.utils.toHex(7900000),
            gasPrice: web3.utils.toHex(web3.utils.toWei('12', 'gwei')),
        };
        console.log('nonce             : ' + txData.nonce);
        console.log('gasPrice          : ' + txData.gasPrice);

        // contract creation
        web3.eth.accounts.signTransaction(txData, FROM_PRIVATE_KEY).then(signed => {
            console.log('sign transaction  : OK');

            web3.eth.sendSignedTransaction(signed.rawTransaction)
                .on("error", error => {
                    console.log('send transaction  : ' + error);
                    throw "DEPLOY FAILED";
                })
                .on("receipt", (receipt) => {
                    console.log('send transaction  : OK');
                    let contractAddress = (receipt.contractAddress === null) ? 'n.a.' : receipt.contractAddress;
                    console.log("CONTRACT_ADDRESS  : " + contractAddress);
                });
            });
        });
    });

依赖项 web3 v1.2.1 和 fs v0.0.2

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM