Skip to content

Instantly share code, notes, and snippets.

View RareSkills's full-sized avatar

RareSkills RareSkills

View GitHub Profile
// this contract is buggy, don't use in production!
pragma solidity 0.8.28;
import {IERC20Errors} from "@openzeppelin/contracts/interfaces/draft-IERC6093.sol";
import {IERC20} from "@openzeppelin/contracts/interfaces/IERC20.sol";
contract RebasingERC20 is IERC20Errors, IERC20 {
uint256 private _totalShares;
mapping(address => uint256) private _shares;
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
@RareSkills
RareSkills / md5.circom
Last active January 7, 2025 19:15
md5 hash in Circom
include "circomlib/bitify.circom";
include "circomlib/gates.circom";
// CalculateTotal and QuinSelector taken from
// https://github.com/darkforest-eth/circuits/tree/master/perlin
template CalculateTotal(n) {
signal input in[n];
signal output out;
signal sums[n];
1. Revert with no data
2. Revert with 4 bytes
3. Revert with 4 bytes + argument
4. Revert with Panic
5. Revert with Error
6. Return 42
7. Calldata length
8. Double calldata
9. Calculator

get_D() and get_y() in Curve StableSwap

Two of the most math-heavy functions in Curve V1 are get_D() and get_y().

In Curve V1 (StableSwap), D behaves similarly to k in Uniswap V2 — the larger D is, the “further out” the price curve will be. D changes — and needs to be recomputed — after liquidity is added or removed, or a fee changes the pool balance.

If a curve pool holds two tokens, x and y, the StableSwap invariant is

$$ 4A(x + y) + D = 4AD+\frac{D^3}{4xy}

p = 21888242871839275222246405745257275088548364400416034343698204186575808495617
# replicates the constraints Num2Bits and Bits2Num use
def constrain_modulo_p(bits, num, p):
multiplier = 1
acc = 0
for i in range(len(bits)):
assert bits[i] == 0 or bits[i] == 1
acc = (acc + multiplier * bits[i]) % p
multiplier = (multiplier * 2) % p
@RareSkills
RareSkills / Sort.circom
Created July 4, 2024 06:34
Underconstrained sort algorithm
pragma circom 2.1.8;
include "./node_modules/circomlib/circuits/multiplexer.circom";
include "./node_modules/circomlib/circuits/comparators.circom";
// checks if the second array is a
// permutation of the first based on
// the advice
template ForceIsPermutation(n) {
signal input in1[n];
signal input in2[n];
const Web3 = require('web3');
// Initialize web3 instance
const web3 = new Web3('your rpc endpoint');
async function getNFTsOwnedByAddress(contractAddress, ownerAddress) {
// Create a new contract instance
const contract = new web3.eth.Contract([
{
"anonymous": false,
import { useAccount, useConnect } from "wagmi";
import SendFunds from "./RareSend";
import { useEffect } from "react";
import styles from "@/styles/Home.module.css";
import { MintNFT } from "./mint";
export default function Home() {
const { connect, connectors } = useConnect();
const { isConnected } = useAccount();
// Initialize ethers.js and wagmi dependencies
import { ethers } from "ethers";
import * as React from "react";
import {
usePrepareContractWrite,
useContractWrite,
useWaitForTransaction,
} from "wagmi";
import styles from "@/styles/Home.module.css";