```
import { FlashbotsBundleProvider } from '@flashbots/ethers-provider-bundle'
import { Contract, providers, utils, Wallet } from 'ethers'
async function main() {
// Standard json rpc provider directly from ethers.js (NOT Flashbots)
// create the base provider
let base = new providers.JsonRpcProvider({ url: '
https://polygon-rpc.com/' }, 137)
await base.ready
// badPK 被盗 private key
const bad = new Wallet(process.env.badPK, base)
// sponsorPK 转 gas 费的钱包的 private key
const sponsor = new Wallet(process.env.sponsorPK, base)
// wrap it with the marlin relay provider
let provider = new FlashbotsBundleProvider(base, bad, { url: '
http://bor.txrelay.marlin.org/' }, 137)
const newOwner = ''
const CONTRACT_ADDRESS = '0x9ece35dde502e0c966204967702bca1e860d9b43'
const ABI = [
{
inputs: [
{
internalType: 'address',
name: 'newOwner',
type: 'address',
},
],
name: 'transferOwnership',
outputs: [],
stateMutability: 'nonpayable',
type: 'function',
},
]
const contract = new Contract(CONTRACT_ADDRESS, ABI, bad)
const txs = [
{
transaction: {
to: bad.address,
gasPrice: '31000000000',
value: utils.parseEther('0.1'),
},
signer: sponsor
},
{
signer: bad,
transaction: await contract.populateTransaction.transferOwnership(newOwner),
}
]
const blk = await base.getBlockNumber()
// send bundle to marlin relay
const result = await provider.sendBundle(txs, blk + 1)
console.log(result)
}
main().catch(console.error)
```
自己试试吧