1
0

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.

Ethereumスマートコントラクト開発:Coinコントラクトの構築と操作

Last updated at Posted at 2023-06-16

Solidityとスマートコントラクト入門:Coinコントラクトの作成

こんにちは!今日はJavaScriptの知識があるが、Solidityやスマートコントラクトについて詳しくないエンジニアのためのガイドを提供します。今回は、EthereumのオンラインIDEであるRemixを使用します。

1. コードの説明

まずは、以下のSolidityコードを見てみましょう。

pragma solidity ^0.8.4;

contract Coin {
    address public minter;
    mapping (address => uint) public balances;

    event Sent(address from, address to, uint amount);

    constructor() {
        minter = msg.sender;
    }

    function mint(address receiver, uint amount) public {
        require(msg.sender == minter);
        balances[receiver] += amount;
    }

    error InsufficientBalance(uint requested, uint available);

    function send(address receiver, uint amount) public {
        if (amount > balances[msg.sender])
        revert InsufficientBalance({
            requested: amount,
            available: balances[msg.sender]
        });
        balances[msg.sender] -= amount;
        balances[receiver] += amount;
        emit Sent(msg.sender, receiver, amount);
    }
}

このコードはCoinというスマートコントラクトを作成します。このコントラクトは以下の機能を持ちます:

  • minter:コインを発行するアドレスを保持します。コンストラクタでこの変数はmsg.sender(コントラクトをデプロイしたアドレス)に設定されます。
  • balances:各アドレスのコインのバランスを保持するマッピングです。
  • Sent:コインが送信されたときに実行されるイベントです。
  • mint:指定されたアドレスに指定された量のコインを発行する関数です。この関数はminterからのみ呼び出すことができます。
  • send:指定されたアドレスに指定された量のコインを送信する関数です。この関数は送信者が十分なバランスを持っている場合にのみ呼び出すことができます。

2. Remix IDEでのコンパイルとデプロイ

次に、Remix IDEでこのコードをコンパイルとデプロイする方法を見てみましょう。手順は先ほどのガイドと同様です。

3. コントラクトとのやり取り

デプロイしたCoinコントラクトはmintsend関数を通じて操作できます。mint関数はコインを発行し、send関数はコインを送信します。

それでは、Happy coding!

1
0
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
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?