Skip to content

Instantly share code, notes, and snippets.

View PaulRBerg's full-sized avatar

Paul Razvan Berg PaulRBerg

View GitHub Profile
@PaulRBerg
PaulRBerg / SignatureVerification-verify.sol
Created May 15, 2023 11:51
Reducing the control flow nesting depth of
function verify(bytes calldata signature, bytes32 hash, address claimedSigner) internal view {
bytes32 r;
bytes32 s;
uint8 v;
if (claimedSigner.code.length > 0) {
bytes4 magicValue = IERC1271(claimedSigner).isValidSignature(hash, signature);
if (magicValue != IERC1271.isValidSignature.selector) revert InvalidContractSignature();
return;
}
@PaulRBerg
PaulRBerg / IWrappedNativeAsset.sol
Last active May 15, 2023 09:43
Generic interface for wrapping native assets into ERC-20 form
// SPDX-License-Identifier: MIT
pragma solidity >=0.8.19;
import { IERC20 } from "@openzeppelin/token/ERC20/IERC20.sol";
/// @notice An interface for contracts that wrap native assets in ERC-20 form, such as WETH.
/// @dev A generic name is used instead of "WETH" to accommodate chains with different native assets.
interface IWrappedNativeAsset is IERC20 {
/// @notice Deposits native assets to receive ERC-20 wrapped assets.
function deposit() external payable;
@PaulRBerg
PaulRBerg / eslint.config.js
Last active March 23, 2023 12:31
Experimental flat config `eslint.config.js` for my TypeScript template
import js from "@eslint/js";
import globals from "globals";
import prettierConfig from "eslint-config-prettier";
import typescriptEslintRecommended from "@typescript-eslint/eslint-plugin/configs/recommended";
import typescriptEslint from "@typescript-eslint/eslint-plugin";
export default [
js.configs.recommended,
typescriptEslintRecommended,
prettierConfig,
@PaulRBerg
PaulRBerg / git-cm.toml
Created March 6, 2023 10:23
Git alias to copy the message of a commit SHA
[alias]
# Copy message of provided commit: git cm COMMIT_SHA
cm = !"git rev-list --format=%B --max-count=1 $1 | tail +2 | pbcopy"
@PaulRBerg
PaulRBerg / AbiEncode.sol
Created February 25, 2023 21:36
Toy example to see how `abi.encode` works with zero values
// SPDX-License-Identifier: MIT
pragma solidity >=0.8.19;
contract Contract {
function foo(bytes32 seed) external view returns (bytes memory) {
return abi.encode(tx.origin, seed);
}
function getBytes32(uint256 number) external pure returns (bytes32) {
return bytes32(number);
// SPDX-License-Identifier: MIT
pragma solidity >=0.8.19;
import { Script } from "forge-std/Script.sol";
abstract contract BaseScript is Script {
/// @dev Included to enable compilation of the script without a $MNEMONIC environment variable.
string internal constant TEST_MNEMONIC = "test test test test test test test test test test test junk";
/// @dev Needed for the deterministic deployments.
@PaulRBerg
PaulRBerg / foo.t.sol
Created February 12, 2023 13:43
Simple Solidity test tree
// SPDX-License-Identifier: UNLICENSED
pragma solidity >=0.8.18;
import { PRBTest } from "@prb/test/PRBTest.sol";
contract Foo is PRBTest {
function test_RevertWhen_CallerNotOwner() external {
vm.expectRevert();
// ...
}
@PaulRBerg
PaulRBerg / testFuzzOrderedArr.sol
Last active March 13, 2023 20:24
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];