Solidity 学习笔记

nodejs与以太坊进行交互

2018-12-16  本文已影响6人  那个大螺丝
  • nodejs与以太坊交互的库用的是ethers.js,它比web3.js容易使用。
  • ethers.js自带了链接主网和测试网的功能,不用在本地启用geth客户端。(文档地址)
  • 合约编译用到的工具库都是solc-js(文档地址)
  • 本项目所有代码

安装ethers.js

$ npm install  ethers

创建钱包

// demo1.js
const ethers = require('ethers');
const fs = require('fs');
const util = require('util');
const readFile = util.promisify(fs.readFile);

// const randomWallet = ethers.Wallet.createRandom();
// console.log( randomWallet.signingKey.mnemonic);

// 助记词由以上随机函数生成,为了保持账号统一,这里记录了两组助记词
const mnemonic1 = 'utility opinion husband upset finger side round exhaust arm allow pilot hospital';
const mnemonic2 = 'method expand rule tool impact wedding just body slogan offer rate pass';

// 根据两组助记词生成两个钱包对象
const _wallet1 = ethers.Wallet.fromMnemonic(mnemonic1);

// address1 0xBe255696870b84C69F6e2b902177Cf2a2cB57B58
// privateKey1 0x056ef7c6a165f877a5aedb3cfe24b2bbcdd6c680d12df9a82092705fc03ce37f

const _wallet2 = ethers.Wallet.fromMnemonic(mnemonic2);
// address2 0xbe79D5B66A5D44607F91E312ec5E35b8c92db5bf
// privateKey2 0x8544e404dea9123dd6fe1b6b35702a738284e055223c0e2afd41ec7694a2bfda

给账号充值

屏幕快照 2018-12-05 下午10.45.29.png

钱包对象链接网络

let provider = ethers.getDefaultProvider('rinkeby');
const wallet1 = _wallet1.connect(provider);
const wallet2 = _wallet2.connect(provider);

查询一下账号余额

const getBalance = async () => {
  // 返回的余额单位是ether,要转换成ETH
  const _balance1 =  await wallet1.getBalance();
  // 第一次获取是18.75, 总之这里是非零就说明水龙头转账成功,并且连接测试网成功了
  const balance1 =  ethers.utils.formatEther(_balance1);

  const _balance2 =  await wallet2.getBalance();
  const balance2 =  ethers.utils.formatEther(_balance2);
  console.log(balance1, balance2);
};

getBalance();

转账测试

const transfer = async () => {
  let tx = {
    // 这里写一个接收人的地址,就写我们的wallet2吧
    to: "0xbe79D5B66A5D44607F91E312ec5E35b8c92db5bf",
    // 填写一个金额
    value: ethers.utils.parseEther('2.33')
  };

  // 广播转账信息
  const result =  await wallet1.sendTransaction(tx);
  console.log(result);
};

transfer();

// 转账结束后,查询一下余额,数正确,没毛病
getBalance().then();

合约部署

//  ./contracts/SimpleStorage.sol
// 这个合约只有简单写入与读取功能,还有一个通知前端的事件。
pragma solidity ^0.5.1;

contract SimpleStorage {

    event ValueChanged(address indexed author, string oldValue, string newValue);

    string _value;

    constructor(string memory value) public {
        emit ValueChanged(msg.sender, _value, value);
        _value = value;
    }

    function getValue() view public returns (string memory) {
        return _value;
    }

    function setValue(string memory value) public {
        emit ValueChanged(msg.sender, _value, value);
        _value = value;
    }
}
$ npm install solc
// 编译bytecode
../node_modules/solc/solcjs ./SimpleStorage.sol --bin
// 编译abi
../node_modules/solc/solcjs ./SimpleStorage.sol --abi

如果命令出错,提示丢失smtchecker.js,运行cp ./fixBug/smtchecker.js ./node_modules/solc/

// demo4.js
const ethers = require('ethers');
const fs = require('fs');
const util = require('util');
const readFile = util.promisify(fs.readFile);
const  bytecodePath = './contracts/__SimpleStorage_sol_SimpleStorage.bin';
const abiPath = './contracts/__SimpleStorage_sol_SimpleStorage.abi';

const mnemonic1 = 'utility opinion husband upset finger side round exhaust arm allow pilot hospital';
const _wallet1 = ethers.Wallet.fromMnemonic(mnemonic1);
let provider = ethers.getDefaultProvider('rinkeby');
const wallet1 = _wallet1.connect(provider);

const deploy = async () => {
  const bytecode = await readFile(bytecodePath,{encoding: 'utf8'});
  const abi = await readFile(abiPath,{encoding: 'utf8'});
  let factory = new ethers.ContractFactory(abi, bytecode, wallet1);
  let contract = await factory.deploy("Hello World");
  console.log('contract.address',contract.address);
  await contract.deployed();
  console.log('hash',contract.deployTransaction.hash);
};

deploy();

合约执行

// 以上代码运行后,合约地址为0x5Dbcdb3d61Bf83d5Fb6C926F23717A0138f536d9
const contractAddress = '0x5Dbcdb3d61Bf83d5Fb6C926F23717A0138f536d9';

const getContractValue = async () => {
  const abi = await readFile(abiPath,{encoding: 'utf8'});
  const contract = new ethers.Contract(contractAddress, abi, provider);
  const currentValue = await contract.getValue();
  console.log(currentValue);
};

getContractValue();

const setContractValue = async (value) => {
  const abi = await readFile(abiPath,{encoding: 'utf8'});
  const contract = new ethers.Contract(contractAddress, abi, provider);

  const currentValue = await contract.getValue();
  console.log('currentValue', currentValue);

  const contractWithSigner = contract.connect(wallet1);
  const tx = await contractWithSigner.setValue(value);
  console.log('tx.hash',tx.hash);
  await tx.wait();

  const netValue = await contract.getValue();
  console.log('netValue', netValue);

};

setContractValue('KKKK').catch();

合约监听

// demo3.js
const ethers = require('ethers');
const fs = require('fs');
const util = require('util');
const readFile = util.promisify(fs.readFile);

const provider = ethers.getDefaultProvider('rinkeby');
const contractAddress = '0x5Dbcdb3d61Bf83d5Fb6C926F23717A0138f536d9';

const listening = async ()=> {
  const abi = await readFile('./contracts/SimpleStorage_sol_SimpleStorage.abi',{encoding: 'utf8'});
  const contract = new ethers.Contract(contractAddress, abi, provider);

// 监听合约的事件,
  contract.on("ValueChanged", (author, oldValue, newValue, event) => {
    // Called when anyone changes the value

    console.log('author', author);
    // "0x14791697260E4c9A71f18484C9f997B308e59325"

    console.log('oldValue', oldValue);
    // "Hello World"

    console.log('newValue', newValue);
    // "Ilike turtles."

    // See Event Emitter below for all properties on Event
    console.log('blockNumber', event.blockNumber);
    // 4115004
  });
};

listening().catch(console.log);

本项目所有代码

上一篇下一篇

猜你喜欢

热点阅读