Last active
September 14, 2023 23:32
-
-
Save chuckbergeron/687900c111e4a36f473cceaecc7f6d32 to your computer and use it in GitHub Desktop.
PoolTogether v5 Hyperstructure - Draw Auction Bot, main and setup
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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; | |
chainlinkVRFV2DirectRngAuctionHelperContract: Contract; | |
remoteOwnerContract: Contract; | |
rngAuctionContract: Contract; | |
rngRelayAuctionContract: Contract; | |
rngAuctionRelayerRemoteOwnerContract: Contract; | |
} | |
const CONTRACTS_VERSION = { | |
major: 1, | |
minor: 0, | |
patch: 0, | |
}; | |
const ERC_5164_MESSAGE_DISPATCHER_ADDRESS = { | |
1: '0xa8f85bab964d7e6be938b54bf4b29a247a88cd9d', // mainnet -> optimism | |
}; | |
const RNG_CHAIN_ID = 1; // eth mainnet | |
const RELAY_CHAIN_ID = 10; // optimism | |
const RNG_JSON_RPC_URI = ''; // your Alchemy, Infura, etc. RPC URI for target RNG chain | |
const RELAY_JSON_RPC_URI = ''; // your Alchemy, Infura, etc. RPC URI for target RELAY chain | |
const REWARD_RECIPIENT = '' // address the rewards should be sent to | |
const main = async () => { | |
const helpers = await getHelpers(); | |
const context = await getDrawAuctionContext(helpers); | |
if (!context.state) { | |
console.warn(`Currently no Rng or RngRelay auctions to complete. Exiting ...`); | |
return; | |
} | |
if (context.state === DrawAuctionState.RngStartVrfHelper) { | |
await checkBalance(context); | |
await increaseRngFeeAllowance(signer, context, helpers.auctionContracts); | |
} | |
// Get estimate for gas cost ... | |
const profitable = await calculateProfit(gasCostUsd, context); | |
if (profitable) { | |
// We can simply pass the rngRelayer and rngReadProvider here since | |
// both transactions we create will be on the L1 | |
await sendTransaction(rngRelayer, auctionContracts, context); | |
} | |
}; | |
main(); | |
const getHelpers = async () => { | |
const rngReadProvider = new ethers.providers.JsonRpcProvider(RNG_JSON_RPC_URI, RNG_CHAIN_ID); | |
const relayReadProvider = new ethers.providers.JsonRpcProvider( | |
RELAY_JSON_RPC_URI, | |
RELAY_CHAIN_ID, | |
); | |
const rngContracts = await downloadContractsBlob(RNG_CHAIN_ID); | |
const relayContracts = await downloadContractsBlob(RELAY_CHAIN_ID); | |
const auctionContracts = getAuctionContracts( | |
rngReadProvider, | |
relayReadProvider, | |
rngContracts, | |
relayContracts, | |
); | |
return { auctionContracts, rngReadProvider, relayReadProvider }; | |
}; | |
const getAuctionContracts = ( | |
rngReadProvider: Provider, | |
relayReadProvider: Provider, | |
rngContractsBlob: ContractsBlob, | |
relayContractsBlob: ContractsBlob, | |
): AuctionContracts => { | |
const rngContracts = getRngContracts(rngReadProvider, rngContractsBlob); | |
const relayContracts = getRelayContracts(relayReadProvider, relayContractsBlob); | |
return { | |
...rngContracts, | |
...relayContracts, | |
}; | |
}; | |
const getRngContracts = (provider: Provider, contracts: ContractsBlob) => { | |
const chainId = RNG_CHAIN_ID; | |
const version = CONTRACTS_VERSION; | |
const rngAuctionContract = getContract('RngAuction', chainId, provider, contracts, version); | |
const chainlinkVRFV2DirectRngAuctionHelperContract = getContract( | |
'ChainlinkVRFV2DirectRngAuctionHelper', | |
chainId, | |
provider, | |
contracts, | |
version, | |
); | |
const rngAuctionRelayerRemoteOwnerContract = getContract( | |
'RngAuctionRelayerRemoteOwner', | |
chainId, | |
provider, | |
contracts, | |
version, | |
); | |
return { | |
chainlinkVRFV2DirectRngAuctionHelperContract, | |
rngAuctionContract, | |
rngAuctionRelayerRemoteOwnerContract, | |
}; | |
}; | |
const getRelayContracts = (provider: Provider, contracts: ContractsBlob) => { | |
const chainId = RELAY_CHAIN_ID; | |
const version = CONTRACTS_VERSION; | |
const prizePoolContract = getContract('PrizePool', chainId, provider, contracts, version); | |
const rngRelayAuctionContract = getContract( | |
'RngRelayAuction', | |
chainId, | |
provider, | |
contracts, | |
version, | |
); | |
const remoteOwnerContract = getContract('RemoteOwner', chainId, provider, contracts, version); | |
return { | |
prizePoolContract, | |
remoteOwnerContract, | |
rngRelayAuctionContract, | |
}; | |
}; |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment