Skip to content

Instantly share code, notes, and snippets.

View chuckbergeron's full-sized avatar
🎯
pooltogether.com

Chuck Bergeron chuckbergeron

🎯
pooltogether.com
View GitHub Profile
@chuckbergeron
chuckbergeron / past-prizes-queries.js
Last active September 30, 2021 16:40
Code to query historical prizes from the graph protocol
// This would be called with some code like this:
import { poolPrizesQuery } from 'queries/poolPrizesQuery'
const variables = {
poolAddress: prizePool.address.toLowerCase(),
first: pageSize,
skip: (page - 1) * pageSize
}
const graphQLClient = new GraphQLClient(url)
@chuckbergeron
chuckbergeron / pt-v5-arb-1-find-liquidation-pairs.ts
Last active September 11, 2023 19:27
PoolTogether v5 Hyperstructure - Arbitrage Bot, Finding Liquidation Pairs
const liquidationPairFactoryContract = new ethers.Contract(
liquidationPairFactoryAddress,
LiquidationPairFactoryAbi,
readProvider
);
let liquidationPairContracts: Contract[] = [];
const numPairs = await liquidationPairFactoryContract.totalPairs();
for (let i = 0; i < numPairs; i++) {
@chuckbergeron
chuckbergeron / pt-v5-arb-2-calculate-amounts.ts
Last active September 11, 2023 19:38
PoolTogether v5 Hyperstructure - Arbitrage Bot, Calculate Amounts
const maxAmountOut = await liquidationPairContract.callStatic.maxAmountOut();
const exactAmountIn = await liquidationPairContract.callStatic.computeExactAmountIn(
wantedAmountOut,
);
@chuckbergeron
chuckbergeron / pt-v5-arb-6-balanceOf.ts
Last active September 11, 2023 22:14
PoolTogether v5 Hyperstructure - Arbitrage Bot, Balance
// Your Relayer (the EOA or "externally owned account") will be swapping POOL
// tokens, so it will need to have a POOL balance.
const relayerAddress = '0x49ca801A80e31B1ef929eAB13Ab3FBbAe7A55e8F';
// Check if tokenIn balance for relayer (bot) account is sufficient
const tokenInContract = new ethers.Contract(tokenInAddress, ERC20Abi, writeProvider);
const tokenInBalance = await tokenInContract.balanceOf(relayerAddress);
const sufficientBalance = tokenInBalance.gt(exactAmountIn);
if (!sufficientBalance) {
@chuckbergeron
chuckbergeron / pt-v5-arb-7-allowance.ts
Last active September 20, 2023 22:01
PoolTogether v5 Hyperstructure - Arbitrage Bot, Allowance
// Get allowance approval
const allowance = await tokenInContract.allowance(
relayerAddress,
liquidationRouterContract.address
);
// Note that we're setting it to the max allowance as we trust the security
// audits of the LiquidationRouter contract
if (allowance.lt(exactAmountIn)) {
const tx = await tokenInContract.approve(
@chuckbergeron
chuckbergeron / pt-v5-arb-8-swap-transaction.ts
Last active September 20, 2023 22:06
PoolTogether v5 Hyperstructure - Arbitrage Bot, Swap Transaction
if (profitable) {
const transactionPopulated = await
liquidationRouterContract.populateTransaction.swapExactAmountOut(
liquidationPair.address,
swapRecipient,
wantedAmountsOut[selectedIndex],
amountInMin,
Math.floor(Date.now() / 1000) + 100 // deadline
);
@chuckbergeron
chuckbergeron / pt-v5-prize-1-main-function.ts
Last active September 19, 2023 18:26
PoolTogether v5 Hyperstructure - Prize Claiming Bot, main() Function
import { Contract, BigNumber } from 'ethers';
import { JsonRpcProvider } from '@ethersproject/providers'
import { formatUnits } from '@ethersproject/units';
import {
computeDrawWinners,
downloadContractsBlob,
flagClaimedRpc,
Claim,
ContractsBlob,
} from '@generationsoftware/pt-v5-utils-js';
@chuckbergeron
chuckbergeron / pt-v5-prize-5-calculate-profit.ts
Last active September 12, 2023 22:45
PoolTogether v5 Hyperstructure - Prize Claiming Bot, Calculating Profitable Claims
const calculateProfit = async (
vault: string,
tierIndex: number,
claimerContract: Contract,
groupedClaims: any,
): Promise<ClaimPrizesParams> => {
const { claimCount, claimFeesUsd, totalCostUsd, minVrgdaFeePerClaim } =
await getClaimInfo(
claimerContract,
tierIndex,
@chuckbergeron
chuckbergeron / pt-v5-prize-7-execute-prize-tx.ts
Last active September 7, 2023 22:09
PoolTogether v5 Hyperstructure - Prize Claiming Bot, Executing claimPrizes Transaction
const executeTransaction = async (claimPrizesParams, claimerContract: Contract) => {
// It's profitable if there is at least 1 claim to claim
if (claimPrizesParams.winners.length > 0) {
const populatedTx = await claimerContract.populateTransaction.claimPrizes(
...Object.values(claimPrizesParams),
);
const tx = await relayer.sendTransaction({
data: populatedTx.data,
to: populatedTx.to,
@chuckbergeron
chuckbergeron / pt-v5-prize-3-find-unclaimed.ts
Last active September 12, 2023 20:18
PoolTogether v5 Hyperstructure - Prize Claiming Bot, Find and Group Unclaimed
const findAndGroupUnclaimed = async (claims: Claim[]) => {
// Cross-reference claimed prizes to flag if a prize has been claimed or not
claims = await flagClaimedRpc(readProvider, contracts, claims);
let unclaimedClaims = claims.filter((claim) => !claim.claimed);
console.log(`${unclaimedClaims.length} prizes remaining to be claimed...`);
if (unclaimedClaims.length === 0) {
console.log(`No prizes left to claim. Exiting ...`);
return;