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

asset-swapper: RFQ-T firm quotes #2541

Merged
merged 36 commits into from
Apr 15, 2020
Merged

asset-swapper: RFQ-T firm quotes #2541

merged 36 commits into from
Apr 15, 2020

Conversation

feuGeneA
Copy link
Contributor

@feuGeneA feuGeneA commented Apr 7, 2020

Description

These changes support 0xProject/0x-api#162

Every commit is crafted to be completely independent and atomic. I suggest you review them one at a time, in order to give better context, rather than just reviewing all of the changes at once.

Testing instructions

While there aren't any tests here yet, this code is currently being exercised by a 0x-api test, which you can see in the RFQ-T commit at 0xProject/0x-api#162 , and which is using the asset-swapper, and contract-addresses packages from this branch.

The deployment of ERC20BridgeSampler was necessary not just for the RFQ-T test but also for the vanilla /swap/v0/quote endpoint test as well. Before that deployment, the market_operation_utils code was reverting when calling into the sampler, resulting in a HTTP 400 response from the 0x API.

Types of changes

  • Bug fix (non-breaking change which fixes an issue)

  • New feature (non-breaking change which adds functionality)

Checklist:

@feuGeneA feuGeneA self-assigned this Apr 7, 2020
@feuGeneA feuGeneA changed the title asset-swapper: RFQ-T asset-swapper: RFQ-T firm quotes Apr 7, 2020

const ordersWithStringInts = responses.map(response => response.data); // not yet BigNumber

const orders: SignedOrder[] = ordersWithStringInts.map(orderWithStringInts => {
Copy link
Contributor Author

Choose a reason for hiding this comment

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

Find a helper function do this for us. @steveklebanoff ?

Copy link
Member

Choose a reason for hiding this comment

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

I have a feeliing they exist in @0x/connect

Copy link
Contributor

Choose a reason for hiding this comment

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

@dekz Are you open to exporting typeConverters or orderParsingUtils from the @0x/orderbook package?

https://github.com/0xProject/0x-monorepo/blob/master/packages/connect/src/utils/type_converters.ts#L24

Or is there somewhere else where this is already defined & exported?

@feuGeneA feuGeneA mentioned this pull request Apr 8, 2020
2 tasks
@feuGeneA feuGeneA requested review from dekz and dorothy-zbornak April 8, 2020 22:34
packages/asset-swapper/src/swap_quoter.ts Outdated Show resolved Hide resolved
packages/asset-swapper/src/swap_quoter.ts Outdated Show resolved Hide resolved
packages/asset-swapper/src/utils/quote_requestor.ts Outdated Show resolved Hide resolved
packages/asset-swapper/src/utils/quote_requestor.ts Outdated Show resolved Hide resolved
packages/asset-swapper/src/utils/quote_requestor.ts Outdated Show resolved Hide resolved
packages/asset-swapper/src/swap_quoter.ts Outdated Show resolved Hide resolved
packages/asset-swapper/src/swap_quoter.ts Outdated Show resolved Hide resolved
packages/asset-swapper/src/utils/quote_requestor.ts Outdated Show resolved Hide resolved
packages/asset-swapper/src/utils/quote_requestor.ts Outdated Show resolved Hide resolved
feuGeneA added a commit that referenced this pull request Apr 9, 2020
feuGeneA added a commit that referenced this pull request Apr 9, 2020
@coveralls
Copy link

coveralls commented Apr 9, 2020

Coverage Status

Coverage increased (+0.1%) to 79.613% when pulling 1da8f68 on rfq-t into 0e196a5 on development.

packages/asset-swapper/src/constants.ts Outdated Show resolved Hide resolved
@@ -161,10 +165,13 @@ export class SwapQuoter {
this.orderbook = orderbook;
this.expiryBufferMs = expiryBufferMs;
this.permittedOrderFeeTypes = permittedOrderFeeTypes;
this.rfqtTakerApiKeyWhitelist = options.rfqtTakerApiKeyWhitelist || [];
this.rfqtMakerEndpoints = options.rfqtMakerEndpoints || [];
Copy link
Member

Choose a reason for hiding this comment

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

rfqtMakerEndpoints seems to only be used to construct a QuoteRequestor, does it need to be set as a public accessor?

Copy link
Member

Choose a reason for hiding this comment

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

Are we asserting they are uris?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Addressed the rfqtMakerEndpoints bit in 3c795d3.

We are not directly asserting that they are URI's. We're just passing them through to the quote requestor, and letting it fail silently if they're not valid.

packages/asset-swapper/src/swap_quoter.ts Outdated Show resolved Hide resolved
];
if (options.intentOnFilling && options.apiKey) {
if (!this.rfqtTakerApiKeyWhitelist.includes(options.apiKey)) {
throw new Error('API key not permissioned for RFQ-T');
Copy link
Member

Choose a reason for hiding this comment

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

Is this a throwable validation at this layer? Or should it fail silently and give a normal quote?

If someone gets delisted they're going to start getting Errors rather than quotes, seems quite disruptive.

Copy link
Contributor

Choose a reason for hiding this comment

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

Yeah, agree that we should fallback to providing a regular quote in this case.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Addressed in 5f4778c

packages/asset-swapper/src/swap_quoter.ts Outdated Show resolved Hide resolved
packages/asset-swapper/src/utils/quote_requestor.ts Outdated Show resolved Hide resolved
packages/asset-swapper/src/utils/quote_requestor.ts Outdated Show resolved Hide resolved
packages/asset-swapper/src/utils/quote_requestor.ts Outdated Show resolved Hide resolved

const ordersWithStringInts = responses.map(response => response.data); // not yet BigNumber

const orders: SignedOrder[] = ordersWithStringInts.map(orderWithStringInts => {
Copy link
Member

Choose a reason for hiding this comment

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

I have a feeliing they exist in @0x/connect

packages/asset-swapper/src/types.ts Outdated Show resolved Hide resolved
feuGeneA added a commit that referenced this pull request Apr 9, 2020
feuGeneA added a commit that referenced this pull request Apr 11, 2020
feuGeneA added a commit that referenced this pull request Apr 11, 2020
Steve Klebanoff and others added 3 commits April 15, 2020 00:11
* test for returning a 200 with invalid data, and additonal logging for that case

* Ensure RFQ-T response has asset data we expect

* validate signed order schema

* give more descriptive variable names and test an unsigned order

* takeout unused var
SwapQuoter._getSwapQuoteAsync() was merging defaults into the options
sent into SwapQuoteCalculator.calculateMarket{Buy,Sell}SwapQuoteAsync(),
but it was using the unmerged options function parameter for the RFQ-T
options and also for the gas price option.
@feuGeneA
Copy link
Contributor Author

@dekz

If possible I'd try and take one of the pre-existing Asset-swapper tests and test that nothing goes awry when the RFQT system is misconfigured. I.e test the user gets a swap when no MM quote endpoint can be reached.

Means we don't have to set up a golden path environment, but still get to exercise a large chunk of the logic around this.

We were able to get solid coverage of the new QuoteRequestor class (thanks @steveklebanoff!). What we did not cover in this repo is any of the changes to SwapQuoter, which do have some complex conditional logic, but are otherwise relatively trivial. However, we have integration tests in the 0x API repo that do exercise these SwapQuoter changes.

I feel this PR is ready for a final review. If you guys think any follow-up work is warranted, please be specific, and I'll be sure to track it.

@feuGeneA feuGeneA requested a review from steveklebanoff April 15, 2020 07:04
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.

LGTM, just some optional nits.

@@ -53,6 +54,8 @@
"@0x/orderbook": "^2.2.5",
"@0x/utils": "^5.4.1",
"@0x/web3-wrapper": "^7.0.7",
"axios": "^0.19.2",
"axios-mock-adapter": "^1.18.1",
Copy link
Member

Choose a reason for hiding this comment

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

devDep?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

No because clients that use the exported mock need these deps.

@@ -0,0 +1,37 @@
import axios from 'axios';
Copy link
Member

Choose a reason for hiding this comment

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

should this be under test/utils?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

No because it's exported for external use. Specifically, we're using it from within the 0x API tests.

@feuGeneA feuGeneA marked this pull request as ready for review April 15, 2020 14:37
takerAddress: string,
options?: Partial<RfqtFirmQuoteRequestOpts>,
): Promise<SignedOrder[]> {
const { makerEndpointMaxResponseTimeMs } = _.merge({}, constants.DEFAULT_RFQT_FIRM_QUOTE_REQUEST_OPTS, options);
Copy link
Contributor

Choose a reason for hiding this comment

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

So you like lodash now, huh? 😆 If you're using it here, would prefer to use flatten as well 🤓

I'm ok leaving as it though ❤️

@@ -11,7 +11,7 @@
"build": "tsc -b",
"build:ci": "yarn build",
"clean": "shx rm -rf lib ${npm_package_config_snapshot_name} ${npm_package_config_snapshot_name}-*.zip",
"lint": "tslint --format stylish --project .",
"lint": "tslint --format stylish --project . && prettier --check 'src/**/*.{ts,tsx,json,md}' --config ../../.prettierrc",
Copy link
Contributor

Choose a reason for hiding this comment

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

It seems like you agreed to keep prettier as a separate script in the asset-swapper package, can we do the same here?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Addressed in 1da8f68

@steveklebanoff
Copy link
Contributor

@steveklebanoff
Copy link
Contributor

Let's go 💪 💪 💪 !!!

@feuGeneA feuGeneA merged commit 110e1af into development Apr 15, 2020
@feuGeneA feuGeneA deleted the rfq-t branch April 15, 2020 19:24
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Projects
None yet
Development

Successfully merging this pull request may close these issues.

7 participants