ethers js基础操作

查看一个地址的余额

创建provider调用其getBalance方法
web2通过http获取内容 web3通过jsonrpc获取内容 存储在变量provider中

// 引入ethers
const { ethers } = require("ethers");

// 设置JsonRpc连接

const INFURA_ID = "";
const provider = new ethers.providers.JsonRpcProvider(
  `https://mainnet.infura.io/v3/${INFURA_ID}`  创建以太坊节点(通过infura网页)
);

const address = "0x73BCEb1Cd57C711feaC4224D062b0F6ff338501e";
//这一步需要异步操作,因为它很慢,用async await包裹
const main = async () => {
  const balance = await provider.getBalance(address);
  console.log(
    // 需要格式化地址formatEther
    `\nETH Balance of ${address} --> ${ethers.utils.formatEther(balance)} ETH\n`
  );
};

main();

读取智能合约

创建智能合约 参数分别是地址 abi provider
const contract = new ethers.Contract(address, ERC20_ABI, provider);

const { ethers } = require("ethers");

const INFURA_ID = "";
const provider = new ethers.providers.JsonRpcProvider(
  `https://mainnet.infura.io/v3/${INFURA_ID}`
);

//abi: 数组 里面是string类型的函数
const ERC20_ABI = [
  "function name() view returns (string)",
  "function symbol() view returns (string)",
  "function totalSupply() view returns (uint256)",
  "function balanceOf(address) view returns (uint)",
];

const address = "0x6B175474E89094C44Da98b954EedeAC495271d0F"; // DAI Contract
// 关键步骤:创建智能合约 参数有3个:智能合约的地址、abi、provider
const contract = new ethers.Contract(address, ERC20_ABI, provider);

const main = async () => {
  const name = await contract.name();
  const symbol = await contract.symbol();
  const totalSupply = await contract.totalSupply();

  console.log(`\nReading from ${address}\n`);
  console.log(`Name: ${name}`);
  console.log(`Symbol: ${symbol}`);
  console.log(`Total Supply: ${totalSupply}\n`);

  const balance = await contract.balanceOf(
    "0x6c6Bc977E13Df9b0de53b251522280BB72383700"
  );

  console.log(`Balance Returned: ${balance}`);
  console.log(`Balance Formatted: ${ethers.utils.formatEther(balance)}\n`);
};

main();

从一个钱包转到另一个钱包

将私钥和provider传入ether.wallet创建钱包
wallet.sendTransaction//转账

const { ethers } = require("ethers");

const INFURA_ID = "";
const provider = new ethers.providers.JsonRpcProvider(
  `https://kovan.infura.io/v3/${INFURA_ID}`
);

// 创建变量存储第一个钱包地址
// 创建变量存储第二个钱包地址
const account1 = ""; // Your account address 1
const account2 = ""; // Your account address 2

const privateKey1 = ""; // 存储第一个钱包的私匙
const wallet = new ethers.Wallet(privateKey1, provider); //创建钱包

const main = async () => {
  // 获得账号1的转账前余额
  const senderBalanceBefore = await provider.getBalance(account1);
  // 获得账号2的转账前余额
  const recieverBalanceBefore = await provider.getBalance(account2);

  console.log(
    `\nSender balance before: ${ethers.utils.formatEther(senderBalanceBefore)}`
  );
  console.log(
    `reciever balance before: ${ethers.utils.formatEther(
      recieverBalanceBefore
    )}\n`
  );

  //   交易
  const tx = await wallet.sendTransaction({
    to: account2,
    value: ethers.utils.parseEther("0.025"),
  });
  // 等待交易完成
  await tx.wait();
  console.log(tx); //在控制台显示交易过程

  const senderBalanceAfter = await provider.getBalance(account1);
  const recieverBalanceAfter = await provider.getBalance(account2);

  console.log(
    `\nSender balance after: ${ethers.utils.formatEther(senderBalanceAfter)}`
  );
  console.log(
    `reciever balance after: ${ethers.utils.formatEther(
      recieverBalanceAfter
    )}\n`
  );
};

main();

你可能感兴趣的:(区块链)