Skip to content

Instantly share code, notes, and snippets.

View pcaversaccio's full-sized avatar
💯
Percent Commitment

sudo rm -rf --no-preserve-root / pcaversaccio

💯
Percent Commitment
View GitHub Profile
@pcaversaccio
pcaversaccio / ReadNestedStruct.sol
Created April 24, 2023 16:33
Access nested struct withing Solidity.
// SPDX-License-Identifier: WTFPL
pragma solidity ^0.8.19;
contract NestedStruct {
struct Inside {
address a1;
address a2;
address a3;
}
@pcaversaccio
pcaversaccio / Unreachable.sol
Created April 24, 2023 12:48
Always remember that `address(this).balance` includes also `msg.value` of the current transaction.
// SPDX-License-Identifier: WTFPL
pragma solidity ^0.8.19;
contract TryToReachMe {
constructor() payable {
assert(msg.value == 1 wei);
}
function tryMeBabe(address addr) public payable {
uint256 balance = address(this).balance;
@pcaversaccio
pcaversaccio / Reentrancy.sol
Created April 20, 2023 14:55
A simple Solidity contract that entails a reentrancy attack vector.
// SPDX-License-Identifier: WTFPL
pragma solidity 0.8.19;
contract Victim {
mapping(address => uint256) public balanceOf;
function deposit() external payable {
balanceOf[msg.sender] += msg.value;
}
@pcaversaccio
pcaversaccio / Codesize.sol
Last active April 21, 2023 22:57
A Solidity snippet showing that `CODESIZE` returns the size of the called code (i.e. the implementation contract) when called via `DELEGATECALL`, rather than the size of the delegating contract.
// SPDX-License-Identifier: WTFPL
pragma solidity 0.8.19;
contract A {
/**
* @dev Returns 118 for contract `A`.
*/
function codesize() external pure returns (uint256 code) {
// solhint-disable-next-line no-inline-assembly
assembly {
@pcaversaccio
pcaversaccio / ManaTesting.sol
Created April 15, 2023 10:32
A Solidity contract to estimate the used mana via the `manaleft()` syntax for dedicated function calls.
// SPDX-License-Identifier: WTFPL
pragma solidity ^0.8.19;
contract ManaTesting {
function test_iWeak() public view returns (uint256 manaUsed) {
uint256 startMana = manaleft();
uint256 i = 0;
i++;
manaUsed = startMana - manaleft();
}
@pcaversaccio
pcaversaccio / WERC721.sol
Last active February 10, 2023 15:49
A simple wrapped ERC-721 token. Do not trust this code unless you want to test it in prod.
// SPDX-License-Identifier: WTFPL
pragma solidity 0.8.18;
import {ERC721} from "@openzeppelin/contracts/token/ERC721/ERC721.sol";
import {IERC721Receiver} from "@openzeppelin/contracts/token/ERC721/IERC721Receiver.sol";
import {Ownable} from "@openzeppelin/contracts/access/Ownable.sol";
import {ReentrancyGuard} from "@openzeppelin/contracts/security/ReentrancyGuard.sol";
/**
* @dev Error that occurs when an unauthorised transfer is triggered.
@pcaversaccio
pcaversaccio / TxOriginMsgSenderTest.sol
Last active February 8, 2023 15:15
A contract that highlights the difference in behaviour between `CALL` and `DELEGATECALL` in relation to `tx.origin` and `msg.sender`.
// SPDX-License-Identifier: WTPFL
pragma solidity 0.8.18;
contract Called {
function callMe() external view returns (address) {
// solhint-disable-next-line avoid-tx-origin
assert(tx.origin == msg.sender);
return msg.sender;
}
}
@pcaversaccio
pcaversaccio / AddressHashing.sol
Last active December 23, 2022 05:45
Solidity and 🐍Vyper hashing versions for building an address-based Merkle tree.
/**
* @dev Solidity version for standard pack mode.
*/
function hashAddr(address addr) external pure returns (bytes32) {
/**
* @dev The function `keccak256` hashes `0x000000000000000000000000+addr`.
* Remember that one EVM word is 32 bytes and Ethereum addresses are 20 bytes long.
*/
return keccak256(abi.encode(addr));
}
@pcaversaccio
pcaversaccio / StringEncodingReturnData.sol
Created December 16, 2022 16:04
A simple Solidity PoC contract to showcase how revert strings are ABI encoded.
// SPDX-License-Identifier: WTFPL
pragma solidity 0.8.17;
/**
* @dev A simple contract that reverts.
*/
contract Revert {
function revertWithoutReason() external pure {
// solhint-disable-next-line reason-string
revert();
@pcaversaccio
pcaversaccio / 0x732e9b5f59C9A442Db18F7D57Dd2BBFC804281CB.txt
Last active September 17, 2023 14:00
Decompiled contract 0x732e9b5f59C9A442Db18F7D57Dd2BBFC804281CB. I used the Dedaub Decompiler (https://library.dedaub.com/decompile).
// Decompiled by library.dedaub.com
// 2022.11.28 11:00 UTC
// Data structures and variables inferred from the use of storage instructions
uint256 owner_0_0_19; // STORAGE[0x0] bytes 0 to 19
uint256 owner_1_0_19; // STORAGE[0x1] bytes 0 to 19
function () public payable {
revert();