概要
作成したL1 Blockchain上にコントラクトをデプロイしていきます。 事前にコマンドとして用意されているものを使用したり、任意のコントラクトを自身で作成してデプロイする手順を確認します。
コントラクトのdeploy
スマートコントラクトをデプロイするためのコマンドです。
avalanche contract deploy [subcommand] [flags]ERC20のdeploy
指定したL1 Blockchain上にERC20トークンのコントラクトをデプロイします。
ネットワーク指定(--local/--testnet/--mainnet/--devnet など)と、デプロイ先チェーン指定(--blockchain/--blockchain-id/--c-chain/--rpc など)を組み合わせます。
以下のコマンドを実行してください。
avalanche contract deploy erc20 [subcommand] [flags]どのネットワークを使用するか聞かれるので、「Local Network」を選択してください。
? Choose a network for the operation:
▸ Local Network
Devnet
Fuji Testnet
Mainnetデプロイ先のL1 Blockchainの選択を求められるので、「Blockchain chain1」を選択してください。 「chain1」は作成したL1 Blockchainの名前です。
? Where do you want to Deploy the ERC-20 Token?:
C-Chain
▸ Blockchain chain1
Customデプロイを行うアドレスについて聞かれるので、チェーン作成時に生成した鍵を指定するので、「Use the private key of the Genesis Allocated address...」を選択してください。
RPC Endpoint: http://127.0.0.1:63149/ext/bc/2MSr5Z6NWFGALEkC6GYzifQ4jq53peXf1MyBpoDrpJXHN797QV/rpc
A private key is needed to pay for the contract deploy fees.
It will also be considered the owner address of the contract, beign able to call
the contract methods only available to owners.
Use the arrow keys to navigate: ↓ ↑ → ←
? Which private key do you want to use to deploy the contract?:
▸ Use the private key of the Genesis Allocated address 0x8db97C7cEcE249c2b98bDC0226Cc4C2A57BF52FC
Get private key from an existing stored key (created from avalanche key create or avalanche key import)
CustomデプロイするERC20コントラクトの初期化関数に渡す必要がある情報を設定していきます。
まずは、トークンのシンボル名を聞かれているので、「TESTT01」など好きな値を入力してください。
Which is the token symbol?
✗ Token symbol: █次に最大発行量を幾つにするか聞かれるので、「100000」など好きな値を入力してください。
Which is the total token supply?
✗ Token supply: █このコントラクトは、先ほど設定した最大発行量分のトークンをコントラクトデプロイ時に全て発行するようになっているので、発行先アドレスの入力を求められます。 こちらもチェーン作成時に生成した鍵を指定するので、「Use the Genesis Allocated address...」を選択してください。
? Which address do you want to receive the total token supply?:
▸ Use the Genesis Allocated address 0x8db97C7cEcE249c2b98bDC0226Cc4C2A57BF52FC
Get address from an existing stored key (created from avalanche key create or avalanche key import)
Custom以下のように出力されればERC20コントラクトのデプロイは成功しています。
Token Address: 0x4Ac1d98D9cEF99EC6546dEd4Bd550b0b287aaD6D
ERC20 Contract Successfully Deployed!以下のようにいくつかオプションがあるので1つずつ簡単に説明していきます。
--blockchain string deploy the ERC20 contract into the given CLI blockchain
--blockchain-id string deploy the ERC20 contract into the given blockchain ID/Alias
--c-chain deploy the ERC20 contract into C-Chain
--cluster string operate on the given cluster
--devnet operate on a devnet network
--endpoint string use the given endpoint for network operations
-f, --fuji testnet operate on fuji (alias to testnet
--funded string set the funded address
--genesis-key use genesis allocated key as contract deployer
-h, --help help for erc20
--key string CLI stored key to use as contract deployer
-l, --local operate on a local network
-m, --mainnet operate on mainnet
--private-key string private key to use as contract deployer
--rpc string deploy the contract into the given rpc endpoint
--supply uint set the token supply
--symbol string set the token symbol
-t, --testnet fuji operate on testnet (alias to fuji)
--config string config file (default is $HOME/.avalanche-cli/config.json)--blockchain stringAvalanche CLIで管理しているL1 Blockchain名を指定してコントラクトをデプロイします(例:chain1)。--blockchain-id stringブロックチェーン ID(CB58やエイリアス)を直接指定してデプロイします。--c-chainC-Chainへデプロイします。--cluster stringクラスタ(Avalanche CLIで命名したノード集合)を指定して操作します。--devnetDevnetへデプロイします。--endpoint stringネットワーク操作に使うエンドポイント(接続先 URL)を指定します。-f, --fujiFuji(テストネット)へデプロイします。--testnetのエイリアスです。--funded string資金を支払うアドレス(十分な残高があり手数料を支払うアドレス)を指定します。--genesis-keyGenesis 割当キー(ローカルの初期配布鍵)をデプロイ実行者として使用します。--key stringAvalanche CLIに保存済みのキーストア名を指定し、その鍵をデプロイ実行者として使います。-l, --localローカルネットワークへデプロイします。-m, --mainnetMainnetで操作します。--private-key string秘密鍵(16進などの生値)を直接指定し、デプロイ実行者として使います。--rpc stringデプロイに使うRPC エンドポイントを指定します(例:http://127.0.0.1:8545)。--supply uintトークン総供給量を指定します。--symbol stringトークンシンボル(例:TK01)を指定します。-t, --testnetTestnetで操作します。--fujiのエイリアスです。--config stringAvalanche CLIの設定ファイルを指定します(既定:$HOME/.avalanche-cli/config.json)。
任意のコントラクトデプロイ
自身で用意したコントラクトをデプロイして動作を確認したい場合があります。 その時は、コントラクトの開発から行い、ローカルで作成したL1 Blockchain上にデプロイします。
コントラクトの確認
まずはコントラクトの確認からしていきます。 以下のGithubにアクセスしてください。
以下の README.md に使い方がまとめられています。
今回デプロイするコントラクトは以下のシンプルなERC721コントラクトです。
コントラクトデプロイ準備
コントラクトの確認ができたので、次にコントラクトをデプロイするための準備をしていきます。
秘密鍵の設定
まずは、README.md に記載されているように以下のコマンドで .env ファイルを作成してください。
cp .env.example .env作成したら、PRIVATE_KEY に自信がMetamaskなどで管理しているアドレスの秘密鍵を設定する必要があるので、「0x0000000000000000000000000000000000000000000000000000000000000000」の 0x 以降に貼り付けてください。
- 例
PRIVATE_KEY=0x123...RPCの設定
次にデプロイ先のL1 BlockchainのRPC URLを設定していきます。
まずは、作成したL1 BlockchainのRPC URLを確認します。 以下のコマンドを実行してください。 「chain name」には、デプロイ先となるL1 Blockchainの名前を入れてください。
avalanche blockchain describe <chain name>- 例
avalanche blockchain describe chain1出力の最後に以下のように表示されている部分があります。 この部分の「Network RPC URL」に表示されている、「**http://127.0.0.1:63149/ext/bc/2MSr5Z6NWFGALEkC6GYzifQ4jq53peXf1MyBpoDrpJXHN797QV/rpc**」部分をコピーしてください。
+--------------------------------------------------------------------------------------------------------+
| WALLET CONNECTION |
+-----------------+--------------------------------------------------------------------------------------+
| Network RPC URL | http://127.0.0.1:63149/ext/bc/2MSr5Z6NWFGALEkC6GYzifQ4jq53peXf1MyBpoDrpJXHN797QV/rpc |
+-----------------+--------------------------------------------------------------------------------------+
| Network Name | chain1 |
+-----------------+--------------------------------------------------------------------------------------+
| Chain ID | 11 |
+-----------------+--------------------------------------------------------------------------------------+
| Token Symbol | TK01 |
+-----------------+--------------------------------------------------------------------------------------+
| Token Name | TK01 Token |
+-----------------+--------------------------------------------------------------------------------------+コピーができたら、AVALANCHE_L1_RPC_URL に入力してください。
- 例
AVALANCHE_L1_RPC_URL=http://127.0.0.1:63149/ext/bc/2MSr5Z6NWFGALEkC6GYzifQ4jq53peXf1MyBpoDrpJXHN797QV/rpcコントラクトのデプロイ
コントラクトのデプロイの準備ができたので、次にコントラクトをデプロイしていきます。
simple-erc721 で以下のコマンドを実行してください。
forge script script/Deploy.s.sol:DeployScript \
--rpc-url avalanche_l1 \
--broadcast以下のようにな出力がされていればデプロイ成功です。
[⠊] Compiling...
No files changed, compilation skipped
Script ran successfully.
== Logs ==
SimpleNFT deployed at: 0xa4DfF80B4a1D748BF28BC4A271eD834689Ea3407
Name: Simple NFT
Symbol: SNFT
Base URI: https://api.example.com/metadata/
## Setting up 1 EVM.
==========================
Chain 11
Estimated gas price: 50.000000001 gwei
Estimated total gas used for script: 1636398
Estimated amount required: 0.081819900001636398 ETH
==========================
##### 11
✅ [Success] Hash: 0xa7595c22c03b4630fe6de9270a130cf916b774ad839811ec38e7a6343e720903
Contract Address: 0xa4DfF80B4a1D748BF28BC4A271eD834689Ea3407
Block: 11
Paid: 0.031469200001258768 ETH (1258768 gas * 25.000000001 gwei)
✅ Sequence #1 on 11 | Total Paid: 0.031469200001258768 ETH (1258768 gas * avg 25.000000001 gwei)
==========================
ONCHAIN EXECUTION COMPLETE & SUCCESSFUL.
Transactions saved to: /Users/<username>/Desktop/work/ava/avalanche-sample/simple-erc721/broadcast/Deploy.s.sol/11/run-latest.json
Sensitive values saved to: /Users/<username>/Desktop/work/ava/avalanche-sample/simple-erc721/cache/Deploy.s.sol/11/run-latest.json0xa4DfF80B4a1D748BF28BC4A271eD834689Ea3407 というのがデプロイしたコントラクトのアドレスです。
11 というのはL1 Blockchainを作成した時のチェーンIDです。
これにより、自身が作成したL1 Blockchainにデプロイされたことが確認できました。
念の為、トランザクションの情報を取得して確認してみます。 以下のコマンドを実行してください。 「トランザクションハッシュ」には、先ほどの出力の中の「✅ [Success] Hash:」の値を入力してください。
make receipt <トランザクションハッシュ>- 例
make receipt 0xa7595c22c03b4630fe6de9270a130cf916b774ad839811ec38e7a6343e720903以下のように出力されていれば、先ほどのデプロイがちゃんと実行されていることを確認できます。
Transaction receipt for 0xa7595c22c03b4630fe6de9270a130cf916b774ad839811ec38e7a6343e720903:
blockHash 0x8d5aca3bd5ce6f7cc2c5c56e21ddb243b61a7f78d8cc88683cf4a0f43dd5260c
blockNumber 11
contractAddress 0xa4DfF80B4a1D748BF28BC4A271eD834689Ea3407
cumulativeGasUsed 1258768
effectiveGasPrice 25000000001
from 0x8db97C7cEcE249c2b98bDC0226Cc4C2A57BF52FC
gasUsed 1258768
logs [{"address":"0xa4dff80b4a1d748bf28bc4a271ed834689ea3407","topics":["0x8be0079c531659141344cd1fd0a4f28419497f9722a3daafe3b4186f6b6457e0","0x0000000000000000000000000000000000000000000000000000000000000000","0x0000000000000000000000008db97c7cece249c2b98bdc0226cc4c2a57bf52fc"],"data":"0x","blockHash":"0x8d5aca3bd5ce6f7cc2c5c56e21ddb243b61a7f78d8cc88683cf4a0f43dd5260c","blockNumber":"0xb","transactionHash":"0xa7595c22c03b4630fe6de9270a130cf916b774ad839811ec38e7a6343e720903","transactionIndex":"0x0","logIndex":"0x0","removed":false}]
logsBloom 0x00000000000000000000000000000000000000000000000000800000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000001000000000000000000000000000000000000020000000000000000000800000000000000000000000000080000400000000000000000000000000000000000000000000000000008000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000001000000000000800000000000000020000000000000000000000000000000000000000001000000000080000000000000
root
status 1 (success)
transactionHash 0xa7595c22c03b4630fe6de9270a130cf916b774ad839811ec38e7a6343e720903
transactionIndex 0
type 2
blobGasPrice
blobGasUsed
authorizationList logs の address 部分に先ほどデプロイした時の出力内で表示されていたアドレスと同じアドレスが格納されているはずです。
mint実行
では、本当にデプロイされたかを確認するためにコントラクトを実行してみます。
.env の NFT_ADDRESS にデプロイしたコントラクトのアドレスが入力されていることを確認してください。
もし入力されていない場合は、以下のように先ほどデプロイしたコントラクトのアドレスを設定してください。
- 例
NFT_ADDRESS=0xa4DfF80B4a1D748BF28BC4A271eD834689Ea3407設定が確認できたら、以下のコマンドを実行してください。 「mint先アドレス」には、NFTを発行したいアドレスを入力してください。
make mint <mint先アドレス>- 例
make mint 0xAe451f1873B4C4D7263631Bf0ea141D54Ba39eEa以下のように出力されていれば実行成功です。
blockHash 0xe802ee635bae56bdc783f199229c89b487a3b7fd17a448bb40b0dbc5a0dc0fa8
blockNumber 12
contractAddress
cumulativeGasUsed 93609
effectiveGasPrice 25000000001
from 0x8db97C7cEcE249c2b98bDC0226Cc4C2A57BF52FC
gasUsed 93609
logs [{"address":"0xa4dff80b4a1d748bf28bc4a271ed834689ea3407","topics":["0xddf252ad1be2c89b69c2b068fc378daa952ba7f163c4a11628f55a4df523b3ef","0x0000000000000000000000000000000000000000000000000000000000000000","0x000000000000000000000000ae451f1873b4c4d7263631bf0ea141d54ba39eea","0x0000000000000000000000000000000000000000000000000000000000000000"],"data":"0x","blockHash":"0xe802ee635bae56bdc783f199229c89b487a3b7fd17a448bb40b0dbc5a0dc0fa8","blockNumber":"0xc","transactionHash":"0x0b5f67f400d5dd01fec6b0cc011a6cc975ebad76e451b4bf1078067adceb7828","transactionIndex":"0x0","logIndex":"0x0","removed":false}]
logsBloom 0x00000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000200000000000000000000008000000000000000000000000000000000000000000000000020000000000000000000800000000000000000000000010080000000000000000000000000000000000000000000000000000000008000000000000000000000000000000000000000000000000000000000000000000000000000000000002000000000000000000000001000000000000000000000040200020000000000000000000000000000000000000000000000000000000000000000000
root
status 1 (success)
transactionHash 0x0b5f67f400d5dd01fec6b0cc011a6cc975ebad76e451b4bf1078067adceb7828
transactionIndex 0
type 2
blobGasPrice
blobGasUsed
authorizationList
to 0xa4DfF80B4a1D748BF28BC4A271eD834689Ea3407
make: *** No rule to make target `0xAe451f1873B4C4D7263631Bf0ea141D54Ba39eEa'. Stop.status が 1 (success) となっているので、実行が成功していることがわかります。
では、次に本当にMintされているかを確認するために、NFTの保有数を確認してみます。 以下のコマンドを実行してください。 「mint先アドレス」には、先ほどMint実行時に指定したアドレスを入力してください。
make balance <mint先アドレス>- 例
make mint 0xAe451f1873B4C4D7263631Bf0ea141D54Ba39eEa以下のように「1」と出力されていれば、NFTを1つ保有しているということなので、ちゃんとMintが実行されていることが確認できます。
Balance of 0xAe451f1873B4C4D7263631Bf0ea141D54Ba39eEa:
1
make: *** No rule to make target `0xAe451f1873B4C4D7263631Bf0ea141D54Ba39eEa'. Stop.バリデータコントラクトのデプロイ
L1 Blockchain上にValidator Manager(バリデータ管理)コントラクトをデプロイし、PoAまたはPoSの初期バリデータ集合を設定します。
signature aggregator は、バリデータの署名を集約して検証に使う署名集約プロセスのことです。
以下のコマンドを実行してください、
「chain name」には、Validator ManagerコントラクトをデプロイしたいL1 Blockchainの名前を入力してください。
avalanche contract initValidatorManager <chain name>- 例
avalanche contract initValidatorManager chain1ネットワークの選択を求められるので、「Local Network」を選択してください。
? Choose a network for the operation:
▸ Local Network
Devnet
Fuji Testnet
MainnetValidator Managerコントラクトのデプロイと初期化のためにガス代を支払うためのアドレスの選択を求められるので、「Use the private key of the Genesis Allocated address...」か「Get private key from an existing stored key...」のどちらかを選択してください。
? Which private key do you want to use to pay for initializing Validator Manager contract? (Uses Blockchain gas token)?:
▸ Use the private key of the Genesis Allocated address 0x8db97C7cEcE249c2b98bDC0226Cc4C2A57BF52FC
Get private key from an existing stored key (created from avalanche key create or avalanche key import)
Customしかし、既にValidator Managerコントラクトがデプロイされているため、以下のようにエラーになります。 Validator Managerコントラクトがない場合にこのコマンドが役に立ちます。
Initializing Proof of Authority Validator Manager contract on blockchain chain1
Error: failure initializing validators set on poa manager: validators set already initialized (txHash=0xac0783aaef8974578c7bb14cf58d2f789940d96e485e7f165bf23c7ae81edfdc)以下のようにいくつかオプションがあるので1つずつ簡単に説明します。
--aggregator-allow-private-peers allow the signature aggregator to connect to peers with private IP (default true)
--aggregator-extra-endpoints strings endpoints for extra nodes that are needed in signature aggregation
--aggregator-log-level string log level to use with signature aggregator (default "Debug")
--aggregator-log-to-stdout dump signature aggregator logs to stdout
--cluster string operate on the given cluster
--devnet operate on a devnet network
--endpoint string use the given endpoint for network operations
-f, --fuji testnet operate on fuji (alias to testnet
--genesis-key use genesis allocated key as contract deployer
--key string CLI stored key to use as contract deployer
-l, --local operate on a local network
-m, --mainnet operate on mainnet
--pos-maximum-stake-amount uint (PoS only) maximum stake amount (default 1000)
--pos-maximum-stake-multiplier uint8 (PoS only )maximum stake multiplier (default 1)
--pos-minimum-delegation-fee uint16 (PoS only) minimum delegation fee (default 1)
--pos-minimum-stake-amount uint (PoS only) minimum stake amount (default 1)
--pos-minimum-stake-duration uint (PoS only) minimum stake duration (in seconds) (default 100)
--pos-reward-calculator-address string (PoS only) initialize the ValidatorManager with reward calculator address
--pos-weight-to-value-factor uint (PoS only) weight to value factor (default 1)
--private-key string private key to use as contract deployer
--rpc string deploy the contract into the given rpc endpoint
-t, --testnet fuji operate on testnet (alias to fuji)--aggregator-allow-private-peers署名集約プロセスがプライベートIP(例: 192.168.x.x)のピアに接続することを許可します。 デフォルトはtrueです。--aggregator-extra-endpoints strings署名集約に追加で必要なノードのエンドポイント群を指定します(カンマ区切りなど)。--aggregator-log-level string署名集約プロセスのログレベルを指定します。 デフォルトは"Debug"です。--aggregator-log-to-stdout署名集約プロセスのログをコンソールへ出します。--cluster string操作対象のクラスタ名を指定します(Avalanche CLIで名前を付けたノード集合)。--devnetDevnetを対象にします。--endpoint stringネットワーク操作に使うRPCエンドポイントURLを指定します。-f, --fujiFujiを対象にします。--testnetのエイリアスです。--genesis-keyネットワーク作成時に作成された鍵(ローカル初期配布キー)をコントラクトのデプロイ鍵として使います。--key stringAvalanche CLIに保存済みのキー名を指定し、コントラクトのデプロイ鍵として使います。-l, --localローカルネットワークを対象にします。-m, --mainnetMainnetを対象にします。--pos-maximum-stake-amount uint(PoSのみ) 1バリデータの最大ステーク量を指定します。 デフォルトは1000です。--pos-maximum-stake-multiplier uint8(PoSのみ) 最大ステーク倍率を指定します(ベースに対し何倍まで許容するか)。 デフォルトは1です。--pos-minimum-delegation-fee uint16(PoSのみ) 最小デリゲーション手数料(委任者が支払う手数料の下限)を指定します。 デフォルトは1です。--pos-minimum-stake-amount uint(PoSのみ) 最小ステーク量を指定します。 デフォルトは1です。--pos-minimum-stake-duration uint(PoSのみ) 最小ステーク期間(秒)を指定します。 デフォルトは100です。--pos-reward-calculator-address string(PoSのみ) 報酬を計算するリワード計算コントラクトのアドレスを指定し、Validator Managerコントラクトをその設定で初期化します。--pos-weight-to-value-factor uint(PoSのみ) 重み(weight)→価値(value)変換係数を指定します。 デフォルトは1です。--private-key string秘密鍵を直接指定し、コントラクトのデプロイ鍵として使います。--rpc stringデプロイに使うRPC エンドポイントを指定します。-t, --testnetTestnetを対象にします。--fujiのエイリアスです。