web3.js链接以太坊并查询钱包u余额

这篇具有很好参考价值的文章主要介绍了web3.js链接以太坊并查询钱包u余额。希望对大家有所帮助。如果存在错误或未考虑完全的地方,请大家不吝赐教,您也可以点击"举报违法"按钮提交疑问。

环境:

web3.js版本:6.14.15
不会安装以及使用web3.js的小伙伴看:https://blog.csdn.net/qq_45844443/article/details/124330035

一、链接以太坊主网:

  1. 获取以太坊的链接地址(也就是以太坊官网的主网链接)
    获取地址: https://chainlist.org/.
    web3.js如何连接chainlist,区块链,以太坊,区块链,以太坊,web3.js
  2. 创建一个js文件并且创建"providers",填上刚才随意取得的主网链接.
    web3.js如何连接chainlist,区块链,以太坊,区块链,以太坊,web3.js
let Web3 = require('web3');
var web3 = new Web3(new Web3.providers.HttpProvider("https://rpc.flashbots.net"));

二、创建一个ABI

那么该如何获取ABI?

  1. 有过交易的信息,上面都会显示交易的合约地址,我们找到后直接复制下来
    随便找一个以太坊账户查询的网址,查找该账户的交易信息,并找到合约地址
    给你们推荐一个:https://www.yitaifang.com/accounts/.
    直接右上角查询账户地址,找到里面随意一个USDT的交易并复制下来该合约地址
    web3.js如何连接chainlist,区块链,以太坊,区块链,以太坊,web3.js
    web3.js如何连接chainlist,区块链,以太坊,区块链,以太坊,web3.js
  2. 通过合约地址查询该ABI
    查询合约地址的AB链接 :https://etherscan.io/
    复制的合约地址通过该页面查询
    web3.js如何连接chainlist,区块链,以太坊,区块链,以太坊,web3.js
    web3.js如何连接chainlist,区块链,以太坊,区块链,以太坊,web3.js
    web3.js如何连接chainlist,区块链,以太坊,区块链,以太坊,web3.js
    获取了相应的ABI后,创建个变量
var abi=...
//太长了,就不全部贴出来了

web3.js如何连接chainlist,区块链,以太坊,区块链,以太坊,web3.js

三、查询代码

var tokenContract = new web3.eth.Contract(abi, '0xdac17f958d2ee523a2206206994597c13d831ec7'); //第二个参数是刚才复制查询的ABI的合约地址
tokenContract.methods.balanceOf('0x5041ed759dd4afc3a72b8192c143f72f4724081a').call(
    {from: '0x5041ed759dd4afc3a72b8192c143f72f4724081a'}, function (error, result) {
    console.log(error);
    console.log(result);
    console.log(web3.utils.fromWei(result, 'mwei')); //转换成mwei是因为wei与USDT的数量转化比为"1:1000000"
});
//new web3.eth.Contract(abi, '刚才复制查询的ABI的合约地址')
//balanceOf('查询的账号地址')
//{form:'查询的账号地址'}

web3.js如何连接chainlist,区块链,以太坊,区块链,以太坊,web3.js

四、输出结果

web3.js如何连接chainlist,区块链,以太坊,区块链,以太坊,web3.js

五、整体代码

let Web3 = require('web3');
var web3 = new Web3(new Web3.providers.HttpProvider("https://rpc.flashbots.net"));

var abi = [{
    "constant": true,
    "inputs": [],
    "name": "name",
    "outputs": [{"name": "", "type": "string"}],
    "payable": false,
    "stateMutability": "view",
    "type": "function"
}, {
    "constant": false,
    "inputs": [{"name": "_upgradedAddress", "type": "address"}],
    "name": "deprecate",
    "outputs": [],
    "payable": false,
    "stateMutability": "nonpayable",
    "type": "function"
}, {
    "constant": false,
    "inputs": [{"name": "_spender", "type": "address"}, {"name": "_value", "type": "uint256"}],
    "name": "approve",
    "outputs": [],
    "payable": false,
    "stateMutability": "nonpayable",
    "type": "function"
}, {
    "constant": true,
    "inputs": [],
    "name": "deprecated",
    "outputs": [{"name": "", "type": "bool"}],
    "payable": false,
    "stateMutability": "view",
    "type": "function"
}, {
    "constant": false,
    "inputs": [{"name": "_evilUser", "type": "address"}],
    "name": "addBlackList",
    "outputs": [],
    "payable": false,
    "stateMutability": "nonpayable",
    "type": "function"
}, {
    "constant": true,
    "inputs": [],
    "name": "totalSupply",
    "outputs": [{"name": "", "type": "uint256"}],
    "payable": false,
    "stateMutability": "view",
    "type": "function"
}, {
    "constant": false,
    "inputs": [{"name": "_from", "type": "address"}, {"name": "_to", "type": "address"}, {
        "name": "_value",
        "type": "uint256"
    }],
    "name": "transferFrom",
    "outputs": [],
    "payable": false,
    "stateMutability": "nonpayable",
    "type": "function"
}, {
    "constant": true,
    "inputs": [],
    "name": "upgradedAddress",
    "outputs": [{"name": "", "type": "address"}],
    "payable": false,
    "stateMutability": "view",
    "type": "function"
}, {
    "constant": true,
    "inputs": [{"name": "", "type": "address"}],
    "name": "balances",
    "outputs": [{"name": "", "type": "uint256"}],
    "payable": false,
    "stateMutability": "view",
    "type": "function"
}, {
    "constant": true,
    "inputs": [],
    "name": "decimals",
    "outputs": [{"name": "", "type": "uint256"}],
    "payable": false,
    "stateMutability": "view",
    "type": "function"
}, {
    "constant": true,
    "inputs": [],
    "name": "maximumFee",
    "outputs": [{"name": "", "type": "uint256"}],
    "payable": false,
    "stateMutability": "view",
    "type": "function"
}, {
    "constant": true,
    "inputs": [],
    "name": "_totalSupply",
    "outputs": [{"name": "", "type": "uint256"}],
    "payable": false,
    "stateMutability": "view",
    "type": "function"
}, {
    "constant": false,
    "inputs": [],
    "name": "unpause",
    "outputs": [],
    "payable": false,
    "stateMutability": "nonpayable",
    "type": "function"
}, {
    "constant": true,
    "inputs": [{"name": "_maker", "type": "address"}],
    "name": "getBlackListStatus",
    "outputs": [{"name": "", "type": "bool"}],
    "payable": false,
    "stateMutability": "view",
    "type": "function"
}, {
    "constant": true,
    "inputs": [{"name": "", "type": "address"}, {"name": "", "type": "address"}],
    "name": "allowed",
    "outputs": [{"name": "", "type": "uint256"}],
    "payable": false,
    "stateMutability": "view",
    "type": "function"
}, {
    "constant": true,
    "inputs": [],
    "name": "paused",
    "outputs": [{"name": "", "type": "bool"}],
    "payable": false,
    "stateMutability": "view",
    "type": "function"
}, {
    "constant": true,
    "inputs": [{"name": "who", "type": "address"}],
    "name": "balanceOf",
    "outputs": [{"name": "", "type": "uint256"}],
    "payable": false,
    "stateMutability": "view",
    "type": "function"
}, {
    "constant": false,
    "inputs": [],
    "name": "pause",
    "outputs": [],
    "payable": false,
    "stateMutability": "nonpayable",
    "type": "function"
}, {
    "constant": true,
    "inputs": [],
    "name": "getOwner",
    "outputs": [{"name": "", "type": "address"}],
    "payable": false,
    "stateMutability": "view",
    "type": "function"
}, {
    "constant": true,
    "inputs": [],
    "name": "owner",
    "outputs": [{"name": "", "type": "address"}],
    "payable": false,
    "stateMutability": "view",
    "type": "function"
}, {
    "constant": true,
    "inputs": [],
    "name": "symbol",
    "outputs": [{"name": "", "type": "string"}],
    "payable": false,
    "stateMutability": "view",
    "type": "function"
}, {
    "constant": false,
    "inputs": [{"name": "_to", "type": "address"}, {"name": "_value", "type": "uint256"}],
    "name": "transfer",
    "outputs": [],
    "payable": false,
    "stateMutability": "nonpayable",
    "type": "function"
}, {
    "constant": false,
    "inputs": [{"name": "newBasisPoints", "type": "uint256"}, {"name": "newMaxFee", "type": "uint256"}],
    "name": "setParams",
    "outputs": [],
    "payable": false,
    "stateMutability": "nonpayable",
    "type": "function"
}, {
    "constant": false,
    "inputs": [{"name": "amount", "type": "uint256"}],
    "name": "issue",
    "outputs": [],
    "payable": false,
    "stateMutability": "nonpayable",
    "type": "function"
}, {
    "constant": false,
    "inputs": [{"name": "amount", "type": "uint256"}],
    "name": "redeem",
    "outputs": [],
    "payable": false,
    "stateMutability": "nonpayable",
    "type": "function"
}, {
    "constant": true,
    "inputs": [{"name": "_owner", "type": "address"}, {"name": "_spender", "type": "address"}],
    "name": "allowance",
    "outputs": [{"name": "remaining", "type": "uint256"}],
    "payable": false,
    "stateMutability": "view",
    "type": "function"
}, {
    "constant": true,
    "inputs": [],
    "name": "basisPointsRate",
    "outputs": [{"name": "", "type": "uint256"}],
    "payable": false,
    "stateMutability": "view",
    "type": "function"
}, {
    "constant": true,
    "inputs": [{"name": "", "type": "address"}],
    "name": "isBlackListed",
    "outputs": [{"name": "", "type": "bool"}],
    "payable": false,
    "stateMutability": "view",
    "type": "function"
}, {
    "constant": false,
    "inputs": [{"name": "_clearedUser", "type": "address"}],
    "name": "removeBlackList",
    "outputs": [],
    "payable": false,
    "stateMutability": "nonpayable",
    "type": "function"
}, {
    "constant": true,
    "inputs": [],
    "name": "MAX_UINT",
    "outputs": [{"name": "", "type": "uint256"}],
    "payable": false,
    "stateMutability": "view",
    "type": "function"
}, {
    "constant": false,
    "inputs": [{"name": "newOwner", "type": "address"}],
    "name": "transferOwnership",
    "outputs": [],
    "payable": false,
    "stateMutability": "nonpayable",
    "type": "function"
}, {
    "constant": false,
    "inputs": [{"name": "_blackListedUser", "type": "address"}],
    "name": "destroyBlackFunds",
    "outputs": [],
    "payable": false,
    "stateMutability": "nonpayable",
    "type": "function"
}, {
    "inputs": [{"name": "_initialSupply", "type": "uint256"}, {"name": "_name", "type": "string"}, {
        "name": "_symbol",
        "type": "string"
    }, {"name": "_decimals", "type": "uint256"}],
    "payable": false,
    "stateMutability": "nonpayable",
    "type": "constructor"
}, {
    "anonymous": false,
    "inputs": [{"indexed": false, "name": "amount", "type": "uint256"}],
    "name": "Issue",
    "type": "event"
}, {
    "anonymous": false,
    "inputs": [{"indexed": false, "name": "amount", "type": "uint256"}],
    "name": "Redeem",
    "type": "event"
}, {
    "anonymous": false,
    "inputs": [{"indexed": false, "name": "newAddress", "type": "address"}],
    "name": "Deprecate",
    "type": "event"
}, {
    "anonymous": false,
    "inputs": [{"indexed": false, "name": "feeBasisPoints", "type": "uint256"}, {
        "indexed": false,
        "name": "maxFee",
        "type": "uint256"
    }],
    "name": "Params",
    "type": "event"
}, {
    "anonymous": false,
    "inputs": [{"indexed": false, "name": "_blackListedUser", "type": "address"}, {
        "indexed": false,
        "name": "_balance",
        "type": "uint256"
    }],
    "name": "DestroyedBlackFunds",
    "type": "event"
}, {
    "anonymous": false,
    "inputs": [{"indexed": false, "name": "_user", "type": "address"}],
    "name": "AddedBlackList",
    "type": "event"
}, {
    "anonymous": false,
    "inputs": [{"indexed": false, "name": "_user", "type": "address"}],
    "name": "RemovedBlackList",
    "type": "event"
}, {
    "anonymous": false,
    "inputs": [{"indexed": true, "name": "owner", "type": "address"}, {
        "indexed": true,
        "name": "spender",
        "type": "address"
    }, {"indexed": false, "name": "value", "type": "uint256"}],
    "name": "Approval",
    "type": "event"
}, {
    "anonymous": false,
    "inputs": [{"indexed": true, "name": "from", "type": "address"}, {
        "indexed": true,
        "name": "to",
        "type": "address"
    }, {"indexed": false, "name": "value", "type": "uint256"}],
    "name": "Transfer",
    "type": "event"
}, {"anonymous": false, "inputs": [], "name": "Pause", "type": "event"}, {
    "anonymous": false,
    "inputs": [],
    "name": "Unpause",
    "type": "event"
}]

var tokenContract = new web3.eth.Contract(abi, '0xdac17f958d2ee523a2206206994597c13d831ec7');
tokenContract.methods.balanceOf('0x5041ed759dd4afc3a72b8192c143f72f4724081a').call(
    {from: '0x5041ed759dd4afc3a72b8192c143f72f4724081a'}, function (error, result) {
    console.log(error);
    console.log(result);
    console.log(web3.utils.fromWei(result, 'mwei'));
});

web3.js如何连接chainlist,区块链,以太坊,区块链,以太坊,web3.js文章来源地址https://www.toymoban.com/news/detail-780295.html

到了这里,关于web3.js链接以太坊并查询钱包u余额的文章就介绍完了。如果您还想了解更多内容,请在右上角搜索TOY模板网以前的文章或继续浏览下面的相关文章,希望大家以后多多支持TOY模板网!

本文来自互联网用户投稿,该文观点仅代表作者本人,不代表本站立场。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如若转载,请注明出处: 如若内容造成侵权/违法违规/事实不符,请点击违法举报进行投诉反馈,一经查实,立即删除!

领支付宝红包 赞助服务器费用

相关文章

  • 【Web3】认识以太坊钱包

    目录 区块链钱包概念 密码 私钥 Private Key 公钥Public Key  Keystore 助记词 Mnemonic 如何解锁账户 区块链钱包概念 钱包用来存钱的,在区块链中,我们的数字资产都会对应到一个账户地址上, 只有拥 有账户的钥匙(私钥)才可以对资产进行消费(用私钥对消费交易签名)。 数字

    2024年02月12日
    浏览(36)
  • 使用 Web3.js 连接以太坊节点并查询区块链数据

    Web3.js 是一个用于连接以太坊网络的 JavaScript 库。在本文中,我们将介绍如何使用 Web3.js 来连接以太坊节点,并且查询以太坊区块链上的数据。 1. 安装 Web3.js 首先,我们需要安装 Web3.js。在命令行中,输入以下命令: 2. 连接以太坊节点 在使用 Web3.js 之前,我们需要先连接到以

    2023年04月26日
    浏览(51)
  • 使用WalletConnect Web3Modal v3 链接钱包基础教程

    我使用的是vue+ethers@5.7.2 官方文档:WalletConnect 1.安装 2.引用 新建一个js文件,在main.js中引入,初始化配置sdk 获取 address chainId isConnected 相关信息 调用签名 以上调用方法可以自己结合使用, 具体可以参考官方文档 3.网络组件 点这里看文档 初始化引用之后再调用 4.自定义组合

    2024年02月04日
    浏览(67)
  • web3.js 查看钱包金额

    ajax与web服务器交互,传递数据; 等同于 web3.js 向以太坊网络某一节点交互; 见下图来自link. 所以接下来的问题是: JSON RPC(JSON远程调用)作为程序和EVM client的web3 interface,是如何作用的?? 类似jQuery ajax库来读写web服务器上的数据??这又是什么? 注册infura,创建新proj

    2024年02月16日
    浏览(39)
  • vue3 使用 web3.js;钱包转账;唤醒钱包.......

    web3.js基本使用 本篇文章不适合没有经验的小白,需要对web3有一定的了解,所以有些参数需要自己懂! 准备 安装web3.js依赖包 npm install web3 文档教程地址:https://web3.tryblockchain.org/ 这是我自己封装的类 web3.js 直接复制过去就可以用

    2024年02月03日
    浏览(52)
  • 使用web3.js连接Metamask钱包并完成合约交互

    Web3.js是一个用于与以太坊网络进行交互的JavaScript库。Metamask是一个以太坊钱包插件,可以在浏览器中创建和管理以太坊账户。本文将介绍如何使用web3.js库连接Metamask钱包,并完成与智能合约的交互。 Web3.js是一个用于与以太坊网络进行交互的JavaScript库。它提供了一些方法和功

    2024年03月16日
    浏览(51)
  • 使用Web3.js与OKX Web3钱包进行交互:实现去中心化应用的无缝连接

    随着Web3.0技术的不断发展,越来越多的开发者开始探索如何利用去中心化钱包与区块链网络进行交互。在这篇文章中,我们将深入探讨如何通过Web3.js库来连接和操作OKX Web3钱包,以实现对加密货币、NFT以及链上智能合约的管理和交易。 Web3.js 是一个由以太坊社区开发的核心

    2024年02月19日
    浏览(49)
  • 【以太坊开发05】Web3.js

    正式开发环境中,我们一般不是使用remix去编译和部署合约,而是使用goland调用web3进行开发。 Web3工作模式 : 使用脚锁架创建一个react项目,初始化NPM项目,执行下面命令,创建package.json,描述当前模块属性的⽂件。 一路yes下来即可。 项目的目录结构如下: 2.1 安装编译器

    2024年02月16日
    浏览(41)
  • 用Web3.py、Infura和Graph查询以太坊数据

    本文我们将使用Web3.py这个Python库直接查询以太坊数据。Web3.py是一个为与Ethereum区块链交互而建立的Python库。有了它,我们可以为去中心化的应用程序建立各种核心功能。我们可以直接与智能合约互动,收集区块链数据,并发送交易。让我们开始安装Web3.py。 Web3.py的功能是连接

    2024年01月15日
    浏览(67)
  • 以太坊标准JS API库——浅谈Web3.js与Ethers.js

    版本号: Web3.js v1.7.3 Ethers.js v.5.6 web3.js是一个标准的以太坊JavaScript API库,该JS库由以太坊基金会开发维护,同时它也是最早且使用最广泛的一个ETH API库。由于Moonbeam完全兼容以太坊EVM,因此web3.js可以在Moonbeam上进行正常交互 web3.js(v1.7.3)库官方使用文档:https://web3js.readthedoc

    2024年01月23日
    浏览(45)

觉得文章有用就打赏一下文章作者

支付宝扫一扫打赏

博客赞助

微信扫一扫打赏

请作者喝杯咖啡吧~博客赞助

支付宝扫一扫领取红包,优惠每天领

二维码1

领取红包

二维码2

领红包