1
1

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

More than 1 year has passed since last update.

5分で体験するWEB3.0の世界。Remix IDEで始めるSolidity

Last updated at Posted at 2022-07-03

WEB3.0にちょっと興味があって、
Solidity(ソリディティ)ちょっとやってみようかな〜って人向け。
WEB3.0は次のインターネットみたいな感じ。分散型インターネットのイメージ。
SolidityはそのWEB3.0の代表的な言語。もちろん他の言語もあるけど、それなりに知名度があるので使ってみてもいいと思う。

5分で終わらなかったら文句言ってください。

ローカルに環境構築するか、オンラインのIDE使うか選べる。
Remix IDEはオンラインのIDEエディタ。
今回はRemixを使う。

ここにアクセス。
https://remix.ethereum.org/

こんなのが出る。
スクリーンショット 2022-07-03 15.00.41.png

右クリでファイルを作成

スクリーンショット 2022-07-03 15.01.07.png

1.sol
pragma solidity ^0.5.0;
contract SolidityTest {
   constructor() public{
   }
   function getResult() public view returns(uint){
      uint a = 1;
      uint b = 2;
      uint result = a + b;
      return result;
   }
}

ファイルを作成して、コードを貼り付け。

スクリーンショット 2022-07-03 15.01.19.png

コンパイルする

スクリーンショット 2022-07-03 15.01.29.png

デプロイ

スクリーンショット 2022-07-03 15.01.49.png

デプロイすると、定義したgetResult関数が出現。

スクリーンショット 2022-07-03 15.02.04.png

getResultの結果を確認できる。

スクリーンショット 2022-07-03 15.02.16.png

Debugの中のdecoded outputの中に3の値が返ってきてる。

{
	"0": "uint256: 3"
}

あとはsolの構文とかを勉強すれば良い。

良さそうな記事

solidity勉強会
https://note.com/standenglish/m/mdcbd0e6e59f1

テックピットでsolを学ぶ
https://www.techpit.jp/courses/36/curriculums/37/sections/302/parts/1011

このゲームが勉強になりそう。

ゾンビのtutorialの1個目を追加しました。solの0.5系ではエラーが出るので、0.4系を使用するように変更。

2.sol

// pragma solidity ^0.5.0;
pragma solidity ^0.4.0;

contract SolidityTest {
   constructor() public{
   }
   uint dnaDigits = 16;
   uint dnaModulus = 10 ** dnaDigits;
   //structは配列
   struct Zombie {
      string name;
      uint dna;
   }
   //パブリックなゾンビ
   Zombie[] public zombies;
   event NewZombie(uint zombieId, string name, uint dna);

   //プライベートな関数は_をつける。Pythonと似てる。
   function _createZombie(string _name, uint _dna) private {
      zombies.push(Zombie(_name, _dna));
      uint id = zombies.push(Zombie(_name, _dna)) - 1;
      NewZombie(id, _name, _dna);
   }
   function _generateRandomDna(string _str) private view returns (uint) {
      //keccak256はランダム生成。uintでキャスト
      uint rand = uint(keccak256(_str));
      return rand % dnaModulus;
   }
   function createRandomZombie(string _name) public {
      uint randDna = _generateRandomDna(_name);
      _createZombie(_name, randDna);
   }

   function getResult() public view returns(uint){
      uint a = 1;
      uint b = 2;
      uint result = a + b;

      
      // 2要素の固定長の配列の場合:
      uint[2] fixedArray;
      // 別の固定長配列の例。5つの文字列を格納できる:
      string[5] stringArray;
      // 可変長配列 - 決まったサイズはないので、格納し続けることができるぞ:
      uint[] dynamicArray;
      



      return result;
   }
}


では、良いエンジニアライフを〜

1
1
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
1
1

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?