Skip to content

Instantly share code, notes, and snippets.

@PaulRBerg
PaulRBerg / testFuzzOrderedArr.sol
Last active April 6, 2025 07:35
Test that fuzzes ordered arrays in Solidity
// SPDX-License-Identifier: UNLICENSED
pragma solidity >=0.8.18;
import { Test } from "forge-std/Test.sol";
import { arange } from "solidity-generators/Generators.sol";
contract MyTest is Test {
function testFuzz_OrderedArr(uint256[] memory arr) internal view {
vm.assume(arr.length != 0);
// SPDX-License-Identifier: UNLICENSED
pragma solidity >=0.8.17;
import { PRBTest } from "@prb/test/PRBTest.sol";
contract ForgeTestNamingConvention is PRBTest {
// Standard tests
function test_Description() external {}
// Tests expecting a revert
@PaulRBerg
PaulRBerg / quicksort.sol
Created December 25, 2022 19:51
QuickSort implementation in Solidity v0.8.17
pragma solidity =0.8.17;
function quickSort(uint40[] memory arr, uint40 left, uint40 right) internal pure {
if (left >= right) {
return;
}
unchecked {
// p = the pivot element
uint40 p = arr[(left + right) / 2];
@PaulRBerg
PaulRBerg / foundry-cache-rpc-github-actions.yml
Created December 14, 2022 16:03
Simple YAML script for caching RPC responses when fork testing with Foundry in GitHub Actions
- name: "Cache RPC Responses"
uses: "actions/cache@v3"
with:
path: "~/.foundry/cache/rpc/mainnet/16183456"
key: "${{ runner.os }}-mainnet-16183456"
@PaulRBerg
PaulRBerg / allMathFunctions.sol
Created November 29, 2022 16:47
All math functions in SD59x18 (v3.0.0)
// SPDX-License-Identifier: UNLICENSED
pragma solidity >=0.8.13;
import "@prb/math/SD59x18.sol";
function allMathFunctions(SD59x18 x, SD59x18 y) pure {
abs(x);
avg(x, y);
ceil(x);
div(x, y);
@PaulRBerg
PaulRBerg / msb.sol
Last active November 29, 2022 16:47
High-level Solidity function that calculates the most significant bit
/// @notice Finds the zero-based index of the first 1 in the binary representation of x.
/// @dev See the note on msb in the "Find First Set" Wikipedia article https://en.wikipedia.org/wiki/Find_first_set
/// @param x The uint256 number for which to find the index of the most significant bit.
/// @return result The index of the most significant bit as an uint256.
function msb(uint256 x) pure returns (uint256 result) {
unchecked {
if (x >= 2 ** 128) {
x >>= 128;
result += 128;
}
@PaulRBerg
PaulRBerg / foundry-badge.md
Created October 16, 2022 09:56
GitHub badge for Foundry

Foundry

@PaulRBerg
PaulRBerg / checkSameSign.sol
Created July 10, 2022 21:13
Most gas efficient way to check that two integers have the same sign in Solidity
// SPDX-License-Identifier: UNLICENSED
pragma solidity >=0.8.15;
function checkSameSign(int256 a, int256 b) pure returns (bool sameSign) {
// Get the signs of a and b.
uint256 sa;
uint256 sb;
assembly {
// This works due to two's complement representation.
// "sgt" stamds for "signed greater than".
@PaulRBerg
PaulRBerg / StructMemberGetter.sol
Created July 4, 2022 14:09
Dummy contract to access a struct member
// SPDX-License-Identifier: UNLICENSED
pragma solidity >=0.8.15;
contract StructMemberGetter {
error NotAuthorized(address caller);
struct MyStruct {
address owner;
}

0x0d5aaf2f3cb80039dc489378a620f78034fdb30ba46d721c3191c92e9f56a06f