//tips
//smart contract
なぜturffleを使用するのかの簡単な説明があった。
・easy smart contract compilation
・automated ABI generation
・integrated smart contract testing - there's even support for Mocha and Chai!
・support for multiple networks - code can be deployed to Rinkeby, Ethereum or even to Loom.
近頃はhardhatの使用も聞くが、Truffle と異なり、 Hardhat 自体が ローカルイーサリアムネットワークを構築でき、Ganacheなどを使わずともHardhat のみでSolidity で作ったスマートコントラクトのコンパイル・テスト・デプロイが可能。
本番環境前のテストがやりやすいという感じか。
truffle initを利用することでパッケージをインストールした後にtruffle-hdwallet-providerも一緒にインストールしたが、これはinfuraにはプライベートキーを管理する機能がなく、トランザクションへのサインを実行することができない。これを補うためにtruffle-hdwallet-providerを入れているとのこと。
Since deploying a smart contract requires Truffle to sign transactions, we are going to need a tool called truffle-hdwallet-provider. Its only purpose is to handle the transaction signing.
EVMはプログラミング言語を理解できないので01ax..などのバイトコードにコンパイル(翻訳)し直してあげる必要がある。
The Ethereum Virtual Machine can't directly understand Solidity source code as we write it. Thus, we need to run a compiler that will "translate" our smart contract into machine-readable bytecode.
truffle compileを実行するとthe build artifactsを生成し、./build/contracts に配置する。
This command should create the build artifacts and place them in the ./build/contracts directory.
Initで生成された./contracts/1_initial_migration.js. の中身を確認。
var Migrations = artifacts.require("./Migrations.sol");
こちらでMigrations contractへの接続を可能にする。
その後にdeployerを引数として、オブジェクトを受け取る関数をエクスポート。
module.exports = function(deployer) {
deployer.deploy(Migrations);
};
この{}の中身は自分とtruffle開発エンジンのインターフェース・共通基盤として機能する。
This object acts as an interface between you (the developer) and Truffle's deployment engine.
同様に下記のようにコントラクトもオブジェクトを格納した機能をエクスポートできる。
var CryptoZombies = artifacts.require("./CryptoZombies.sol");
module.exports = function(deployer) {
deployer.deploy(CryptoZombies);
};
さらに進め、configuration fileの方も編集する。トランザクションの署名をできるようにtruffle-hdwallet-providerにも接続しておく。
var HDWalletProvider = require("truffle-hdwallet-provider");
var mnemonic = "onions carrots beans ...";
mnemonicを通常で組み込むのは危険なので.gitignoreから参照させるようにする。