ethers.jsを使用してトークンを送信する
ETHERS.JS
ERC-20
トークン
初級
Kim YongJun
2021年4月6日
4 分の読書
ethers.js(5.0)を使用したトークンの送信
このチュートリアルで学ぶこと
- ethers.jsのインポート
- トークンの転送
- ネットワークの混雑状況に応じたガス代の設定
はじめに
始めるには、まずethers.jsライブラリをJavaScriptにインポートする必要があります。 ethers.js(5.0)をインクルードします。
インストール
1/home/ricmoo> npm install --save ethersブラウザでES6を使用するには次のようにします。
1<script type="module">2 import { ethers } from "https://cdn.ethers.io/lib/ethers-5.0.esm.min.js"3 // ここにコードを記述...4</script>ブラウザでES3(UMD)を使用するには次のようにします。
1<script2 src="https://cdn.ethers.io/lib/ethers-5.0.umd.min.js"3 type="application/javascript"4></script>パラメータ
contract_address: トークンのコントラクトアドレス(転送したいトークンがイーサでない場合は、コントラクトアドレスが必要となります)send_token_amount: 受取人に送る量to_address: 受取人のアドレスsend_account: 送信者のアドレスprivate_key: トランザクションに署名し、実際にトークンを転送するための送信者の秘密鍵
注意
signTransaction(tx)は、sendTransaction()の内部で実行されるため削除されました。
送信手順
1. ネットワークに接続 (テストネット)
プロバイダーの設定 (Infura)
Ropstenテストネットに接続します。
1window.ethersProvider = new ethers.providers.InfuraProvider("ropsten")2. ウォレットの作成
1let wallet = new ethers.Wallet(private_key)3. ウォレットをネットワークに接続
1let walletSigner = wallet.connect(window.ethersProvider)4. 現在のガス価格の取得
1window.ethersProvider.getGasPrice() // gasPrice5. トランザクションの定義
以下で定義されている変数は、send_token()に依存します。
トランザクションパラメータ
send_account: トークン送信者のアドレスto_address: トークンの受取人のアドレスsend_token_amount: 送信するトークンの量gas_limit: ガスリミットgas_price: ガス価格
1const tx = {2 from: send_account,3 to: to_address,4 value: ethers.utils.parseEther(send_token_amount),5 nonce: window.ethersProvider.getTransactionCount(send_account, "latest"),6 gasLimit: ethers.utils.hexlify(gas_limit), // 1000007 gasPrice: gas_price,8}6. 転送
1walletSigner.sendTransaction(tx).then((transaction) => {2 console.dir(transaction)3 alert("送信完了!")4})使用方法
1let private_key =2 "41559d28e936dc92104ff30691519693fc753ffbee6251a611b9aa1878f12a4d"3let send_token_amount = "1"4let to_address = "0x4c10D2734Fb76D3236E522509181CC3Ba8DE0e80"5let send_address = "0xda27a282B5B6c5229699891CfA6b900A716539E6"6let gas_limit = "0x100000"7let wallet = new ethers.Wallet(private_key)8let walletSigner = wallet.connect(window.ethersProvider)9let contract_address = ""10window.ethersProvider = new ethers.providers.InfuraProvider("ropsten")1112send_token(13 contract_address,14 send_token_amount,15 to_address,16 send_address,17 private_key18)すべて表示成功しました!
send_token()
1function send_token(2 contract_address,3 send_token_amount,4 to_address,5 send_account,6 private_key7) {8 let wallet = new ethers.Wallet(private_key)9 let walletSigner = wallet.connect(window.ethersProvider)1011 window.ethersProvider.getGasPrice().then((currentGasPrice) => {12 let gas_price = ethers.utils.hexlify(parseInt(currentGasPrice))13 console.log(`gas_price: ${gas_price}`)1415 if (contract_address) {16 // 一般的なトークン送信17 let contract = new ethers.Contract(18 contract_address,19 send_abi,20 walletSigner21 )2223 // トークンの数量24 let numberOfTokens = ethers.utils.parseUnits(send_token_amount, 18)25 console.log(`numberOfTokens: ${numberOfTokens}`)2627 // トークンを送信28 contract.transfer(to_address, numberOfTokens).then((transferResult) => {29 console.dir(transferResult)30 alert("トークンを送信しました")31 })32 } // ether送信33 else {34 const tx = {35 from: send_account,36 to: to_address,37 value: ethers.utils.parseEther(send_token_amount),38 nonce: window.ethersProvider.getTransactionCount(39 send_account,40 "latest"41 ),42 gasLimit: ethers.utils.hexlify(gas_limit), // 10000043 gasPrice: gas_price,44 }45 console.dir(tx)46 try {47 walletSigner.sendTransaction(tx).then((transaction) => {48 console.dir(transaction)49 alert("送信完了!")50 })51 } catch (error) {52 alert("送信に失敗しました!!")53 }54 }55 })56}すべて表示最終更新: 2023年12月4日
