web3.js link to Ethereum and query wallet u balance

environment:

web3.js version: 6.14.15
will not be installed and friends who use web3.js see: https://blog.csdn.net/qq_45844443/article/details/124330035

1. Link to the Ethereum mainnet:

  1. Get the link address of Ethereum (that is, the main network link of the official website of Ethereum)
    to get the address: https://chainlist.org/ .
    insert image description here
  2. Create a js file and create "providers", fill in the mainnet link you just obtained at random.
    insert image description here
let Web3 = require('web3');
var web3 = new Web3(new Web3.providers.HttpProvider("https://rpc.flashbots.net"));

2. Create an ABI

So how to get the ABI?

  1. If there is any transaction information, the contract address of the transaction will be displayed on it. After we find it, we will copy it directly and
    find an Ethereum account query URL, find the transaction information of the account, and find the contract address.
    Recommend one for you: https:// www.yitaifang.com/accounts/ .
    Directly query the account address in the upper right corner, find any USDT transaction in it and copy the contract address
    insert image description here
    insert image description here
  2. Query the ABI through the contract address
    to query the AB link of the contract address: https://etherscan.io/
    The copied contract address can be queried through this page
    insert image description here
    insert image description here
    insert image description here
    After obtaining the corresponding ABI, create a variable
var abi=...
//太长了,就不全部贴出来了

insert image description here

3. Query code

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:'查询的账号地址'}

insert image description here

4. Output result

insert image description here

Five, the overall code

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'));
});

insert image description here

Guess you like

Origin blog.csdn.net/qq_45844443/article/details/124374080