3
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.

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

Last updated at Posted at 2022-04-08

ブラウザからMetamaskで接続してコントラクトを叩くのではなくスクリプト的に簡単にコントラクトを叩くコード
ethers.jsのABIを配列で簡単に定義して使えるのが超絶便利

ethers.jsでシンプルにコントラクト叩く

const ethers = require('ethers')

// InfuraのURL指定
let network = 'rinkeby' 
let provider = ethers.getDefaultProvider(network)

// WalletのPrivate key
let privateKey = 'xxxxxxxxx'
const signer = new ethers.Wallet(
    privateKey,
    provider
)

// Contract Address
const contractAddress = "xxxxxx"

// Contract ABI
const contractAbi = ["function mint(string memory tokenURI) public returns (uint256)"]
const contract = new ethers.Contract(contractAddress, contractAbi, signer)

// transaction実行
const tokenURI = "xxxxx"
const func = async () => {
    const res = await contract.mint(tokenURI)
    console.log(res)
}

func()

コントラクトに対してvalueを指定

他に指定可能な値は以下のドキュメント参照
https://docs.ethers.io/v5/api/contract/contract/#Contract-functionsCall

const options = {
  value: ethers.utils.parseEther("0.01"),
  gasLimit: 3000000
}
const res = await contract.mint(tokenURI, options)
3
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
3
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?