//tips
暑すぎた。
//smart contract
先の内容につづけてTruffle, Mocha, Chaiを利用してdeployの流れについても確認。
cryptozombies.ioもまだ開発途中なので、ページを変えるときはURLに数値をダイレクト入力しないと元の場所に戻れないなどの問題がある。あとは途中までしか開発されていないので開発されている部分をここからは英語で確認していく形になる。
https://cryptozombies.io/en/lesson/11/chapter/1
touch test/CryptoZombies.jsを実行してテストを実施していく。
まずはスマートコントラクトをコンパイルするときはabiを含む JSON fileが生成され、buildフォルダ下に格納される。それから、deployをmigration機能を使って行う。
you compile a smart contract, the Solidity compiler generates a JSON file (referred to as build artifacts) which contains the binary representation of that contract and saves it in the build/contracts folder.
Next, when you run a migration, Truffle updates this file with the information related to that network.
コントラクトをこのようにテストできる。artifacts.requireの部分はコンパイル部分build artifactsから引っ張りますよということのよう。デプロイ前にこれらの検証をしておくことでデプロイ時の失敗をなくせることができる。テスト環境はGanacheで良い。
const CryptoZombies = artifacts.require("CryptoZombies");
contract("CryptoZombies", (accounts) => {
it("should be able to create a new zombie", () => {
})
})
contract abstractionのままではチェーンと対話できないので、コントラクトをインスタンス化してJavaScriptオブジェクトとする。
const contractInstance = await MyAwesomeContract.new();とすることでインスタンス化できる。
const zombieNames = ["Zombie #1", "Zombie #2"];
contractInstance.createRandomZombie(zombieNames[0]);
テスト環境でartifacts.requireを利用するとTruffleが自動的にログを生成してくれてresult.logs[0].args.name. の形でid、_dna、tx、receipt.statusなどを受け取ることが可能。
下記テストをtruffle testで実行すると、Aliceに最初のゾンビを与えることができる。
contract("CryptoZombies", (accounts) => {
let [alice, bob] = accounts;
it("should be able to create a new zombie", async () => {
const contractInstance = await CryptoZombies.new();
const result = await contractInstance.createRandomZombie(zombieNames[0], {from: alice});
assert.equal(result.receipt.status, true);
assert.equal(result.logs[0].args.name, zombieNames[0]);
})