LoginSignup
9
9

More than 5 years have passed since last update.

Ethereum Web3の使い方

Posted at

Ethereum Web3.js v1.0について解説した文献が英語でもほとんど存在しないので、書きます。Beta版だから、解説するのも面倒だというのもあるけど、今後1.0が主流になるんだから、書きます。

これは下記記事のおまけ
https://note.mu/missmonacoin/n/n4c3baaa2351c

下準備


const Web3 = require('web3')

const hdkey = require('ethereumjs-wallet/hdkey')
const web3 = new Web3()

Ether残高を取得

web3.eth.getBalance(address).then(balanceWei=>{
    const balanceEth=web3.utils.fromWei(balanceWei, "ether")
    console.log(`Your balance is ${balanceWei} (${balanceEth}ETH)`)
})

スマートコントラクトを読み込み(読み込み系)

const erc20ABI=[/* JSON Interface(ABI)がここに入る。ここではERC20のtransfer(address)があると家庭 */]
const contractAddress="0x.............."
const callerAddress="0x..........." //関数を呼び出す人は誰かを指定


const contract =new web3.eth.Contract(erc20ABI,contractAddress,{
  from:callerAddress
})
const balance=async contract.methods["balanceOf"](
              callerAddress
            ).call()

スマートコントラクトを読み込み(書き込み系)

const erc20ABI=[/* JSON Interface(ABI)がここに入る。ここではERC20のtransfer(address)があると家庭 */]
const contractAddress="0x.............."
const callerAddress="0x..........." //関数を呼び出す人は誰かを指定

const nonce = async web3.eth.getTransactionCount(callerAddress) //Ethereum特有のTX整理番号
const tx={
            nonce,
            from:callerAddress,
            gasPrice: web3.utils.numberToHex(100000000000000000),
            gas: web3.utils.numberToHex(50000),
            chainId: CHAIN_ID,
            to: contractAddress,
            value: "0x0"
          }
const contract =new web3.eth.Contract(erc20ABI,contractAddress,{
  from:callerAddress
})
tx.data=contract.methods["transfer"](
              this.sendAddress,
              10000000 //実際に使うときはdecimalsも注意
            ).encodeABI()
const signedTx = async web3.eth.accounts.privateKeyToAccount("0x"+hdkey.fromMasterSeed(seed)
    .derivePath(HD_DERIVATION_PATH).getWallet()
    .getPrivateKey().toString("hex"))
    .signTransaction(tx)
const response = async web3.eth.sendSignedTransaction(signedTx.rawTransaction)

スマートコントラクトをデプロイ

const erc20ABI=[/* JSON Interface(ABI)がここに入る。ここではERC20のtransfer(address)があると家庭 */]
const contractAddress="0x.............."
const callerAddress="0x..........." //関数を呼び出す人は誰かを指定

const nonce = async web3.eth.getTransactionCount(callerAddress) //Ethereum特有のTX整理番号
const tx={
            nonce,
            from:callerAddress,
            gasPrice: web3.utils.numberToHex(100000000000000000),
            gas: web3.utils.numberToHex(50000),
            chainId: CHAIN_ID,
            to: contractAddress,
            value: "0x0"
          }
const contract =(new web3.eth.Contract([])).deploy({
          data:"0x"+this.deployment.data
        })
tx.data=contract.encodeABI()
const signedTx = async web3.eth.accounts.privateKeyToAccount("0x"+hdkey.fromMasterSeed(seed)
    .derivePath(HD_DERIVATION_PATH).getWallet()
    .getPrivateKey().toString("hex"))
    .signTransaction(tx)
const response = async web3.eth.sendSignedTransaction(signedTx.rawTransaction)

Etherを送金

const erc20ABI=[/* JSON Interface(ABI)がここに入る。ここではERC20のtransfer(address)があると家庭 */]
const from="0x.............."
const to="0x..........." //関数を呼び出す人は誰かを指定

const nonce = async web3.eth.getTransactionCount(from) //Ethereum特有のTX整理番号
const tx={
            nonce,
            from,
            gasPrice: web3.utils.numberToHex(100000000000000000),
            gas: web3.utils.numberToHex(50000),
            chainId: CHAIN_ID,
            to,
            value: web3.utils.numberToHex(web3.utils.toWei(""+sendEther, "ether"))
          }
const signedTx = async web3.eth.accounts.privateKeyToAccount("0x"+hdkey.fromMasterSeed(seed)
    .derivePath(HD_DERIVATION_PATH).getWallet()
    .getPrivateKey().toString("hex"))
    .signTransaction(tx)
const response = async web3.eth.sendSignedTransaction(signedTx.rawTransaction)

ガスリミットを計算

const erc20ABI=[/* JSON Interface(ABI)がここに入る。ここではERC20のtransfer(address)があると家庭 */]
const from="0x.............."
const to="0x..........." //関数を呼び出す人は誰かを指定

gasProm=web3.eth.estimateGas({
            from,
            to
          })

変数名がところどころ間違っているけどご愛嬌

9
9
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
9
9