Description:
ERC20 token contract with Factory capabilities. Standard implementation for fungible tokens on Ethereum.
Blockchain: Ethereum
Source Code: View Code On The Blockchain
Solidity Source Code:
{{
"language": "Solidity",
"sources": {
"contracts/Sun.sol": {
"content": "// SPDX-License-Identifier: MIT\r
pragma solidity ^0.8.24;\r
\r
/* \r
┌────────────────────────────────────────────────────────────────────────┐\r
│ │\r
│ ███████ ██ ██ ███ ██ │\r
│ ██ ██ ██ ████ ██ │\r
│ ███████ ██ ██ ██ ██ ██ │\r
│ ██ ██ ██ ██ ██ ██ │\r
│ ███████ ██████ ██ ████ │\r
│ │\r
│ │\r
│ Holding SUN gives you free reflections. MOON uses swap taxes to buy │\r
│ the tokens it’s paired with. Those tokens are automatically reflected │\r
│ to SUN holders—connecting them with every project in MOON’s orbit. │\r
│ │\r
│ SUN behavior │\r
│ • Standard ERC-20: fixed supply, no fees, no taxes, no admin keys. │\r
│ • One addition: a lightweight post-transfer hook allows MOON to keep │\r
│ it's reflection accounting precise. │\r
│ • Holders may optionally call Moon.collect() to synchronize │\r
│ reflections at any time, though this is not required. │\r
│ │\r
└────────────────────────────────────────────────────────────────────────┘\r
*/\r
\r
/*──────────────────────────── INTERFACES ───────────────────────────────*/\r
\r
/// @notice Minimal ERC‑20 interface used by Sun.\r
interface IERC20 {\r
function totalSupply() external view returns (uint256);\r
function balanceOf(address owner) external view returns (uint256);\r
function allowance(address owner, address spender) external view returns (uint256);\r
function approve(address spender, uint256 amount) external returns (bool);\r
function transfer(address recipient, uint256 amount) external returns (bool);\r
function transferFrom(address sender, address recipient, uint256 amount) external returns (bool);\r
\r
event Transfer(address indexed from, address indexed to, uint256 value);\r
event Approval(address indexed owner, address indexed spender, uint256 value);\r
}\r
\r
/// @notice Moon’s hook; Sun notifies Moon before every balance‑changing transfer.\r
/// @dev Moon uses these pre‑transfer balances to maintain its own accounting.\r
interface IMoon {\r
function zz__transferSun(address from, address to, uint256 fromBal, uint256 toBal, uint256 amount) external;\r
}\r
\r
/*────────────────────────────── CONTRACT ───────────────────────────────*/\r
\r
contract Sun is IERC20 {\r
/*//////////////////////////////////////////////////////////////////////////\r
METADATA\r
//////////////////////////////////////////////////////////////////////////*/\r
\r
/// @notice ERC‑20 name.\r
string public constant name = "Sun";\r
/// @notice ERC‑20 symbol.\r
string public constant symbol = "SUN";\r
/// @notice ERC‑20 decimals (fixed at 18).\r
uint8 public constant decimals = 18;\r
\r
/*//////////////////////////////////////////////////////////////////////////\r
IMMUTABLES\r
//////////////////////////////////////////////////////////////////////////*/\r
\r
/// @notice Fixed total supply (scaled by 1e18 in the constructor).\r
uint256 public immutable override totalSupply;\r
/// @notice The trusted Moon contract that receives transfer hooks.\r
address public immutable moon;\r
\r
/*//////////////////////////////////////////////////////////////////////////\r
ERC-20 CORE STORAGE\r
//////////////////////////////////////////////////////////////////////////*/\r
\r
/// @dev Balance mapping per ERC‑20.\r
mapping(address => uint256) internal balances;\r
/// @dev Allowance mapping per ERC‑20 (`owner => (spender => amount)`).\r
mapping(address => mapping(address => uint256)) internal allowances;\r
\r
/*//////////////////////////////////////////////////////////////////////////\r
CONSTRUCTOR\r
//////////////////////////////////////////////////////////////////////////*/\r
\r
/// @param supply Human‑readable supply (will be scaled by 1e18).\r
/// @param moonAddress The Moon contract to notify on transfers (must be non‑zero).\r
/// Mints the entire fixed supply to `msg.sender`.\r
constructor(uint256 supply, address moonAddress) {\r
require(moonAddress != address(0), "Sun: zero Moon addr");\r
moon = moonAddress;\r
\r
// Scale by 1e18 to align with `decimals = 18`.\r
totalSupply = supply * 1e18;\r
\r
// Mint fixed supply to deployer.\r
balances[msg.sender] = totalSupply;\r
emit Transfer(address(0), msg.sender, totalSupply);\r
}\r
\r
/*//////////////////////////////////////////////////////////////////////////\r
ERC-20 VIEW FUNCTIONS\r
//////////////////////////////////////////////////////////////////////////*/\r
\r
/// @notice Returns the current balance of an address.\r
function balanceOf(address owner) public view override returns (uint256) {\r
return balances[owner];\r
}\r
\r
/// @notice Returns the remaining allowance from `owner` to `spender`.\r
function allowance(address owner, address spender)\r
public\r
view\r
override\r
returns (uint256)\r
{\r
return allowances[owner][spender];\r
}\r
\r
/*//////////////////////////////////////////////////////////////////////////\r
ERC-20 EXTERNAL MUTATIVE FUNCTIONS\r
//////////////////////////////////////////////////////////////////////////*/\r
\r
/// @notice Sets `spender`’s allowance over the caller’s tokens to `amount`.\r
/// Emits an {Approval} event.\r
function approve(address spender, uint256 amount)\r
external\r
override\r
returns (bool)\r
{\r
_approve(msg.sender, spender, amount);\r
return true;\r
}\r
\r
/// @notice Moves `amount` tokens from caller to `to`.\r
/// Emits a {Transfer} event and **notifies Moon** before state changes.\r
function transfer(address to, uint256 amount)\r
external\r
override\r
returns (bool)\r
{\r
_transfer(msg.sender, to, amount);\r
return true;\r
}\r
\r
/// @notice Moves `amount` tokens from `from` to `to` using the caller’s allowance.\r
/// Deducts allowance unless it is `type(uint256).max` (infinite allowance UX).\r
/// Emits a {Transfer} event and **notifies Moon** before state changes.\r
function transferFrom(address from, address to, uint256 amount)\r
external\r
override\r
returns (bool)\r
{\r
uint256 cur = allowances[from][msg.sender];\r
if (cur != type(uint256).max) {\r
require(cur >= amount, "allowance");\r
unchecked {\r
allowances[from][msg.sender] = cur - amount;\r
}\r
emit Approval(from, msg.sender, cur - amount);\r
}\r
\r
_transfer(from, to, amount);\r
return true;\r
}\r
\r
/*//////////////////////////////////////////////////////////////////////////\r
INTERNAL APPROVAL HELPER\r
//////////////////////////////////////////////////////////////////////////*/\r
\r
/// @dev Internal setter for allowances; emits {Approval}.\r
function _approve(address owner, address spender, uint256 amount) internal {\r
allowances[owner][spender] = amount;\r
emit Approval(owner, spender, amount);\r
}\r
\r
/*//////////////////////////////////////////////////////////////////////////\r
INTERNAL TRANSFER\r
//////////////////////////////////////////////////////////////////////////*/\r
\r
/// @dev Internal move of tokens with Moon transfer notification.\r
/// Non‑standard behaviors (documented):\r
/// - We call `Moon.transferSun` with pre‑transfer balances.\r
function _transfer(address from, address to, uint256 amount) internal {\r
// Standard ERC‑20 validations.\r
require(to != address(0), "ERC20: transfer to the zero address");\r
uint256 fromBal = balances[from];\r
require(fromBal >= amount, "ERC20: transfer amount exceeds balance");\r
uint256 toBal = balances[to];\r
\r
// Apply balance changes (unchecked is safe due to the require above).\r
unchecked {\r
balances[from] -= amount;\r
balances[to] += amount;\r
}\r
\r
// Inform Moon of the transfer using pre‑transfer balances. Moon can\r
// update reward indices, membership status, or any other accounting.\r
IMoon(moon).zz__transferSun(from, to, fromBal, toBal, amount);\r
\r
// Emit standard ERC‑20 Transfer event.\r
emit Transfer(from, to, amount);\r
}\r
}\r
"
}
},
"settings": {
"evmVersion": "paris",
"metadata": {
"bytecodeHash": "ipfs"
},
"optimizer": {
"enabled": true,
"runs": 6000
},
"remappings": [],
"viaIR": true,
"outputSelection": {
"*": {
"*": [
"evm.bytecode",
"evm.deployedBytecode",
"devdoc",
"userdoc",
"metadata",
"abi"
]
}
}
}
}}
Submitted on: 2025-10-30 13:22:01
Comments
Log in to comment.
No comments yet.