コンテンツへスキップ

Visual Studio Code (手動)

Web3.pyを使ったデプロイ方法の仕組みを理解するために細かいコードを手動で書いています。

仕組みが理解できたらWeb3.pyの利用を簡素化しているBrownieを使うことを推奨します。

環境準備

  1. Visual Studio CodeMicrosoft C++ Build ToolsPythonGanacheがインストールされていなければダウンロードしてインストールする。Microsoft C++ Build Toolsをインストールする際は「C++によるデスクトップ開発」を含めるのを忘れない。
  2. Visual Studio Codeを開き、Solidityの拡張機能をインストールする。
  3. Visual Studio CodeのTerminalを開き、必要なパッケージをインストールする。
pip install black
pip install py-solc-x
pip install web3
  1. Visual Studio Codeの設定で以下の設定をする。
    ・「Format On Save」をオン
    ・「JSON > Format: Enable」をオン
    ・「Solidity: Formatter」はprettier
    ・「Python > Formatting: Provider」はblack
  2. 左側のExplorerアイコンから、ワークスペースに設定したいフォルダを選択する。
  3. Ganacheを開き、QUICKSTARTを選択する。設定からNetwork IDを1337に変更する。
  4. PowerShellを管理者として開き、Set-ExecutionPolicy RemoteSignedを実行し、yを入力する。

コントラクトの記載

ワークスペースに.solファイルを作成し、コントラクトを記載します。(以下は例)

// SPDX-License-Identifier: MIT

pragma solidity 0.8.13;

contract AccountMapping {
    // 自然数の変数を定義
    uint256 accountNumber;

    // 自然数に値を追加
    function store(uint256 _accountNumber) public returns (uint256) {
        accountNumber = _accountNumber;
        return _accountNumber;
    }

    // 自然数を確認
    function retrieve() public view returns (uint256) {
        return accountNumber;
    }
}

デプロイ用のpyコード作成

コントラクトをローカルブロックチェーンであるGanacheにデプロイしてみます。

テストネットにデプロイする場合は「HTTP Provider」と「Chain ID」とアドレスを変更します。

# 必要なライブラリをインポート
from solcx import compile_standard, install_solc
import json
from web3 import Web3

# コントラクトが書かれているsolファイルを読み込む
with open("./SimpleStorage.sol", "r", encoding="utf-8") as file:
    simple_storage_file = file.read()

# コントラクトをコンパイルする
install_solc("0.8.13")
compiled_sol = compile_standard(
    {
        # 設定可能なパラメータはDocを参照(https://docs.soliditylang.org/en/latest/using-the-compiler.html#compiler-input-and-output-json-description)
        "language": "Solidity",
        "sources": {"SimpleStorage.sol": {"content": simple_storage_file}},
        "settings": {
            "outputSelection": {
                "*": {"*": ["abi", "metadata", "evm.bytecode", "evm.sourceMap"]}
            }
        },
    },
    solc_version="0.8.13",
)

# コンパイルしたものをjsonファイルとして保存する
with open("compiled_code.json", "w") as file:
    json.dump(compiled_sol, file)

# デプロイに必要なbytecodeとABIを取得する
bytecode = compiled_sol["contracts"]["SimpleStorage.sol"]["AccountMapping"]["evm"][
    "bytecode"
]["object"]
abi = compiled_sol["contracts"]["SimpleStorage.sol"]["AccountMapping"]["abi"]

# ブロックチェーンに接続する
# Ganacheの場合は、Ganache画面に出ているRPC SERVERを設定する
w3 = Web3(Web3.HTTPProvider("http://127.0.0.1:7545"))
# テストネットの場合は、End PointをInfuraから取得するのが楽です
# w3 = Web3(Web3.HTTPProvider("https://kovan.infura.io/v3/971f96ec494347ff99b8271f8a6a8fb8"))

# Chain IDを指定する
# Ganacheの場合は1337
chain_id = 1337
# テストネットのChain IDはここ参照(https://chainlist.org/)
# chain_id = 42

# ガス代を払う自分のアドレス
my_address = "0x728a539b36243EC032518B140070a2B124fFA8a9"
# my_address = "0x10eb57969EedFeF0fd7BA022AF3552E0437d4D80"

# 秘密鍵には先頭に0xを付け加える
# 練習なのでコードに秘密鍵を含めているが本来は書くべきではない!!!
private_key = "0xdaf669ce870af2df9671b162dd7a25cc364cf54edfe27618bdcea1655cd2115e"
# private_key = "0x8c2fd76e6890ec1dbc16bf2bf8f56661b65b8c9fad64bd2ac427a548a2db3e71"

# Pythonでコントラクトを読み込む
SimpleStorage = w3.eth.contract(abi=abi, bytecode=bytecode)

# 最新のブロックを取得する
nonce = w3.eth.getTransactionCount(my_address)

# トランザクションを作成する
transaction = SimpleStorage.constructor().buildTransaction(
    {
        "gasPrice": w3.eth.gas_price,
        "chainId": chain_id,
        "from": my_address,
        "nonce": nonce,
    }
)

# トランザクションに秘密鍵で署名する
signed_txn = w3.eth.account.sign_transaction(transaction, private_key=private_key)

# トランザクションを送信してコントラクトをデプロイする
tx_hash = w3.eth.send_raw_transaction(signed_txn.rawTransaction)
tx_receipt = w3.eth.wait_for_transaction_receipt(tx_hash)

# デプロイしたコントラクトをブロックチェーンから取得する
simple_storage = w3.eth.contract(address=tx_receipt.contractAddress, abi=abi)

# Call はreturn値を得るだけで新しいトランザクションを作成しないためガス代が不要
# まだ何もコントラクトに変更を加えていないためリターン値は0であるはず
print(simple_storage.functions.retrieve().call())

# Transactはトランザクションを作成するため署名とガス代が必要
# デプロイしたコントラクトに数値を格納してみる
store_transaction = simple_storage.functions.store(15).buildTransaction(
    {
        "gasPrice": w3.eth.gas_price,
        "chainId": chain_id,
        "from": my_address,
        "nonce": nonce + 1,  # 同じnonceは使えない
    }
)
store_signed_txn = w3.eth.account.sign_transaction(
    store_transaction, private_key=private_key
)
store_txn_hash = w3.eth.send_raw_transaction(store_signed_txn.rawTransaction)
store_txn_receipt = w3.eth.wait_for_transaction_receipt(store_txn_hash)

# もう一度Callするとリターン値が15になっている
print(simple_storage.functions.retrieve().call())