0
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

More than 5 years have passed since last update.

ethers.js で Ethereum アカウントの残高を取得

Posted at

ethers.js で Ethereum アカウントの残高を取得するサンプルコードです。

// ethers.js をインポート
const ethers = require('ethers')

// 接続するネットワーク
let network = 'homestead'

// 接続するノード(INFURA および Etherscan のノードに同時に接続)
let provider = ethers.getDefaultProvider(network)

// 残高を取得するアドレス(何もしなくてもENSの名前解決をしてくれる!)
// piyopiyo.eth == 0xF02c1c8e6114b1Dbe8937a39260b5b0a374432bB
let address = "piyopiyo.eth"	

// 渡したアドレスの Ether 残高を取得
provider.getBalance(address)
.then((balance) => {
	let balanceInEth = ethers.utils.formatEther(balance)
	console.log("Balance:", balanceInEth)
})
  • getDefaultProvider で INFURA と Etherscan に勝手に接続してくれるのすごく楽 
  • ENS の名前解決を何もしなくてもやってくれるの感動
  • もちろん裏側の仕組みをしっておいたほうがいいとおもうけど、Dapps 開発の敷居をすごく下げてくれるのはとても良いのでは

Ref

0
0
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
0
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?