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 / pt-v5-draw-4-send-transaction.ts
Created September 14, 2023 23:35
PoolTogether v5 Hyperstructure - Draw Auction Bot, Send Transaction
const sendTransaction = async (
relayer: Relayer,
auctionContracts: AuctionContracts,
context: DrawAuctionContext,
) => {
let populatedTx: PopulatedTransaction;
if (context.drawAuctionState === DrawAuctionState.RngStartVrfHelper) {
const chainlinkRngAuctionHelper = auctionContracts.chainlinkVRFV2DirectRngAuctionHelperContract;
populatedTx = await chainlinkRngAuctionHelper.populateTransaction.transferFeeAndStartRngRequest(
REWARD_RECIPIENT,
@chuckbergeron
chuckbergeron / pt-v5-draw-3-balance-and-allowance.ts
Last active September 14, 2023 21:43
PoolTogether v5 Hyperstructure - Draw Auction Bot, Calculate Profit
const checkBalance = (context: DrawAuctionContext) => {
const cannotAffordRngFee = context.relayer.rngFeeTokenBalance.lt(context.rngFeeAmount);
if (cannotAffordRngFee) {
console.warn(
`Need to increase relayer/bot's balance of '${context.rngFeeToken.symbol}' to pay RNG fee.`,
);
} else {
console.log('Sufficient balance ✔');
}
};
@chuckbergeron
chuckbergeron / pt-v5-draw-4-calculate-profit.ts
Last active September 14, 2023 23:35
PoolTogether v5 Hyperstructure - Draw Auction Bot, Calculate Profit
const calculateProfit = async (
gasCostUsd: number,
context: DrawAuctionContext,
): Promise<boolean> => {
const grossProfitUsd =
context.state === DrawAuctionState.RngStartVrfHelper
? context.rngExpectedRewardUsd
: context.rngRelayExpectedRewardUsd;
let netProfitUsd;
@chuckbergeron
chuckbergeron / pt-v5-draw-2-get-state-pt-3.ts
Last active September 19, 2023 21:49
PoolTogether v5 Hyperstructure - Draw Auction Bot, Gather Contract State pt. 3
export const getRelay = async (
readProvider: Provider,
auctionContracts: AuctionContracts,
rngContext: RngDrawAuctionContext,
): Promise<RelayDrawAuctionContext> => {
// Prize Pool Info
const prizePoolOpenDrawEndsAt = await auctionContracts.prizePoolContract.openDrawEndsAt();
const rewardTokenAddress = await auctionContracts.prizePoolContract.prizeToken();
const rewardTokenContract = new ethers.Contract(rewardTokenAddress, ERC20Abi, readProvider);
@chuckbergeron
chuckbergeron / pt-v5-draw-2-get-state-pt-2.ts
Last active September 19, 2023 22:02
PoolTogether v5 Hyperstructure - Draw Auction Bot, Gather Contract State pt. 2
export const getRng = async (
rngReadProvider: Provider,
auctionContracts: AuctionContracts,
reserve: BigNumber,
): Promise<RngDrawAuctionContext> => {
// RNG Auction Service Info
const rngService = await auctionContracts.rngAuctionContract.getNextRngService();
const rngServiceContract = new ethers.Contract(rngService, VrfRngAbi, rngReadProvider);
const rngServiceRequestFee = await rngServiceContract.getRequestFee();
@chuckbergeron
chuckbergeron / pt-v5-draw-2-get-state-pt-1.ts
Last active September 14, 2023 19:55
PoolTogether v5 Hyperstructure - Draw Auction Bot, Gather Contract State pt. 1
import { BigNumber, ethers } from 'ethers';
import { formatUnits } from '@ethersproject/units';
import { Provider } from '@ethersproject/providers';
import {
AuctionContracts,
DrawAuctionContext,
DrawAuctionRelayerContext,
RelayDrawAuctionContext,
RngDrawAuctionContext,
TokenWithRate,
@chuckbergeron
chuckbergeron / pt-v5-draw-1-main-and-setup.ts
Last active September 14, 2023 23:32
PoolTogether v5 Hyperstructure - Draw Auction Bot, main and setup
import { ethers, Contract } from 'ethers';
import { Provider } from '@ethersproject/providers';
import {
ContractsBlob,
downloadContractsBlob,
getContract,
} from '@generationsoftware/pt-v5-utils-js';
export interface AuctionContracts {
prizePoolContract: Contract;
@chuckbergeron
chuckbergeron / pt-v5-arb-5-calculate-profit.ts
Last active September 11, 2023 22:22
PoolTogether v5 Hyperstructure - Arbitrage Bot, Calculate Profit
import { ethers, BigNumber } from 'ethers';
const { profitable, selectedIndex } = await calculateProfit(wantedAmountsIn, wantedAmountsOut);
// Calculates the amount of profit the bot will make on this swap
// and if it's profitable or not
const calculateProfit = async (
wantedAmountsIn: BigNumber[],
wantedAmountsOut: BigNumber[],
): Promise<{ profitable: boolean; selectedIndex: number }> => {
@chuckbergeron
chuckbergeron / pt-v5-arb-4-optimal-amount-in.ts
Last active September 11, 2023 22:09
PoolTogether v5 Hyperstructure - Arbitrage Bot, Optimal Amount Ins
import { BigNumber, ethers } from 'ethers';
import { Contract } from 'ethers';
import { Provider } from '@ethersproject/providers';
import { getLiquidationPairComputeExactAmountInMulticall }
from '@generationsoftware/pt-v5-autotasks-library';
const { amountIn, amountInMin, wantedAmountsIn } = await calculateAmountIn(
readProvider,
liquidationPairContract,
originalMaxAmountOut,
@chuckbergeron
chuckbergeron / pt-v5-arb-3-optimal-amount-out.ts
Last active September 11, 2023 22:00
PoolTogether v5 Hyperstructure - Arbitrage Bot, Optimal Amount Outs
const { originalMaxAmountOut, wantedAmountsOut } = await calculateAmountOut(
liquidationPairContract,
context,
);
// Calculates necessary input parameters for the swap call based on current state
// of the contracts
const calculateAmountOut = async (
liquidationPair: Contract,
context: ArbLiquidatorContext,