Skip to content

Instantly share code, notes, and snippets.

@0fuz
Created November 26, 2022 07:43
Show Gist options
  • Save 0fuz/53ad7c45491ad51f12ae030ea3a15073 to your computer and use it in GitHub Desktop.
Save 0fuz/53ad7c45491ad51f12ae030ea3a15073 to your computer and use it in GitHub Desktop.
An example of sending bundle to polygon chain
import {ethers, providers, Wallet} from "ethers";
import {FlashbotsBundleProvider, FlashbotsBundleResolution} from "@flashbots/ethers-provider-bundle";
import {parseUnits} from "ethers/lib/utils";
export async function timeout(ms: number) {
return new Promise(res => setTimeout(res, ms))
}
const polygonRpc = 'https://rpc.ankr.com/polygon'
const privateKey = '0x123123...'
async function main() {
let base = new ethers.providers.JsonRpcProvider({url: polygonRpc}, 137)
await base.ready
const user = new ethers.Wallet(privateKey, base)
let provider = new FlashbotsBundleProvider(base, user, {url: 'http://bor.txrelay.marlin.org/'}, 137)
// this contract used to send miner fees directly
const CONTRACT_ADDRESS = "0x0b787209ecb980e444babce86e980162288fafc3"
const ABI = [{"inputs":[],"name":"bribe","outputs":[],"stateMutability":"payable","type":"function"}]
const contract = new ethers.Contract(CONTRACT_ADDRESS, ABI, user)
const txes = [
{
signer: user,
transaction: await contract.populateTransaction.bribe({
value: ethers.utils.parseEther("0.5"),
gasPrice: parseUnits("15500", 9),
})
},
// write own txes here
];
let blockOffset = 2 // best offset to get more stable bundle accepted
while (true) {
try {
let blk = await base.getBlockNumber()
console.log('blockNumber', blk, 'offset', blockOffset);
let targetBlock = blk + blockOffset
const signedTransactions = await provider.signBundle(txes)
const bundleSubmission = await provider.sendRawBundle(signedTransactions, targetBlock)
console.log('bundle submitted, waiting')
if ('error' in bundleSubmission) {
throw new Error(bundleSubmission.error.message)
}
console.log(bundleSubmission)
const waitResponse = await bundleSubmission.wait()
console.log(`Wait Response: ${FlashbotsBundleResolution[waitResponse]}`)
if (waitResponse === FlashbotsBundleResolution.BundleIncluded || waitResponse === FlashbotsBundleResolution.AccountNonceTooHigh) {
console.log('Bundle completed')
// bundle mined, exit
process.exit(0)
} else {
console.log({
userStats: await provider.getUserStats(),
receipts: await bundleSubmission.receipts(),
})
}
} catch (e:any) {
console.error('error', e.message);
}
await timeout(500)
}
}
main().catch(console.error)
Prepare:
1. Prepare polygon_flashbots.ts
2. Ensure that mev-bor validator is alive https://polygonscan.com/address/0x88c5e96c1459d224383dcb1fe0cedd1fcee25ffb#mine
3. Send miner fees using this contract https://polygonscan.com/address/0x0b787209ecb980e444babce86e980162288fafc3 or by own
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment