web3js获取区块链钱包地址指定代币余额

2023-3-20|2023-3-24
夜火/xloong
夜火/xloong
type
Post
status
Published
date
Mar 20, 2023
slug
web3js-get-specified-token-balance
summary
web3js获取区块链钱包地址指定代币余额
tags
开发
区块链
以太坊
metamask
category
技术分享
icon
password
URL
Property
Mar 24, 2023 12:54 AM

获取指定钱包地址默认代币

var account = ethereum.selectedAddress // 获取余额 ethereum.request({ method: 'eth_getBalance', params: [ account , 'latest' ] }) .then((result) => { console.log("获取余额success--->" + result) let formatEther = ethers.utils.formatEther(result); //16进制的wei console.log(formatEther) }) .catch((error) => { console.error(error) });

获取指定钱包地址指定代币

和发起指定代币交易一样,需要创建代币的智能合约实例,通过实例的方法,获取钱包地址的对应代币余额
var account = ethereum.selectedAddress const USDTContractAddress = "..."; // USDT合约地址 const USDTContractABI = [...]; // USDT合约ABI // 创建一个web3实例,连接到metamask提供的provider const web3 = new Web3(window.ethereum); // 创建一个USDT合约实例 const USDTContract = new web3.eth.Contract(USDTContractABI, USDTContractAddress); USDTContract.methods.balanceOf(account).call((error, result) => { if (error) { console.error(error) } else { console.log(result); let formatEther = ethers.utils.formatEther(result); //16进制的wei console.log(formatEther) } });
通过网页切换metamask主网metamask小狐狸钱包 eth_sendTransaction接口发起指定币种交易