useDAppuseDApp
  • 开始
  • 指南
  • 代币
⌘ K
安装
读取
写入
Create Eth App
最后更新时间:
帮助改进此文档
Made with ❤️ by 紫升

读取

有许多有用的 Hooks 可以用来读取区块链状态,例如:

  • useBlockMeta() - 返回最近挖掘的区块的元信息(timestamp 和 difficulty)
  • useEtherBalance(address) - 返回给定地址的以太币余额(或 undefined)
  • useTokenBalance(tokenAddress, address) - 返回给定地址的给定代币 BigNumber 格式的余额(或 undefined)
  • useTokenAllowance(tokenAddress, ownerAddress, spenderAddress) - 返回给定所有者和支出者地址对的给定代币 BigNumber 格式的资金(或 undefined)

示例

Ether Balance

useEtherBalance(address: string)

提供一种获取账户余额的方法。该方法以账户地址作为参数,并返回 BigNumber 格式余额,如果数据不可用(即未连接),则返回 undefined。要获取当前连接的账户,请使用 useEthers() 函数。

Token Balance

useTokenBalance(address: string, tokenAddress: string)

提供一种获取指定地址 ERC20 代币余额的方法。该方法以代币地址作为参数,并返回 BigNumber 格式的余额,如果数据不可用,则返回 undefined。

自定义 Hooks

See the Custom Hooks guide if you need to read state not supported by the built-in hooks.

在没有浏览器钱包的情况下进行读取操作

为了在没有浏览器钱包的情况下以只读模式与区块链进行交互,只需在配置文件中指定 readOnlyChainId 和 readOnlyUrls,无需激活即可使用。