[Ethereum] DDTによる初めてのスマートコントラクト

$ truffle init
$ tree
.
├── contracts
│   └── Migrations.sol
├── migrations
│   └── 1_initial_migration.js
├── test
└── truffle-config.js

Compile: truffle compile
Migrate: truffle migrate
Test contracts: truffle test

test/greeter_test.js

const GreeterContract = artifacts.require("Greeter");

contract("Greeter", () => {
	it("has been deployed successfully", async() => {
		const greeter = await GreeterContract.deployed();
		assert(greeter, "contract was not deployed");
	});
});

$ sudo truffle test

Compiling your contracts…
===========================
> Compiling ./contracts/Migrations.sol
> Artifacts written to /tmp/test-20211113-60133-1nsib01.f2gyk
> Compiled successfully using:
– solc: 0.5.16+commit.9c3226ce.Emscripten.clang

Error: Could not find artifacts for Greeter from any sources

contracts/Greeter.sol

pragma solidity >= 0.4.0 < 0.7.0;

contract Greeter {
	
}

$ truffle test

Compiling your contracts…
===========================
> Compiling ./contracts/Greeter.sol
> Compiling ./contracts/Migrations.sol
> Artifacts written to /tmp/test-20211113-60435-1cj02xv.58fvg
> Compiled successfully using:
– solc: 0.5.16+commit.9c3226ce.Emscripten.clang

Contract: Greeter
1) has been deployed successfully
> No events were emitted

truffle testを実行するたびにコントラクトをコンパイルしてテストネットワークにデプロイする
ネットワークに追加するにはマイグレーションを作成する必要がある

migrations/2_deploy_greeter.js

const GreeterContract = artifacts.require("Greeter");

module.exports = function(deployer){
	deployer.deploy(GreeterContract);
}

$ truffle test

Compiling your contracts…
===========================
> Compiling ./contracts/Greeter.sol
> Compiling ./contracts/Migrations.sol
> Artifacts written to /tmp/test-20211113-63614-1lnqpgc.prxq
> Compiled successfully using:
– solc: 0.5.16+commit.9c3226ce.Emscripten.clang

Contract: Greeter
✓ has been deployed successfully

1 passing (42ms)

contactをデプロイすると、テストをパスする