Skip to content

Instantly share code, notes, and snippets.

View karmacoma-eth's full-sized avatar

karmacoma karmacoma-eth

View GitHub Profile
@karmacoma-eth
karmacoma-eth / IERC1155MetadataURI.json
Created September 20, 2021 22:33
IERC1155MetadataURI JSON ABI
[
{
"anonymous": false,
"inputs": [
{
"indexed": true,
"internalType": "address",
"name": "account",
"type": "address"
},
@karmacoma-eth
karmacoma-eth / sending-ether-cheat-sheet.md
Last active August 22, 2024 17:07
Sending Ether Cheat Sheet

Sending Ether Cheat Sheet

TLDR

🥇 Instead of sending Ether, use the withdrawal pattern

🥈 If you really need to send Ether, use a safe wrapper like OpenZeppelin's Address.sendValue(addr, amount)

🥉 If you really need to send Ether without dependencies, use (bool success, ) = addr.call{value: amount}("")

@karmacoma-eth
karmacoma-eth / CheckBeforeStore.sol
Created February 8, 2022 19:08
Is it cheaper to check if a new value is different before storing it?
// SPDX-License-Identifier: Unlicense
pragma solidity >=0.8.0;
contract CheckBeforeStore {
event Log(address);
bytes32 stored;
function setUp() public {
stored = keccak256(abi.encode(address(this)));
@karmacoma-eth
karmacoma-eth / AdversarialRoyalties.sol
Created February 15, 2022 02:26
What if a contract returns different data when viewed off-chain vs during a transaction?
// SPDX-License-Identifier: MIT
pragma solidity >=0.8.0;
contract AdversarialRoyalties {
bytes4 private constant _INTERFACE_ID_ERC2981 = 0x2a55205a;
function supportsInterface(bytes4 interfaceID) external pure returns (bool) {
return interfaceID == _INTERFACE_ID_ERC2981;
}
@karmacoma-eth
karmacoma-eth / parallel-wait.py
Last active May 30, 2023 15:34
starts n processes with some random amount of work, and they exit whenever the first one is finished
import os
import random
import time
from multiprocessing import Pool, Event
def init_pool_processes(the_shutdown_event):
'''
Initialize each process with the global shutdown event
@karmacoma-eth
karmacoma-eth / quine.etk
Last active March 18, 2023 18:28
EVM quine
# quine.etk
# ⬜ => ⬜
# A quine is a computer program which takes no input and produces a copy of its own source code as its only output.
# 0x80...f3 is the compiled code excluding the push16 instruction (from dup1 to return)
push16 0x8060801b17606f5953600152602136f3
# --- stack ---
dup1 # code code
push1 128 # 128 code code
@karmacoma-eth
karmacoma-eth / FizzBuzz.sol
Created December 2, 2022 22:06
FizzBuzz in Solidity but ridiculous
// SPDX-License-Identifier: MIT
pragma solidity 0.8.15;
import {console2} from "forge-std/console2.sol";
contract FizzBuzz {
modifier fizz(uint256 n) {
_;
if (n % 3 == 0 && n % 5 != 0) {
console2.log("Fizz");
@karmacoma-eth
karmacoma-eth / TestJson.sol
Last active December 6, 2022 18:14
solidity-json-testing
// SPDX-License-Identifier: MIT
pragma solidity ^0.8.17;
import {Test} from "forge-std/Test.sol";
import {stdJson} from "forge-std/StdJson.sol";
import {console2} from "forge-std/console2.sol";
contract TestJson is Test {
/// @dev fields need to be sorted alphabetically (see docs of vm.parseJson())
struct ContractURISchema {
@karmacoma-eth
karmacoma-eth / xor-decoder.huff
Created December 19, 2022 00:31
A little EVM program that decodes and returns XOR-encoded data (fits in a tweet)
// compiles to the following bin-runtime: 7f2a447b3991925d0aa0728f5e78315948d6cb14530bc6ae127f114971abe35db87f452f5b4ef4b2336fc516af2a17113e27f69f612162a8c9321c7e2401c78629dd18600052596000f3
// (run with e.g. `evm --code <bin-runtime> run | cast --to-ascii`)
#define macro MAIN() = takes(0) returns(0) {
0x2a447b3991925d0aa0728f5e78315948d6cb14530bc6ae127f114971abe35db8 // ciphertext
0x452f5b4ef4b2336fc516af2a17113e27f69f612162a8c9321c7e2401c78629dd // key (which happens to be the keccak hash of the message)
xor
0x00 mstore
// returns 0x6F6B207765206E65656420746F20676F20547572696E6720636F6D706C657465
@karmacoma-eth
karmacoma-eth / rev-calldata-chunks.huff
Created February 23, 2023 18:35
reverse calldata in chunks of 32 bytes
// Reverse bytes received in calldata (abcd -> dcba)
// https://twitter.com/huff_language/status/1583894073487654913
// code length 206
/// @author Philippe Dumonet <[email protected]> -- https://twitter.com/real_philogy/status/1584304102418223104
/// @author karma (@0xkarmacoma) -- https://twitter.com/0xkarmacoma/status/1584239664310779904
/// @author kaden.eth (@0xKaden) -- https://twitter.com/0xKaden/status/1584280521089376256/
#define macro reverse_word() = takes(1) returns(1) {
// [x0]
0x00ff00ff00ff00ff00ff00ff00ff00ff00ff00ff00ff00ff00ff00ff00ff00ff