Skip to content
This repository has been archived by the owner on Jul 9, 2021. It is now read-only.

ERC20BridgeSampler: batchCall() #2477

Merged
merged 9 commits into from
Feb 11, 2020
Merged

Conversation

dorothy-zbornak
Copy link
Contributor

@dorothy-zbornak dorothy-zbornak commented Feb 8, 2020

Description

We kept adding wrapper functions upon wrapper functions to ERC20BridgeSampler as our needs evolved while keeping the RPC calls to the bare minimum, which resulted in a bloated mess.

This PR stems tech debt by removing all the wrapper functions, leaving only atomic ones, and introducing a batchCall() function that:

  • Allows you to call 1 or more public functions on the sampler contract in a single transaction.
  • Takes an array of ABI encoded call data for each function call.
  • Returns an array of ABI encoded call results for each function call.
  • Can even call batchCall() again to recurse.

The upshot is this allows us to compose function calls off-chain instead of adding higher order wrapper functions to and redeploying the sampler contract. This is going to be relevant to fees/gas accounting because we will probably need to sample pairs and quantities that aren't involved in a swap to establish a baseline. Otherwise, we'd be adding some stupid sounding function like getEthPriceAndFillableAmountsAndSampleMarketSell() to the contract 😫.

DexOrderSampler

The DexOrderSampler class has been rewritten to be a convenient wrapper for neatly performing an arbitrary number of batch operations. So instead of doing this:

const [fillableAmounts, dexQuotes] = await
    sampler.getFillableAmountsAndSampleMarketSellAsync(
        orders,
        sampleAmounts,
        sources,
    );

We now do this:

const [fillableAmounts, dexQuotes] = await sampler.executeAsync(
    DexOrderSampler.ops.getOrderFillableTakerAmounts(orders),
    DexOrderSampler.ops.getSellQuotes(
        sources,
        makerToken,
        takerToken,
        sampleAmounts,
    ),
);

Which is much clearer and scalable, imo.

Testing instructions

Types of changes

Checklist:

  • Prefix PR title with [WIP] if necessary.
  • Add tests to cover changes as needed.
  • Update documentation as needed.
  • Add new entries to the relevant CHANGELOG.jsons.

@coveralls
Copy link

coveralls commented Feb 8, 2020

Coverage Status

Coverage decreased (-0.2%) to 79.437% when pulling a36ff9e on feat/batched-sampler into 1b83ebd on development.

@dorothy-zbornak dorothy-zbornak marked this pull request as ready for review February 8, 2020 01:47
@dorothy-zbornak dorothy-zbornak force-pushed the feat/batched-sampler branch 3 times, most recently from fe109b1 to 31ea30f Compare February 10, 2020 04:50
Copy link
Member

@dekz dekz left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Looks good, happy to get away from using the addresses as id's. Seems clear the changes for PLP and other sources. Much easier to add a */WETH sample than having to perform 2 calls simultaneously.

// prettier-ignore
public async executeAsync<
T1, T2, T3, T4, T5, T6, T7, T8
>(...ops: [T1, T2, T3, T4, T5, T6, T7, T8]): Promise<[
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

😢

Copy link
Contributor

@moodlezoup moodlezoup left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

okie dokie to me 👍

nativeOrders,
DexOrderSampler.getSampleAmounts(takerAmount, _opts.numSamples, _opts.sampleDistributionBase),
difference(SELL_SOURCES, _opts.excludedSources),
const [makerToken, takerToken] = getOrderTokens(nativeOrders[0]);
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

nit: you've got const [outputToken, inputToken] = getOrderTokens(nativeOrders[0]); later in this function

function createOrder(overrides?: Partial<SignedOrder>): SignedOrder {
return {
chainId: CHAIN_ID,
exchangeAddress: hexUtils.random(20),
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

why no randomAddress ?

});

it('getKyberSellQuotes()', async () => {
const expectedTakerToken = hexUtils.random(20);
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

ditto

@dorothy-zbornak dorothy-zbornak merged commit dfd9443 into development Feb 11, 2020
@dorothy-zbornak dorothy-zbornak deleted the feat/batched-sampler branch February 11, 2020 21:32
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

5 participants