2
1

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 1 year has passed since last update.

web3.jsでコントラクト叩くスクリプト例

Last updated at Posted at 2022-04-08

web3.jsでMetamask等ブラウザを利用せずにnodejsだけでコントラクトを叩くスクリプト例
ehters.js版も記載したがweb3.jsの方が複雑でわかりにくいイメージ

const Web3 = require('web3')

// ABIの読み込み
const fs = require('fs')
const metadata = JSON.parse(fs.readFileSync('./xxxxx.json', 'utf8'))

const web3 = new Web3('Infura指定')

// アカウント指定
const privateKey = 'xxxxxxxxxx'
const account = web3.eth.accounts.privateKeyToAccount(privateKey)

// コントラクト生成
const contractAddress = "xxxxxxxxxx"
const contract = new web3.eth.Contract(metadata.abi, contractAddress)

// トランザクションのデータ生成
const tokenURI = "xxxxxxxxxx"
const encodeABI = contract.methods.mint(tokenURI).encodeABI()

const func = async () => {
    const tx = { 
        to: contractAddress,
        data: encodeABI,
        gas: 1500000
    }
    const signedTx = await account.signTransaction(tx)
    const res = await web3.eth.sendSignedTransaction(signedTx.rawTransaction)
    console.log(res) 
}  

func()
2
1
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
2
1

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?