Verified contractExactMIT
NoroshiClans
Address0xc062df00a124c1d213aef38a8688e05928b45f73
- Balance
0 ETH
- Transactions
1
- Compiler
solc 0.8.38
- Same code
Only this contract
Details
- Compiler
- solc 0.8.38+commit.1835d247
- Language
- solidity
- Optimization
- Yes, 1,000 runs
- EVM version
- cancun
- License
- MIT
- Match
- Exactcreation code: yes, runtime code: yes
- Verified on Sourcify
- Created in block
- 26,506,092
- Creation transaction
- 0x4bbd8fcbef…705143390f
contracts/NoroshiClans.sol
// SPDX-License-Identifier: MIT
pragma solidity 0.8.38;
/*
*
* _ _ ___ ____ ___ ____ _ _ ___
* | \ | | / _ \ | _ \ / _ \ / ___| | | | | |_ _|
* | \| | | | | | | |_) | | | | | \___ \ | |_| | | |
* | |\ | | |_| | | _ < | |_| | ___) | | _ | | |
* |_| \_| \___/ |_| \_\ \___/ |____/ |_| |_| |___|
*
* signal fires on ethereum
*
*/
interface INoroshiToken {
function ownerOf(uint256 tokenId) external view returns (address);
function geneOf(uint256 tokenId) external view returns (uint128);
}
interface IPriceFeed {
function latestRoundData()
external
view
returns (uint80, int256, uint256, uint256, uint80);
function getRoundData(uint80 roundId)
external
view
returns (uint80, int256, uint256, uint256, uint80);
}
interface IWETH {
function balanceOf(address account) external view returns (uint256);
function withdraw(uint256 amount) external;
}
// The clan war: every round, the warriors of the market that rose the most share the prize.
contract NoroshiClans {
uint256 public constant MARKETS = 6;
uint256 public constant ROUND = 30 days;
uint256 public constant CLAIM_WINDOW = 24 hours;
uint256 public constant NOTICE = 30 days;
uint256 private constant _STALE_AFTER = 48 hours;
uint256 private constant _MAX_STEPS = 3;
uint256 private constant _MAX_ID = 1000;
INoroshiToken public immutable noroshi;
IWETH public immutable weth;
address public immutable owner;
uint256 public immutable start;
// Chainlink USD feeds, in gene order: BTC, ETH, SOL, BNB, LINK, ZEC.
address private immutable _feed0;
address private immutable _feed1;
address private immutable _feed2;
address private immutable _feed3;
address private immutable _feed4;
address private immutable _feed5;
struct Stake {
address holder;
uint32 from;
uint8 market;
uint16 index;
}
struct Round {
uint128 pool;
uint128 prize;
uint128 claimed;
uint16 winners;
uint8 winner;
bool settled;
}
struct Board {
uint256 round;
uint256 startsAt;
uint256 endsAt;
uint256 pool;
uint256 next;
int256[6] change;
bool[6] live;
uint256[6] fighters;
uint256[6] joining;
uint256 closeAt;
uint256 lastRound;
}
mapping(uint256 => Stake) public stakes;
mapping(uint256 => uint256[]) private _clans;
mapping(uint256 => Round) public rounds;
mapping(uint256 => int256[6]) private _opening;
mapping(uint256 => uint256[4]) private _won;
uint256 public pot;
uint256 public reserved;
uint256 public ownerDue;
uint256 public synced;
uint256 public released;
uint256 public closeAt;
uint256 public lastRound = type(uint256).max;
event Income(uint256 amount);
event Staked(uint256 indexed tokenId, address indexed holder, uint256 market, uint256 fromRound);
event Opened(uint256 indexed round, uint256 pool);
event Settled(uint256 indexed round, uint256 winner, uint256 winners, uint256 prize);
event Claimed(uint256 indexed round, address indexed holder, uint256 warriors, uint256 amount);
event Released(uint256 indexed round, uint256 amount);
event CloseAnnounced(uint256 closeAt, uint256 lastRound);
event Closed(uint256 amount);
error NotHolder(uint256 tokenId);
error AlreadyStaked(uint256 tokenId);
error NotWinner(uint256 tokenId);
error NotSettled();
error ClaimClosed();
error Closing();
error TooEarly();
error NotOwner();
error TransferFailed();
constructor(
address token,
address wrappedEther,
address owner_,
uint256 start_,
address[6] memory feeds
) {
noroshi = INoroshiToken(token);
weth = IWETH(wrappedEther);
owner = owner_;
start = start_;
_feed0 = feeds[0];
_feed1 = feeds[1];
_feed2 = feeds[2];
_feed3 = feeds[3];
_feed4 = feeds[4];
_feed5 = feeds[5];
}
receive() external payable {}
/*//////////////////////////////////////////////////////////////
PLAYERS
//////////////////////////////////////////////////////////////*/
// Non-custodial: the warrior stays in the wallet. It fights from the next round on.
function stake(uint256[] calldata tokenIds) external {
_sync(_MAX_STEPS);
uint256 target = currentRound() + 1;
if (target > lastRound) revert Closing();
for (uint256 i; i < tokenIds.length; ++i) {
_stake(tokenIds[i], target);
}
}
function claim(uint256 round, uint256[] calldata tokenIds) external {
_sync(_MAX_STEPS);
Round storage r = rounds[round];
if (!r.settled) revert NotSettled();
if (block.timestamp >= roundEnd(round) + CLAIM_WINDOW) revert ClaimClosed();
uint256[4] storage bits = _won[round];
for (uint256 i; i < tokenIds.length; ++i) {
uint256 id = tokenIds[i];
if (id == 0 || id > _MAX_ID) revert NotWinner(id);
uint256 word = (id - 1) >> 8;
uint256 bit = 1 << ((id - 1) & 255);
Stake memory s = stakes[id];
if (
bits[word] & bit == 0 || s.holder != msg.sender || s.from > round
|| _ownerOf(id) != msg.sender
) revert NotWinner(id);
bits[word] &= ~bit;
}
uint256 amount = uint256(r.prize) * tokenIds.length;
r.claimed += uint128(amount);
reserved -= amount;
emit Claimed(round, msg.sender, tokenIds.length, amount);
_send(msg.sender, amount);
}
// Opens and settles rounds whose time has come. Anyone may call it.
function sync() external {
_sync(_MAX_STEPS);
}
/*//////////////////////////////////////////////////////////////
OWNER
//////////////////////////////////////////////////////////////*/
// The owner's share of every income. Paid to the owner, whoever calls.
function withdraw() external {
_collect();
uint256 amount = ownerDue;
ownerDue = 0;
_send(owner, amount);
}
// Public notice: the round running at `closeAt` is the last one.
function announceClose() external {
if (msg.sender != owner) revert NotOwner();
if (closeAt != 0) revert Closing();
uint256 when = block.timestamp + NOTICE;
closeAt = when;
lastRound = when < start ? 0 : (when - start) / ROUND + 1;
emit CloseAnnounced(when, lastRound);
}
// After the last round and its claim window, what is left goes back to the owner.
function close() external {
if (msg.sender != owner) revert NotOwner();
_sync(_MAX_STEPS);
if (closeAt == 0 || block.timestamp < closeAt) revert TooEarly();
uint256 last = lastRound;
if (last != 0 && (!rounds[last].settled || released < last)) revert TooEarly();
uint256 amount = pot + ownerDue;
pot = 0;
ownerDue = 0;
emit Closed(amount);
_send(owner, amount);
}
/*//////////////////////////////////////////////////////////////
VIEWS
//////////////////////////////////////////////////////////////*/
function currentRound() public view returns (uint256) {
if (block.timestamp < start) return 0;
return (block.timestamp - start) / ROUND + 1;
}
function roundStart(uint256 round) public view returns (uint256) {
return start + (round - 1) * ROUND;
}
function roundEnd(uint256 round) public view returns (uint256) {
return start + round * ROUND;
}
function feedOf(uint256 market) public view returns (address) {
if (market == 0) return _feed0;
if (market == 1) return _feed1;
if (market == 2) return _feed2;
if (market == 3) return _feed3;
if (market == 4) return _feed4;
if (market == 5) return _feed5;
return address(0);
}
// Last answer at or before `time`, if the feed was alive then.
function priceAt(uint256 market, uint256 time)
public
view
returns (int256 price, uint256 updatedAt, uint80 roundId)
{
return _priceAt(feedOf(market), time);
}
function opening(uint256 round) external view returns (int256[6] memory) {
return _opening[round];
}
function clan(uint256 market) external view returns (uint256[] memory) {
return _clans[market];
}
function wonBits(uint256 round) external view returns (uint256[4] memory) {
return _won[round];
}
// The result of `round`, or what settling it would give now. Winner is market + 1, 0 for none;
// rise is the winner's change over the round, 1e18 = 100%.
function preview(uint256 round)
external
view
returns (uint256 winner, uint256 winners, uint256 prize, bool settled, int256 rise)
{
if (round == 0 || block.timestamp < roundEnd(round)) return (0, 0, 0, false, 0);
int256[6] memory open = round <= synced ? _opening[round] : _prices(roundStart(round));
int256[6] memory close_ = _prices(roundEnd(round));
Round memory r = rounds[round];
if (r.settled) {
(winner, winners, prize, settled) = (r.winner, r.winners, r.prize, true);
} else {
uint256 best = _best(open, close_);
if (best == MARKETS) return (0, 0, 0, false, 0);
winner = best + 1;
(winners,) = _validWinners(best, round);
if (winners != 0) prize = (round <= synced ? r.pool : pot + _pending()) / winners;
}
if (winner != 0) rise = _rise(open[winner - 1], close_[winner - 1]);
}
function board() external view returns (Board memory b) {
uint256 cur = currentRound();
b.round = cur;
b.startsAt = cur == 0 ? start : roundStart(cur);
b.endsAt = cur == 0 ? start : roundEnd(cur);
b.closeAt = closeAt;
b.lastRound = lastRound;
uint256 money = pot + _pending();
if (cur != 0 && cur <= synced) {
b.pool = rounds[cur].pool;
b.next = money > b.pool ? money - b.pool : 0;
} else {
uint256 held = _toReserve();
b.pool = money > held ? money - held : 0;
}
if (cur != 0) {
int256[6] memory open = cur <= synced ? _opening[cur] : _prices(roundStart(cur));
int256[6] memory now_ = _prices(block.timestamp);
for (uint256 m; m < MARKETS; ++m) {
if (open[m] > 0 && now_[m] > 0) {
b.live[m] = true;
b.change[m] = ((now_[m] - open[m]) * 1e18) / open[m];
}
}
}
for (uint256 m; m < MARKETS; ++m) {
uint256[] storage ids = _clans[m];
for (uint256 i; i < ids.length; ++i) {
uint256 from = stakes[ids[i]].from;
if (from <= cur) ++b.fighters[m];
else if (from == cur + 1) ++b.joining[m];
}
}
}
/*//////////////////////////////////////////////////////////////
INTERNALS
//////////////////////////////////////////////////////////////*/
function _stake(uint256 id, uint256 target) private {
if (_ownerOf(id) != msg.sender) revert NotHolder(id);
Stake memory s = stakes[id];
uint256 from = target;
if (s.holder != address(0)) {
if (s.holder == msg.sender) revert AlreadyStaked(id);
// It changed wallet: the place is lost and it sits out one round.
from = target + 1;
_drop(id, s);
}
uint256 market = uint256(noroshi.geneOf(id)) >> 120;
uint256[] storage ids = _clans[market];
stakes[id] = Stake(msg.sender, uint32(from), uint8(market), uint16(ids.length));
ids.push(id);
emit Staked(id, msg.sender, market, from);
}
function _drop(uint256 id, Stake memory s) private {
uint256[] storage ids = _clans[s.market];
uint256 last = ids[ids.length - 1];
ids[s.index] = last;
stakes[last].index = s.index;
ids.pop();
delete stakes[id];
}
function _sync(uint256 steps) private {
_collect();
uint256 cur = currentRound();
uint256 last = lastRound;
for (uint256 i; i < steps; ++i) {
uint256 n = synced;
if (cur <= n) break;
if (n >= last) {
if (n != 0 && !rounds[n].settled) _settle(n, _prices(roundEnd(n)));
break;
}
int256[6] memory prices = _prices(roundStart(n + 1));
if (n != 0) _settle(n, prices);
_open(n + 1, prices);
synced = n + 1;
}
_release();
}
function _collect() private {
uint256 w = weth.balanceOf(address(this));
if (w != 0) weth.withdraw(w);
uint256 known = pot + reserved + ownerDue;
uint256 balance = address(this).balance;
if (balance > known) {
uint256 income = balance - known;
uint256 cut = (income * 2) / 5;
ownerDue += cut;
pot += income - cut;
emit Income(income);
}
}
function _pending() private view returns (uint256) {
uint256 known = pot + reserved + ownerDue;
uint256 balance = address(this).balance + weth.balanceOf(address(this));
if (balance <= known) return 0;
uint256 income = balance - known;
return income - (income * 2) / 5;
}
function _open(uint256 n, int256[6] memory prices) private {
_opening[n] = prices;
rounds[n].pool = uint128(pot);
emit Opened(n, pot);
}
function _settle(uint256 n, int256[6] memory closing) private {
Round storage r = rounds[n];
r.settled = true;
uint256 best = _best(_opening[n], closing);
if (best == MARKETS) {
emit Settled(n, 0, 0, 0);
return;
}
r.winner = uint8(best + 1);
(uint256 count, uint256[4] memory bits) = _validWinners(best, n);
uint256 prize = count == 0 ? 0 : r.pool / count;
if (prize != 0) {
r.winners = uint16(count);
r.prize = uint128(prize);
for (uint256 w; w < 4; ++w) {
if (bits[w] != 0) _won[n][w] = bits[w];
}
uint256 total = prize * count;
pot -= total;
reserved += total;
}
emit Settled(n, best, r.winners, prize);
}
// What settling the last opened round will set aside for its winners, if it has ended unsettled.
function _toReserve() private view returns (uint256) {
uint256 n = synced;
if (n == 0 || rounds[n].settled || block.timestamp < roundEnd(n)) return 0;
uint256 best = _best(_opening[n], _prices(roundEnd(n)));
if (best == MARKETS) return 0;
(uint256 count,) = _validWinners(best, n);
if (count == 0) return 0;
return (rounds[n].pool / count) * count;
}
// Prize money nobody claimed in time goes back to the pot.
function _release() private {
uint256 n = released + 1;
while (n <= synced && rounds[n].settled && block.timestamp >= roundEnd(n) + CLAIM_WINDOW) {
Round storage r = rounds[n];
uint256 left = uint256(r.prize) * r.winners - r.claimed;
if (left != 0) {
reserved -= left;
pot += left;
emit Released(n, left);
}
released = n;
++n;
}
}
function _rise(int256 open, int256 close_) private pure returns (int256) {
if (open <= 0 || close_ <= 0) return 0;
return ((close_ - open) * 1e18) / open;
}
// The market with the largest rise; MARKETS when no market had prices at both ends.
function _best(int256[6] memory open, int256[6] memory close_) private pure returns (uint256 best) {
best = MARKETS;
int256 top;
for (uint256 m; m < MARKETS; ++m) {
if (open[m] <= 0 || close_[m] <= 0) continue;
int256 change = ((close_[m] - open[m]) * 1e18) / open[m];
if (best == MARKETS || change > top) {
best = m;
top = change;
}
}
}
// Warriors of `market` in round `n` still held by the wallet that entered them.
function _validWinners(uint256 market, uint256 n)
private
view
returns (uint256 count, uint256[4] memory bits)
{
uint256[] storage ids = _clans[market];
for (uint256 i; i < ids.length; ++i) {
uint256 id = ids[i];
Stake memory s = stakes[id];
if (s.from > n || _ownerOf(id) != s.holder) continue;
bits[(id - 1) >> 8] |= 1 << ((id - 1) & 255);
++count;
}
}
function _prices(uint256 time) private view returns (int256[6] memory prices) {
for (uint256 m; m < MARKETS; ++m) {
(prices[m],,) = _priceAt(feedOf(m), time);
}
}
function _priceAt(address feed, uint256 time) private view returns (int256, uint256, uint80) {
if (feed.code.length == 0) return (0, 0, 0);
uint80 latest;
int256 answer;
uint256 updated;
try IPriceFeed(feed).latestRoundData() returns (
uint80 id, int256 a, uint256, uint256 u, uint80
) {
(latest, answer, updated) = (id, a, u);
} catch {
return (0, 0, 0);
}
if (answer <= 0 || updated == 0) return (0, 0, 0);
if (updated > time) (latest, answer, updated) = _search(feed, latest, time);
if (updated == 0 || updated > time || time - updated > _STALE_AFTER) return (0, 0, 0);
return (answer, updated, latest);
}
// Last round of the current phase at or before `time`: gallop back, then bisect.
function _search(address feed, uint80 latest, uint256 time)
private
view
returns (uint80, int256, uint256)
{
uint80 phase = latest >> 64;
(uint64 lo, uint64 hi) = _gallop(feed, phase, uint64(latest), time);
if (lo == 0) return (0, 0, 0);
lo = _bisect(feed, phase, lo, hi, time);
(, int256 answer, uint256 updated) = _round(feed, phase, lo);
return ((phase << 64) | uint80(lo), answer, updated);
}
// Steps back 1, 2, 4... rounds until one is at or before `time`; lo is 0 if none is.
function _gallop(address feed, uint80 phase, uint64 top, uint256 time)
private
view
returns (uint64 lo, uint64 hi)
{
hi = top;
uint64 step = 1;
for (;;) {
uint64 candidate = step < hi ? hi - step : 1;
(bool ok,, uint256 updated) = _round(feed, phase, candidate);
if (!ok) return (0, hi);
if (updated <= time) return (candidate, hi);
if (candidate == 1) return (0, hi);
hi = candidate;
step <<= 1;
}
}
function _bisect(address feed, uint80 phase, uint64 lo, uint64 hi, uint256 time)
private
view
returns (uint64)
{
while (hi - lo > 1) {
uint64 mid = lo + (hi - lo) / 2;
(bool ok,, uint256 updated) = _round(feed, phase, mid);
if (!ok) break;
if (updated <= time) lo = mid;
else hi = mid;
}
return lo;
}
function _round(address feed, uint80 phase, uint64 aggregatorRound)
private
view
returns (bool, int256, uint256)
{
try IPriceFeed(feed).getRoundData((phase << 64) | uint80(aggregatorRound)) returns (
uint80, int256 answer, uint256, uint256 updatedAt, uint80
) {
return (answer > 0 && updatedAt != 0, answer, updatedAt);
} catch {
return (false, 0, 0);
}
}
function _ownerOf(uint256 id) private view returns (address) {
try noroshi.ownerOf(id) returns (address holder) {
return holder;
} catch {
return address(0);
}
}
function _send(address to, uint256 amount) private {
if (amount == 0) return;
(bool ok,) = to.call{ value: amount }("");
if (!ok) revert TransferFailed();
}
}
Compiler settingsShow
{
"optimizer": {
"runs": 1000,
"enabled": true
},
"evmVersion": "cancun"
}Source code and metadata: Sourcify.
The source code belongs to the authors of the contract.