Skip to content

Instantly share code, notes, and snippets.

View PaulRBerg's full-sized avatar

Paul Razvan Berg PaulRBerg

View GitHub Profile
@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

// SPDX-License-Identifier: Unlicense
pragma solidity >=0.8.4;
/// @title Create2Utility
/// @author Paul Razvan Berg
/// @dev Forked from OpenZeppelin
/// https://github.com/OpenZeppelin/openzeppelin-contracts/blob/v4.5.0/contracts/utils/Create2.sol
contract Create2Utility {
/// @dev Returns the address where a contract will be stored if deployed via {deploy}. Any change in the
/// `bytecodeHash` or `salt` will result in a new destination address.
@PaulRBerg
PaulRBerg / print-github-secret.yaml
Last active March 8, 2022 18:17
Simple workflow to print a GitHub Secret because GitHub doesn't offer this feature yet
name: "Echo GitHub secret"
on:
push:
branches:
- "main"
jobs:
print-github-secret:
runs-on: "ubuntu-latest"