美文网首页Solidity 学习笔记
nodejs与以太坊进行交互

nodejs与以太坊进行交互

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

安装ethers.js

  • 只要运行一条简单的命令
$ npm install  ethers

创建钱包

  • random方法会随机生成钱包,并且钱包中包含了助记词,这里把助记词提出来,以便每次都生成同样都钱包。
// 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

给账号充值

  • 现在给账号充值一点ETH测试转账,
  • 这里选择测试网rinkeby
  • 水龙头地址
  • 发一条带有自己地址推文,粘贴在水龙头页面的输入框即可,如下图所示。
  • 这里都链接只支持Facebook、Twitter、google puls,请科学上网。
屏幕快照 2018-12-05 下午10.45.29.png

钱包对象链接网络

  • 设定一个provider,参数填入rinkeby即可,如果链接正式网,就不需要填写参数。
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();

转账测试

  • 填写接收者的地址,发出对象就可以了,非常简单,
  • 有一点注意都是,金额需要用 parseEther进行转行,因为在区块链网络中都用ether这个单位。
  • 一切顺利都话,会打印出交易结果。
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();

合约部署

  • 在部署合约之前,需要把一个智能合约进行编译。
  • 准备好一份solidty合约,这里用到的是一份在contracts目录下,名为SimpleStorage.sol的合约。
  • 分别编译完bytecodeabi后,会在contracts生成__SimpleStorage_sol_SimpleStorage.abi__SimpleStorage_sol_SimpleStorage.bin文件。
  • 在node读取两个编译后的文件,上传区块就可以了。
//  ./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/

  • 设置好abi bytecode钱包 把对象发到区块链上,
  • 如果一切顺利,会打印出部署成后的地址,以及交易哈希。
// 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();

合约执行

  • 合约执行分两类,收费和免费,
    收费的一般都是修改数据,免费的一般都是查看数据。
  • 合约的调用是通过api,部署了那些abi就可以在node中调用那些函数
  • 可以查看__SimpleStorage_sol_SimpleStorage.abi文件都有具体有那些方法,目前的例子有getValue /setValue
  • 顺利运行后,会看到打印当前值和设置值'KKKK'
// 以上代码运行后,合约地址为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();

合约监听

  • 在合约中还有一个 ValueChanged事件,提供监听合约的变化
  • 运行监听代码后,只要合约的值产生变化,会返回到前端。
// 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);

本项目所有代码

相关文章

  • nodejs与以太坊进行交互

    nodejs与以太坊交互的库用的是ethers.js,它比web3.js容易使用。ethers.js自带了链接主网...

  • NodeJs01

    一:DAPP 基于以太坊 开发语言:solidity 前台界面与用户进行交互,前台界面与以太坊进行,交互的语言是n...

  • 第三章 以太坊区块链交互机制

    第三章 以太坊区块链交互机制 本章主要介绍的内容是如何与以太坊区块链进行交互,包括JSONRPC与web3的介绍。...

  • 以太坊交易的生命周期

    了解以太坊交易是如何生成并在网络中广播的 交易是以太坊区块链(或任何类似的区块链)的核心。在与以太坊区块链进行交互...

  • 通过web3.js与以太坊智能合约交互

      web3.js是以太坊提供的一个NodeJs库,封装了一些和以太坊交互的常用对象、函数。主要包括版本、账号、交...

  • 以太坊钱包的开发3 -- 编写API

    在以太坊钱包的开发1 与以太坊钱包的开发2 中,我们用NodeJs搭了个以太坊钱包的后台,实现了查询余额、转账、查...

  • 使用脚本与以太坊交互

    除了控制台外,以太坊还支持用脚本与geth交互。除了JS外,还支持Python、PHP Java等。 以太坊的交互...

  • 通过Web3.py和Infura在Windows中设置托管以太坊

    如果你想与以太坊区块链进行交互以获取最新区块的详细信息,可以使用以太网节点、本地节点或托管节点连接到以太坊区块链的...

  • Web3与智能合约交互实战(附代码)

    在最初学习以太坊的时候,很多人都是自己创建以太坊节点后,使用geth与之交互。这种使用命令行交互的方法虽然让很多程...

  • Geth 控制台使用及 Web3.js 使用实战

    在开发以太坊去中心化应用,免不了和以太坊进行交互,那就离不开Web3。Geth 控制台(REPL)实现了所有的we...

网友评论

    本文标题:nodejs与以太坊进行交互

    本文链接:https://www.haomeiwen.com/subject/eycxkqtx.html