zoukankan      html  css  js  c++  java
  • ethers.js

    ethers.js

    https://docs.ethers.io/v5/

    一个完备的,紧致的JS库,可以和以太坊区块链和其生态进行交互。

    What is Ethers?

    The ethers.js library aims to be a complete and compact library for interacting with the Ethereum Blockchain and its ecosystem. It was originally designed for use with ethers.io and has since expanded into a more general-purpose library.

    Common Terminology

    Provider 是对 connection 的抽象

    Signer 是对 account 的抽象

    Contract是对 合约的 抽象

    Signer是为Contract服务的。

    This section needs work...

    Provider A Provider (in ethers) is a class which provides an abstraction for a connection to the Ethereum Network. It provides read-only access to the Blockchain and its status.  
    Signer A Signer is a class which (usually) in some way directly or indirectly has access to a private key, which can sign messages and transactions to authorize the network to charge your account ether to perform operations.  
    Contract A Contract is an abstraction which represents a connection to a specific contract on the Ethereum Network, so that applications can use it like a normal JavaScript object.  
    Common Terms


    Connecting to Ethereum: Metamask

    https://docs.ethers.io/v5/getting-started/#getting-started--connecting

    The quickest and easiest way to experiment and begin developing on Ethereum is to use Metamask, which is a browser extension that provides:

    • A connection to the Ethereum network (a Provider)
    • Holds your private key and can sign things (a Signer)
    // A Web3Provider wraps a standard Web3 provider, which is
    // what Metamask injects as window.ethereum into each page
    const provider = new ethers.providers.Web3Provider(window.ethereum)
    
    // The Metamask plugin also allows signing transactions to
    // send ether and pay to change state within the blockchain.
    // For this, you need the account signer...
    const signer = provider.getSigner()

    Contracts

    https://docs.ethers.io/v5/getting-started/#getting-started--contracts

    A Contract is an abstraction of program code which lives on the Ethereum blockchain.

    The Contract object makes it easier to use an on-chain Contract as a normal JavaScript object, with the methods mapped to encoding and decoding data for you.

    If you are familiar with Databases, this is similar to an Object Relational Mapper (ORM).

    In order to communicate with the Contract on-chain, this class needs to know what methods are available and how to encode and decode the data, which is what the Application Binary Interface (ABI) provides.

    This class is a meta-class, which means its methods are constructed at runtime, and when you pass in the ABI to the constructor it uses it to determine which methods to add.

    // You can also use an ENS name for the contract address
    const daiAddress = "dai.tokens.ethers.eth";
    
    // The ERC-20 Contract ABI, which is a common contract interface
    // for tokens (this is the Human-Readable ABI format)
    const daiAbi = [
      // Some details about the token
      "function name() view returns (string)",
      "function symbol() view returns (string)",
    
      // Get the account balance
      "function balanceOf(address) view returns (uint)",
    
      // Send some of your tokens to someone else
      "function transfer(address to, uint amount)",
    
      // An event triggered whenever anyone transfers to someone else
      "event Transfer(address indexed from, address indexed to, uint amount)"
    ];
    
    // The Contract object
    const daiContract = new ethers.Contract(daiAddress, daiAbi, provider);
    // Get the ERC-20 token name
    await daiContract.name()
    // 'Dai Stablecoin'
    
    // Get the ERC-20 token symbol (for tickers and UIs)
    await daiContract.symbol()
    // 'DAI'
    
    // Get the balance of an address
    balance = await daiContract.balanceOf("ricmoo.firefly.eth")
    // { BigNumber: "1193738008374328791075" }
    
    // Format the DAI for displaying to the user
    ethers.utils.formatUnits(balance, 18)
    // '1193.738008374328791075'
    // The DAI Contract is currently connected to the Provider,
    // which is read-only. You need to connect to a Signer, so
    // that you can pay to send state-changing transactions.
    const daiWithSigner = contract.connect(signer);
    
    // Each DAI has 18 decimal places
    const dai = ethers.utils.parseUnits("1.0", 18);
    
    // Send 1 DAI to "ricmoo.firefly.eth"
    tx = daiWithSigner.transfer("ricmoo.firefly.eth", dai);

    metamask

    https://metamask.io/

    A crypto wallet & gateway to blockchain apps

    Start exploring blockchain applications in seconds.  Trusted by over 1 million users worldwide.

    出处:http://www.cnblogs.com/lightsong/ 本文版权归作者和博客园共有,欢迎转载,但未经作者同意必须保留此段声明,且在文章页面明显位置给出原文连接。
  • 相关阅读:
    css3多列
    伪元素
    text文本样式二
    透明登录框
    透明度设置opacity
    超链接
    meta标签
    奇偶选择器
    OC跟Swift混编
    Swift中as as! as?的区别
  • 原文地址:https://www.cnblogs.com/lightsong/p/14892754.html
Copyright © 2011-2022 走看看