Solidity備忘録③(2021年5月19日)

Remixでsolidityを作ってみようと思います。

Remixはこっち↓

せっかくなので、まずはReadmeを読んでみようと思います。

Remix example project is present when Remix loads very first time or there are no files existing in the File Explorer.

例のプロジェクトはRemixさんからのプレゼントだそうです。

 It contains 3 directories:

3つのファイルから成り立ちます。

1. 'contracts': Holds three contracts with different complexity level, denoted with number prefix in file name.
2. 'scripts': Holds two scripts to deploy a contract. It is explained below.
3. 'tests': Contains one test file for 'Ballot' contract with unit tests in Solidity.

1.Contracts 
 ⇒3つあり、難易度が違うようです。
 まずは①のStorage.solからがいいかもですね。
 確かにコードの長さが全然違います。

2.Scripts 
 ⇒2つあり、コントラクトをデプロイするのに必要だそう。

3.tests 
 ⇒Contractsの最高難度の「Ballot」コントラクト用にあるみたい。
  であるならば、今は無視します。

SCRIPTS
The 'scripts' folder contains example async/await scripts for deploying the 'Storage' contract.
For the deployment of any other contract, 'contractName' and 'constructorArgs' should be updated (along with other code if required).
Scripts have full access to the web3.js and ethers.js libraries.

2の「Scripts」には1のContractの「Storage」用にasync/await スクリプトがあるようです。

他のコントラクトをデプロイする際には
①contractName
②constructorArgs

を更新してとのことです。
確かに「2.Scripts」のそれぞれのファイルにこの名前の定数が入っていました。

そして、Scriptsは
①web3.js 
②ehers.js

に完全にアクセスしているとのことです。

To run a script, right click on file name in the file explorer and click 'Run'. Remember, Solidity file must already be compiled.

スクリプトを動かすためにはファイル名を右クリックし、「Run」を押すとのこと。また、solidityファイルは既にコンパイルされているようです

Output from script will appear in remix terminal.

スクリプトの出力は「remix terminal」に出力されるとのことです。


◎Remix terminal

「Script」を右クリックし、「Run」を押すと、下に文字が現れました。

この下の部分が「remix terminal」なのですね。


⑤Solidityの実行

ここまで長かったですが、solidityの実行をしていきたいと思います。

 ①コードの作成

とりあえず、まずは一通りやってみようと思うので、このサイトのコードを用います。(バージョンだけは直そうと思います。)

pragma solidity ^0.4.0;
contract HelloWorld {
   string greeting = "HelloWorld";
   function sayHelloWorld() public view returns(string) {
       return greeting;
   }
}

「Contracts」⇒「New File」で「HelloWorld.sol」で新規に作成し、コードを貼り付けてみます。

②コンパイルの実行

左の「solidity compiler」を選択します。

上の「Compiler」をコードのバージョンと合わせます。

下の方に何やら✔項目も

COMPILER CONFIGURATION(コンパイラの設定)

①Auto compile(自動コンパイル)
②Enable optimization(最適化を有効にする)
③Hide warnings(警告を隠す)

②のEnable optimizationはチェックをつけてもいいような気がしますが、最初なので、何もつけずに実行
(Compile HelloWorld.sol)

待ちに待った、、、と思いきや。エラー発生

③バグの修正

こんなふうに書いてあります。

TypeError: Data location must be "memory" or "calldata" for return parameter in function, but none was given. --> contracts/HelloWorld.sol:6:50: | 6 | function sayHelloWorld() public view returns(string) { | ^^^^^^

型のエラー
⇒なにやら返り値「returns (string)」の部分に"memory"か"calldata"が必要のようです。何かを読んだわけではなさそうなので、”memory”をつけてみます。

returns(memory string)

こんな感じで、「memory」をつけてみました。

すると、またしてもエラー

ParserError: Expected type name --> contracts/HelloWorld.sol:6:50: | 6 | function sayHelloWorld() public view returns(memory string) { | ^^^^^^

解析のエラー:予期された名前
⇒使っちゃいけない名前が使われたのでしょうか?そうなるとさっきのmemoryが怪しいですね。

うーん、わからないので、そもそもコードが通るのか、「Cryptozombies」というサービスをもとにコードを使います。
⇒このあたりは5月22日の記事で扱いたいと思います。



contract ZombieFactory {
   uint dnaDigits = 16;
   uint dnaModulus = 10 ** dnaDigits;
   struct Zombie {
       string name;
       uint dna;
   }
   // ここにzombiesというパブリックな配列を定義するのだ
}

このコードを貼り付けた状態で、「Compile HelloWorkd.sol」を押すと、次のような画面になります。

スクリーンショット (119)

「solidity compiler」にカーソルを合わせると、

「Compilation finished successful with warning」(警告が出てコンピレーションは成功した)と出ました。

ちなみに警告は左下のオレンジ色の部分

スクリーンショット (120)

ソースコードの中に「SPDX ライセンス識別子」がない。
発行前に「SPDX-License-Identifier:<SPDX-License>」を入れるように考慮せよとのことです。

そして、オープンソースコードではないものには
「SPDX-License-identifier:UNLICENSED」を使うよう書いてあります。

では、このSPDXとは何なのでしょうか。

続きは次回にまわします。

サポートをしていただけたらすごく嬉しいです😄 いただけたサポートを励みに、これからもコツコツ頑張っていきます😊