[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をデプロイすると、テストをパスする

[Ethereum] 開発環境の準備

イーサリアムクライアントとNode.jsを使う
Node.jsはTuffle用のJS環境
ブロックチェーンとのやりとりはJSON-RPCを使う
今回はParityを使う

### Parityのインストール(ubuntu)

$ bash <(curl https://get.parity.io -L)
  % Total    % Received % Xferd  Average Speed   Time    Time     Time  Current
                                 Dload  Upload   Total   Spent    Left  Speed
  0     0    0     0    0     0      0      0 --:--:-- --:--:-- --:--:--     0curl: (6) Could not resolve host: get.parity.io

あれ…

### Parityのインストール(ubuntu)
$ sudo apt update
$ sudo apt install snapd
$ sudo snap install parity
parity v2.7.2 from Parity DevOps (devops-parity) installed

動作テスト
$ parity –chain=goerli
2021-12-13 02:44:58 UTC Starting Parity-Ethereum/v2.7.2-stable-2662d19-20200206/x86_64-unknown-linux-gnu/rustc1.41.0
2021-12-13 02:44:58 UTC Keys path /home/vagrant/snap/parity/16707/.local/share/io.parity.ethereum/keys/goerli
2021-12-13 02:44:58 UTC DB path /home/vagrant/snap/parity/16707/.local/share/io.parity.ethereum/chains/goerli/db/b1d518969eab529d
2021-12-13 02:44:58 UTC State DB configuration: fast
2021-12-13 02:44:58 UTC Operating mode: active
2021-12-13 02:44:59 UTC Configured for Görli Testnet using Clique engine

### MetaMask
JSON-RPCを通じてブロックチェーンのやり取りをする
12単語のハーモニックを格納できる

### node.js
$ curl -sL https://deb.nodesource.com/setup_14.x -o nodesource_setup.sh
$ sudo bash nodesource_setup.sh
$ sudo apt install nodejs
$ node -v
v14.18.2

### Truffle Suite
$ npm install -g truffle@5.1.31
// npm install -g truffle

### Ganache
自分専用のブロックチェーンであり、JSON-RPCサーバを実行する

なんだこれえええええええええええええ

[Ethereum] 基礎

– EVM上で動く 
– Solidityはスマートコントラクトの開発によく使われている
– イーサリアムが評価されたのはスマートコントラクトをデプロイ・実行するためのプラットフォームを提供した為
– 世界中のイーサリアムノードで接続している
– パブリックイーサリアムネットワークに接続するには、ブルーノード(信頼性の高いIP)のリストがある
– フォークした場合、最も長いチェーンが勝つ
– Proof-of-stake: Ethereumを提供する
– トランザクション手数料をガスと言う
– トランザクションの処理は13秒に1つ程度
– メモリプールが起きた場合、高いガスを払うと、処理される
– メモリプールに複数のトランザクションが含まれている場合、ブロック作成者はナンスによってトランザクションを決める
– チェーンに新しいブロックが追加されてからトランザクションが確定
– ハードフォークを行なうときは議論になり、2016年のハードフォークで以前のプロトコルをイーサリアムクラシックと呼ぶ
– 1Gweiは10億ウェイで1イーサ
– プアブリックイーサリアムネットワークはmainnet、ステージング環境はtestnetでfaucetという無料配布の仕組みがある
– EVMのオペコードはPUSH1、MLOADといった命令があり、ガス費用が割り当てられる
– gasPriceを設定することができる フロントランニングでgasPriceを設定する
– gas属性とgasPrice属性が含まれていないといけない

### アカウント
– 基本的トランザクションはEOA間でのイーサ送金

### コントラクト
オブジェクト指向スタイル、 状態変数と関数
読み取り変数をpureとview

### ブロック作成者とトランザクション
gasPrice, origin, data, sender, sig, value, number, timestamp, blockhash, difficulty, gaslimit, coinbase
ブロック作成者はトランザクションの順序を操作できる

### トランザクション署名
秘密鍵、暗号署名はMetaMaskというウォレットソフトウェアに任せる
nonce, gasPrice, gas, to, value, data, chainId

### トークン
所有権を意味する

– ERC-20
相互に交換可能
totalSupply(), balanceOf(address _owner), transfer(address _to, unit256 _value), transferFrom(address _from, address _to, unit256 _value), approve(address _spender, unit256 _value), allowance(address _owner, address _spender)

– ERC-721
互いに異なっているトークン
balanceOf(address _owner), ownerOf(unit256 _tokenId), safeTranferFrom(address _from, address _to, unit256, _token_Id, bytes data), safeTranferFrom(address _from, address _to, unit256 _tokenId), tranferFrom(address _from, address _to, unit256 _tokenId), approve(address _approved, unit256 _tokenId), setApprovalForAll(address _operator, bool _approved), getApproved(unit256 _tokenId), isApprovedForAll(address _owner, address _operator)

– サプライチェーン
生産の条件やルートなどが記載される

### 永続的な記録
航空日誌を記録する など

チェーン上に記録できるのは、色々な機能としてできそう