Verified contractExactMIT
DadsRenderer
Address0x2428c01abf09cc91830c7836dc6eccab2a2f7d85
- Balance
0 ETH
- Transactions
1
- Compiler
solc 0.8.28
- Same code
Only this contract
Details
- Compiler
- solc 0.8.28+commit.7893614a
- Language
- solidity
- Optimization
- Yes, 200 runs
- EVM version
- cancun
- Via IR
- yes
- License
- MIT
- Match
- Exactcreation code: yes, runtime code: yes
- Verified on Sourcify
- Created in block
- 26,428,877
- Creation transaction
- 0x6f1f2a2e81…bdc5bb2456
2 files
- src/DadsRenderer.solmain26.05 KB
- lib/openzeppelin-contracts/contracts/utils/Base64.sol5.18 KB
src/DadsRenderer.sol
// SPDX-License-Identifier: MIT
pragma solidity ^0.8.28;
// #### ### #### ####
// # # # # # # #
// # # ##### # # ###
// # # # # # # #
// #### # # #### ####
import {Base64} from "@openzeppelin/contracts/utils/Base64.sol";
interface IDadsReveal {
function revealed() external view returns (bool);
function offset() external view returns (uint256);
}
/// @title DadsRenderer
/// @notice Draws every dad from pixel layers held on-chain and returns its picture and traits.
/// @dev The data is written once, by the deploy script, into plain data contracts ("chunks") whose addresses
/// are fixed in the constructor. Nothing here can be changed afterwards and there is no owner.
/// To change the art later, the owner of the Dads token deploys a new renderer and points the token at it.
///
/// Until the reveal, every token shows the same sealed picture. At the reveal the sale contract fixes
/// one random number (the offset), and token N then shows dad ((N - 1 + offset) mod 5,000) + 1.
/// The byte layout of the art file is produced by tools/compile.py; the two must change together.
/// The drawing steps mirror the collection's own renderer and are tested pixel for pixel against all
/// 5,000 finished pictures (test/RendererPixels.t.sol).
contract DadsRenderer {
uint256 public constant SUPPLY = 5000;
uint256 internal constant N = 24;
uint256 internal constant PIXELS = N * N;
uint256 internal constant CATS = 15;
uint256 internal constant ROWS_PER_CHUNK = 1500;
uint256 internal constant NONE = 0xFFFF;
uint256 internal constant NO_COLOUR = 1 << 24;
uint256 internal constant OUTLINE = 1; // the code compile.py gives the '#' outline character
uint256 internal constant HOLD_REC = 14; // a held thing: three maps (one per build), palette, three sets of steam pixels
uint256 internal constant KIND_REC = 19; // a type: palette, map, wide jaw, front map, flags, palette in each of five skins
uint256 internal constant EXPR_REC = 10; // an expression: three maps (one per build), its colours, the skin colours of a face paint
// sections of the art file, in header order
uint256 internal constant S_BMP = 0;
uint256 internal constant S_PAL = 1;
uint256 internal constant S_LOC = 2;
uint256 internal constant S_WIDEN = 3;
uint256 internal constant S_KINDS = 4;
uint256 internal constant S_BUILDS = 5;
uint256 internal constant S_TOPS = 6;
uint256 internal constant S_NECK = 7;
uint256 internal constant S_FACIAL = 8;
uint256 internal constant S_HAIRCOL = 9;
uint256 internal constant S_EXPR = 10;
uint256 internal constant S_EYEWEAR = 11;
uint256 internal constant S_HATS = 12;
uint256 internal constant S_HAIR = 13;
uint256 internal constant S_EARS = 14;
uint256 internal constant S_MOUTH = 15;
uint256 internal constant S_HOLDING = 16;
uint256 internal constant S_MISC = 17;
uint256 internal constant S_NAMES = 18;
uint256 internal constant S_SEALED = 19;
// positions in a trait row
uint256 internal constant T_BACKGROUND = 0;
uint256 internal constant T_TYPE = 1;
uint256 internal constant T_SKIN = 2;
uint256 internal constant T_BUILD = 3;
uint256 internal constant T_TOP = 4;
uint256 internal constant T_HEADWEAR = 5;
uint256 internal constant T_HAIR = 6;
uint256 internal constant T_HAIR_COLOR = 7;
uint256 internal constant T_FACIAL = 8;
uint256 internal constant T_EYEWEAR = 9;
uint256 internal constant T_EXPRESSION = 10;
uint256 internal constant T_OVER_EAR = 11;
uint256 internal constant T_NECK = 12;
uint256 internal constant T_MOUTH = 13;
uint256 internal constant T_HOLDING = 14;
address[] public artChunks;
address[] public traitChunks;
/// @notice keccak256 of the 75,000-byte trait table (5,000 rows of 15 bytes).
bytes32 public immutable traitsHash;
/// @notice keccak256 of the art file.
bytes32 public immutable artHash;
string public description;
/// @notice Where the reveal is recorded (the sale contract).
IDadsReveal public immutable reveal;
error BadTraitData();
error NoSuchDad(uint256 id);
error NotRevealed();
constructor(address[] memory art, address[] memory traits, string memory description_, IDadsReveal reveal_) {
reveal = reveal_;
artChunks = art;
traitChunks = traits;
description = description_;
bytes memory all = _concat(traits);
if (all.length != SUPPLY * CATS) revert BadTraitData();
for (uint256 i; i + 1 < traits.length; ++i) {
if (traits[i].code.length - 1 != ROWS_PER_CHUNK * CATS) revert BadTraitData();
}
traitsHash = keccak256(all);
artHash = keccak256(_concat(art));
}
// ------------------------------------------------------------------------------------------------
// public views
// ------------------------------------------------------------------------------------------------
/// @notice Which dad (1 to 5,000, as numbered in the finished collection) a token shows, once revealed.
function dadOf(uint256 tokenId) public view returns (uint256) {
if (tokenId == 0 || tokenId > SUPPLY) revert NoSuchDad(tokenId);
if (!reveal.revealed()) revert NotRevealed();
return ((tokenId - 1 + reveal.offset()) % SUPPLY) + 1;
}
/// @notice The 15 trait numbers of a dad (0 = none), in metadata order.
function traitsOf(uint256 id) public view returns (uint8[15] memory t) {
if (id == 0 || id > SUPPLY) revert NoSuchDad(id);
uint256 row = id - 1;
address chunk = traitChunks[row / ROWS_PER_CHUNK];
uint256 off = (row % ROWS_PER_CHUNK) * CATS + 1;
bytes32 word;
assembly ("memory-safe") {
mstore(0, 0)
extcodecopy(chunk, 0, off, 15)
word := mload(0)
}
for (uint256 i; i < CATS; ++i) {
t[i] = uint8(word[i]);
}
}
/// @notice The finished picture as 576 pixels, 3 bytes each (red, green, blue), left to right, top down.
function pixels(uint256 id) external view returns (bytes memory out) {
uint256[] memory g = _draw(_loadArt(), traitsOf(id));
out = new bytes(PIXELS * 3);
for (uint256 i; i < PIXELS; ++i) {
uint256 c = g[i];
out[i * 3] = bytes1(uint8(c >> 16));
out[i * 3 + 1] = bytes1(uint8(c >> 8));
out[i * 3 + 2] = bytes1(uint8(c));
}
}
/// @notice The picture every dad shows before the reveal (an animated SVG).
function sealedSvg() external view returns (string memory) {
return string(_sealed(_loadArt()));
}
/// @notice The finished picture as an SVG.
function svg(uint256 id) external view returns (string memory) {
return string(_svg(_draw(_loadArt(), traitsOf(id))));
}
/// @notice Name, picture and traits, as a self-contained data: link (nothing is fetched from anywhere).
/// Before the reveal: the same animated picture for every dad, and no traits.
function tokenURI(uint256 id) external view returns (string memory) {
if (id == 0 || id > SUPPLY) revert NoSuchDad(id);
uint256 a = _loadArt();
uint8[15] memory t;
bytes memory picture;
if (reveal.revealed()) {
t = traitsOf(dadOf(id));
picture = _svg(_draw(a, t));
} else {
picture = _sealed(a);
}
return _json(a, id, picture, t);
}
function _json(uint256 a, uint256 id, bytes memory picture, uint8[15] memory t) internal view returns (string memory) {
bytes memory image = bytes(Base64.encode(picture));
bytes memory desc = bytes(description);
bytes memory out = new bytes(image.length + desc.length + 2048);
uint256 start;
assembly ("memory-safe") {
start := add(out, 32)
}
uint256 p = _put(start, '{"name":"Dads #');
p = _putUint(p, id);
p = _put(p, '",');
if (desc.length != 0) {
p = _put(p, '"description":"');
p = _put(p, desc);
p = _put(p, '",');
}
p = _put(p, '"image":"data:image/svg+xml;base64,');
p = _put(p, image);
p = _put(p, '","attributes":[');
bool first = true;
for (uint256 c; c < CATS; ++c) {
if (t[c] == 0) continue;
p = _put(p, first ? bytes('{"trait_type":"') : bytes(',{"trait_type":"'));
first = false;
uint256 cat = a + _u24(_sec(a, S_NAMES) + 3 * c);
uint256 nameLen = _u8(cat);
p = _putRaw(p, cat + 1, nameLen);
p = _put(p, '","value":"');
uint256 v = a + _u24(cat + 2 + nameLen + 3 * (uint256(t[c]) - 1));
p = _putRaw(p, v + 1, _u8(v));
p = _put(p, '"}');
}
p = _put(p, "]}");
assembly ("memory-safe") {
mstore(out, sub(p, start))
}
return string.concat("data:application/json;base64,", Base64.encode(out));
}
// ------------------------------------------------------------------------------------------------
// drawing
// ------------------------------------------------------------------------------------------------
/// @dev The dad alone on the key colour (the collection renderer's `figure`).
function _figure(uint256 a, uint8[15] memory t) internal pure returns (uint256[] memory g, uint256 key, uint256 trace, bool milk) {
uint256 misc = _sec(a, S_MISC);
trace = _u24(misc + 2);
key = _u24(misc + 5);
uint256 kind = _sec(a, S_KINDS) + KIND_REC * (uint256(t[T_TYPE]) - 1);
uint256 flags = _u8(kind + 8);
milk = flags & 1 != 0;
uint256 build = uint256(t[T_BUILD]) - 1; // 0 Standard, 1 Dad Bod, 2 Jacked
bool shaped = build != 0 && !milk;
uint256[64] memory base;
// his own colours; a type that comes in the five skins has one palette for each
_palette(a, base, t[T_SKIN] == 0 ? _u16(kind) : _u16(kind + 9 + 2 * (uint256(t[T_SKIN]) - 1)), true);
g = new uint256[](PIXELS);
for (uint256 i; i < PIXELS; ++i) {
g[i] = key;
}
if (milk) {
_trace(g, a, _u16(misc), trace);
} else {
_paint(g, a, _u16(misc), base, NONE);
_paint(g, a, _u16(kind + 2), base, NONE);
if (shaped) {
_paint(g, a, _u16(_sec(a, S_BUILDS) + 7 * (build - 1)), base, NONE);
_paint(g, a, _u16(kind + 4), base, NONE);
}
}
_layers(g, a, t, base, build);
_paint(g, a, _u16(kind + 6), base, NONE);
if (shaped) {
uint256 b = _sec(a, S_BUILDS) + 7 * (build - 1);
uint256 top = t[T_TOP] == 0 ? 0 : _sec(a, S_TOPS) + 15 * (uint256(t[T_TOP]) - 1);
if ((top == 0 || _u8(top + 14) != 0) && _u8(b + 4) & 2 != 0) {
_paint(g, a, _u16(b + 2), base, NONE);
if (t[T_NECK] != 0) {
// a neck piece lies on top of the bare-chest lines
uint256 r = _sec(a, S_NECK) + 4 * (uint256(t[T_NECK]) - 1);
_paint(g, a, _u16(r), base, _u16(r + 2));
}
}
if (_u8(b + 4) & 1 != 0) g[16 * N + 15] = g[16 * N + 14];
_widen(g, a, top == 0 ? _u16(b + 5) : _u16(top + 4 + 4 * build));
}
if (t[T_HOLDING] != 0 && !milk) {
uint256 h = _sec(a, S_HOLDING) + HOLD_REC * (uint256(t[T_HOLDING]) - 1);
_paint(g, a, _u16(h + 2 * build), base, _u16(h + 6));
}
if (flags & 2 != 0) _grey(g, misc, key);
}
/// @dev Top, neck, (face paint,) facial hair, expression, eyewear, hair, hat, over-ear (and the hat brim again), mouth.
function _layers(uint256[] memory g, uint256 a, uint8[15] memory t, uint256[64] memory base, uint256 build) internal pure {
if (t[T_TOP] != 0) {
uint256 r = _sec(a, S_TOPS) + 15 * (uint256(t[T_TOP]) - 1);
_paint(g, a, _u16(r + 2 + 4 * build), base, _u16(r));
}
if (t[T_NECK] != 0) {
uint256 r = _sec(a, S_NECK) + 4 * (uint256(t[T_NECK]) - 1);
_paint(g, a, _u16(r), base, _u16(r + 2));
}
uint256 col = t[T_HAIR_COLOR] == 0 ? 0 : _sec(a, S_HAIRCOL) + 5 * (uint256(t[T_HAIR_COLOR]) - 1);
// An expression that is a face paint goes on before facial hair, and the skin that hair and hats draw
// takes the paint's colours. Any other expression goes on after facial hair.
uint256 ex = t[T_EXPRESSION] == 0 ? 0 : _sec(a, S_EXPR) + EXPR_REC * (uint256(t[T_EXPRESSION]) - 1);
uint256 paint = ex == 0 ? NONE : _u16(ex + 8);
if (paint != NONE) _paint(g, a, _u16(ex + 2 * build), base, _u16(ex + 6));
if (t[T_FACIAL] != 0) {
uint256 r = _sec(a, S_FACIAL) + 6 * (uint256(t[T_FACIAL]) - 1);
_paint(g, a, _u16(r + 2 * build), base, col == 0 ? NONE : _u16(col + 2));
}
if (ex != 0 && paint == NONE) _paint(g, a, _u16(ex + 2 * build), base, _u16(ex + 6));
if (t[T_EYEWEAR] != 0) {
uint256 r = _sec(a, S_EYEWEAR) + 4 * (uint256(t[T_EYEWEAR]) - 1);
_paint(g, a, _u16(r), base, _u16(r + 2));
}
uint256 hat = t[T_HEADWEAR] == 0 ? 0 : _sec(a, S_HATS) + 7 * (uint256(t[T_HEADWEAR]) - 1);
uint256 shape = hat == 0 ? 0 : _u8(hat); // 0 = no hat, else the hat's shape, numbered from 1
uint256 shapes = _u8(_sec(a, S_MISC) + 8); // how many hat shapes there are
if (t[T_HAIR] != 0) {
uint256 speck = col == 0 ? 0 : _u8(col + 4);
uint256 r = _sec(a, S_HAIR) + 4 * (shapes + 1) * (uint256(t[T_HAIR]) - 1) + 2 * (shapes + 1) * speck + 2 * shape;
_paint2(g, a, _u16(r), base, col == 0 ? NONE : _u16(col), paint);
}
if (hat != 0) _paint2(g, a, _u16(hat + 3), base, _u16(hat + 1), paint);
if (t[T_OVER_EAR] != 0) {
uint256 r = _sec(a, S_EARS) + (4 + 2 * shapes) * (uint256(t[T_OVER_EAR]) - 1);
_paint(g, a, _u16(r + 2 + 2 * shape), base, _u16(r));
if (hat != 0) _paint(g, a, _u16(hat + 5), base, _u16(hat + 1));
}
if (t[T_MOUTH] != 0) {
uint256 r = _sec(a, S_MOUTH) + 4 * (uint256(t[T_MOUTH]) - 1);
_paint(g, a, _u16(r), base, _u16(r + 2));
}
}
/// @dev The dad standing in his location (the collection renderer's `draw`).
function _draw(uint256 a, uint8[15] memory t) internal pure returns (uint256[] memory g) {
uint256 key;
uint256 trace;
bool milk;
(g, key, trace, milk) = _figure(a, t);
uint256 loc = a + _u24(_sec(a, S_LOC) + 3 * (uint256(t[T_BACKGROUND]) - 1));
uint256 pal = loc + 1;
uint256 front = pal + 3 * _u8(loc);
uint256 nFront = _u16(front);
uint256 run = front + 2 + 5 * nFront;
uint256 i;
while (i < PIXELS) {
uint256 len = _u8(run);
uint256 back = _u24(pal + 3 * _u8(run + 1));
run += 2;
for (uint256 end = i + len; i < end; ++i) {
uint256 c = g[i];
if (c == key) {
g[i] = back;
} else if (milk && c == trace) {
g[i] = ((((back >> 16) * 62) / 100) << 16) | (((((back >> 8) & 0xff) * 62) / 100) << 8) | (((back & 0xff) * 62) / 100);
}
}
}
if (t[T_HOLDING] != 0 && !milk) {
_steam(g, a, _u16(_sec(a, S_HOLDING) + HOLD_REC * (uint256(t[T_HOLDING]) - 1) + 8 + 2 * (uint256(t[T_BUILD]) - 1)));
}
for (uint256 f; f < nFront; ++f) {
uint256 q = front + 2 + 5 * f;
g[_u8(q + 1) * N + _u8(q)] = _u24(q + 2);
}
}
/// @dev Steam off a held mug: each listed pixel is whatever is there, moved part of the way to white.
/// The art file holds the result for every level 0 to 255.
function _steam(uint256[] memory g, uint256 a, uint256 bmp) internal pure {
if (bmp == NONE) return;
uint256 misc = _sec(a, S_MISC);
uint256 table = misc + 10 + 4 * _u8(misc + 9);
uint256 p = a + _u24(_sec(a, S_BMP) + 3 * bmp);
uint256 rows = _u8(p);
++p;
for (uint256 r; r < rows; ++r) {
uint256 at = _u8(p) * N + _u8(p + 1);
uint256 len = _u8(p + 2);
p += 3;
for (uint256 x; x < len; ++x) {
if (_u8(p + x) != 0) {
uint256 c = g[at + x];
g[at + x] = (_u8(table + (c >> 16)) << 16) | (_u8(table + ((c >> 8) & 0xff)) << 8) | _u8(table + (c & 0xff));
}
}
p += len;
}
}
/// @dev The picture every dad shows before the reveal: a finished, animated SVG file held in the art data
/// and returned exactly as stored (tools/sealed.py builds it and checks its frames).
function _sealed(uint256 a) internal pure returns (bytes memory out) {
uint256 p = _sec(a, S_SEALED);
uint256 len = _u16(p);
out = new bytes(len);
assembly ("memory-safe") {
mcopy(add(out, 32), add(p, 2), len)
}
}
/// @dev Paint one pixel map onto the grid, colours from the dad's own palette with the layer's laid over it.
function _paint(uint256[] memory g, uint256 a, uint256 bmp, uint256[64] memory base, uint256 palId) internal pure {
_paint2(g, a, bmp, base, palId, NONE);
}
/// @dev The same, with a second palette laid over the first (a face paint over hair and hat colours).
function _paint2(uint256[] memory g, uint256 a, uint256 bmp, uint256[64] memory base, uint256 palId, uint256 palId2) internal pure {
if (bmp == NONE) return;
uint256[64] memory pal;
if (palId == NONE && palId2 == NONE) {
pal = base;
} else {
for (uint256 i; i < 64; ++i) {
pal[i] = base[i];
}
_palette(a, pal, palId, false);
_palette(a, pal, palId2, false);
}
uint256 p = a + _u24(_sec(a, S_BMP) + 3 * bmp);
uint256 rows = _u8(p);
++p;
for (uint256 r; r < rows; ++r) {
uint256 at = _u8(p) * N + _u8(p + 1);
uint256 len = _u8(p + 2);
p += 3;
for (uint256 x; x < len; ++x) {
uint256 code = _u8(p + x);
if (code != 0) {
uint256 c = pal[code];
if (c != NO_COLOUR) g[at + x] = c;
}
}
p += len;
}
}
/// @dev Went for Milk: only every other pixel of the base outline, in the trace colour.
function _trace(uint256[] memory g, uint256 a, uint256 bmp, uint256 trace) internal pure {
uint256 p = a + _u24(_sec(a, S_BMP) + 3 * bmp);
uint256 rows = _u8(p);
++p;
for (uint256 r; r < rows; ++r) {
uint256 y = _u8(p);
uint256 x0 = _u8(p + 1);
uint256 len = _u8(p + 2);
p += 3;
for (uint256 x; x < len; ++x) {
if (_u8(p + x) == OUTLINE && (x0 + x + y) % 2 == 0) g[y * N + x0 + x] = trace;
}
p += len;
}
}
function _palette(uint256 a, uint256[64] memory pal, uint256 palId, bool clear) internal pure {
if (clear) {
for (uint256 i; i < 64; ++i) {
pal[i] = NO_COLOUR;
}
}
if (palId == NONE) return;
uint256 p = a + _u24(_sec(a, S_PAL) + 3 * palId);
uint256 n = _u8(p);
++p;
for (uint256 i; i < n; ++i) {
pal[_u8(p)] = _u24(p + 1);
p += 4;
}
}
/// @dev Dad Bod and Jacked: push the listed columns of a row outward by one pixel, left side then right.
function _widen(uint256[] memory g, uint256 a, uint256 id) internal pure {
if (id == NONE) return;
uint256 p = a + _u24(_sec(a, S_WIDEN) + 3 * id);
uint256 rows = _u8(p);
++p;
for (uint256 r; r < rows; ++r) {
uint256 row = _u8(p) * N;
uint256 n = _u8(p + 1);
p += 2;
for (uint256 j; j < n; ++j) {
uint256 k = _u8(p + j);
for (uint256 x; x < k; ++x) {
g[row + x] = g[row + x + 1];
}
}
p += n;
n = _u8(p);
++p;
for (uint256 j; j < n; ++j) {
uint256 k = _u8(p + j);
for (uint256 x = N - 1; x > k; --x) {
g[row + x] = g[row + x - 1];
}
}
p += n;
}
}
/// @dev TV Dad: every pixel of the dad becomes its grey level, floor(0.30 r + 0.59 g + 0.11 b).
/// The art file lists the few colours where whole-number maths and the original differ.
function _grey(uint256[] memory g, uint256 misc, uint256 key) internal pure {
uint256 nFix = _u8(misc + 9);
for (uint256 i; i < PIXELS; ++i) {
uint256 c = g[i];
if (c == key) continue;
uint256 v = (30 * (c >> 16) + 59 * ((c >> 8) & 0xff) + 11 * (c & 0xff)) / 100;
for (uint256 f; f < nFix; ++f) {
if (_u24(misc + 10 + 4 * f) == c) v = _u8(misc + 13 + 4 * f);
}
g[i] = (v << 16) | (v << 8) | v;
}
}
// ------------------------------------------------------------------------------------------------
// output
// ------------------------------------------------------------------------------------------------
function _svg(uint256[] memory g) internal pure returns (bytes memory out) {
out = new bytes(256 + PIXELS * 64);
uint256 start;
assembly ("memory-safe") {
start := add(out, 32)
}
uint256 p = _put(start, '<svg xmlns="http://www.w3.org/2000/svg" viewBox="0 0 24 24" width="1200" height="1200" shape-rendering="crispEdges">');
for (uint256 y; y < N; ++y) {
uint256 x;
while (x < N) {
uint256 c = g[y * N + x];
uint256 w = 1;
while (x + w < N && g[y * N + x + w] == c) ++w;
p = _put(p, '<rect x="');
p = _putUint(p, x);
p = _put(p, '" y="');
p = _putUint(p, y);
p = _put(p, '" width="');
p = _putUint(p, w);
p = _put(p, '" height="1" fill="#');
p = _putHex(p, c);
p = _put(p, '"/>');
x += w;
}
}
p = _put(p, "</svg>");
assembly ("memory-safe") {
mstore(out, sub(p, start))
}
}
function _put(uint256 p, bytes memory s) internal pure returns (uint256) {
uint256 len = s.length;
assembly ("memory-safe") {
mcopy(p, add(s, 32), len)
}
return p + len;
}
function _putRaw(uint256 p, uint256 src, uint256 len) internal pure returns (uint256) {
assembly ("memory-safe") {
mcopy(p, src, len)
}
return p + len;
}
function _putUint(uint256 p, uint256 v) internal pure returns (uint256) {
uint256 digits = 1;
for (uint256 t = v; t >= 10; t /= 10) {
++digits;
}
for (uint256 i = digits; i != 0; --i) {
uint256 d = 48 + (v % 10);
uint256 at = p + i - 1;
assembly ("memory-safe") {
mstore8(at, d)
}
v /= 10;
}
return p + digits;
}
function _putHex(uint256 p, uint256 rgb) internal pure returns (uint256) {
bytes16 digits = "0123456789abcdef";
for (uint256 i; i < 6; ++i) {
uint256 d = uint8(digits[(rgb >> (20 - 4 * i)) & 0xf]);
uint256 at = p + i;
assembly ("memory-safe") {
mstore8(at, d)
}
}
return p + 6;
}
// ------------------------------------------------------------------------------------------------
// reading the stored data
// ------------------------------------------------------------------------------------------------
/// @dev The whole art file in memory; returns the address of its first byte.
function _loadArt() internal view returns (uint256 a) {
bytes memory blob = _concat(artChunks);
assembly ("memory-safe") {
a := add(blob, 32)
}
}
/// @dev Join data chunks. Each chunk's code is one STOP byte followed by the data.
function _concat(address[] memory chunks) internal view returns (bytes memory out) {
uint256 total;
for (uint256 i; i < chunks.length; ++i) {
total += chunks[i].code.length - 1;
}
out = new bytes(total);
uint256 at;
for (uint256 i; i < chunks.length; ++i) {
address c = chunks[i];
uint256 len = c.code.length - 1;
assembly ("memory-safe") {
extcodecopy(c, add(add(out, 32), at), 1, len)
}
at += len;
}
}
function _sec(uint256 a, uint256 i) internal pure returns (uint256) {
return a + _u24(a + 3 * i);
}
function _u8(uint256 p) internal pure returns (uint256 v) {
assembly ("memory-safe") {
v := byte(0, mload(p))
}
}
function _u16(uint256 p) internal pure returns (uint256 v) {
assembly ("memory-safe") {
v := shr(240, mload(p))
}
}
function _u24(uint256 p) internal pure returns (uint256 v) {
assembly ("memory-safe") {
v := shr(232, mload(p))
}
}
}
lib/openzeppelin-contracts/contracts/utils/Base64.sol
// SPDX-License-Identifier: MIT
// OpenZeppelin Contracts (last updated v5.4.0) (utils/Base64.sol)
pragma solidity ^0.8.20;
/**
* @dev Provides a set of functions to operate with Base64 strings.
*/
library Base64 {
/**
* @dev Base64 Encoding/Decoding Table
* See sections 4 and 5 of https://datatracker.ietf.org/doc/html/rfc4648
*/
string internal constant _TABLE = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/";
string internal constant _TABLE_URL = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789-_";
/**
* @dev Converts a `bytes` to its Bytes64 `string` representation.
*/
function encode(bytes memory data) internal pure returns (string memory) {
return _encode(data, _TABLE, true);
}
/**
* @dev Converts a `bytes` to its Bytes64Url `string` representation.
* Output is not padded with `=` as specified in https://www.rfc-editor.org/rfc/rfc4648[rfc4648].
*/
function encodeURL(bytes memory data) internal pure returns (string memory) {
return _encode(data, _TABLE_URL, false);
}
/**
* @dev Internal table-agnostic conversion
*/
function _encode(bytes memory data, string memory table, bool withPadding) private pure returns (string memory) {
/**
* Inspired by Brecht Devos (Brechtpd) implementation - MIT licence
* https://github.com/Brechtpd/base64/blob/e78d9fd951e7b0977ddca77d92dc85183770daf4/base64.sol
*/
if (data.length == 0) return "";
// If padding is enabled, the final length should be `bytes` data length divided by 3 rounded up and then
// multiplied by 4 so that it leaves room for padding the last chunk
// - `data.length + 2` -> Prepare for division rounding up
// - `/ 3` -> Number of 3-bytes chunks (rounded up)
// - `4 *` -> 4 characters for each chunk
// This is equivalent to: 4 * Math.ceil(data.length / 3)
//
// If padding is disabled, the final length should be `bytes` data length multiplied by 4/3 rounded up as
// opposed to when padding is required to fill the last chunk.
// - `4 * data.length` -> 4 characters for each chunk
// - ` + 2` -> Prepare for division rounding up
// - `/ 3` -> Number of 3-bytes chunks (rounded up)
// This is equivalent to: Math.ceil((4 * data.length) / 3)
uint256 resultLength = withPadding ? 4 * ((data.length + 2) / 3) : (4 * data.length + 2) / 3;
string memory result = new string(resultLength);
assembly ("memory-safe") {
// Prepare the lookup table (skip the first "length" byte)
let tablePtr := add(table, 1)
// Prepare result pointer, jump over length
let resultPtr := add(result, 0x20)
let dataPtr := data
let endPtr := add(data, mload(data))
// In some cases, the last iteration will read bytes after the end of the data. We cache the value, and
// set it to zero to make sure no dirty bytes are read in that section.
let afterPtr := add(endPtr, 0x20)
let afterCache := mload(afterPtr)
mstore(afterPtr, 0x00)
// Run over the input, 3 bytes at a time
for {} lt(dataPtr, endPtr) {} {
// Advance 3 bytes
dataPtr := add(dataPtr, 3)
let input := mload(dataPtr)
// To write each character, shift the 3 byte (24 bits) chunk
// 4 times in blocks of 6 bits for each character (18, 12, 6, 0)
// and apply logical AND with 0x3F to bitmask the least significant 6 bits.
// Use this as an index into the lookup table, mload an entire word
// so the desired character is in the least significant byte, and
// mstore8 this least significant byte into the result and continue.
mstore8(resultPtr, mload(add(tablePtr, and(shr(18, input), 0x3F))))
resultPtr := add(resultPtr, 1) // Advance
mstore8(resultPtr, mload(add(tablePtr, and(shr(12, input), 0x3F))))
resultPtr := add(resultPtr, 1) // Advance
mstore8(resultPtr, mload(add(tablePtr, and(shr(6, input), 0x3F))))
resultPtr := add(resultPtr, 1) // Advance
mstore8(resultPtr, mload(add(tablePtr, and(input, 0x3F))))
resultPtr := add(resultPtr, 1) // Advance
}
// Reset the value that was cached
mstore(afterPtr, afterCache)
if withPadding {
// When data `bytes` is not exactly 3 bytes long
// it is padded with `=` characters at the end
switch mod(mload(data), 3)
case 1 {
mstore8(sub(resultPtr, 1), 0x3d)
mstore8(sub(resultPtr, 2), 0x3d)
}
case 2 {
mstore8(sub(resultPtr, 1), 0x3d)
}
}
}
return result;
}
}
Compiler settingsShow
{
"viaIR": true,
"metadata": {
"bytecodeHash": "ipfs"
},
"optimizer": {
"runs": 200,
"enabled": true
},
"evmVersion": "cancun",
"remappings": [
"/Users/blakewilson/dads/contracts/lib/creator-token-standards/:@openzeppelin/contracts/=lib/openzeppelin-contracts-v4/contracts/",
"/Users/blakewilson/dads/contracts/lib/openzeppelin-contracts/:forge-std/=lib/openzeppelin-contracts/lib/forge-std/src/",
"@limitbreak/creator-token-standards/=lib/creator-token-standards/",
"@limitbreak/permit-c/=lib/PermitC/src/",
"@opensea/tstorish/=lib/creator-token-standards/lib/tstorish/src/",
"@openzeppelin-v4/contracts/=lib/openzeppelin-contracts-v4/contracts/",
"@openzeppelin/=lib/PermitC/lib/openzeppelin-contracts/",
"@openzeppelin/contracts/=lib/openzeppelin-contracts/contracts/",
"@rari-capital/solmate/=lib/PermitC/lib/solmate/",
"PermitC/=lib/PermitC/src/",
"creator-token-standards/=lib/creator-token-standards/src/",
"ds-test/=lib/PermitC/lib/forge-std/lib/ds-test/src/",
"erc4626-tests/=lib/openzeppelin-contracts/lib/erc4626-tests/",
"erc721a/=lib/creator-token-standards/lib/ERC721A/",
"forge-gas-metering/=lib/PermitC/lib/forge-gas-metering/",
"forge-std/=lib/forge-std/src/",
"halmos-cheatcodes/=lib/openzeppelin-contracts/lib/halmos-cheatcodes/src/",
"murky/=lib/creator-token-standards/lib/murky/src/",
"openzeppelin-contracts-v4/=lib/openzeppelin-contracts-v4/",
"openzeppelin-contracts/=lib/openzeppelin-contracts/",
"openzeppelin/=lib/openzeppelin-contracts-v4/contracts/",
"lib/creator-token-standards/:@openzeppelin/contracts/=lib/openzeppelin-contracts-v4/contracts/",
"lib/openzeppelin-contracts/:forge-std/=lib/openzeppelin-contracts/lib/forge-std/src/"
]
}Source code and metadata: Sourcify.
The source code belongs to the authors of the contract.