-
Notifications
You must be signed in to change notification settings - Fork 12
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
* Add Blocknative (#1) * Add blocknative tx-preview * Linitng * Merge commits from public repo (#2) * Feature | Misc updates (#3) * Update per mds1 comments - add gasUsed field from simulation and console.log it * Makefile save and Readme update
- Loading branch information
1 parent
782d204
commit 43ca8d5
Showing
6 changed files
with
90 additions
and
0 deletions.
There are no files selected for viewing
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
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
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
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
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,3 @@ | ||
. ./.env | ||
|
||
time yarn ts-node ./scripts/convex.blocknative.ts |
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,78 @@ | ||
import fetchUrl, { FETCH_OPT } from 'micro-ftch' | ||
import { Contract } from '@ethersproject/contracts' | ||
import { JsonRpcProvider } from '@ethersproject/providers' | ||
|
||
// Add your keys via .env | ||
const credentials = process.env.BN_API_KEY + ':' + process.env.BN_SECRET_KEY | ||
const bnEndPoint = 'https://api.blocknative.com/simulate' | ||
|
||
async function main(): Promise<void> { | ||
// Get owner address - gracefully copied homework from the Tenderly benchmark script :^) | ||
const convexAddr = '0xF403C135812408BFbE8713b5A23a04b3D48AAE31' | ||
const provider = new JsonRpcProvider(process.env.ETH_RPC_URL) | ||
const abi = ['function owner() view external returns (address)'] | ||
const convex = new Contract(convexAddr, abi, provider) | ||
|
||
// We use current blocknumber owner as Blocknative simulates on tip block number as of now | ||
const ownerAddr = await convex.owner() | ||
|
||
// Double check the keys were given above! | ||
if (credentials === ':') { | ||
throw new Error('No credentials for simulation endpoint given!') | ||
} | ||
|
||
// Information to how tx-preview works is here: | ||
// https://docs.blocknative.com/simulation-platform/transaction-preview-api | ||
const fetchOptions = <Partial<FETCH_OPT>>{ | ||
method: 'POST', | ||
type: 'json', | ||
full: true, | ||
expectStatusCode: false, | ||
headers: { | ||
'Content-Type': 'application/json', | ||
'credentials': credentials | ||
}, | ||
data: { | ||
system: "ethereum", | ||
network: "main", | ||
transaction: { | ||
to: convexAddr, | ||
from: ownerAddr, | ||
gas: Number(process.env.GAS_LIMIT), | ||
gasPrice: 0, | ||
input: "0x354af919", | ||
value: 0 | ||
} | ||
} | ||
} | ||
|
||
console.time('Simulate-shutdown') | ||
|
||
// Fetch simulation via Blocknative RESTful endpoint | ||
const response = await fetchUrl(bnEndPoint, fetchOptions) | ||
|
||
if (response.status !== 200) { | ||
console.log(`Simulation error code: ${response.status} - ${JSON.stringify(response.body)}`) | ||
process.exit(1) | ||
} | ||
|
||
console.timeEnd('Simulate-shutdown') | ||
|
||
// Print the response with decoded internal transactions, address | ||
// balance changes of ETH and tokens, and any errors in EVm execution | ||
// console.log(JSON.stringify(response.body, null, 2)) | ||
|
||
// Print gasUsed for execution during certain block | ||
console.log(`Simulation generated on blocknumber: ${response.body.simulatedBlockNumber}`) | ||
console.log(`Gas used: ${response.body.gasUsed}`) | ||
|
||
// Blocknative's E2E latency | ||
console.log(`Blocknative's end to end latency: ${response.body.simDetails.e2eMs}ms`) | ||
} | ||
|
||
main() | ||
.then(() => process.exit(0)) | ||
.catch((error: Error) => { | ||
console.error(error) | ||
process.exit(1) | ||
}); |