LoginSignup
0

More than 5 years have passed since last update.

⛓️solidityでブロックチェーンに文字を書いてみた⛓️

Posted at

はじめに

この記事ではsolidityというjsライクな言語をつかってブロックチェーンにメッセージを書いたり、ブロックチェーンに書かれているメッセージを取得したいと思います。

ダウンロード.png

どのバージョンのsolidityで書くかを明記

pragma solidity ^0.4.18;

contract の定義

contract Message{

}

ブロックチェーンにメッセージを書いてみる

contractのなかに 書かれたメッセージを引数にもち、その引数をブロックチェーン上に記述する関数を書く。

   string public message;

 function createMessage(string writtenMessage) public {

        message = writtenMessage;

    }

ブロックチェーンに書いたメッセージを読む

contractのなかに ブロックチェーン上に保存されているmessageという変数の値を返す関数を書く。

function readMessage() public view returns(string) {

        return message;

    }

以上でsolidityでかく処理は終わりました。あとは、このcontractをデプロイしてweb3.jsを使ってフロントエンドと繋げれば動きます! ちなみに ブロックチェーンにメッセージを書く処理はweb3.jsのmethods.myMethod.send()を使わないといけないのに対し、ブロックチェーンに書いたメッセージを読む処理はweb3.jsのmethods.myMethod.call()を使います。なぜなら 書き込むのにはethが必要ですが 読むのにはethが必要ないからです。

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
0