? ? ethers.js是一個非常精簡的以太坊操作庫,它包含如下四個模塊:
? ? ? ? Ethers.provider
? ? ? ? Ethers.contract
? ? ? ? Ethers.utils
? ? ? ? Ethers.wallets
? ? 其中,Ethers.provider負責與以太坊節點進行連接,查詢交易、廣播交易,獲取賬戶余額等功能;
? ? Ethers.contract負責與智能合約進行互動,包括部署合約、監聽合約里的事件、獲取合約里的資訊,呼叫合約里的函式等功能;
? ? Ethers.utils是一個工具庫,主要用于處理輸入、輸出資料,資料的型別與格式轉換;
? ? Ethers.wallets主要用于創建新錢包,連接或切換現有錢包,以及對交易進行簽名等功能,
? ? 下面,介紹使用Ethers.js來部署智能合約,
1、新建一個工程sendtokenone
mkdir sendtokenone
cd sendtokenone
npm init -y
truffle init
2、修改package.json并安裝依賴包
? ? a)修改后的package.json檔案如下:
? ? //package.json
{
"name": "sendtokenone",
"version": "1.0.0",
"description": "ethers.js部署合約",
"main": "",
"scripts": {
"test": "echo \"Error: no test specified\" && exit 1"
},
"keywords": [],
"author": "",
"license": "ISC",
"dependencies": {
"@openzeppelin/contracts": "^3.4",
"@truffle/hdwallet-provider": "^1.5.0",
"bignumber": "^1.1.0",
"bignumber.js": "^8.1.1",
"chai": "^4.2.0",
"chai-as-promised": "^7.1.1",
"eslint": "^5.15.0",
"ethereumjs-tx": "^1.3.7",
"ethers": "^5.4.7",
"request": "^2.88.2",
"web3": "^1.3.0"
},
"devDependencies": {
"@babel/core": "^7.12.3",
"@babel/preset-env": "^7.12.1"
}
}
? ? b) 安裝依賴包
npm install
3、新建智能合約
3.1 創建一個EventValue.sol合約
? ? 在sendtokenone/contacts目錄,創建一個創建一個EventValue.sol合約,內容如下:
? ? // EventValue.sol
// SPDX-License-Identifier: MIT
pragma solidity ^0.6.0;
contract EventValue {
event ValueChanged(address indexed author,uint oldValue,uint newValue);
uint _value;
constructor(uint value) public {
uint tmp = _value;
_value = value;
emit ValueChanged(msg.sender, tmp, value);
}
function getValue() view public returns (uint) {
return _value;
}
function setValue(uint value) public {
uint tmp = _value;
_value = value;
emit ValueChanged(msg.sender, tmp, value);
}
}
3.2 撰寫部署腳本
? ? 新建一個檔案夾名稱為migDeploy,然后在這個檔案夾里,創建部署腳本1_deploy_event.js
mkdir migDeploy
cd migDeploy
touch 1_deploy_event.js
1_deploy_event.js的內容如下:
// sendtokenone/migDeploy/1_deploy_event.js
const {ethers} = require("ethers")
const fs = require('fs')
let provider = new ethers.providers.JsonRpcProvider('http://localhost:8545')
function getHexString(prikeyPath) {
const privKeyFile = fs.readFileSync(prikeyPath).toString().trim();
const privKey = new Buffer.from(privKeyFile, 'hex');
return privKey
}
// var privKey = getHexString(".secret")
var privKey = '0x403d...23d5'
let wallet = new ethers.Wallet(privKey,provider)
var jsonStr = fs.readFileSync('./build/contracts/EventValue.json')
var jsonInfo = JSON.parse(jsonStr)
var jsonAbi = jsonInfo.abi
var bytecode = jsonInfo.bytecode
async function deployContract(abi,bytecode,wallet) {
let factory = new ethers.ContractFactory(abi,bytecode,wallet)
let contractObj = await factory.deploy(100)
console.log('contractAddress=',contractObj.address)
console.log('deploy txHash=',contractObj.deployTransaction.hash)
await contractObj.deployed()
}
deployContract(jsonAbi,bytecode,wallet)
3.3 編譯合約
? ? a)設定ganache的IP為127.0.0.1,埠為8545
? ? b) 在truffle-config.js里,開啟development網段、solc指定版本為0.6.6,具體如下:
? ? // truffle-config.js
module.exports = {
networks: {
development: {
host: "127.0.0.1", // Localhost (default: none)
port: 8545, // Standard Ethereum port (default: none)
network_id: "*", // Any network (default: none)
},
},
// Set default mocha options here, use special reporters etc.
mocha: {
// timeout: 100000
},
// Configure your compilers
compilers: {
solc: {
version: "0.6.6", // Fetch exact version from solc-bin (default: truffle's version)
// docker: true, // Use "0.5.1" you've installed locally with docker (default: false)
// settings: { // See the solidity docs for advice about optimization and evmVersion
// optimizer: {
// enabled: false,
// runs: 200
// },
// evmVersion: "byzantium"
// }
}
},
};
? ? 打開一個黑框框控制臺,使用truffle編譯該合約
cd sendtokenone
truffle console
compile
3.4 部署合約
? ? 在黑框框終端里,輸入如下命令,即可部署合約
cd sendtokenone
node migDeploy\1_deploy_event.js
? ? 效果如下:
? ? 可以列印合約地址和txHash,說明合約部署成功,
轉載請註明出處,本文鏈接:https://www.uj5u.com/qukuanlian/335253.html
標籤:區塊鏈
上一篇:元宇宙游戲Murphy是否會成為BSC上最耀眼的新星?
下一篇:監聽Solidity合約事件
