Skip to content

Instantly share code, notes, and snippets.

View petejkim's full-sized avatar
💭
I may be slow to respond.

Pete Kim petejkim

💭
I may be slow to respond.
View GitHub Profile
@petejkim
petejkim / PolygonVotes.sol
Created April 27, 2024 16:34
PolygonVotes.sol
// SPDX-License-Identifier: MIT
pragma solidity ^0.8.20;
interface IERC20 {
event Transfer(address indexed from, address indexed to, uint256 value);
event Approval(address indexed owner, address indexed spender, uint256 value);
function totalSupply() external view returns (uint256);
function balanceOf(address account) external view returns (uint256);
function transfer(address to, uint256 value) external returns (bool);
@petejkim
petejkim / types.test.ts
Created September 23, 2021 21:52
decimal string <> BN
import BN from "bn.js";
import * as types from "./types";
test("decimalStringFromBN", () => {
expect(types.decimalStringFromBN(new BN(0))).toEqual("0");
expect(types.decimalStringFromBN(new BN(0), 1)).toEqual("0");
expect(types.decimalStringFromBN(new BN(1000000))).toEqual("1000000");
expect(types.decimalStringFromBN(new BN(1000000), 1)).toEqual("100000");
expect(types.decimalStringFromBN(new BN(1000001), 1)).toEqual("100000.1");
expect(types.decimalStringFromBN(new BN(1000100), 1)).toEqual("100010");
@petejkim
petejkim / starting-a-local-mainnet-fork
Created December 21, 2020 20:42
USDC v2: Upgrading a $1.4B Token - Starting a Local Mainnet Fork
$ ganache-cli --fork <NODE_RPC_URL> --chainId 1 --secure --unlock <ADMIN_ADDRESS>
@petejkim
petejkim / testing-storage-slot-9.js
Created December 21, 2020 20:40
USDC v2: Upgrading a $1.4B Token - Testing Storage Slot 9
// Storage slot 9 contains "balances" (mapping(address => uint256))
// Derive slot position for the key k
const k = holderAddr.slice(2).toLowerCase().padStart(64, "0");
const p = "9".padStart(64, "0");
const valueSlotPos = web3.utils.keccak256("0x" + k + p);
const data = await web3.eth.getStorageAt(contract.address, valueSlotPos);
expect(new BN(data.slice(2), 16).eq(expectedBalance)).to.be.true;
@petejkim
petejkim / testing-storage-slot-4.js
Created December 21, 2020 20:39
USDC v2: Upgrading a $1.4B Token - Testing Storage Slot 4
// Storage slot 4 contains "name" (string)
const data = await web3.eth.getStorageAt(contract.address, 4);
const len2 = parseInt(data.slice(-2), 16); // Last 1 byte = length * 2
// Read the text (skip "0x")
const text = Buffer.from(data.slice(2, 2 + len2), "hex").toString("utf8");
expect(text).to.equal(name);
@petejkim
petejkim / testing-storage-slot-1.js
Created December 21, 2020 20:38
USDC v2: Upgrading a $1.4B Token - Testing Storage Slot 0
// Storage slot 1 contains "pauser" (bool) and "paused" (address)
const data = await web3.eth.getStorageAt(contract.address, 1);
const pauserData = data.slice(-40); // Take the last 20 bytes
const pausedData = data.slice(-42, -40); // Take 1 byte before that
expect(pauserData).to.equal(pauserAddress.slice(2).toLowerCase());
expect(!!parseInt(pausedData, 16)).to.equal(paused); // Convert to boolean
@petejkim
petejkim / testing-storage-slot-0.js
Created December 21, 2020 20:36
USDC v2: Upgrading a $1.4B Token - Testing Storage Slot 0
// Storage slot 0 contains "owner" (address)
const data = await web3.eth.getStorageAt(contract.address, 0);
// Take the last 20 bytes, left-pad with zeros if needed
const ownerData = data.slice(-40).padStart(40, "0");
expect(ownerData).to.equal(
ownerAddress.slice(2).toLowerCase() // Remove "0x"
);
@petejkim
petejkim / sload-sstore-example.sol
Last active December 21, 2020 20:37
USDC v2: Upgrading a $1.4B Token - sload/sstore example
contract Foo {
bytes32 private constant NUM_SLOT = keccak256("Foo.num");
function num() public view returns (uint256 val) {
bytes32 slot = NUM_SLOT;
assembly {
val := sload(slot)
}
}
@petejkim
petejkim / USDCFaucet.sol
Created September 17, 2020 14:32
USDC Faucet in Goerli
// SPDX-License-Identifier: MIT
// Copyright (c) 2020 petejkim
pragma solidity 0.6.12;
import { Ownable } from "https://github.com/OpenZeppelin/openzeppelin-contracts/blob/v3.2.0/contracts/access/Ownable.sol";
import { IERC20 } from "https://github.com/OpenZeppelin/openzeppelin-contracts/blob/v3.2.0/contracts/token/ERC20/IERC20.sol";
import { SafeERC20 } from "https://github.com/OpenZeppelin/openzeppelin-contracts/blob/v3.2.0/contracts/token/ERC20/SafeERC20.sol";
contract USDCFaucet is Ownable {
@petejkim
petejkim / decimalString.test.ts
Last active August 26, 2020 01:53
BN <-> Decimal String
test("decimalStringFromBN", () => {
expect(types.decimalStringFromBN(new BN(0))).toEqual("0");
expect(types.decimalStringFromBN(new BN(0), 1)).toEqual("0");
expect(types.decimalStringFromBN(new BN(1000000))).toEqual("1000000");
expect(types.decimalStringFromBN(new BN(1000000), 1)).toEqual("100000");
expect(types.decimalStringFromBN(new BN(1000001), 1)).toEqual("100000.1");
expect(types.decimalStringFromBN(new BN(1000100), 1)).toEqual("100010");
expect(types.decimalStringFromBN(new BN(1000000), 2)).toEqual("10000");
expect(types.decimalStringFromBN(new BN(1000001), 2)).toEqual("10000.01");
expect(types.decimalStringFromBN(new BN(1000100), 2)).toEqual("10001");