Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Using flashbot to submit transaction #98

Open
wants to merge 4 commits into
base: azalea
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -46,6 +46,7 @@
},
"homepage": "https://github.com/cennznet/cennznet#readme",
"dependencies": {
"@flashbots/ethers-provider-bundle": "^0.5.0",
"@types/chai": "^4.2.19",
"@types/mocha": "^8.2.2",
"dotenv": "^8.2.0",
Expand Down
34 changes: 26 additions & 8 deletions scripts/subscribeEventProof.js
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ const mongoose = require('mongoose');
const { EventProcessed } = require('../src/mongo/models');
const ethers = require('ethers');
const bridgeAbi = require("../abi/CENNZnetBridge.json").abi;
const { FlashbotsBundleProvider, FlashbotsTransactionResolution} = require("@flashbots/ethers-provider-bundle");

const timeoutMs = 20000;
const BUFFER = 1000;
Expand Down Expand Up @@ -86,16 +87,34 @@ async function getEventPoofAndSubmit(api, eventId, bridge, txExecutor, newValida
logger.info('percentGasPrice:',percentGasPrice.toString());
const increasedGasPrice = gasPrice.add(percentGasPrice);
logger.info('Gas price nw;:', gasPrice.toString());

const gasEstimated = await bridge.estimateGas.setValidators(newValidators, newValidatorSetId, proof, {gasLimit: 5000000, gasPrice: increasedGasPrice});
const eventExists = await bridge.eventIds(eventId.toString()); // check storage again, before sending event proof
const gasEstimated = await bridge.estimateGas.setValidators(newValidators, newValidatorSetId, proof, {gasLimit: 5000000, gasPrice: increasedGasPrice});
if (!eventExists) {
const tx = await bridge.setValidators(newValidators, newValidatorSetId, proof, {
const iface = new ethers.utils.Interface(bridgeAbi);
const data = iface.encodeFunctionData("setValidators", [newValidators, newValidatorSetId, proof]);
const flashbotsProvider = await FlashbotsBundleProvider.create(provider, txExecutor);
const transaction = {
from: txExecutor.address,
to: process.env.BRIDGE_CONTRACT,
data: data,
gasPrice: increasedGasPrice,
gasLimit: gasEstimated.add(BUFFER),
gasPrice: increasedGasPrice
}
const res = await flashbotsProvider.sendPrivateTransaction({
transaction,
txExecutor,
}, {
maxBlockNumber: (await provider.getBlockNumber()) + 15, // only allow tx to be mined for the next 15 blocks
});
await tx.wait(); // wait till tx is mined
logger.info(JSON.stringify(tx));

const waitRes = await res.wait();
if (waitRes === FlashbotsTransactionResolution.TransactionIncluded) {
logger.info("Private transaction successfully mined.");
logger.info(JSON.stringify(waitRes));
} else if (waitRes === FlashbotsTransactionResolution.TransactionDropped) {
logger.info("Private transaction was not mined and has been removed from the system.");
await sendSlackNotification(`Failed to submit transaction via flashbot`);
}
await updateLastEventProcessed(eventId, blockHash.toString());
const balance = await provider.getBalance(txExecutor.address);
logger.info(`IMP Balance is: ${balance}`);
Expand All @@ -113,8 +132,7 @@ async function getEventPoofAndSubmit(api, eventId, bridge, txExecutor, newValida
logger.error(`IMP Error: ${e.stack}`);
// send slack notification when proof submission fails
const message = ` 🚨 Issue while submitting validator set on ethereum bridge
proof: ${JSON.stringify(proof)}
newValidators: ${newValidators}
event proof id: ${eventProof.eventId}
newValidatorSetId: ${newValidatorSetId}
on CENNZnets ${process.env.NETWORK} chain`;
await sendSlackNotification(message);
Expand Down
25 changes: 25 additions & 0 deletions yarn.lock
Original file line number Diff line number Diff line change
Expand Up @@ -1089,6 +1089,14 @@
dependencies:
ajv "^6.12.6"

"@flashbots/ethers-provider-bundle@^0.5.0":
version "0.5.0"
resolved "https://registry.yarnpkg.com/@flashbots/ethers-provider-bundle/-/ethers-provider-bundle-0.5.0.tgz#068dd6a078066c50c36ac81b92d4726636768853"
integrity sha512-w7vc6aWDtgaHkDSgACjda0NoKYjOJ4mkr2av+u0NctvsoeNTg5dji65zeyU+98Fx3s6IbK0mfUTSGHAUtyt21A==
dependencies:
ts-node "^9.1.0"
typescript "^4.1.2"

"@humanwhocodes/config-array@^0.5.0":
version "0.5.0"
resolved "https://registry.yarnpkg.com/@humanwhocodes/config-array/-/config-array-0.5.0.tgz#1407967d4c6eecd7388f83acf1eaf4d0c6e58ef9"
Expand Down Expand Up @@ -9782,6 +9790,18 @@ ts-node@^10.0.0:
source-map-support "^0.5.17"
yn "3.1.1"

ts-node@^9.1.0:
version "9.1.1"
resolved "https://registry.yarnpkg.com/ts-node/-/ts-node-9.1.1.tgz#51a9a450a3e959401bda5f004a72d54b936d376d"
integrity sha512-hPlt7ZACERQGf03M253ytLY3dHbGNGrAq9qIHWUY9XHYl1z7wYngSr3OQ5xmui8o2AaxsONxIzjafLUiWBo1Fg==
dependencies:
arg "^4.1.0"
create-require "^1.1.0"
diff "^4.0.1"
make-error "^1.1.1"
source-map-support "^0.5.17"
yn "3.1.1"

[email protected]:
version "2.0.1"
resolved "https://registry.yarnpkg.com/tslib/-/tslib-2.0.1.tgz#410eb0d113e5b6356490eec749603725b021b43e"
Expand Down Expand Up @@ -9894,6 +9914,11 @@ typedarray@^0.0.6:
resolved "https://registry.yarnpkg.com/typedarray/-/typedarray-0.0.6.tgz#867ac74e3864187b1d3d47d996a78ec5c8830777"
integrity sha1-hnrHTjhkGHsdPUfZlqeOxciDB3c=

typescript@^4.1.2:
version "4.7.4"
resolved "https://registry.yarnpkg.com/typescript/-/typescript-4.7.4.tgz#1a88596d1cf47d59507a1bcdfb5b9dfe4d488235"
integrity sha512-C0WQT0gezHuw6AdY1M2jxUO83Rjf0HP7Sk1DtXj6j1EwkQNZrHAg2XPWlq62oqEhYvONq5pkC2Y9oPljWToLmQ==

typescript@^4.3.4, typescript@^4.3.5:
version "4.3.5"
resolved "https://registry.yarnpkg.com/typescript/-/typescript-4.3.5.tgz#4d1c37cc16e893973c45a06886b7113234f119f4"
Expand Down