0xweb-org / dequanto

Blockchain Client
101 stars 4 forks source link

Add flashbots bundles #2

Open ishfx opened 7 months ago

ishfx commented 7 months ago

Hi,

First of all, kudos for this great web3 lib :tada:

It would be a great addition if this libs could handle flashbots bundle submission.

A good start is the well know flashbots for ethereum network: https://docs.flashbots.net/flashbots-auction/advanced/rpc-endpoint Others can be added for BSC, Polygon, etc after too.

Is this something you consider adding ?

Thanks in advance!

tenbits commented 7 months ago

Hi, thank you; I appreciate your feedback. Adding Flashbot relays has definitely been on my radar for a while. I have a question regarding some stats: Do you mostly submit multiple transactions in a bundle, or just a single transaction?

ishfx commented 7 months ago

Thanks for the reply. I'd say multiple transactions in a bundle.

tenbits commented 7 months ago

Hi @ishfx,

I've been working to integrate Flashbots support. Here are the key updates:

  1. The default configuration now includes relay URLs for Ethereum chains.

  2. TypeScript classes for the contracts have a new wrapper, $signed, for generating signed transactions. For example:

    import { FooContract } from '@0xc/eth/FooContract';
    import { ChainAccountService } from '@dequanto/ChainAccountService';
    
    const sender = await ChainAccountService.get('myaccount');
    const foo = new FooContract();
    const { error, signed } = await foo.$signed().myTxMethod(sender, param1, param2);
    // "signed" is the raw transaction hex for myTxMethod

    This feature facilitates the collection of signed transactions for later submission to the Flashbots service.

  3. Flashbots RPC methods have been implemented in Rpc.ts, derived from flashbots.json.

  4. FlashbotsProvider.ts has been added to submit signed transactions to the Flashbots relays.

You can review some integration tests in flashbots.spec.ts.


Nonce management: per default, when a transaction is being submitted, we calculate automatically the nonce, based on transaction count. In case, where multiple transactions must be first signed, and then submitted to the relay, you must pass the nonce manager to tx signer:

import { TxNonceManager } from '@dequanto/txs/TxNonceManager';

let nonce = TxNonceManager.create(foo.client, sender);
let { signed: tx1 } = await foo.$signed({ nonce }).myTxMethod(sender, param1, param2);
let { signed: tx2 } = await foo.$signed({ nonce }).otherTxMethod(sender);
// ..

In this example the first tx1 gets the current nonce from the blockchain, tx2..txN will get incremented nonce accordingly.


I would appreciate your feedback on this implementation and its potential improvements.

ishfx commented 7 months ago

Hello @tenbits

In terms of implementation, LGTM. I didn't go into detail, but the fundamentals are there.

I tried running the lib locally but quickly realized that it depends heavily on your own framework. I guess I can't test it using standard ts-node ?

tenbits commented 7 months ago

ts-node shouldn't be the problem. Maybe some ts configuration issue. It uses additional npm dependencies, maybe they should be additionally installed, if you install dequanto as git submodule. Could you elaborate more regarding your setup, and what is the error?

ishfx commented 7 months ago

It's a basic setup:

Here is the repo: https://github.com/ishfx/dequanto-test/tree/main

tenbits commented 7 months ago

Thank you very much for the example. It seems that by default, ts-node prioritizes .json files over .ts.

https://github.com/microsoft/TypeScript/issues/47870

https://github.com/ishfx/dequanto-test/tree/main/0xc/eth/chainlink/oracle-eth In this case, there are two files - one ABI json and another a TypeScript class.

However, there is a ts-node flag --preferTsExts that can be used to override this behavior.


I have also noticed that ts-node throws some typecheck errors. I use TypeScript in a sloppy mode but plan to address all the typecheck errors soon. For now, you should include the "--transpileOnly" flag with the ts-node command to avoid these issues.

ishfx commented 6 months ago

Alright, thank you for the debug.

I'll wait for the typescript support because I'm not comfortable with disabling the ts checks. What could be better, also, is to publish it as a lib on npm.