xgqfrms™, xgqfrms® : xgqfrms's offical website of cnblogs! xgqfrms™, xgqfrms® : xgqfrms's offical website of GitHub!

Web3.js All In One

Web3.js All In One

Web3

Web3.js

A JavaScript library for building on Ethereum

  1. Comprehensive.
    Everything you need to start interacting with the Ethereum blockchain
  2. Community-driven.
    Open-source and continuously updated since 2015
  3. Modular.
    Import only the packages you need

https://web3js.org/

https://github.com/ChainSafe/web3.js

https://github.com/web3/web3.js

$ npm i web3
# or
$ yarn add web3

https://www.npmjs.com/package/web3

https://web3js.readthedocs.io/en/v1.10.0/

docs

image

import Web3 from 'web3';

const web3 = new Web3(/* PROVIDER*/);

// calling any method that interact with the network would involve using the early passed provider.
await web3.eth.sendTransaction({
    from,
    to,
    value,
});

https://docs.web3js.org/

https://blog.chainsafe.io/announcing-the-full-release-of-web3-js-v4-e9e26275a3dc

demos


  import {Web3} from "web3";

  // set a provider in the sepolia testnet using node rpc url
  const web3 = new Web3("https://rpc.sepolia.org");
  
  // interacting with the smart contract
  const abi = [
      {
      outputs: [
          {
          internalType: "uint256",
          name: "randomNo",
          type: "uint256",
          },
      ],
      name: "generateRandomNumber",
      stateMutability: "nonpayable",
      type: "function",
      },
  ] as const;
  
  const address = "0xA36432F7B12f160F685717c4Ab12EB883a682810";
  
  // create a new contract object, providing the ABI and address
  const contract = new web3.eth.Contract(abi, address);
  
  // using contract.methods to get value
  contract.methods
      .generateRandomNumber()
      .call()
      .then(console.log);

  import {Web3} from "web3";
  
  // set a provider to the sepolia testner using node rpc url
  const web3 = new Web3("wss://sepolia.infura.io/ws/v3/API_KEY");

  // interacting with the smart contract
  const abi = [
    {
      inputs: [
        {
          internalType: "uint256",
          name: "randomNo",
          type: "uint256",
        },
      ],
      name: "RandomNo",
      type: "event",
    },
  ] as const;

  const address = "0xA36432F7B12f160F685717c4Ab12EB883a682810";

  // create a new contract object, providing the ABI and address
  const contract = new web3.eth.Contract(abi, address);

// using contract.methods to get value
const subscription = contract.events.RandomNo()
subscription.on("data", console.log); // new value every time the event is emitted

  import {Web3} from "web3"
  
  // set a provider such as using infura
  const web3 = new Web3("https://rpc.sepolia.org") 
  
  // Get the balance of an Ethereum address
  web3.eth.getBalance("0xF8561Dc64E577B0CF53dd3FC11329e80B1A8343e").then(console.log) // result: 1.000.000 eth
  
  // Get the latest block number
   web3.eth.getBlockNumber().then(console.log) // result: BigInt(xxx)

  import {Web3} from "web3";

  // Create a sha3 hash of a string input
  console.log(Web3.utils.sha3("example")) // '0x6fd43e7cffc31bb581d7421c8698e29aa2bd8e7186a394b85299908b4eb9b175'
  
  // Convert any ether value into wei
  console.log(Web3.utils.toWei('1', 'ether')) // 1000000000000000000
  console.log(Web3.utils.toWei('1', 'gwei')) // 1000000000
  
  // Adds a padding on the left of a string, useful for adding paddings to HEX strings.
  console.log(Web3.utils.padLeft('0x3456ff', 20)); // '0x000000000000003456ff'

(🐞 反爬虫测试!打击盗版⚠️)如果你看到这个信息, 说明这是一篇剽窃的文章,请访问 https://www.cnblogs.com/xgqfrms/ 查看原创文章!

refs



©xgqfrms 2012-2021

www.cnblogs.com/xgqfrms 发布文章使用:只允许注册用户才可以访问!

原创文章,版权所有©️xgqfrms, 禁止转载 🈲️,侵权必究⚠️!


posted @ 2023-09-28 00:44  xgqfrms  阅读(13)  评论(1编辑  收藏  举报