日韩性视频-久久久蜜桃-www中文字幕-在线中文字幕av-亚洲欧美一区二区三区四区-撸久久-香蕉视频一区-久久无码精品丰满人妻-国产高潮av-激情福利社-日韩av网址大全-国产精品久久999-日本五十路在线-性欧美在线-久久99精品波多结衣一区-男女午夜免费视频-黑人极品ⅴideos精品欧美棵-人人妻人人澡人人爽精品欧美一区-日韩一区在线看-欧美a级在线免费观看

歡迎訪問 生活随笔!

生活随笔

當(dāng)前位置: 首頁 > 编程资源 > 编程问答 >内容正文

编程问答

以太坊智能合约 编译脚本

發(fā)布時間:2023/12/13 编程问答 25 豆豆
生活随笔 收集整理的這篇文章主要介紹了 以太坊智能合约 编译脚本 小編覺得挺不錯的,現(xiàn)在分享給大家,幫大家做個參考.

前提準(zhǔn)備工作

  • 創(chuàng)建新的項目文件夾 mkdir contract_workflow
  • mkdir contracts? ?創(chuàng)建合約文件夾
  • mkdir scripts? ? ? ?創(chuàng)建腳本文件夾
  • mkdir compiled? ?創(chuàng)建編譯文件存放的文件夾
  • mkdir tests? ? ? ? ? 創(chuàng)建測試文件夾
  • 使用ls可以看到創(chuàng)建的文件夾,這個時候需要引入包管理工具,初始化列表,使用命令 npm init
  • 安裝所需要的插件 solc 使用命令 npm install solc@0.4.25
  • 安裝所需要的插件 npm install fs-extra ,這個用于將compile.js文件的輸出結(jié)果存儲為一個文件

準(zhǔn)備源碼

  • 將Car.sol存放到contracts文件夾下面
pragma solidity ^0.4.17;contract Car{string brand;uint public price;constructor(string initBrand,uint initPrice)public{brand = initBrand;price = initPrice;}function setBrand(string newBrand)public{brand = newBrand;}function getBrand() public view returns (string){return brand;}function setPrice(uint newPrice)public{price = newPrice;} }

編譯腳本

  • 將compile.js編譯腳本放到scrips文件夾下面
  • 使用命令node compile.js進(jìn)行對于合約的編譯
const fs = require('fs-extra') const solc = require('solc') const path = require('path')const contractPath = path.resolve(__dirname,'../contracts','Car.sol'); const contractSource = fs.readFileSync(contractPath,'utf-8'); let compileResult = solc.compile(contractSource,1);console.log(compileResult);Object.keys(compileResult.contracts).forEach(name=>{let contractName = name.replace(/^:/,'');let filepath = path.resolve(__dirname,'../compiled',`${contractName}.json`);fs.outputJsonSync(filepath,compileResult.contracts[name]);console.log("Saving json file to ,"filepath); })
  • 操作顯示的結(jié)果,如下?

  • 每次都需要重新生成對應(yīng)的json文件,(ABI文件/存儲在compiled文件夾下的內(nèi)容),就需要刪除舊的文件
const compiledPath = path.resolve(__dirname,'../compiled'); fs.removeSync(compiledPath); fs.ensureDirSync(compiledPath);
  • 測試文件是否是新生成的,在compiled文件夾下,使用命令 ls -l? ?對比文件的生成時間和當(dāng)前時間進(jìn)行驗證

  • 如果程序報錯,對于錯誤輸出的格式化代碼
//check errors if(Array.isArray(result.errors) && result.errors.length){throw new Error(result.errors[0]); }

compile.js完整代碼

const fs = require('fs-extra'); const solc = require('solc'); const path = require('path');const compiledPath = path.resolve(__dirname,'../compiled'); fs.removeSync(compiledPath); fs.ensureDirSync(compiledPath);//check errors if(Array.isArray(result.errors) && result.errors.length){throw new Error(result.errors[0]); }const contractPath = path.resolve(__dirname,'../contracts','Car.sol'); const contractSource = fs.readFileSync(contractPath,'utf-8'); let compileResult = solc.compile(contractSource,1);//console.log(compileResult);Object.keys(compileResult.contracts).forEach(name=>{let contractName = name.replace(/^:/,'');let filepath = path.resolve(__dirname, '../compiled', `${contractName}.json`);fs.outputJsonSync(filepath,compileResult.contracts[name]);console.log("Saving json file to ",filepath); });

部署腳本

  • 進(jìn)入scripts文件夾下,創(chuàng)建deploy.js文件,寫入部署代碼? 1.0版本
const Web3 = require('web3') const web3 = new Web3(new Web3.providers.HttpProvider("Http://localhost:8545")); const fs = require('fs-extra') const path = require('path')const filePath = path.resolve(__dirname,'../compiled/Car.json'); const {interface , bytecode} = require(filePath);(async()=>{let accounts = await web3.eth.getAccounts;let result = await new web3.eth.Contract(JSON.parse(interface)).deploy({data:bytecode,arguments:["BWM"]}).send({from:accounts[0],gas:5000000});console.log("result:",result); })();
  • console.time("deploy time")
  • console.timeEnd("deploy time")? 里面的內(nèi)容得一致,比如deploy time,成對出現(xiàn)
  • 0.20版本
const Web3 = require('web3') const web3 = new Web3(new Web3.providers.HttpProvider("Http://localhost:8545")); const path = require('path')const filePath = path.resolve(__dirname,'../compiled/Car.json'); const {interface , bytecode} = require(filePath);const abi = JSON.parse(interface); const contract = web3.eth.contract(abi); const _from = web3.eth.accounts[0]; const txObj = {data:bytecode,from:_from,gas:5000000};let contractInstance = contract.new(txObj,(err,res)=>{if(err)console.log("Error:",err);elseconsole.log("Result:",res); });

?

總結(jié)

以上是生活随笔為你收集整理的以太坊智能合约 编译脚本的全部內(nèi)容,希望文章能夠幫你解決所遇到的問題。

如果覺得生活随笔網(wǎng)站內(nèi)容還不錯,歡迎將生活随笔推薦給好友。