Skip to content

Instantly share code, notes, and snippets.

@frangio
frangio / Swap.t.sol
Created April 5, 2024 18:03
Testing potential gas savings for Uniswap v4 Swap events
// SPDX-License-Identifier: UNLICENSED
pragma solidity ^0.8.13;
import {Test} from "forge-std/Test.sol";
contract SwapTest is Test {
event Gasused(uint);
event Swap(
bytes32 indexed id,
@frangio
frangio / Fib.lean
Last active February 28, 2024 17:20
import Std.Data.Nat.Lemmas
import Std.Tactic.Omega
import Std.Tactic.Simpa
def fib (n : Nat) :=
match n with
| 0 | 1 => n
| n' + 2 => fib (n' + 1) + fib n'
def fastfib (n : Nat) := loop n 1 0
@frangio
frangio / rs
Last active February 8, 2024 19:56
ripsed: use ripgrep as a sed replacement. requires sponge from moreutils
#!/usr/bin/env bash
set -o errexit -o pipefail
if [ "$#" -eq 0 ]
then
echo "usage: rs PATTERN REPLACEMENT [PATH...]" > /dev/stderr
exit 1
fi
@frangio
frangio / ruby-polyfill.css
Created April 12, 2012 19:17
Ruby Polyfill
/* Simplified version of http://www.useragentman.com/blog/2010/10/29/cross-browser-html5-ruby-annotations-using-css/
for use with Modernizr.load() */
ruby {
display: inline-table;
text-align: center;
border-collapse: collapse;
border: none;
vertical-align: middle;
border-bottom: solid 0.75em transparent;
"use strict";
const defaultOptions = {
reverse: true,
python: false
}
function merge(sequences) {
let result = [];
sequences = sequences.map(s => s.slice());
@frangio
frangio / ascii.sol
Last active March 17, 2023 01:35
Solidity function to check if a string contains only printable ASCII characters and is at most 32 characters long. Branch-free "vectorized" implementation.
// SPDX-License-Identifier: MIT
pragma solidity 0.8.17;
function isAsciiPrintableShortString(string memory str) pure returns (bool) {
unchecked {
uint256 b = uint256(bytes32(hex"0101010101010101010101010101010101010101010101010101010101010101"));
bytes32 chars = bytes32(bytes(str));
uint256 len = bytes(str).length;
@frangio
frangio / Pausable.sol
Last active February 2, 2023 19:06
Lightweight explicit state machines in Solidity.
// SPDX-License-Identifier: MIT
pragma solidity 0.8.16;
import "./StateMachines.sol";
contract Pausable is StateMachines {
StateMachine m = initial("unpaused");
event Paused(bool paused);
@frangio
frangio / utf8-length.js
Last active January 14, 2023 20:38
Fastest way I found to compute the UTF-8 length of a UTF-16 string.
// larger buffer => faster
const BUFFER_SIZE = 1024;
let buffer;
/**
* @param {string} text
* @returns {number}
*/
function utf8Length(text) {
@frangio
frangio / npm-deprecated.sh
Created January 7, 2023 02:54
Print a tree of deprecated npm dependencies
npm ls $(jq -r '.packages | to_entries[] | select(.value.deprecated != null) | .key | split("node_modules/")[-1]' package-lock.json)
pragma solidity ^0.8.0;
function keccak256_2(bytes memory data) external pure returns (bytes32) {
bytes32 h1 = keccak256(data);
bytes32 h2;
/// @solidity memory-safe-assembly
assembly {
mstore(0, h1)
h2 := keccak256(0, 32)
}