// SPDX-License-Identifier: MIT pragma solidity ^0.8.24; /** * OpsikomCoin - a test coin that follows one Opsikom feed (Sepolia test network). * Made to be deployed from opsikom.com/create with your wallet: every choice of the form is a constructor parameter, * so the same compiled contract serves every coin. TEST NETWORK ONLY. Not audited, no value. * * How it works * - At launch() the creator opens a Uniswap V2 pool with poolPercent of the supply and the ETH sent; the liquidity is * locked forever (LP tokens to the dead address). The rest of the supply goes to the creator. * - Every time the feed publishes a new value, anyone can call update(). * - The change of the value is counted in steps. Each step moves bpsPerStep of the pool, at most maxBps per update: * in one direction coins are BURNED from the pool (fewer coins, higher price), in the other new coins are MINTED into * the pool (more coins, lower price). burnWhenUp says which way. Then the pool is synced. * - Whoever calls update() receives rewardBps of the coins burned or minted, as newly minted coins. * - Nobody can change the rules after deployment. */ interface IOpsikomHub { function latestRoundData(bytes32 feedId) external view returns (uint80 roundId, int256 answer, uint256 updatedAt); } interface IUniswapV2Router02 { function factory() external view returns (address); function WETH() external view returns (address); function addLiquidityETH(address token, uint256 amountTokenDesired, uint256 amountTokenMin, uint256 amountETHMin, address to, uint256 deadline) external payable returns (uint256 amountToken, uint256 amountETH, uint256 liquidity); } interface IUniswapV2Factory { function getPair(address a, address b) external view returns (address); function createPair(address a, address b) external returns (address); } interface IUniswapV2Pair { function sync() external; } contract OpsikomCoin { struct Params { string name; // e.g. "Heat Coin" string symbol; // e.g. "HEAT" string description; // one or two sentences address hub; // Opsikom hub (Sepolia) string feedName; // e.g. "GLOBAL_TEMP" uint256 step; // one step, in raw feed units (e.g. 100 = 0.01 C for GLOBAL_TEMP) uint256 bpsPerStep; // share of the pool moved per step, in basis points (100 = 1%) uint256 maxBps; // most the pool can move in one update, in basis points bool burnWhenUp; // true: the value going up burns coins; false: it mints uint256 maxAge; // ignore a feed value older than this, in seconds uint256 supply; // whole coins at the start uint256 poolPercent; // share of the supply that goes into the pool at launch uint256 rewardBps; // reward for whoever calls update(), basis points of the coins moved (max 100) address router; // Uniswap V2 router } // ---------- the coin ---------- string public name; string public symbol; string public description; uint8 public constant decimals = 18; uint256 public totalSupply; mapping(address => uint256) public balanceOf; mapping(address => mapping(address => uint256)) public allowance; // ---------- the rules (fixed forever) ---------- IOpsikomHub public immutable hub; bytes32 public immutable feedId; string public feedName; uint256 public immutable step; uint256 public immutable bpsPerStep; uint256 public immutable maxBps; bool public immutable burnWhenUp; uint256 public immutable maxAge; uint256 public immutable initialSupply; uint256 public immutable poolPercent; uint256 public immutable rewardBps; IUniswapV2Router02 public immutable router; address public constant DEAD = 0x000000000000000000000000000000000000dEaD; address public immutable creator; address public pair; // the Uniswap pool, set by launch() uint80 public lastRound; // last feed round applied int256 public lastValue; // last feed value applied event Transfer(address indexed from, address indexed to, uint256 value); event Approval(address indexed owner, address indexed spender, uint256 value); event Launched(address pair, uint256 tokens, uint256 eth); event Updated(uint80 round, int256 value, bool burned, uint256 amount, address caller, uint256 reward); constructor(Params memory p) { require(bytes(p.name).length > 0 && bytes(p.name).length <= 40, "name: 1 to 40 characters"); require(bytes(p.symbol).length > 0 && bytes(p.symbol).length <= 11, "symbol: 1 to 11 characters"); require(bytes(p.description).length <= 280, "description: at most 280 characters"); require(p.hub != address(0) && p.router != address(0), "zero address"); require(p.step > 0, "step must be above 0"); require(p.bpsPerStep >= 1 && p.bpsPerStep <= 1000, "strength: 0.01% to 10% per step"); require(p.maxBps >= 10 && p.maxBps <= 2500, "biggest move: 0.1% to 25%"); require(p.maxAge >= 1 days && p.maxAge <= 400 days, "max age: 1 to 400 days"); require(p.supply >= 1_000 && p.supply <= 1_000_000_000_000, "supply: 1,000 to 1 trillion"); require(p.poolPercent >= 50 && p.poolPercent <= 100, "pool: 50% to 100%"); require(p.rewardBps <= 100, "update reward: at most 1%"); bytes32 id = keccak256(bytes(p.feedName)); (, , uint256 updatedAt) = IOpsikomHub(p.hub).latestRoundData(id); // the feed must exist and have data require(updatedAt > 0, "feed has no data"); name = p.name; symbol = p.symbol; description = p.description; hub = IOpsikomHub(p.hub); feedId = id; feedName = p.feedName; step = p.step; bpsPerStep = p.bpsPerStep; maxBps = p.maxBps; burnWhenUp = p.burnWhenUp; maxAge = p.maxAge; initialSupply = p.supply * 10**18; poolPercent = p.poolPercent; rewardBps = p.rewardBps; router = IUniswapV2Router02(p.router); creator = msg.sender; uint256 pool = initialSupply * p.poolPercent / 100; _mint(address(this), pool); // waits here until launch() if (initialSupply > pool) _mint(msg.sender, initialSupply - pool); } // ---------- launch: open the pool (once, by the creator) ---------- function launch() external payable { require(msg.sender == creator, "only the creator"); require(pair == address(0), "already launched"); require(msg.value > 0, "send some test ETH with launch()"); (uint80 r, int256 v, ) = hub.latestRoundData(feedId); lastRound = r; lastValue = v; IUniswapV2Factory f = IUniswapV2Factory(router.factory()); address p = f.getPair(address(this), router.WETH()); if (p == address(0)) p = f.createPair(address(this), router.WETH()); pair = p; uint256 amount = balanceOf[address(this)]; allowance[address(this)][address(router)] = amount; router.addLiquidityETH{value: msg.value}(address(this), amount, amount, msg.value, DEAD, block.timestamp); emit Launched(p, amount, msg.value); } // ---------- update: follow the feed (anyone can call it) ---------- function preview() external view returns (bool ready, bool burn, uint256 amount) { if (pair == address(0)) return (false, false, 0); (uint80 r, int256 v, uint256 updatedAt) = hub.latestRoundData(feedId); if (r <= lastRound || block.timestamp - updatedAt > maxAge) return (false, false, 0); (burn, amount) = _move(v - lastValue); ready = true; } function update() external returns (bool changed) { require(pair != address(0), "not launched yet"); (uint80 r, int256 v, uint256 updatedAt) = hub.latestRoundData(feedId); require(r > lastRound, "no new value yet"); require(block.timestamp - updatedAt <= maxAge, "feed value too old"); (bool burn, uint256 amount) = _move(v - lastValue); lastRound = r; lastValue = v; uint256 reward; if (amount > 0) { if (burn) _burn(pair, amount); else _mint(pair, amount); IUniswapV2Pair(pair).sync(); // the pool sees its new balance: the price moves reward = amount * rewardBps / 10000; if (reward > 0) _mint(msg.sender, reward); changed = true; } emit Updated(r, v, burn, amount, msg.sender, reward); } function _move(int256 diff) internal view returns (bool burn, uint256 amount) { uint256 steps = uint256(diff < 0 ? -diff : diff) / step; if (steps == 0) return (false, 0); uint256 bps = steps * bpsPerStep; if (bps > maxBps) bps = maxBps; amount = balanceOf[pair] * bps / 10000; burn = (diff > 0) == burnWhenUp; } // ---------- standard ERC-20 ---------- function transfer(address to, uint256 amount) external returns (bool) { _transfer(msg.sender, to, amount); return true; } function approve(address spender, uint256 amount) external returns (bool) { allowance[msg.sender][spender] = amount; emit Approval(msg.sender, spender, amount); return true; } function transferFrom(address from, address to, uint256 amount) external returns (bool) { uint256 a = allowance[from][msg.sender]; if (a != type(uint256).max) { require(a >= amount, "allowance too low"); allowance[from][msg.sender] = a - amount; } _transfer(from, to, amount); return true; } function _transfer(address from, address to, uint256 amount) internal { require(to != address(0), "zero address"); require(balanceOf[from] >= amount, "balance too low"); balanceOf[from] -= amount; balanceOf[to] += amount; emit Transfer(from, to, amount); } function _mint(address to, uint256 amount) internal { totalSupply += amount; balanceOf[to] += amount; emit Transfer(address(0), to, amount); } function _burn(address from, uint256 amount) internal { balanceOf[from] -= amount; totalSupply -= amount; emit Transfer(from, address(0), amount); } }