Skip to content

Instantly share code, notes, and snippets.

@brolag
Created September 3, 2024 01:06
Show Gist options
  • Save brolag/c0a7ce7f7a3c67155a5c85f3bd6cd26d to your computer and use it in GitHub Desktop.
Save brolag/c0a7ce7f7a3c67155a5c85f3bd6cd26d to your computer and use it in GitHub Desktop.
Coin
// SPDX-License-Identifier: GPL-3.0
pragma solidity ^0.8.26;
// This will only compile via IR
contract Coin {
// The keyword "public" makes variables
// accessible from other contracts
address public minter;
mapping(address => uint) public balances;
// Events allow clients to react to specific
// contract changes you declare
event Sent(address from, address to, uint amount);
// Constructor code is only run when the contract
// is created
constructor() {
minter = msg.sender;
}
// Sends an amount of newly created coins to an address
// Can only be called by the contract creator
function mint(address receiver, uint amount) public {
require(msg.sender == minter);
balances[receiver] += amount;
}
// Errors allow you to provide information about
// why an operation failed. They are returned
// to the caller of the function.
error InsufficientBalance(uint requested, uint available);
// Sends an amount of existing coins
// from any caller to an address
function send(address receiver, uint amount) public {
require(amount <= balances[msg.sender], InsufficientBalance(amount, balances[msg.sender]));
balances[msg.sender] -= amount;
balances[receiver] += amount;
emit Sent(msg.sender, receiver, amount);
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment