Skip to content

Instantly share code, notes, and snippets.

@emmaodia
Created September 8, 2021 05:22
Show Gist options
  • Save emmaodia/a81a39e38da029f7bd82e28e1c0e87a2 to your computer and use it in GitHub Desktop.
Save emmaodia/a81a39e38da029f7bd82e28e1c0e87a2 to your computer and use it in GitHub Desktop.
Created using remix-ide: Realtime Ethereum Contract Compiler and Runtime. Load this file by pasting this gists URL or ID at https://remix.ethereum.org/#version=soljson-v0.8.4+commit.c7e474f2.js&optimize=false&runs=200&gist=
// SPDX-License-Identifier: GPL-3.0
pragma solidity ^0.8.4;
contract Coin {
// The keyword "public" makes variables
// accessible from other contracts
address public minter;
address public faucet;
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);
error wrongFaucetBalanceRequest(uint requested, string available);
// Sends an amount of existing coins
// from any caller to an address
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);
}
//Sends an amount of newly minted tokens to a designated faucet address
//Can only be called by the contract creator
function fundFaucet(address _to, uint amount) public {
require(msg.sender == minter);
faucet = _to;
balances[faucet] += amount;
}
//An address can request from token from the faucet
//Requested tokens must be < 1000 per address
function requestFundsFromFaucet(address _to, uint amount) public {
if (amount > 1000 )
revert wrongFaucetBalanceRequest({
requested: amount,
available: "You can not withdraw more than 1000 tokens. Thank you"
});
balances[faucet] -= amount;
balances[_to] += amount;
emit Sent(faucet, _to, amount);
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment