Compare commits

..

67 Commits

Author SHA1 Message Date
Lawrence Forman
f732d3eb9b remove unused functions 2021-11-30 22:49:01 -05:00
Lawrence Forman
5c57efe8a8 get rfq working 2021-11-30 22:47:08 -05:00
Lawrence Forman
da240653f4 get multiplex working 2021-11-30 00:06:47 -05:00
Lawrence Forman
22ec626870 get vips working 2021-11-29 16:57:58 -05:00
Lawrence Forman
2f60eb1c79 multihop with nice breakdowns 2021-11-23 17:10:26 -05:00
Lawrence Forman
313420473a getting multihop back up + refactors for treating all quotes as n-hop 2021-11-19 00:20:37 -05:00
Lawrence Forman
89a9424ae1 @0x/asset-swapper: Handle per-fill gas cost correctly in more places 2021-11-08 15:12:53 -05:00
Lawrence Forman
eabca7a2ee cleanup 2021-11-06 23:27:08 -04:00
Lawrence Forman
a5912c293e @0x/asset-swapper: use gasCost from sampler service 2021-11-06 00:23:15 -04:00
Lawrence Forman
83cae575fa @0x/asset-swapper: hack together basic sampler service integration 2021-11-03 15:18:31 -04:00
Lawrence Forman
fce3664258 @0x/contracts-zero-ex: Register transformERC20() and remove transformERC20Staging() (#355)
Co-authored-by: Lawrence Forman <me@merklejerk.com>
2021-10-25 20:08:58 -04:00
Github Actions
aa522fe49b Publish
- @0x/contracts-erc20@3.3.21
 - @0x/contracts-test-utils@5.4.12
 - @0x/contracts-treasury@1.4.4
 - @0x/contracts-utils@4.8.2
 - @0x/contracts-zero-ex@0.29.2
 - @0x/asset-swapper@16.30.0
 - @0x/contract-addresses@6.8.0
 - @0x/contract-wrappers@13.18.1
 - @0x/migrations@8.1.9
 - @0x/protocol-utils@1.9.3
2021-10-19 18:27:29 +00:00
Github Actions
c03653ebd7 Updated CHANGELOGS & MD docs 2021-10-19 18:27:25 +00:00
Shawn
ae08f77381 Feat/ftm (#347)
* ftm deployment

* add Fantom Curve

* add support for ftm

* add more Fantom liquidity sources

* clean up codes

* lint

* prettier codes

* modify CHANGLOG

* undo some changes

* use lowercase addresses

* Update EP FlashWallet

* Update addresses and remove timestamps

Co-authored-by: Jacob Evans <jacob@dekz.net>

* Cleanup

* cleanup json

Co-authored-by: Romain Butteaud <romain.butteaud@gmail.com>
Co-authored-by: Jacob Evans <jacob@dekz.net>
2021-10-19 09:51:07 -07:00
Github Actions
73a07e512d Publish
- @0x/asset-swapper@16.29.3
2021-10-18 10:36:39 +00:00
Github Actions
7cff09f40a Updated CHANGELOGS & MD docs 2021-10-18 10:36:36 +00:00
Kim Persson
a6d690f10a chore: update to new router version and address breaking changes (#344)
* chore: update to new router version and address breaking changes

* chore: add changelog entry
2021-10-18 11:15:31 +01:00
Github Actions
d7cff52e75 Publish
- @0x/contracts-treasury@1.4.3
 - @0x/asset-swapper@16.29.2
2021-10-13 17:44:45 +00:00
Github Actions
cf1f29a37d Updated CHANGELOGS & MD docs 2021-10-13 17:44:41 +00:00
mzhu25
9af22110b4 Chore: go back to using transformERC20 in AS (#343)
* go back to using transformERC20

* Update changelog
2021-10-12 22:47:29 -07:00
mzhu25
2c187c7e85 Add Proposal 2 and test (#339) 2021-10-08 13:04:09 -07:00
Phạm Minh Đức
d46756ae2e fix(asset-swapper): Check MAX_IN_RATIO in sampleBuysFromBalancer (#338)
* fix: for swapExactAmountIn

* chore: update change log

* Update packages/asset-swapper/CHANGELOG.json

Co-authored-by: Lawrence Forman <lawrence@0xproject.com>
2021-10-05 16:25:02 -04:00
Github Actions
d9a16ed1f9 Publish
- @0x/contracts-treasury@1.4.2
 - @0x/contracts-zero-ex@0.29.1
 - @0x/asset-swapper@16.29.1
 - @0x/migrations@8.1.8
 - @0x/protocol-utils@1.9.2
2021-10-04 19:01:10 +00:00
Github Actions
57a1120997 Updated CHANGELOGS & MD docs 2021-10-04 19:01:06 +00:00
Romain Butteaud
0945d4cef2 fix: removing Clipper custom integration (to add it later as a real PLP) (#335)
* fix: removing Clipper custom integration (to add it later as a real PLP)

* fix: update CHHANGELOG

* fix: keep Clipper as BridgeProtocols so we dont have to redeploy and comment this is not used

* fix: prettier
2021-10-04 11:39:07 -07:00
Github Actions
8f6f7ad453 Publish
- @0x/asset-swapper@16.29.0
2021-10-04 12:21:46 +00:00
Github Actions
bb4fad37fa Updated CHANGELOGS & MD docs 2021-10-04 12:21:43 +00:00
Kim Persson
d06daf2957 feat: initial integration of new router (#295)
* feat: integrate Rust router with asset-swapper WIP

* fix: produce outputFees in the format the Rust router expects

* fix: correct output fee calc and only use the rust router for sells

* fix: make sure numbers sent to the rust router are integers

* hack: try to debug why rust router output is being overestimated WIP

* refactor: clean up router debugging code

* fix: don't use negative output fees for sells

* feat: try VIP sources in isolation and compare with routing all sources

* fix: adjust for FQT overhead when choosing between VIP, all sources WIP

* fix: pass gasPrice to path_optimizer for EP overhead calculations

* feat: buy support with the Rust Router WIP

* chore: WIP commit trying to get buys working

* refactor: use samples instead of fills for the Rust router

* feat: add vip handling hack to sample based routing

* fix: revert to 200 samplings for rust router when using pure samples

* refactor: remove old hacky Path based Rust code, add back feature toggle

* fix: scale both fill output and adjustedOutput my same factor as input

* feat: initial plumbing for supporting RFQ/Limit orders

* fix: incorrect bump of input amount by one base unit before routing

* fix: add fake samples for rfq/limit orders to fulfill the 3 sample req

* fix pass rfq orders in the correct format to the rust router

* chore: remove debugging logs and clean up code & comments

* fix: use published version of @0x/neon-router

* hack: scale routed amounts to account for precision loss of number/f64

* refactor: clean up code and address initial review comments

* fix: only remove trailing 0 output samples before passing to the router

* refactor: consolidate eth to output token calc into ethToOutputAmount fn

* fix: interpolate input between samples on output amount instead of price

* fix: return no path when we have no samples, add sanity asserts

* refactor: fix interpolation comment wording

* fix: remove double adjusted source route input amount

* chore: update changelog for asset-swapper
2021-10-04 12:09:54 +02:00
Megan
34314960ef Updated docs for zero protocol fees (#337) 2021-10-01 13:04:25 +02:00
Github Actions
832ba737ec Publish
- @0x/contracts-treasury@1.4.1
 - @0x/contracts-zero-ex@0.29.0
 - @0x/asset-swapper@16.28.0
 - @0x/contract-artifacts@3.16.0
 - @0x/contract-wrappers@13.18.0
 - @0x/migrations@8.1.7
 - @0x/protocol-utils@1.9.1
2021-09-29 23:19:10 +00:00
Github Actions
6950fdbebb Updated CHANGELOGS & MD docs 2021-09-29 23:19:06 +00:00
mzhu25
1849b1bb9a Update ExchangeProxySwapQuoteConsumer for MultiplexV2 (#282)
* Update ExchangeProxySwapQuoteConsumer for MultiplexV2

* Move TransformERC20FeatureContract instance into private readonly
2021-09-29 16:02:13 -07:00
Megan
a883139220 feat: remove protocol fees for V4 in AssetSwapper WIP (#333)
* Set AssetSwapper protocol fee multiplier to zero

* Set market_operation_utils protocol fee multiplier to zero

* Updated CHANGELOG.json

* Removed whitespace in CHANGELOG.json

* Remove unnecessary timestamp in packages/asset-swapper/CHANGELOG.json

Co-authored-by: Lawrence Forman <lawrence@0xproject.com>

* Updated  param for quote simulation test

* Updated quote simulation test

* fix failing tests

Co-authored-by: Lawrence Forman <lawrence@0xproject.com>
Co-authored-by: Lawrence Forman <me@merklejerk.com>
2021-09-29 12:37:42 -04:00
Kevin Li
bb2c26cb94 fix(docs): pin Sphinx version to 3.x to generate CSS
Custom CSS seems to [have broken in Sphinx 4](https://github.com/sphinx-doc/sphinx/issues/2442).
Guessing readthedocs.io or their package repository upgraded the default
version of Sphinx to 4 and that caused our builds to start breaking.

They [recommend pinning dependencies](https://docs.readthedocs.io/en/stable/guides/reproducible-builds.html#pinning-dependencies), let's do that here for Sphinx!
2021-09-15 18:18:44 -07:00
Github Actions
ba09a0b2bf Publish
- @0x/contracts-erc20@3.3.20
 - @0x/contracts-test-utils@5.4.11
 - @0x/contracts-treasury@1.4.0
 - @0x/contracts-utils@4.8.1
 - @0x/contracts-zero-ex@0.28.5
 - @0x/asset-swapper@16.27.4
 - @0x/contract-artifacts@3.15.1
 - @0x/contract-wrappers@13.17.7
 - @0x/migrations@8.1.6
 - @0x/protocol-utils@1.9.0
2021-09-15 12:58:17 +00:00
Github Actions
b2d54f0238 Updated CHANGELOGS & MD docs 2021-09-15 12:58:13 +00:00
Daniel Pyrathon
4a3096495b Mocked RFQ (#326) 2021-09-15 08:39:10 -04:00
Lawrence Forman
23f6e9e53c fix mixed @0x deps (#328)
Co-authored-by: Lawrence Forman <me@merklejerk.com>
2021-09-15 00:01:15 -04:00
Cece Z
d7dbc0576d Support cast vote by signature in ZrxTreasury (#297)
* Support cast vote by signature in ZrxTreasury

* Address comments and fix existing tests

* test that doesnt work

* test file format

* updates

* address some of the comments

* Remove unused const

* get rid of vote_factory

* unit test for castVoteBySignature p1

* unit test for castVoteBySignature p2

* Add version to domain, and one more test

* unit test for castVoteBySignature p3

* unit test for castVoteBySignature p4

* bump utils version

* remove debug code

* address some comments

* address more pr comments

* move Vote class to protocol-utils

* Address pr comments and update changelogs
2021-09-14 16:12:51 -04:00
Github Actions
15fb00e958 Publish
- @0x/asset-swapper@16.27.3
2021-09-14 19:04:09 +00:00
Github Actions
1d9295cc94 Updated CHANGELOGS & MD docs 2021-09-14 19:04:05 +00:00
Daniel Pyrathon
79f36cf6fb fix: Remove API key whitelist field (#323)
* Refactor integrator ID and add Prometheus metrics

* Update packages/asset-swapper/src/swap_quoter.ts

Co-authored-by: David Walsh <5778036+rhinodavid@users.noreply.github.com>

* Update packages/asset-swapper/src/swap_quoter.ts

Co-authored-by: David Walsh <5778036+rhinodavid@users.noreply.github.com>

* Update packages/asset-swapper/src/swap_quoter.ts

Co-authored-by: David Walsh <5778036+rhinodavid@users.noreply.github.com>

* Added documentation and fixed some minor requests

* Added more metrics

* more docs

* lint fix

* added new Integrator ID addition

* refactor tests

* Refactor new types

Co-authored-by: David Walsh <5778036+rhinodavid@users.noreply.github.com>
2021-09-14 14:43:02 -04:00
Github Actions
6ce4458a5d Publish
- @0x/asset-swapper@16.27.2
2021-09-14 17:13:46 +00:00
Github Actions
fad6e65c07 Updated CHANGELOGS & MD docs 2021-09-14 17:13:42 +00:00
Daniel Pyrathon
840c85373e fix: Refactor integrator ID and add Prometheus metrics (#322)
* Refactor integrator ID and add Prometheus metrics

* Update packages/asset-swapper/src/swap_quoter.ts

Co-authored-by: David Walsh <5778036+rhinodavid@users.noreply.github.com>

* Update packages/asset-swapper/src/swap_quoter.ts

Co-authored-by: David Walsh <5778036+rhinodavid@users.noreply.github.com>

* Update packages/asset-swapper/src/swap_quoter.ts

Co-authored-by: David Walsh <5778036+rhinodavid@users.noreply.github.com>

* Added documentation and fixed some minor requests

* Added more metrics

* more docs

* lint fix

* added new Integrator ID addition

* refactor tests

Co-authored-by: David Walsh <5778036+rhinodavid@users.noreply.github.com>
2021-09-14 12:45:41 -04:00
Github Actions
0479bb5fe1 Publish
- @0x/contracts-erc20@3.3.19
 - @0x/contracts-treasury@1.3.5
 - @0x/contracts-utils@4.8.0
 - @0x/contracts-zero-ex@0.28.4
 - @0x/asset-swapper@16.27.1
 - @0x/migrations@8.1.5
2021-09-08 17:06:09 +00:00
Github Actions
9ecc31ed54 Updated CHANGELOGS & MD docs 2021-09-08 17:06:06 +00:00
Lawrence Forman
eb29abd36c Fix ApproximateBuys sampler to terminate if the buy amount is not met (#319)
Co-authored-by: Lawrence Forman <me@merklejerk.com>
2021-09-08 01:17:42 -04:00
Lawrence Forman
d202e01522 update package mainfests (#318)
Co-authored-by: Lawrence Forman <me@merklejerk.com>
2021-09-07 23:09:30 -04:00
Daniel Pyrathon
e838a6801b fix: Exclusive API keys for select integrations (#317)
* Initial commit of changes

* Added unit tests for filtering process

* linting

* Update packages/asset-swapper/src/utils/quote_requestor.ts

Co-authored-by: phil-ociraptor <philipliao@gmail.com>

* lint and refactor based on feedback

Co-authored-by: phil-ociraptor <philipliao@gmail.com>
2021-09-07 14:26:40 -04:00
Noah Khamliche
7439871aa0 Final Rebase 2021-09-01 17:07:16 -04:00
Noah Khamliche
317f2138c5 feat: fixed json 2021-09-01 17:07:16 -04:00
Noah Khamliche
e5834f1901 m: Fixed json formatting 2021-09-01 17:07:16 -04:00
Noah Khamliche
5063446f93 feat: Added FundRecoveryFeature to CHANGELOG.json 2021-09-01 17:07:16 -04:00
Noah Khamliche
0caf495a1a fixed prettier error with tests 2021-09-01 17:07:16 -04:00
Noah Khamliche
a20de0fc69 Added support for TestMintableERC20TokenContract instead of DummyERC20 2021-09-01 17:07:16 -04:00
Noah Khamliche
9aa0065d2d fixed package.json rebase bug 2021-09-01 17:07:16 -04:00
Noah Khamliche
c24855e627 fixed rebase issues 2021-09-01 17:07:16 -04:00
Noah Khamliche
6bb72dd775 ran yarn prettier to fix issues 2021-09-01 17:07:16 -04:00
Noah Khamliche
77d1ed257c removed unsued bal variable, and removed .only modifier on blockchain tests 2021-09-01 17:07:16 -04:00
Noah Khamliche
5d265360c4 fixed linting and EP transfering less than the total amount of ETH in the wallet 2021-09-01 17:07:16 -04:00
Noah Khamliche
c9097f6e8b fixed rebase issues 2021-09-01 17:07:16 -04:00
Noah Khamliche
d3df985a42 fixed michaels comments and finished off writing the test 2021-09-01 17:07:16 -04:00
Noah Khamliche
7267420874 fixed rebase issues 2021-09-01 17:07:16 -04:00
Noah Khamliche
17e81432f1 Fixed PR comments, now onto writing tests 2021-09-01 17:07:16 -04:00
Noah Khamliche
57c767c3b1 fixed package.json 2021-09-01 17:07:16 -04:00
Noah Khamliche
dbb1c88ad9 initial EpFundRecoveryFeature implementation without tests 2021-09-01 17:07:16 -04:00
157 changed files with 6612 additions and 12853 deletions

3
.gitignore vendored
View File

@@ -75,8 +75,9 @@ generated_docs/
TODO.md
# VSCode file
# IDE file
.vscode
.idea
# generated contract artifacts/
contracts/broker/generated-artifacts/

View File

@@ -1,4 +1,31 @@
[
{
"timestamp": 1634668033,
"version": "3.3.21",
"changes": [
{
"note": "Dependencies updated"
}
]
},
{
"timestamp": 1631710679,
"version": "3.3.20",
"changes": [
{
"note": "Dependencies updated"
}
]
},
{
"timestamp": 1631120757,
"version": "3.3.19",
"changes": [
{
"note": "Dependencies updated"
}
]
},
{
"timestamp": 1630459879,
"version": "3.3.18",

View File

@@ -5,6 +5,18 @@ Edit the package's CHANGELOG.json file only.
CHANGELOG
## v3.3.21 - _October 19, 2021_
* Dependencies updated
## v3.3.20 - _September 15, 2021_
* Dependencies updated
## v3.3.19 - _September 8, 2021_
* Dependencies updated
## v3.3.18 - _September 1, 2021_
* Dependencies updated

View File

@@ -1,6 +1,6 @@
{
"name": "@0x/contracts-erc20",
"version": "3.3.18",
"version": "3.3.21",
"engines": {
"node": ">=6.12"
},
@@ -51,18 +51,18 @@
},
"homepage": "https://github.com/0xProject/protocol/tree/main/contracts/tokens",
"devDependencies": {
"@0x/abi-gen": "^5.6.0",
"@0x/contracts-gen": "^2.0.38",
"@0x/contracts-test-utils": "^5.4.10",
"@0x/contracts-utils": "^4.7.18",
"@0x/dev-utils": "^4.2.7",
"@0x/sol-compiler": "^4.7.3",
"@0x/abi-gen": "^5.6.2",
"@0x/contracts-gen": "^2.0.40",
"@0x/contracts-test-utils": "^5.4.12",
"@0x/contracts-utils": "^4.8.2",
"@0x/dev-utils": "^4.2.9",
"@0x/sol-compiler": "^4.7.5",
"@0x/ts-doc-gen": "^0.0.28",
"@0x/tslint-config": "^4.1.4",
"@0x/types": "^3.3.3",
"@0x/typescript-typings": "^5.2.0",
"@0x/utils": "^6.4.3",
"@0x/web3-wrapper": "^7.5.3",
"@0x/types": "^3.3.4",
"@0x/typescript-typings": "^5.2.1",
"@0x/utils": "^6.4.4",
"@0x/web3-wrapper": "^7.6.0",
"@types/lodash": "4.14.104",
"@types/mocha": "^5.2.7",
"@types/node": "12.12.54",
@@ -70,7 +70,7 @@
"chai-as-promised": "^7.1.0",
"chai-bignumber": "^3.0.0",
"dirty-chai": "^2.0.1",
"ethereum-types": "^3.5.0",
"ethereum-types": "^3.6.0",
"lodash": "^4.17.11",
"make-promises-safe": "^1.1.0",
"mocha": "^6.2.0",
@@ -82,7 +82,7 @@
"typescript": "4.2.2"
},
"dependencies": {
"@0x/base-contract": "^6.4.0",
"@0x/base-contract": "^6.4.2",
"ethers": "~4.0.4"
},
"publishConfig": {

View File

@@ -1,4 +1,22 @@
[
{
"timestamp": 1634668033,
"version": "5.4.12",
"changes": [
{
"note": "Dependencies updated"
}
]
},
{
"timestamp": 1631710679,
"version": "5.4.11",
"changes": [
{
"note": "Dependencies updated"
}
]
},
{
"timestamp": 1630459879,
"version": "5.4.10",

View File

@@ -5,6 +5,14 @@ Edit the package's CHANGELOG.json file only.
CHANGELOG
## v5.4.12 - _October 19, 2021_
* Dependencies updated
## v5.4.11 - _September 15, 2021_
* Dependencies updated
## v5.4.10 - _September 1, 2021_
* Dependencies updated

View File

@@ -1,6 +1,6 @@
{
"name": "@0x/contracts-test-utils",
"version": "5.4.10",
"version": "5.4.12",
"engines": {
"node": ">=6.12"
},
@@ -34,7 +34,7 @@
},
"homepage": "https://github.com/0xProject/protocol/tree/main/contracts/test-utils",
"devDependencies": {
"@0x/sol-compiler": "^4.7.3",
"@0x/sol-compiler": "^4.7.5",
"@0x/tslint-config": "^4.1.4",
"npm-run-all": "^4.1.2",
"shx": "^0.2.2",
@@ -42,20 +42,20 @@
"typescript": "4.2.2"
},
"dependencies": {
"@0x/assert": "^3.0.27",
"@0x/base-contract": "^6.4.0",
"@0x/contract-addresses": "^6.7.0",
"@0x/dev-utils": "^4.2.7",
"@0x/json-schemas": "^6.1.3",
"@0x/assert": "^3.0.29",
"@0x/base-contract": "^6.4.2",
"@0x/contract-addresses": "^6.8.0",
"@0x/dev-utils": "^4.2.9",
"@0x/json-schemas": "^6.3.0",
"@0x/order-utils": "^10.4.28",
"@0x/sol-coverage": "^4.0.37",
"@0x/sol-profiler": "^4.1.27",
"@0x/sol-trace": "^3.0.37",
"@0x/subproviders": "^6.5.3",
"@0x/types": "^3.3.3",
"@0x/typescript-typings": "^5.2.0",
"@0x/utils": "^6.4.3",
"@0x/web3-wrapper": "^7.5.3",
"@0x/sol-coverage": "^4.0.39",
"@0x/sol-profiler": "^4.1.29",
"@0x/sol-trace": "^3.0.39",
"@0x/subproviders": "^6.6.0",
"@0x/types": "^3.3.4",
"@0x/typescript-typings": "^5.2.1",
"@0x/utils": "^6.4.4",
"@0x/web3-wrapper": "^7.6.0",
"@types/bn.js": "^4.11.0",
"@types/js-combinatorics": "^0.5.29",
"@types/lodash": "4.14.104",
@@ -67,7 +67,7 @@
"chai-bignumber": "^3.0.0",
"decimal.js": "^10.2.0",
"dirty-chai": "^2.0.1",
"ethereum-types": "^3.5.0",
"ethereum-types": "^3.6.0",
"ethereumjs-util": "^7.0.10",
"ethers": "~4.0.4",
"js-combinatorics": "^0.5.3",

View File

@@ -1,4 +1,58 @@
[
{
"timestamp": 1634668033,
"version": "1.4.4",
"changes": [
{
"note": "Dependencies updated"
}
]
},
{
"timestamp": 1634147078,
"version": "1.4.3",
"changes": [
{
"note": "Dependencies updated"
}
]
},
{
"timestamp": 1633374058,
"version": "1.4.2",
"changes": [
{
"note": "Dependencies updated"
}
]
},
{
"timestamp": 1632957537,
"version": "1.4.1",
"changes": [
{
"note": "Dependencies updated"
}
]
},
{
"version": "1.4.0",
"changes": [
{
"note": "Support cast vote by signature in Treasury"
}
],
"timestamp": 1631710679
},
{
"timestamp": 1631120757,
"version": "1.3.5",
"changes": [
{
"note": "Dependencies updated"
}
]
},
{
"timestamp": 1630459879,
"version": "1.3.4",

View File

@@ -5,6 +5,30 @@ Edit the package's CHANGELOG.json file only.
CHANGELOG
## v1.4.4 - _October 19, 2021_
* Dependencies updated
## v1.4.3 - _October 13, 2021_
* Dependencies updated
## v1.4.2 - _October 4, 2021_
* Dependencies updated
## v1.4.1 - _September 29, 2021_
* Dependencies updated
## v1.4.0 - _September 15, 2021_
* Support cast vote by signature in Treasury
## v1.3.5 - _September 8, 2021_
* Dependencies updated
## v1.3.4 - _September 1, 2021_
* Dependencies updated

View File

@@ -0,0 +1,60 @@
pragma solidity ^0.6.12;
/**
* @title ISablier
* @author Sablier
*/
interface ISablier {
/**
* @notice Emits when a stream is successfully created.
*/
event CreateStream(
uint256 indexed streamId,
address indexed sender,
address indexed recipient,
uint256 deposit,
address tokenAddress,
uint256 startTime,
uint256 stopTime
);
/**
* @notice Emits when the recipient of a stream withdraws a portion or all their pro rata share of the stream.
*/
event WithdrawFromStream(uint256 indexed streamId, address indexed recipient, uint256 amount);
/**
* @notice Emits when a stream is successfully cancelled and tokens are transferred back on a pro rata basis.
*/
event CancelStream(
uint256 indexed streamId,
address indexed sender,
address indexed recipient,
uint256 senderBalance,
uint256 recipientBalance
);
function balanceOf(uint256 streamId, address who) external view returns (uint256 balance);
function getStream(uint256 streamId)
external
view
returns (
address sender,
address recipient,
uint256 deposit,
address token,
uint256 startTime,
uint256 stopTime,
uint256 remainingBalance,
uint256 ratePerSecond
);
function createStream(address recipient, uint256 deposit, address tokenAddress, uint256 startTime, uint256 stopTime)
external
returns (uint256 streamId);
function withdrawFromStream(uint256 streamId, uint256 funds) external returns (bool);
function cancelStream(uint256 streamId) external returns (bool);
}

View File

@@ -136,8 +136,9 @@ interface IZrxTreasury {
returns (uint256 proposalId);
/// @dev Casts a vote for the given proposal. Only callable
/// during the voting period for that proposal. See
/// `getVotingPower` for how voting power is computed.
/// during the voting period for that proposal.
/// One address can only vote once.
/// See `getVotingPower` for how voting power is computed.
/// @param proposalId The ID of the proposal to vote on.
/// @param support Whether to support the proposal or not.
/// @param operatedPoolIds The pools operated by `msg.sender`. The
@@ -150,6 +151,28 @@ interface IZrxTreasury {
)
external;
/// @dev Casts a vote for the given proposal, by signature.
/// Only callable during the voting period for that proposal.
/// One address/voter can only vote once.
/// See `getVotingPower` for how voting power is computed.
/// @param proposalId The ID of the proposal to vote on.
/// @param support Whether to support the proposal or not.
/// @param operatedPoolIds The pools operated by the signer. The
/// ZRX currently delegated to those pools will be accounted
/// for in the voting power.
/// @param v the v field of the signature
/// @param r the r field of the signature
/// @param s the s field of the signature
function castVoteBySignature(
uint256 proposalId,
bool support,
bytes32[] memory operatedPoolIds,
uint8 v,
bytes32 r,
bytes32 s
)
external;
/// @dev Executes a proposal that has passed and is
/// currently executable.
/// @param proposalId The ID of the proposal to execute.

View File

@@ -34,11 +34,25 @@ contract ZrxTreasury is
using LibRichErrorsV06 for bytes;
using LibBytesV06 for bytes;
/// Contract name
string private constant CONTRACT_NAME = "Zrx Treasury";
/// Contract version
string private constant CONTRACT_VERSION = "1.0.0";
/// The EIP-712 typehash for the contract's domain
bytes32 private constant DOMAIN_TYPEHASH = keccak256("EIP712Domain(string name,string version,uint256 chainId,address verifyingContract)");
/// The EIP-712 typehash for the vote struct
bytes32 private constant VOTE_TYPEHASH = keccak256("TreasuryVote(uint256 proposalId,bool support,bytes32[] operatedPoolIds)");
// Immutables
IStaking public immutable override stakingProxy;
DefaultPoolOperator public immutable override defaultPoolOperator;
bytes32 public immutable override defaultPoolId;
uint256 public immutable override votingPeriod;
bytes32 immutable domainSeparator;
uint256 public override proposalThreshold;
uint256 public override quorumThreshold;
@@ -67,6 +81,15 @@ contract ZrxTreasury is
defaultPoolId = params.defaultPoolId;
IStaking.Pool memory defaultPool = stakingProxy_.getStakingPool(params.defaultPoolId);
defaultPoolOperator = DefaultPoolOperator(defaultPool.operator);
domainSeparator = keccak256(
abi.encode(
DOMAIN_TYPEHASH,
keccak256(bytes(CONTRACT_NAME)),
_getChainId(),
keccak256(bytes(CONTRACT_VERSION)),
address(this)
)
);
}
// solhint-disable
@@ -105,7 +128,7 @@ contract ZrxTreasury is
/// be executed if it passes. Must be at least two epochs
/// from the current epoch.
/// @param description A text description for the proposal.
/// @param operatedPoolIds The pools operated by `msg.sender`. The
/// @param operatedPoolIds The pools operated by the signer. The
/// ZRX currently delegated to those pools will be accounted
/// for in the voting power.
/// @return proposalId The ID of the newly created proposal.
@@ -150,8 +173,9 @@ contract ZrxTreasury is
}
/// @dev Casts a vote for the given proposal. Only callable
/// during the voting period for that proposal. See
/// `getVotingPower` for how voting power is computed.
/// during the voting period for that proposal.
/// One address can only vote once.
/// See `getVotingPower` for how voting power is computed.
/// @param proposalId The ID of the proposal to vote on.
/// @param support Whether to support the proposal or not.
/// @param operatedPoolIds The pools operated by `msg.sender`. The
@@ -165,43 +189,39 @@ contract ZrxTreasury is
public
override
{
if (proposalId >= proposalCount()) {
revert("castVote/INVALID_PROPOSAL_ID");
}
if (hasVoted[proposalId][msg.sender]) {
revert("castVote/ALREADY_VOTED");
}
return _castVote(msg.sender, proposalId, support, operatedPoolIds);
}
Proposal memory proposal = proposals[proposalId];
if (
proposal.voteEpoch != stakingProxy.currentEpoch() ||
_hasVoteEnded(proposal.voteEpoch)
) {
revert("castVote/VOTING_IS_CLOSED");
}
uint256 votingPower = getVotingPower(msg.sender, operatedPoolIds);
if (votingPower == 0) {
revert("castVote/NO_VOTING_POWER");
}
if (support) {
proposals[proposalId].votesFor = proposals[proposalId].votesFor
.safeAdd(votingPower);
hasVoted[proposalId][msg.sender] = true;
} else {
proposals[proposalId].votesAgainst = proposals[proposalId].votesAgainst
.safeAdd(votingPower);
hasVoted[proposalId][msg.sender] = true;
}
emit VoteCast(
msg.sender,
operatedPoolIds,
proposalId,
support,
votingPower
/// @dev Casts a vote for the given proposal, by signature.
/// Only callable during the voting period for that proposal.
/// One address/voter can only vote once.
/// See `getVotingPower` for how voting power is computed.
/// @param proposalId The ID of the proposal to vote on.
/// @param support Whether to support the proposal or not.
/// @param operatedPoolIds The pools operated by voter. The
/// ZRX currently delegated to those pools will be accounted
/// for in the voting power.
/// @param v the v field of the signature
/// @param r the r field of the signature
/// @param s the s field of the signature
function castVoteBySignature(
uint256 proposalId,
bool support,
bytes32[] memory operatedPoolIds,
uint8 v,
bytes32 r,
bytes32 s
)
public
override
{
bytes32 structHash = keccak256(
abi.encode(VOTE_TYPEHASH, proposalId, support, keccak256(abi.encodePacked(operatedPoolIds)))
);
bytes32 digest = keccak256(abi.encodePacked("\x19\x01", domainSeparator, structHash));
address signatory = ecrecover(digest, v, r, s);
return _castVote(signatory, proposalId, support, operatedPoolIds);
}
/// @dev Executes a proposal that has passed and is
@@ -373,4 +393,60 @@ contract ZrxTreasury is
.safeAdd(votingPeriod);
return block.timestamp > voteEndTime;
}
/// @dev Casts a vote for the given proposal. Only callable
/// during the voting period for that proposal. See
/// `getVotingPower` for how voting power is computed.
function _castVote(
address voter,
uint256 proposalId,
bool support,
bytes32[] memory operatedPoolIds
)
private
{
if (proposalId >= proposalCount()) {
revert("_castVote/INVALID_PROPOSAL_ID");
}
if (hasVoted[proposalId][voter]) {
revert("_castVote/ALREADY_VOTED");
}
Proposal memory proposal = proposals[proposalId];
if (
proposal.voteEpoch != stakingProxy.currentEpoch() ||
_hasVoteEnded(proposal.voteEpoch)
) {
revert("_castVote/VOTING_IS_CLOSED");
}
uint256 votingPower = getVotingPower(voter, operatedPoolIds);
if (votingPower == 0) {
revert("_castVote/NO_VOTING_POWER");
}
if (support) {
proposals[proposalId].votesFor = proposals[proposalId].votesFor
.safeAdd(votingPower);
} else {
proposals[proposalId].votesAgainst = proposals[proposalId].votesAgainst
.safeAdd(votingPower);
}
hasVoted[proposalId][voter] = true;
emit VoteCast(
voter,
operatedPoolIds,
proposalId,
support,
votingPower
);
}
/// @dev Gets the Ethereum chain id
function _getChainId() private pure returns (uint256) {
uint256 chainId;
assembly { chainId := chainid() }
return chainId;
}
}

View File

@@ -1,6 +1,6 @@
{
"name": "@0x/contracts-treasury",
"version": "1.3.4",
"version": "1.4.4",
"engines": {
"node": ">=6.12"
},
@@ -32,9 +32,9 @@
"publish:private": "yarn build && gitpkg publish"
},
"config": {
"publicInterfaceContracts": "ZrxTreasury,DefaultPoolOperator",
"publicInterfaceContracts": "ZrxTreasury,DefaultPoolOperator,ISablier",
"abis:comment": "This list is auto-generated by contracts-gen. Don't edit manually.",
"abis": "./test/generated-artifacts/@(DefaultPoolOperator|IStaking|IZrxTreasury|ZrxTreasury).json"
"abis": "./test/generated-artifacts/@(DefaultPoolOperator|ISablier|IStaking|IZrxTreasury|ZrxTreasury).json"
},
"repository": {
"type": "git",
@@ -46,14 +46,14 @@
},
"homepage": "https://github.com/0xProject/protocol/tree/main/contracts/treasury",
"devDependencies": {
"@0x/abi-gen": "^5.6.0",
"@0x/contract-addresses": "^6.7.0",
"@0x/abi-gen": "^5.6.2",
"@0x/contract-addresses": "^6.8.0",
"@0x/contracts-asset-proxy": "^3.7.19",
"@0x/contracts-erc20": "^3.3.18",
"@0x/contracts-gen": "^2.0.38",
"@0x/contracts-erc20": "^3.3.21",
"@0x/contracts-gen": "^2.0.40",
"@0x/contracts-staking": "^2.0.45",
"@0x/contracts-test-utils": "^5.4.10",
"@0x/sol-compiler": "^4.7.3",
"@0x/contracts-test-utils": "^5.4.12",
"@0x/sol-compiler": "^4.7.5",
"@0x/ts-doc-gen": "^0.0.28",
"@0x/tslint-config": "^4.1.4",
"@types/isomorphic-fetch": "^0.0.35",
@@ -72,14 +72,14 @@
"typescript": "4.2.2"
},
"dependencies": {
"@0x/base-contract": "^6.4.0",
"@0x/protocol-utils": "^1.8.4",
"@0x/subproviders": "^6.5.3",
"@0x/types": "^3.3.3",
"@0x/typescript-typings": "^5.2.0",
"@0x/utils": "^6.4.3",
"@0x/web3-wrapper": "^7.5.3",
"ethereum-types": "^3.5.0",
"@0x/base-contract": "^6.4.2",
"@0x/protocol-utils": "^1.9.3",
"@0x/subproviders": "^6.6.0",
"@0x/types": "^3.3.4",
"@0x/typescript-typings": "^5.2.1",
"@0x/utils": "^6.4.4",
"@0x/web3-wrapper": "^7.6.0",
"ethereum-types": "^3.6.0",
"ethereumjs-util": "^7.0.10"
},
"publishConfig": {

View File

@@ -6,8 +6,10 @@
import { ContractArtifact } from 'ethereum-types';
import * as DefaultPoolOperator from '../generated-artifacts/DefaultPoolOperator.json';
import * as ISablier from '../generated-artifacts/ISablier.json';
import * as ZrxTreasury from '../generated-artifacts/ZrxTreasury.json';
export const artifacts = {
ZrxTreasury: ZrxTreasury as ContractArtifact,
DefaultPoolOperator: DefaultPoolOperator as ContractArtifact,
ISablier: ISablier as ContractArtifact,
};

View File

@@ -3,6 +3,8 @@ import { ERC20TokenContract } from '@0x/contracts-erc20';
import { Web3ProviderEngine } from '@0x/subproviders';
import { BigNumber } from '@0x/utils';
import { ISablierContract } from './wrappers';
interface ProposedAction {
target: string;
data: string;
@@ -17,8 +19,14 @@ interface Proposal {
const { zrxToken } = getContractAddressesForChainOrThrow(1);
const zrx = new ERC20TokenContract(zrxToken, new Web3ProviderEngine());
const maticToken = '0x7d1afa7b718fb893db30a3abc0cfc608aacfebb0';
const matic = new ERC20TokenContract(maticToken, new Web3ProviderEngine());
const maticToken = new ERC20TokenContract('0x7d1afa7b718fb893db30a3abc0cfc608aacfebb0', new Web3ProviderEngine());
const sablier = new ISablierContract('0xcd18eaa163733da39c232722cbc4e8940b1d8888', new Web3ProviderEngine());
const ONE_YEAR_IN_SECONDS = new BigNumber('31536000');
const PROPOSAL_2_ZRX_AMOUNT = new BigNumber('485392999999999970448000');
const PROPOSAL_2_MATIC_AMOUNT = new BigNumber('378035999999999992944000');
const PROPOSAL_2_STREAM_START_TIME = new BigNumber('1635188400');
const PROPOSAL_2_RECIPIENT = '0x976378445d31d81b15576811450a7b9797206807';
export const proposals: Proposal[] = [
{
@@ -44,8 +52,8 @@ export const proposals: Proposal[] = [
value: new BigNumber(0),
},
{
target: maticToken,
data: matic
target: maticToken.address,
data: maticToken
.transfer('0xab66cc8fd10457ebc9d13b9760c835f0a4cbc487', new BigNumber('420000e18'))
.getABIEncodedTransactionData(),
value: new BigNumber(0),
@@ -54,4 +62,46 @@ export const proposals: Proposal[] = [
description:
'# Z-2 0x/Polygon Grant Budget for 0xEVE\n\n## Summary\n\nWe propose to transfer 10% of the new Treasury allocation from the recently announced 0x/Polygon initiative to the 0x Ecosystem Value Experiment (0xEVE). The purpose is not so much to increase the budget as it is to enable access to the MATIC that was allocated to the Treasury after 0xEVE was established and to expand the original goals to include use cases on the Polygon Network. A snapshot vote was held to gauge community sentiment with 100% in favor https://snapshot.org/#/0xgov.eth/proposal/QmdcZAAcmNgM3R6CVY586G4sSoPwm6T3CS39DCQ4gPDPB4.\n\n## Background\n\nA proposal to establish the 0x Ecosystem Value Experiment (0xEVE) was passed in early June with a budget of 400K ZRX, which was then transferred to the 0xEVE multisig to fund operations. Shortly afterwards, 0xLabs and 0xPolygon allocated 3.3M ZRX and 4.2M MATIC to the 0xDAO Treasury with the shared goal of bringing 1M new users to the Polygon Network via 0x-powered applications.<br/><br/>\nSince that time, 0xEVE has established a grant program and published a framework for projects seeking support from the 0xDAO. However, because 0xEVE has no access to these new funds, it will be extremely difficult given the current market conditions (and the commensurate devaluation of our operating budget in USD terms) for us to incorporate this new goal into the grant program in any meaningful way, particularly because we are not able to spend any of the MATIC in the Treasury.\n\n## Request for Approval\n\nIn keeping with the original structure of the budget where ~10% of the Treasury was allocated to 0xEVE to fund opportunities such as grants, we propose that 10% of the new funding (ZRX and MATIC) be allocated to 0xEVE to fund activities associated with this new initiative. A separate multisig has been set up to manage and track these expenditures.<br/><br/>\nCompensation will remain as authorized in the original budget, and the new funding will be allocated 100% to grants and other operational activities specific to Polygon. In accordance with the grant program framework, this will enable 0xEVE to fast track grants under $50k using its own budget, while larger grants will require an onchain community vote and will be awarded from Treasury funds.<br/><br/>\nAdditionally, as 0x protocol deploys to additional chains, for any future allocations from similar joint initiatives, we recommend that they be structured the same way (90/10 split between the Treasury and 0xEVE) so that 0xEVE can actively participate in evaluating, distributing, and managing grants and other associated efforts designed to accelerate adoption and ecosystem value capture on those networks.<br/><br/>\nAs stipulated in Z-1, 0xEVE is a limited-duration experiment (26 weeks) and any funds not used will be returned to the Treasury when the experiment concludes.\n\n## Action Required\n\nTransfer 330,813 ZRX and 420,000 MATIC to 0xEVE gnosis safe multisig 0xAB66CC8FD10457ebC9D13B9760C835F0a4CbC487',
},
{
actions: [
{
target: zrxToken,
data: zrx.approve(sablier.address, PROPOSAL_2_ZRX_AMOUNT).getABIEncodedTransactionData(),
value: new BigNumber(0),
},
{
target: maticToken.address,
data: maticToken.approve(sablier.address, PROPOSAL_2_MATIC_AMOUNT).getABIEncodedTransactionData(),
value: new BigNumber(0),
},
{
target: sablier.address,
data: sablier
.createStream(
PROPOSAL_2_RECIPIENT,
PROPOSAL_2_ZRX_AMOUNT,
zrxToken,
PROPOSAL_2_STREAM_START_TIME,
PROPOSAL_2_STREAM_START_TIME.plus(ONE_YEAR_IN_SECONDS),
)
.getABIEncodedTransactionData(),
value: new BigNumber(0),
},
{
target: sablier.address,
data: sablier
.createStream(
PROPOSAL_2_RECIPIENT,
PROPOSAL_2_MATIC_AMOUNT,
maticToken.address,
PROPOSAL_2_STREAM_START_TIME,
PROPOSAL_2_STREAM_START_TIME.plus(ONE_YEAR_IN_SECONDS),
)
.getABIEncodedTransactionData(),
value: new BigNumber(0),
},
],
description:
'# Z-3 Trader.xyz Grant\n\n## Summary\n\nThis proposal seeks authorization of a $950k grant from the treasury to trader.xyz. The community has discussed the merits of the proposal in the governance forum and signaled strong support for moving forward in a snapshot poll:\n\n1. https://gov.0x.org/t/grant-proposal-trader-xyz/1005/\n2. https://snapshot.org/#/0xgov.eth/proposal/Qmcf2C3KmQ1W1XBGownLWsA8yX9hpzY6peLUGKNJnPzN9y\n\n## Grant Details\n\n### What category best describes your grant request?\n\n1. 0x orderbook\n2. 0x protocol feature development\n\n### Grant amount requested\n\n**Amount**: $950k split 50/50 between $ZRX and $MATIC (note: an upfront payment of $50k in $ZRX and $MATIC is being made from the 0xEVE grant budget)\n\n**Price reference**:\n1. [https://www.tradingview.com/symbols/ZRXUSD/technicals/](https://www.tradingview.com/symbols/ZRXUSD/technicals/) ($ZRX 30-day EMA as of 10/4/2021 = 0.97859)\n2. [https://www.tradingview.com/symbols/MATICUSD/technicals/](https://www.tradingview.com/symbols/MATICUSD/technicals/) ($MATIC 30-day EMA as of 10/4/2021 = 1.2564959)\n\n**Payment details**: $950k streamed from Sablier over 365 days (485,393 ZRX + 378,036 MATIC)\n\n**Receiving address**: 0x976378445D31D81b15576811450A7b9797206807 (Gnosis Safe)\n\n### Team background\n\nCore team is comprised of two former 0x core team members (Patryk Adas - former Matcha lead designer, and John Johnson - former Matcha lead engineer)\n\n### Project background\n\nTrader.xyz is a dapp that provides a user-focused trading experience with the goal of becoming the flagship, 0x-powered application for discovering and trading NFTs\n\n### Description of work to be funded\n\nSee detailed explanation at https://gov.0x.org/t/grant-proposal-trader-xyz/1005\n\n### Budget breakdown\n\nSee detailed explanation at https://gov.0x.org/t/grant-proposal-trader-xyz/1005\n\n### Benefit to 0x ecosystem\n\n1. Become the gold standard for exchanging NFTs with 0x protocol\n2. Enable 0xDAO to build organic development capabilities\n3. Improve protocol documentation and developer resources\n4. Add OSS API protocol features\n\n### Risk/Reward factors\n\nRisks include competition, mindshare, flywheel of liquidity, etc. We believe our product and team have the potential to mitigate these risks and bring to market several features and capabilities that will be market-leading.\n\n### Additional info\n\nOur team has a track record of delivering high quality projects, and in order for us to continue our work, we need capital and support. We prefer not to go the venture capital route and instead work directly with the 0xDAO for the best synergies and to align value with 0x. We proved out an initial brand, design, and engineering concept via OTC orders, and it is already the highest quality OTC swap on the market. We would like to work with 0xDAO directly to make sure 0x has a foothold in the NFT market as it continues to evolve and develop. We want to do what Matcha did for DEX ERC20 trading.\n\n## Action Required\n\nStream 485,393 ZRX and 378,036 MATIC to Gnosis Safe 0x976378445D31D81b15576811450A7b9797206807 over 365 days',
},
];

View File

@@ -4,4 +4,5 @@
* -----------------------------------------------------------------------------
*/
export * from '../generated-wrappers/default_pool_operator';
export * from '../generated-wrappers/i_sablier';
export * from '../generated-wrappers/zrx_treasury';

View File

@@ -6,10 +6,12 @@
import { ContractArtifact } from 'ethereum-types';
import * as DefaultPoolOperator from '../test/generated-artifacts/DefaultPoolOperator.json';
import * as ISablier from '../test/generated-artifacts/ISablier.json';
import * as IStaking from '../test/generated-artifacts/IStaking.json';
import * as IZrxTreasury from '../test/generated-artifacts/IZrxTreasury.json';
import * as ZrxTreasury from '../test/generated-artifacts/ZrxTreasury.json';
export const artifacts = {
ISablier: ISablier as ContractArtifact,
DefaultPoolOperator: DefaultPoolOperator as ContractArtifact,
IStaking: IStaking as ContractArtifact,
IZrxTreasury: IZrxTreasury as ContractArtifact,

View File

@@ -7,7 +7,7 @@ import * as _ from 'lodash';
import { proposals } from '../src/proposals';
import { artifacts } from './artifacts';
import { ZrxTreasuryContract, ZrxTreasuryEvents } from './wrappers';
import { ISablierEvents, ZrxTreasuryContract, ZrxTreasuryEvents } from './wrappers';
const SUBGRAPH_URL = 'https://api.thegraph.com/subgraphs/name/mzhu25/zeroex-staking';
const STAKING_PROXY_ADDRESS = '0xa26e80e7dea86279c6d778d702cc413e6cffa777';
@@ -15,9 +15,11 @@ const TREASURY_ADDRESS = '0x0bb1810061c2f5b2088054ee184e6c79e1591101';
const PROPOSER = process.env.PROPOSER || constants.NULL_ADDRESS;
const VOTER = '0xba4f44e774158408e2dc6c5cb65bc995f0a89180';
const VOTER_OPERATED_POOLS = ['0x0000000000000000000000000000000000000000000000000000000000000017'];
const VOTER_2 = '0x9a4eb1101c0c053505bd71d2ffa27ed902dead85';
const VOTER_2_OPERATED_POOLS = ['0x0000000000000000000000000000000000000000000000000000000000000029'];
blockchainTests.configure({
fork: {
unlockedAccounts: [PROPOSER, VOTER],
unlockedAccounts: [PROPOSER, VOTER, VOTER_2],
},
});
@@ -219,4 +221,80 @@ blockchainTests.fork.skip('Treasury proposal mainnet fork tests', env => {
);
});
});
describe('Proposal 2', () => {
it('works', async () => {
const proposal = proposals[2];
let executionEpoch: BigNumber;
if (proposal.executionEpoch) {
executionEpoch = proposal.executionEpoch;
} else {
const currentEpoch = await staking.currentEpoch().callAsync();
executionEpoch = currentEpoch.plus(2);
}
const pools = await querySubgraphAsync(PROPOSER);
const proposeTx = treasury.propose(proposal.actions, executionEpoch, proposal.description, pools);
const calldata = proposeTx.getABIEncodedTransactionData();
logUtils.log('ZrxTreasury.propose calldata:');
logUtils.log(calldata);
const proposalId = await proposeTx.callAsync({ from: PROPOSER });
const receipt = await proposeTx.awaitTransactionSuccessAsync({ from: PROPOSER });
verifyEventsFromLogs(
receipt.logs,
[
{
...proposal,
proposalId,
executionEpoch,
proposer: PROPOSER,
operatedPoolIds: pools,
},
],
ZrxTreasuryEvents.ProposalCreated,
);
await fastForwardToNextEpochAsync();
await fastForwardToNextEpochAsync();
await treasury
.castVote(proposalId, true, VOTER_OPERATED_POOLS)
.awaitTransactionSuccessAsync({ from: VOTER });
await treasury
.castVote(proposalId, true, VOTER_2_OPERATED_POOLS)
.awaitTransactionSuccessAsync({ from: VOTER_2 });
await env.web3Wrapper.increaseTimeAsync(votingPeriod.plus(1).toNumber());
await env.web3Wrapper.mineBlockAsync();
const executeTx = await treasury.execute(proposalId, proposal.actions).awaitTransactionSuccessAsync();
verifyEventsFromLogs(
executeTx.logs,
[
{
proposalId,
},
],
ZrxTreasuryEvents.ProposalExecuted,
);
verifyEventsFromLogs(
executeTx.logs,
[
{
recipient: '0x976378445D31D81b15576811450A7b9797206807',
deposit: new BigNumber('485392999999999970448000'),
tokenAddress: '0xe41d2489571d322189246dafa5ebde1f4699f498',
startTime: new BigNumber(1635188400),
stopTime: new BigNumber(1666724400),
},
{
recipient: '0x976378445D31D81b15576811450A7b9797206807',
deposit: new BigNumber('378035999999999992944000'),
tokenAddress: '0x7d1afa7b718fb893db30a3abc0cfc608aacfebb0',
startTime: new BigNumber(1635188400),
stopTime: new BigNumber(1666724400),
},
],
ISablierEvents.CreateStream,
);
});
});
});

View File

@@ -17,8 +17,9 @@ import {
randomAddress,
verifyEventsFromLogs,
} from '@0x/contracts-test-utils';
import { BigNumber } from '@0x/utils';
import * as _ from 'lodash';
import { TreasuryVote } from '@0x/protocol-utils';
import { BigNumber, hexUtils } from '@0x/utils';
import * as ethUtil from 'ethereumjs-util';
import { artifacts } from './artifacts';
import { DefaultPoolOperatorContract, ZrxTreasuryContract, ZrxTreasuryEvents } from './wrappers';
@@ -55,6 +56,8 @@ blockchainTests.resets('Treasury governance', env => {
let nonDefaultPoolId: string;
let poolOperator: string;
let delegator: string;
let relayer: string;
let delegatorPrivateKey: string;
let actions: ProposedAction[];
async function deployStakingAsync(): Promise<void> {
@@ -105,7 +108,10 @@ blockchainTests.resets('Treasury governance', env => {
}
before(async () => {
[admin, poolOperator, delegator] = await env.getAccountAddressesAsync();
const accounts = await env.getAccountAddressesAsync();
[admin, poolOperator, delegator, relayer] = accounts;
delegatorPrivateKey = hexUtils.toHex(constants.TESTRPC_PRIVATE_KEYS[accounts.indexOf(delegator)]);
zrx = await DummyERC20TokenContract.deployFrom0xArtifactAsync(
erc20Artifacts.DummyERC20Token,
env.provider,
@@ -399,7 +405,7 @@ blockchainTests.resets('Treasury governance', env => {
expect(await treasury.proposalCount().callAsync()).to.bignumber.equal(1);
});
});
describe('castVote()', () => {
describe('castVote() and castVoteBySignature()', () => {
const VOTE_PROPOSAL_ID = new BigNumber(0);
const DELEGATOR_VOTING_POWER = new BigNumber(420);
@@ -418,17 +424,18 @@ blockchainTests.resets('Treasury governance', env => {
.propose(actions, currentEpoch.plus(2), PROPOSAL_DESCRIPTION, [])
.awaitTransactionSuccessAsync({ from: delegator });
});
// castVote()
it('Cannot vote on invalid proposalId', async () => {
await fastForwardToNextEpochAsync();
await fastForwardToNextEpochAsync();
const tx = treasury
.castVote(INVALID_PROPOSAL_ID, true, [])
.awaitTransactionSuccessAsync({ from: delegator });
return expect(tx).to.revertWith('castVote/INVALID_PROPOSAL_ID');
return expect(tx).to.revertWith('_castVote/INVALID_PROPOSAL_ID');
});
it('Cannot vote before voting period starts', async () => {
const tx = treasury.castVote(VOTE_PROPOSAL_ID, true, []).awaitTransactionSuccessAsync({ from: delegator });
return expect(tx).to.revertWith('castVote/VOTING_IS_CLOSED');
return expect(tx).to.revertWith('_castVote/VOTING_IS_CLOSED');
});
it('Cannot vote after voting period ends', async () => {
await fastForwardToNextEpochAsync();
@@ -436,14 +443,14 @@ blockchainTests.resets('Treasury governance', env => {
await env.web3Wrapper.increaseTimeAsync(TREASURY_PARAMS.votingPeriod.plus(1).toNumber());
await env.web3Wrapper.mineBlockAsync();
const tx = treasury.castVote(VOTE_PROPOSAL_ID, true, []).awaitTransactionSuccessAsync({ from: delegator });
return expect(tx).to.revertWith('castVote/VOTING_IS_CLOSED');
return expect(tx).to.revertWith('_castVote/VOTING_IS_CLOSED');
});
it('Cannot vote twice on same proposal', async () => {
await fastForwardToNextEpochAsync();
await fastForwardToNextEpochAsync();
await treasury.castVote(VOTE_PROPOSAL_ID, true, []).awaitTransactionSuccessAsync({ from: delegator });
const tx = treasury.castVote(VOTE_PROPOSAL_ID, false, []).awaitTransactionSuccessAsync({ from: delegator });
return expect(tx).to.revertWith('castVote/ALREADY_VOTED');
return expect(tx).to.revertWith('_castVote/ALREADY_VOTED');
});
it('Can cast a valid vote', async () => {
await fastForwardToNextEpochAsync();
@@ -465,6 +472,109 @@ blockchainTests.resets('Treasury governance', env => {
ZrxTreasuryEvents.VoteCast,
);
});
// castVoteBySignature()
it('Cannot vote by signature on invalid proposalId', async () => {
await fastForwardToNextEpochAsync();
await fastForwardToNextEpochAsync();
const vote = new TreasuryVote({
proposalId: INVALID_PROPOSAL_ID,
verifyingContract: admin,
});
const signature = vote.getSignatureWithKey(delegatorPrivateKey);
const tx = treasury
.castVoteBySignature(INVALID_PROPOSAL_ID, true, [], signature.v, signature.r, signature.s)
.awaitTransactionSuccessAsync({ from: relayer });
return expect(tx).to.revertWith('_castVote/INVALID_PROPOSAL_ID');
});
it('Cannot vote by signature before voting period starts', async () => {
const vote = new TreasuryVote({
proposalId: VOTE_PROPOSAL_ID,
verifyingContract: admin,
});
const signature = vote.getSignatureWithKey(delegatorPrivateKey);
const tx = treasury
.castVoteBySignature(VOTE_PROPOSAL_ID, true, [], signature.v, signature.r, signature.s)
.awaitTransactionSuccessAsync({ from: relayer });
return expect(tx).to.revertWith('_castVote/VOTING_IS_CLOSED');
});
it('Cannot vote by signature after voting period ends', async () => {
await fastForwardToNextEpochAsync();
await fastForwardToNextEpochAsync();
await env.web3Wrapper.increaseTimeAsync(TREASURY_PARAMS.votingPeriod.plus(1).toNumber());
await env.web3Wrapper.mineBlockAsync();
const vote = new TreasuryVote({
proposalId: VOTE_PROPOSAL_ID,
verifyingContract: admin,
});
const signature = vote.getSignatureWithKey(delegatorPrivateKey);
const tx = treasury
.castVoteBySignature(VOTE_PROPOSAL_ID, true, [], signature.v, signature.r, signature.s)
.awaitTransactionSuccessAsync({ from: relayer });
return expect(tx).to.revertWith('_castVote/VOTING_IS_CLOSED');
});
it('Can recover the address from signature correctly', async () => {
const vote = new TreasuryVote({
proposalId: VOTE_PROPOSAL_ID,
verifyingContract: admin,
});
const signature = vote.getSignatureWithKey(delegatorPrivateKey);
const publicKey = ethUtil.ecrecover(
ethUtil.toBuffer(vote.getEIP712Hash()),
signature.v,
ethUtil.toBuffer(signature.r),
ethUtil.toBuffer(signature.s),
);
const address = ethUtil.publicToAddress(publicKey);
expect(ethUtil.bufferToHex(address)).to.be.equal(delegator);
});
it('Can cast a valid vote by signature', async () => {
await fastForwardToNextEpochAsync();
await fastForwardToNextEpochAsync();
const vote = new TreasuryVote({
proposalId: VOTE_PROPOSAL_ID,
verifyingContract: treasury.address,
chainId: 1337,
support: false,
});
const signature = vote.getSignatureWithKey(delegatorPrivateKey);
const tx = await treasury
.castVoteBySignature(VOTE_PROPOSAL_ID, false, [], signature.v, signature.r, signature.s)
.awaitTransactionSuccessAsync({ from: relayer });
verifyEventsFromLogs(
tx.logs,
[
{
voter: delegator,
operatedPoolIds: [],
proposalId: VOTE_PROPOSAL_ID,
support: vote.support,
votingPower: DELEGATOR_VOTING_POWER,
},
],
ZrxTreasuryEvents.VoteCast,
);
});
it('Cannot vote by signature twice on same proposal', async () => {
await fastForwardToNextEpochAsync();
await fastForwardToNextEpochAsync();
await treasury.castVote(VOTE_PROPOSAL_ID, true, []).awaitTransactionSuccessAsync({ from: delegator });
const secondVote = new TreasuryVote({
proposalId: VOTE_PROPOSAL_ID,
verifyingContract: treasury.address,
chainId: 1337,
support: false,
});
const signature = secondVote.getSignatureWithKey(delegatorPrivateKey);
const secondVoteTx = treasury
.castVoteBySignature(VOTE_PROPOSAL_ID, false, [], signature.v, signature.r, signature.s)
.awaitTransactionSuccessAsync({ from: relayer });
return expect(secondVoteTx).to.revertWith('_castVote/ALREADY_VOTED');
});
});
describe('execute()', () => {
let passedProposalId: BigNumber;
@@ -473,7 +583,7 @@ blockchainTests.resets('Treasury governance', env => {
let ongoingVoteProposalId: BigNumber;
before(async () => {
// OPerator has enough ZRX to create and pass a proposal
// Operator has enough ZRX to create and pass a proposal
await staking.stake(TREASURY_PARAMS.quorumThreshold).awaitTransactionSuccessAsync({ from: poolOperator });
await staking
.moveStake(
@@ -549,7 +659,7 @@ blockchainTests.resets('Treasury governance', env => {
});
it('Cannot execute before or after the execution epoch', async () => {
const tooEarly = treasury.execute(passedProposalId, actions).awaitTransactionSuccessAsync();
expect(tooEarly).to.revertWith('_assertProposalExecutable/CANNOT_EXECUTE_THIS_EPOCH');
await expect(tooEarly).to.revertWith('_assertProposalExecutable/CANNOT_EXECUTE_THIS_EPOCH');
await fastForwardToNextEpochAsync();
// Proposal 0 is executable here
await fastForwardToNextEpochAsync();

View File

@@ -4,6 +4,7 @@
* -----------------------------------------------------------------------------
*/
export * from '../test/generated-wrappers/default_pool_operator';
export * from '../test/generated-wrappers/i_sablier';
export * from '../test/generated-wrappers/i_staking';
export * from '../test/generated-wrappers/i_zrx_treasury';
export * from '../test/generated-wrappers/zrx_treasury';

View File

@@ -4,8 +4,10 @@
"include": ["./src/**/*", "./test/**/*", "./generated-wrappers/**/*"],
"files": [
"generated-artifacts/DefaultPoolOperator.json",
"generated-artifacts/ISablier.json",
"generated-artifacts/ZrxTreasury.json",
"test/generated-artifacts/DefaultPoolOperator.json",
"test/generated-artifacts/ISablier.json",
"test/generated-artifacts/IStaking.json",
"test/generated-artifacts/IZrxTreasury.json",
"test/generated-artifacts/ZrxTreasury.json"

View File

@@ -1,4 +1,32 @@
[
{
"timestamp": 1634668033,
"version": "4.8.2",
"changes": [
{
"note": "Dependencies updated"
}
]
},
{
"timestamp": 1631710679,
"version": "4.8.1",
"changes": [
{
"note": "Dependencies updated"
}
]
},
{
"version": "4.8.0",
"changes": [
{
"note": "Added FundRecoveryFeature to the 0x EP",
"pr": 306
}
],
"timestamp": 1631120757
},
{
"timestamp": 1630459879,
"version": "4.7.18",

View File

@@ -5,6 +5,18 @@ Edit the package's CHANGELOG.json file only.
CHANGELOG
## v4.8.2 - _October 19, 2021_
* Dependencies updated
## v4.8.1 - _September 15, 2021_
* Dependencies updated
## v4.8.0 - _September 8, 2021_
* Added FundRecoveryFeature to the 0x EP (#306)
## v4.7.18 - _September 1, 2021_
* Dependencies updated

View File

@@ -1,6 +1,6 @@
{
"name": "@0x/contracts-utils",
"version": "4.7.18",
"version": "4.8.2",
"engines": {
"node": ">=6.12"
},
@@ -50,15 +50,15 @@
},
"homepage": "https://github.com/0xProject/protocol/tree/main/contracts/utils",
"devDependencies": {
"@0x/abi-gen": "^5.6.0",
"@0x/contracts-gen": "^2.0.38",
"@0x/contracts-test-utils": "^5.4.10",
"@0x/dev-utils": "^4.2.7",
"@0x/abi-gen": "^5.6.2",
"@0x/contracts-gen": "^2.0.40",
"@0x/contracts-test-utils": "^5.4.12",
"@0x/dev-utils": "^4.2.9",
"@0x/order-utils": "^10.4.28",
"@0x/sol-compiler": "^4.7.3",
"@0x/sol-compiler": "^4.7.5",
"@0x/tslint-config": "^4.1.4",
"@0x/types": "^3.3.3",
"@0x/web3-wrapper": "^7.5.3",
"@0x/types": "^3.3.4",
"@0x/web3-wrapper": "^7.6.0",
"@types/bn.js": "^4.11.0",
"@types/lodash": "4.14.104",
"@types/mocha": "^5.2.7",
@@ -79,11 +79,11 @@
"typescript": "4.2.2"
},
"dependencies": {
"@0x/base-contract": "^6.4.0",
"@0x/typescript-typings": "^5.2.0",
"@0x/utils": "^6.4.3",
"@0x/base-contract": "^6.4.2",
"@0x/typescript-typings": "^5.2.1",
"@0x/utils": "^6.4.4",
"bn.js": "^4.11.8",
"ethereum-types": "^3.5.0"
"ethereum-types": "^3.6.0"
},
"publishConfig": {
"access": "public"

View File

@@ -1,4 +1,59 @@
[
{
"version": "0.29.3",
"changes": [
{
"note": "Register transformERC20() and remove transformERC20Staging()",
"pr": 355
}
]
},
{
"timestamp": 1634668033,
"version": "0.29.2",
"changes": [
{
"note": "Dependencies updated"
}
]
},
{
"timestamp": 1633374058,
"version": "0.29.1",
"changes": [
{
"note": "Dependencies updated"
}
]
},
{
"version": "0.29.0",
"changes": [
{
"note": "Export TransformERC20FeatureContract",
"pr": 282
}
],
"timestamp": 1632957537
},
{
"timestamp": 1631710679,
"version": "0.28.5",
"changes": [
{
"note": "Dependencies updated"
}
]
},
{
"timestamp": 1631120757,
"version": "0.28.4",
"changes": [
{
"note": "Dependencies updated"
}
]
},
{
"timestamp": 1630459879,
"version": "0.28.3",

View File

@@ -5,6 +5,26 @@ Edit the package's CHANGELOG.json file only.
CHANGELOG
## v0.29.2 - _October 19, 2021_
* Dependencies updated
## v0.29.1 - _October 4, 2021_
* Dependencies updated
## v0.29.0 - _September 29, 2021_
* Export TransformERC20FeatureContract (#282)
## v0.28.5 - _September 15, 2021_
* Dependencies updated
## v0.28.4 - _September 8, 2021_
* Dependencies updated
## v0.28.3 - _September 1, 2021_
* Dependencies updated

View File

@@ -33,6 +33,7 @@ import "./features/interfaces/INativeOrdersFeature.sol";
import "./features/interfaces/IBatchFillNativeOrdersFeature.sol";
import "./features/interfaces/IMultiplexFeature.sol";
import "./features/interfaces/IOtcOrdersFeature.sol";
import "./features/interfaces/IFundRecoveryFeature.sol";
/// @dev Interface for a fully featured Exchange Proxy.
@@ -48,7 +49,8 @@ interface IZeroEx is
INativeOrdersFeature,
IBatchFillNativeOrdersFeature,
IMultiplexFeature,
IOtcOrdersFeature
IOtcOrdersFeature,
IFundRecoveryFeature
{
// solhint-disable state-visibility

View File

@@ -0,0 +1,66 @@
// SPDX-License-Identifier: Apache-2.0
/*
Copyright 2021 ZeroEx Intl.
Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at
http://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
*/
pragma solidity ^0.6.5;
pragma experimental ABIEncoderV2;
import "@0x/contracts-erc20/contracts/src/v06/IERC20TokenV06.sol";
import "../migrations/LibMigrate.sol";
import "../fixins/FixinCommon.sol";
import "./interfaces/IFeature.sol";
import "./interfaces/IFundRecoveryFeature.sol";
import "../transformers/LibERC20Transformer.sol";
contract FundRecoveryFeature is
IFeature,
IFundRecoveryFeature,
FixinCommon
{
/// @dev Name of this feature.
string public constant override FEATURE_NAME = "FundRecoveryFeature";
/// @dev Version of this feature.
uint256 public immutable override FEATURE_VERSION = _encodeVersion(1, 0, 0);
/// @dev Initialize and register this feature.
/// Should be delegatecalled by `Migrate.migrate()`.
/// @return success `LibMigrate.SUCCESS` on success.
function migrate()
external
returns (bytes4 success)
{
_registerFeatureFunction(this.transferTrappedTokensTo.selector);
return LibMigrate.MIGRATE_SUCCESS;
}
/// @dev Recovers ERC20 tokens or ETH from the 0x Exchange Proxy contract
/// @param erc20 ERC20 Token Address. (You can also pass in `0xeeeee...` to indicate ETH)
/// @param amountOut Amount of tokens to withdraw.
/// @param recipientWallet Recipient wallet address.
function transferTrappedTokensTo(
IERC20TokenV06 erc20,
uint256 amountOut,
address payable recipientWallet
)
external
override
onlyOwner
{
if(amountOut == uint256(-1)) {
amountOut = LibERC20Transformer.getTokenBalanceOf(erc20, address(this));
}
LibERC20Transformer.transformerTransfer(erc20, recipientWallet, amountOut);
}
}

View File

@@ -75,7 +75,7 @@ contract TransformERC20Feature is
_registerFeatureFunction(this.setTransformerDeployer.selector);
_registerFeatureFunction(this.setQuoteSigner.selector);
_registerFeatureFunction(this.getQuoteSigner.selector);
_registerFeatureFunction(this.transformERC20Staging.selector);
_registerFeatureFunction(this.transformERC20.selector);
_registerFeatureFunction(this._transformERC20.selector);
if (this.getTransformWallet() == IFlashWallet(address(0))) {
// Create the transform wallet if it doesn't exist.
@@ -145,44 +145,6 @@ contract TransformERC20Feature is
LibTransformERC20Storage.getStorage().wallet = wallet;
}
/// @dev Wrapper for `transformERC20`. This selector will be temporarily
/// registered to the Exchange Proxy so that we can migrate 0x API
/// with no downtime. Once 0x API has been updated to point to this
/// function, we can safely re-register `transformERC20`, point
/// 0x API back to `transformERC20`, and deregister this function.
/// @param inputToken The token being provided by the sender.
/// If `0xeee...`, ETH is implied and should be provided with the call.`
/// @param outputToken The token to be acquired by the sender.
/// `0xeee...` implies ETH.
/// @param inputTokenAmount The amount of `inputToken` to take from the sender.
/// If set to `uint256(-1)`, the entire spendable balance of the taker
/// will be solt.
/// @param minOutputTokenAmount The minimum amount of `outputToken` the sender
/// must receive for the entire transformation to succeed. If set to zero,
/// the minimum output token transfer will not be asserted.
/// @param transformations The transformations to execute on the token balance(s)
/// in sequence.
/// @return outputTokenAmount The amount of `outputToken` received by the sender.
function transformERC20Staging(
IERC20TokenV06 inputToken,
IERC20TokenV06 outputToken,
uint256 inputTokenAmount,
uint256 minOutputTokenAmount,
Transformation[] memory transformations
)
public
payable
returns (uint256 outputTokenAmount)
{
return transformERC20(
inputToken,
outputToken,
inputTokenAmount,
minOutputTokenAmount,
transformations
);
}
/// @dev Executes a series of transformations to convert an ERC20 `inputToken`
/// to an ERC20 `outputToken`.
/// @param inputToken The token being provided by the sender.
@@ -283,8 +245,8 @@ contract TransformERC20Feature is
}
// Transfer output tokens from wallet to recipient
outputTokenAmount = _executeOutputTokenTransfer(
args.outputToken,
state.wallet,
args.outputToken,
state.wallet,
args.recipient
);
}

View File

@@ -0,0 +1,35 @@
// SPDX-License-Identifier: Apache-2.0
/*
Copyright 2020 ZeroEx Intl.
Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at
http://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
*/
pragma solidity ^0.6.5;
pragma experimental ABIEncoderV2;
import "@0x/contracts-erc20/contracts/src/v06/IERC20TokenV06.sol";
/// @dev Exchange Proxy Recovery Functions
interface IFundRecoveryFeature {
/// @dev calledFrom FundRecoveryFeature.transferTrappedTokensTo() This will be delegatecalled
/// in the context of the Exchange Proxy instance being used.
/// @param erc20 ERC20 Token Address.
/// @param amountOut Amount of tokens to withdraw.
/// @param recipientWallet Recipient wallet address.
function transferTrappedTokensTo(
IERC20TokenV06 erc20,
uint256 amountOut,
address payable recipientWallet
)
external;
}

View File

@@ -25,7 +25,6 @@ import "./BridgeProtocols.sol";
import "./mixins/MixinBalancer.sol";
import "./mixins/MixinBalancerV2.sol";
import "./mixins/MixinBancor.sol";
import "./mixins/MixinClipper.sol";
import "./mixins/MixinCoFiX.sol";
import "./mixins/MixinCurve.sol";
import "./mixins/MixinCurveV2.sol";
@@ -51,7 +50,6 @@ contract BridgeAdapter is
MixinBalancer,
MixinBalancerV2,
MixinBancor,
MixinClipper,
MixinCoFiX,
MixinCurve,
MixinCurveV2,
@@ -77,7 +75,6 @@ contract BridgeAdapter is
MixinBalancer()
MixinBalancerV2()
MixinBancor(weth)
MixinClipper(weth)
MixinCoFiX()
MixinCurve(weth)
MixinCurveV2()
@@ -248,13 +245,6 @@ contract BridgeAdapter is
sellAmount,
order.bridgeData
);
} else if (protocolId == BridgeProtocols.CLIPPER) {
boughtAmount = _tradeClipper(
sellToken,
buyToken,
sellAmount,
order.bridgeData
);
} else {
boughtAmount = _tradeZeroExBridge(
sellToken,

View File

@@ -49,5 +49,5 @@ library BridgeProtocols {
uint128 internal constant KYBERDMM = 19;
uint128 internal constant CURVEV2 = 20;
uint128 internal constant LIDO = 21;
uint128 internal constant CLIPPER = 22;
uint128 internal constant CLIPPER = 22; // Not used: Clipper is now using PLP interface
}

View File

@@ -1,148 +0,0 @@
// SPDX-License-Identifier: Apache-2.0
/*
Copyright 2021 ZeroEx Intl.
Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at
http://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
*/
pragma solidity ^0.6.5;
pragma experimental ABIEncoderV2;
import "@0x/contracts-erc20/contracts/src/v06/LibERC20TokenV06.sol";
import "@0x/contracts-erc20/contracts/src/v06/IERC20TokenV06.sol";
import "@0x/contracts-erc20/contracts/src/v06/IEtherTokenV06.sol";
import "../IBridgeAdapter.sol";
import "../../../vendor/ILiquidityProvider.sol";
contract MixinClipper {
using LibERC20TokenV06 for IERC20TokenV06;
/// @dev Mainnet address of the WETH contract.
IEtherTokenV06 private immutable WETH;
constructor(IEtherTokenV06 weth)
public
{
WETH = weth;
}
function _tradeClipper(
IERC20TokenV06 sellToken,
IERC20TokenV06 buyToken,
uint256 sellAmount,
bytes memory bridgeData
)
internal
returns (uint256 boughtAmount)
{
// We can only use ETH with Clipper, no WETH available
(ILiquidityProvider clipper, bytes memory auxiliaryData) =
abi.decode(bridgeData, (ILiquidityProvider, bytes));
if (sellToken == WETH) {
boughtAmount = _executeSellEthForToken(
clipper,
buyToken,
sellAmount,
auxiliaryData
);
} else if (buyToken == WETH) {
boughtAmount = _executeSellTokenForEth(
clipper,
sellToken,
sellAmount,
auxiliaryData
);
} else {
boughtAmount = _executeSellTokenForToken(
clipper,
sellToken,
buyToken,
sellAmount,
auxiliaryData
);
}
return boughtAmount;
}
function _executeSellEthForToken(
ILiquidityProvider clipper,
IERC20TokenV06 buyToken,
uint256 sellAmount,
bytes memory auxiliaryData
)
private
returns (uint256 boughtAmount)
{
// Clipper requires ETH and doesn't support WETH
WETH.withdraw(sellAmount);
boughtAmount = clipper.sellEthForToken{ value: sellAmount }(
buyToken,
address(this),
1,
auxiliaryData
);
}
function _executeSellTokenForEth(
ILiquidityProvider clipper,
IERC20TokenV06 sellToken,
uint256 sellAmount,
bytes memory auxiliaryData
)
private
returns (uint256 boughtAmount)
{
// Optimization: We can transfer the tokens into clipper rather than
// have an allowance updated
sellToken.compatTransfer(address(clipper), sellAmount);
boughtAmount = clipper.sellTokenForEth(
sellToken,
payable(address(this)),
1,
auxiliaryData
);
// we want WETH for possible future trades
WETH.deposit{ value: boughtAmount }();
}
function _executeSellTokenForToken(
ILiquidityProvider clipper,
IERC20TokenV06 sellToken,
IERC20TokenV06 buyToken,
uint256 sellAmount,
bytes memory auxiliaryData
)
private
returns (uint256 boughtAmount)
{
// Optimization: We can transfer the tokens into clipper rather than
// have an allowance updated
sellToken.compatTransfer(address(clipper), sellAmount);
boughtAmount = clipper.sellTokenForToken(
sellToken,
buyToken,
address(this),
1,
auxiliaryData
);
}
}

View File

@@ -1,6 +1,6 @@
{
"name": "@0x/contracts-zero-ex",
"version": "0.28.3",
"version": "0.29.2",
"engines": {
"node": ">=6.12"
},
@@ -43,7 +43,7 @@
"config": {
"publicInterfaceContracts": "IZeroEx,ZeroEx,FullMigration,InitialMigration,IFlashWallet,IERC20Transformer,IOwnableFeature,ISimpleFunctionRegistryFeature,ITransformERC20Feature,FillQuoteTransformer,PayTakerTransformer,PositiveSlippageFeeTransformer,WethTransformer,OwnableFeature,SimpleFunctionRegistryFeature,TransformERC20Feature,AffiliateFeeTransformer,MetaTransactionsFeature,LogMetadataTransformer,BridgeAdapter,LiquidityProviderFeature,ILiquidityProviderFeature,NativeOrdersFeature,INativeOrdersFeature,FeeCollectorController,FeeCollector,CurveLiquidityProvider,BatchFillNativeOrdersFeature,IBatchFillNativeOrdersFeature,MultiplexFeature,IMultiplexFeature,OtcOrdersFeature,IOtcOrdersFeature",
"abis:comment": "This list is auto-generated by contracts-gen. Don't edit manually.",
"abis": "./test/generated-artifacts/@(AffiliateFeeTransformer|BatchFillNativeOrdersFeature|BootstrapFeature|BridgeAdapter|BridgeProtocols|CurveLiquidityProvider|FeeCollector|FeeCollectorController|FillQuoteTransformer|FixinCommon|FixinEIP712|FixinProtocolFees|FixinReentrancyGuard|FixinTokenSpender|FlashWallet|FullMigration|IBatchFillNativeOrdersFeature|IBootstrapFeature|IBridgeAdapter|IERC20Bridge|IERC20Transformer|IFeature|IFlashWallet|ILiquidityProvider|ILiquidityProviderFeature|ILiquidityProviderSandbox|IMetaTransactionsFeature|IMooniswapPool|IMultiplexFeature|INativeOrdersEvents|INativeOrdersFeature|IOtcOrdersFeature|IOwnableFeature|IPancakeSwapFeature|ISimpleFunctionRegistryFeature|IStaking|ITestSimpleFunctionRegistryFeature|ITokenSpenderFeature|ITransformERC20Feature|IUniswapFeature|IUniswapV2Pair|IUniswapV3Feature|IUniswapV3Pool|IZeroEx|InitialMigration|LibBootstrap|LibCommonRichErrors|LibERC20Transformer|LibFeeCollector|LibLiquidityProviderRichErrors|LibMetaTransactionsRichErrors|LibMetaTransactionsStorage|LibMigrate|LibNativeOrder|LibNativeOrdersRichErrors|LibNativeOrdersStorage|LibOtcOrdersStorage|LibOwnableRichErrors|LibOwnableStorage|LibProxyRichErrors|LibProxyStorage|LibReentrancyGuardStorage|LibSignature|LibSignatureRichErrors|LibSimpleFunctionRegistryRichErrors|LibSimpleFunctionRegistryStorage|LibStorage|LibTransformERC20RichErrors|LibTransformERC20Storage|LibWalletRichErrors|LiquidityProviderFeature|LiquidityProviderSandbox|LogMetadataTransformer|MetaTransactionsFeature|MixinBalancer|MixinBalancerV2|MixinBancor|MixinClipper|MixinCoFiX|MixinCryptoCom|MixinCurve|MixinCurveV2|MixinDodo|MixinDodoV2|MixinKyber|MixinKyberDmm|MixinLido|MixinMStable|MixinMakerPSM|MixinMooniswap|MixinNerve|MixinOasis|MixinShell|MixinUniswap|MixinUniswapV2|MixinUniswapV3|MixinZeroExBridge|MooniswapLiquidityProvider|MultiplexFeature|MultiplexLiquidityProvider|MultiplexOtc|MultiplexRfq|MultiplexTransformERC20|MultiplexUniswapV2|MultiplexUniswapV3|NativeOrdersCancellation|NativeOrdersFeature|NativeOrdersInfo|NativeOrdersProtocolFees|NativeOrdersSettlement|OtcOrdersFeature|OwnableFeature|PancakeSwapFeature|PayTakerTransformer|PermissionlessTransformerDeployer|PositiveSlippageFeeTransformer|SimpleFunctionRegistryFeature|TestBridge|TestCallTarget|TestCurve|TestDelegateCaller|TestFeeCollectorController|TestFillQuoteTransformerBridge|TestFillQuoteTransformerExchange|TestFillQuoteTransformerHost|TestFixinProtocolFees|TestFixinTokenSpender|TestFullMigration|TestInitialMigration|TestLibNativeOrder|TestLibSignature|TestLiquidityProvider|TestMetaTransactionsNativeOrdersFeature|TestMetaTransactionsTransformERC20Feature|TestMigrator|TestMintTokenERC20Transformer|TestMintableERC20Token|TestMooniswap|TestNativeOrdersFeature|TestNoEthRecipient|TestOrderSignerRegistryWithContractWallet|TestPermissionlessTransformerDeployerSuicidal|TestPermissionlessTransformerDeployerTransformer|TestRfqOriginRegistration|TestSimpleFunctionRegistryFeatureImpl1|TestSimpleFunctionRegistryFeatureImpl2|TestStaking|TestTokenSpenderERC20Token|TestTransformERC20|TestTransformerBase|TestTransformerDeployerTransformer|TestTransformerHost|TestUniswapV2Factory|TestUniswapV2Pool|TestUniswapV3Factory|TestUniswapV3Feature|TestUniswapV3Pool|TestWeth|TestWethTransformerHost|TestZeroExFeature|TransformERC20Feature|Transformer|TransformerDeployer|UniswapFeature|UniswapV3Feature|WethTransformer|ZeroEx|ZeroExOptimized).json"
"abis": "./test/generated-artifacts/@(AffiliateFeeTransformer|BatchFillNativeOrdersFeature|BootstrapFeature|BridgeAdapter|BridgeProtocols|CurveLiquidityProvider|FeeCollector|FeeCollectorController|FillQuoteTransformer|FixinCommon|FixinEIP712|FixinProtocolFees|FixinReentrancyGuard|FixinTokenSpender|FlashWallet|FullMigration|FundRecoveryFeature|IBatchFillNativeOrdersFeature|IBootstrapFeature|IBridgeAdapter|IERC20Bridge|IERC20Transformer|IFeature|IFlashWallet|IFundRecoveryFeature|ILiquidityProvider|ILiquidityProviderFeature|ILiquidityProviderSandbox|IMetaTransactionsFeature|IMooniswapPool|IMultiplexFeature|INativeOrdersEvents|INativeOrdersFeature|IOtcOrdersFeature|IOwnableFeature|IPancakeSwapFeature|ISimpleFunctionRegistryFeature|IStaking|ITestSimpleFunctionRegistryFeature|ITokenSpenderFeature|ITransformERC20Feature|IUniswapFeature|IUniswapV2Pair|IUniswapV3Feature|IUniswapV3Pool|IZeroEx|InitialMigration|LibBootstrap|LibCommonRichErrors|LibERC20Transformer|LibFeeCollector|LibLiquidityProviderRichErrors|LibMetaTransactionsRichErrors|LibMetaTransactionsStorage|LibMigrate|LibNativeOrder|LibNativeOrdersRichErrors|LibNativeOrdersStorage|LibOtcOrdersStorage|LibOwnableRichErrors|LibOwnableStorage|LibProxyRichErrors|LibProxyStorage|LibReentrancyGuardStorage|LibSignature|LibSignatureRichErrors|LibSimpleFunctionRegistryRichErrors|LibSimpleFunctionRegistryStorage|LibStorage|LibTransformERC20RichErrors|LibTransformERC20Storage|LibWalletRichErrors|LiquidityProviderFeature|LiquidityProviderSandbox|LogMetadataTransformer|MetaTransactionsFeature|MixinBalancer|MixinBalancerV2|MixinBancor|MixinCoFiX|MixinCryptoCom|MixinCurve|MixinCurveV2|MixinDodo|MixinDodoV2|MixinKyber|MixinKyberDmm|MixinLido|MixinMStable|MixinMakerPSM|MixinMooniswap|MixinNerve|MixinOasis|MixinShell|MixinUniswap|MixinUniswapV2|MixinUniswapV3|MixinZeroExBridge|MooniswapLiquidityProvider|MultiplexFeature|MultiplexLiquidityProvider|MultiplexOtc|MultiplexRfq|MultiplexTransformERC20|MultiplexUniswapV2|MultiplexUniswapV3|NativeOrdersCancellation|NativeOrdersFeature|NativeOrdersInfo|NativeOrdersProtocolFees|NativeOrdersSettlement|OtcOrdersFeature|OwnableFeature|PancakeSwapFeature|PayTakerTransformer|PermissionlessTransformerDeployer|PositiveSlippageFeeTransformer|SimpleFunctionRegistryFeature|TestBridge|TestCallTarget|TestCurve|TestDelegateCaller|TestFeeCollectorController|TestFillQuoteTransformerBridge|TestFillQuoteTransformerExchange|TestFillQuoteTransformerHost|TestFixinProtocolFees|TestFixinTokenSpender|TestFullMigration|TestInitialMigration|TestLibNativeOrder|TestLibSignature|TestLiquidityProvider|TestMetaTransactionsNativeOrdersFeature|TestMetaTransactionsTransformERC20Feature|TestMigrator|TestMintTokenERC20Transformer|TestMintableERC20Token|TestMooniswap|TestNativeOrdersFeature|TestNoEthRecipient|TestOrderSignerRegistryWithContractWallet|TestPermissionlessTransformerDeployerSuicidal|TestPermissionlessTransformerDeployerTransformer|TestRfqOriginRegistration|TestSimpleFunctionRegistryFeatureImpl1|TestSimpleFunctionRegistryFeatureImpl2|TestStaking|TestTokenSpenderERC20Token|TestTransformERC20|TestTransformerBase|TestTransformerDeployerTransformer|TestTransformerHost|TestUniswapV2Factory|TestUniswapV2Pool|TestUniswapV3Factory|TestUniswapV3Feature|TestUniswapV3Pool|TestWeth|TestWethTransformerHost|TestZeroExFeature|TransformERC20Feature|Transformer|TransformerDeployer|UniswapFeature|UniswapV3Feature|WethTransformer|ZeroEx|ZeroExOptimized).json"
},
"repository": {
"type": "git",
@@ -55,14 +55,14 @@
},
"homepage": "https://github.com/0xProject/protocol/tree/main/contracts/zero-ex",
"devDependencies": {
"@0x/abi-gen": "^5.6.0",
"@0x/contract-addresses": "^6.7.0",
"@0x/contracts-erc20": "^3.3.18",
"@0x/contracts-gen": "^2.0.38",
"@0x/contracts-test-utils": "^5.4.10",
"@0x/dev-utils": "^4.2.7",
"@0x/abi-gen": "^5.6.2",
"@0x/contract-addresses": "^6.8.0",
"@0x/contracts-erc20": "^3.3.21",
"@0x/contracts-gen": "^2.0.40",
"@0x/contracts-test-utils": "^5.4.12",
"@0x/dev-utils": "^4.2.9",
"@0x/order-utils": "^10.4.28",
"@0x/sol-compiler": "^4.7.3",
"@0x/sol-compiler": "^4.7.5",
"@0x/ts-doc-gen": "^0.0.28",
"@0x/tslint-config": "^4.1.4",
"@types/isomorphic-fetch": "^0.0.35",
@@ -82,14 +82,14 @@
"typescript": "4.2.2"
},
"dependencies": {
"@0x/base-contract": "^6.4.0",
"@0x/protocol-utils": "^1.8.4",
"@0x/subproviders": "^6.5.3",
"@0x/types": "^3.3.3",
"@0x/typescript-typings": "^5.2.0",
"@0x/utils": "^6.4.3",
"@0x/web3-wrapper": "^7.5.3",
"ethereum-types": "^3.5.0",
"@0x/base-contract": "^6.4.2",
"@0x/protocol-utils": "^1.9.3",
"@0x/subproviders": "^6.6.0",
"@0x/types": "^3.3.4",
"@0x/typescript-typings": "^5.2.1",
"@0x/utils": "^6.4.4",
"@0x/web3-wrapper": "^7.6.0",
"ethereum-types": "^3.6.0",
"ethereumjs-util": "^7.0.10",
"ethers": "~4.0.4"
},

View File

@@ -47,6 +47,7 @@ export {
MultiplexFeatureContract,
PayTakerTransformerContract,
PositiveSlippageFeeTransformerContract,
TransformERC20FeatureContract,
WethTransformerContract,
ZeroExContract,
} from './wrappers';

View File

@@ -21,6 +21,7 @@ import * as FixinReentrancyGuard from '../test/generated-artifacts/FixinReentran
import * as FixinTokenSpender from '../test/generated-artifacts/FixinTokenSpender.json';
import * as FlashWallet from '../test/generated-artifacts/FlashWallet.json';
import * as FullMigration from '../test/generated-artifacts/FullMigration.json';
import * as FundRecoveryFeature from '../test/generated-artifacts/FundRecoveryFeature.json';
import * as IBatchFillNativeOrdersFeature from '../test/generated-artifacts/IBatchFillNativeOrdersFeature.json';
import * as IBootstrapFeature from '../test/generated-artifacts/IBootstrapFeature.json';
import * as IBridgeAdapter from '../test/generated-artifacts/IBridgeAdapter.json';
@@ -28,6 +29,7 @@ import * as IERC20Bridge from '../test/generated-artifacts/IERC20Bridge.json';
import * as IERC20Transformer from '../test/generated-artifacts/IERC20Transformer.json';
import * as IFeature from '../test/generated-artifacts/IFeature.json';
import * as IFlashWallet from '../test/generated-artifacts/IFlashWallet.json';
import * as IFundRecoveryFeature from '../test/generated-artifacts/IFundRecoveryFeature.json';
import * as ILiquidityProvider from '../test/generated-artifacts/ILiquidityProvider.json';
import * as ILiquidityProviderFeature from '../test/generated-artifacts/ILiquidityProviderFeature.json';
import * as ILiquidityProviderSandbox from '../test/generated-artifacts/ILiquidityProviderSandbox.json';
@@ -82,7 +84,6 @@ import * as MetaTransactionsFeature from '../test/generated-artifacts/MetaTransa
import * as MixinBalancer from '../test/generated-artifacts/MixinBalancer.json';
import * as MixinBalancerV2 from '../test/generated-artifacts/MixinBalancerV2.json';
import * as MixinBancor from '../test/generated-artifacts/MixinBancor.json';
import * as MixinClipper from '../test/generated-artifacts/MixinClipper.json';
import * as MixinCoFiX from '../test/generated-artifacts/MixinCoFiX.json';
import * as MixinCryptoCom from '../test/generated-artifacts/MixinCryptoCom.json';
import * as MixinCurve from '../test/generated-artifacts/MixinCurve.json';
@@ -198,6 +199,7 @@ export const artifacts = {
TransformerDeployer: TransformerDeployer as ContractArtifact,
BatchFillNativeOrdersFeature: BatchFillNativeOrdersFeature as ContractArtifact,
BootstrapFeature: BootstrapFeature as ContractArtifact,
FundRecoveryFeature: FundRecoveryFeature as ContractArtifact,
LiquidityProviderFeature: LiquidityProviderFeature as ContractArtifact,
MetaTransactionsFeature: MetaTransactionsFeature as ContractArtifact,
NativeOrdersFeature: NativeOrdersFeature as ContractArtifact,
@@ -211,6 +213,7 @@ export const artifacts = {
IBatchFillNativeOrdersFeature: IBatchFillNativeOrdersFeature as ContractArtifact,
IBootstrapFeature: IBootstrapFeature as ContractArtifact,
IFeature: IFeature as ContractArtifact,
IFundRecoveryFeature: IFundRecoveryFeature as ContractArtifact,
ILiquidityProviderFeature: ILiquidityProviderFeature as ContractArtifact,
IMetaTransactionsFeature: IMetaTransactionsFeature as ContractArtifact,
IMultiplexFeature: IMultiplexFeature as ContractArtifact,
@@ -272,7 +275,6 @@ export const artifacts = {
MixinBalancer: MixinBalancer as ContractArtifact,
MixinBalancerV2: MixinBalancerV2 as ContractArtifact,
MixinBancor: MixinBancor as ContractArtifact,
MixinClipper: MixinClipper as ContractArtifact,
MixinCoFiX: MixinCoFiX as ContractArtifact,
MixinCryptoCom: MixinCryptoCom as ContractArtifact,
MixinCurve: MixinCurve as ContractArtifact,

View File

@@ -0,0 +1,96 @@
import { blockchainTests, constants, expect, randomAddress } from '@0x/contracts-test-utils';
import { BigNumber, OwnableRevertErrors } from '@0x/utils';
import { Web3Wrapper } from '@0x/web3-wrapper';
import { IOwnableFeatureContract, IZeroExContract } from '../../src/wrappers';
import { artifacts } from '../artifacts';
import { FundRecoveryFeatureContract } from '../generated-wrappers/fund_recovery_feature';
import { abis } from '../utils/abis';
import { fullMigrateAsync } from '../utils/migration';
import { TestMintableERC20TokenContract } from '../wrappers';
blockchainTests('FundRecovery', async env => {
let owner: string;
let zeroEx: IZeroExContract;
let token: TestMintableERC20TokenContract;
before(async () => {
const INITIAL_ERC20_BALANCE = Web3Wrapper.toBaseUnitAmount(new BigNumber(10000), 18);
[owner] = await env.getAccountAddressesAsync();
zeroEx = await fullMigrateAsync(owner, env.provider, env.txDefaults, {});
token = await TestMintableERC20TokenContract.deployFrom0xArtifactAsync(
artifacts.TestMintableERC20Token,
env.provider,
env.txDefaults,
{},
);
await token.mint(zeroEx.address, INITIAL_ERC20_BALANCE).awaitTransactionSuccessAsync();
const featureImpl = await FundRecoveryFeatureContract.deployFrom0xArtifactAsync(
artifacts.FundRecoveryFeature,
env.provider,
env.txDefaults,
artifacts,
);
await new IOwnableFeatureContract(zeroEx.address, env.provider, env.txDefaults, abis)
.migrate(featureImpl.address, featureImpl.migrate().getABIEncodedTransactionData(), owner)
.awaitTransactionSuccessAsync({ from: owner });
});
blockchainTests.resets('Should delegatecall `transferTrappedTokensTo` from the exchange proxy', () => {
const ETH_TOKEN_ADDRESS = '0xEeeeeEeeeEeEeeEeEeEeeEEEeeeeEeeeeeeeEEeE';
const recipientAddress = randomAddress();
it('Tranfers an arbitrary ERC-20 Token', async () => {
const amountOut = Web3Wrapper.toBaseUnitAmount(new BigNumber(100), 18);
await zeroEx
.transferTrappedTokensTo(token.address, amountOut, recipientAddress)
.awaitTransactionSuccessAsync({ from: owner });
const recipientAddressBalanceAferTransfer = await token.balanceOf(recipientAddress).callAsync();
return expect(recipientAddressBalanceAferTransfer).to.bignumber.equal(amountOut);
});
it('Amount -1 transfers entire balance of ERC-20', async () => {
const balanceOwner = await token.balanceOf(zeroEx.address).callAsync();
await zeroEx
.transferTrappedTokensTo(token.address, constants.MAX_UINT256, recipientAddress)
.awaitTransactionSuccessAsync({ from: owner });
const recipientAddressBalanceAferTransfer = await token.balanceOf(recipientAddress).callAsync();
return expect(recipientAddressBalanceAferTransfer).to.bignumber.equal(balanceOwner);
});
it('Amount -1 transfers entire balance of ETH', async () => {
const amountOut = new BigNumber(20);
await env.web3Wrapper.awaitTransactionMinedAsync(
await env.web3Wrapper.sendTransactionAsync({
from: owner,
to: zeroEx.address,
value: amountOut,
}),
);
const balanceOwner = await env.web3Wrapper.getBalanceInWeiAsync(zeroEx.address);
await zeroEx
.transferTrappedTokensTo(ETH_TOKEN_ADDRESS, constants.MAX_UINT256, recipientAddress)
.awaitTransactionSuccessAsync({ from: owner });
const recipientAddressBalanceAferTransfer = await env.web3Wrapper.getBalanceInWeiAsync(recipientAddress);
return expect(recipientAddressBalanceAferTransfer).to.bignumber.equal(balanceOwner);
});
it('Transfers ETH ', async () => {
const amountOut = new BigNumber(20);
await env.web3Wrapper.awaitTransactionMinedAsync(
await env.web3Wrapper.sendTransactionAsync({
from: owner,
to: zeroEx.address,
value: amountOut,
}),
);
await zeroEx
.transferTrappedTokensTo(ETH_TOKEN_ADDRESS, amountOut.minus(1), recipientAddress)
.awaitTransactionSuccessAsync({ from: owner });
const recipientAddressBalance = await env.web3Wrapper.getBalanceInWeiAsync(recipientAddress);
return expect(recipientAddressBalance).to.bignumber.be.equal(amountOut.minus(1));
});
it('Feature `transferTrappedTokensTo` can only be called by owner', async () => {
const notOwner = randomAddress();
return expect(
zeroEx
.transferTrappedTokensTo(ETH_TOKEN_ADDRESS, constants.MAX_UINT256, recipientAddress)
.awaitTransactionSuccessAsync({ from: notOwner }),
).to.revertWith(new OwnableRevertErrors.OnlyOwnerError(notOwner, owner));
});
});
});

View File

@@ -19,6 +19,7 @@ export * from '../test/generated-wrappers/fixin_reentrancy_guard';
export * from '../test/generated-wrappers/fixin_token_spender';
export * from '../test/generated-wrappers/flash_wallet';
export * from '../test/generated-wrappers/full_migration';
export * from '../test/generated-wrappers/fund_recovery_feature';
export * from '../test/generated-wrappers/i_batch_fill_native_orders_feature';
export * from '../test/generated-wrappers/i_bootstrap_feature';
export * from '../test/generated-wrappers/i_bridge_adapter';
@@ -26,6 +27,7 @@ export * from '../test/generated-wrappers/i_erc20_bridge';
export * from '../test/generated-wrappers/i_erc20_transformer';
export * from '../test/generated-wrappers/i_feature';
export * from '../test/generated-wrappers/i_flash_wallet';
export * from '../test/generated-wrappers/i_fund_recovery_feature';
export * from '../test/generated-wrappers/i_liquidity_provider';
export * from '../test/generated-wrappers/i_liquidity_provider_feature';
export * from '../test/generated-wrappers/i_liquidity_provider_sandbox';
@@ -80,7 +82,6 @@ export * from '../test/generated-wrappers/meta_transactions_feature';
export * from '../test/generated-wrappers/mixin_balancer';
export * from '../test/generated-wrappers/mixin_balancer_v2';
export * from '../test/generated-wrappers/mixin_bancor';
export * from '../test/generated-wrappers/mixin_clipper';
export * from '../test/generated-wrappers/mixin_co_fi_x';
export * from '../test/generated-wrappers/mixin_crypto_com';
export * from '../test/generated-wrappers/mixin_curve';

View File

@@ -52,6 +52,7 @@
"test/generated-artifacts/FixinTokenSpender.json",
"test/generated-artifacts/FlashWallet.json",
"test/generated-artifacts/FullMigration.json",
"test/generated-artifacts/FundRecoveryFeature.json",
"test/generated-artifacts/IBatchFillNativeOrdersFeature.json",
"test/generated-artifacts/IBootstrapFeature.json",
"test/generated-artifacts/IBridgeAdapter.json",
@@ -59,6 +60,7 @@
"test/generated-artifacts/IERC20Transformer.json",
"test/generated-artifacts/IFeature.json",
"test/generated-artifacts/IFlashWallet.json",
"test/generated-artifacts/IFundRecoveryFeature.json",
"test/generated-artifacts/ILiquidityProvider.json",
"test/generated-artifacts/ILiquidityProviderFeature.json",
"test/generated-artifacts/ILiquidityProviderSandbox.json",
@@ -113,7 +115,6 @@
"test/generated-artifacts/MixinBalancer.json",
"test/generated-artifacts/MixinBalancerV2.json",
"test/generated-artifacts/MixinBancor.json",
"test/generated-artifacts/MixinClipper.json",
"test/generated-artifacts/MixinCoFiX.json",
"test/generated-artifacts/MixinCryptoCom.json",
"test/generated-artifacts/MixinCurve.json",

View File

@@ -3,10 +3,14 @@ Protocol Fees
###############################
An ETH protocol fee is paid by the Taker each time a `Limit Order <./orders.html#limit-orders>`_ is `filled <./functions.html>`_.
The fee is proportional to the gas cost of filling an order and scales linearly with gas price. The cost is currently ``70k * tx.gasprice``.
The fee is proportional to the gas cost of filling an order and scales linearly with gas price. The cost is currently ``0 * tx.gasprice``.
At the end of every Staking Epoch, these fees are aggregated and distributed to the makers as a liquidity reward: the reward is proportional to the maker's collected fees and staked ZRX relative to other makers.
To learn more about protocol fees and liquidity incentives, see the `Official Spec <https://github.com/0xProject/0x-protocol-specification/blob/master/staking/staking-specification.md>`_.
.. note::
As of September 29, 2021, protocol fees have been removed for all order types in both Exchange V4 and V3 in accordance with `ZEIP-91 <https://0x.org/zrx/vote/zeip-91>`_.
.. note::
`RFQ Orders <./orders.html#rfq-orders>`_ are introduced in Exchange V4, and there is currently no protocol fee for filling this type of order.

View File

@@ -1,2 +1,3 @@
six
sphinx-markdown-tables
sphinx==3.5.4
sphinx-markdown-tables

View File

@@ -46,12 +46,12 @@
"test:generate_docs:circleci": "for i in ${npm_package_config_packagesWithDocPages}; do yarn generate_doc --package $i || break -1; done;",
"bundlewatch": "bundlewatch",
"lint": "wsrun --fast-exit --parallel --exclude-missing -p $PKG -c lint",
"upgrade_org_deps": "node node_modules/@0x/monorepo-scripts/lib/upgrade_deps.js -p '@0x|ethereum-types'",
"upgrade_org_deps": "node node_modules/@0x/monorepo-scripts/lib/upgrade_deps.js -p '@0x/|ethereum-types'",
"upgrade_deps": "node node_modules/@0x/monorepo-scripts/lib/upgrade_deps.js",
"verdaccio": "docker run --rm -i -p 4873:4873 0xorg/verdaccio"
},
"config": {
"contractsPackages": "@0x/contracts-erc20 @0x/contracts-test-utils @0x/contracts-utils @0x/contracts-zero-ex @0x/contracts-treasury",
"contractsPackages": "@0x/contracts-erc20 @0x/contracts-test-utils @0x/contracts-utils @0x/contracts-zero-ex @0x/contracts-treasury",
"nonContractPackages": "@0x/migrations @0x/contract-wrappers @0x/contract-addresses @0x/contract-artifacts @0x/contract-wrappers-test @0x/asset-swapper",
"ignoreTestsForPackages": "",
"mnemonic": "concert load couple harbor equip island argue ramp clarify fence smart topic",

View File

@@ -1,4 +1,114 @@
[
{
"version": "16.30.0",
"changes": [
{
"note": "Fantom deployment",
"pr": 347
}
],
"timestamp": 1634668033
},
{
"version": "16.29.3",
"changes": [
{
"note": "Update neon-router version and address breaking changes",
"pr": 344
}
],
"timestamp": 1634553393
},
{
"version": "16.29.2",
"changes": [
{
"note": "Check MAX_IN_RATIO in sampleBuysFromBalancer",
"pr": 338
},
{
"note": "Go back to using transformERC20 (instead of transformERC20Staging)",
"pr": 343
}
],
"timestamp": 1634147078
},
{
"version": "16.29.1",
"changes": [
{
"note": "Remove `Clipper` as a custom liquidity source",
"pr": 335
}
],
"timestamp": 1633374058
},
{
"version": "16.29.0",
"changes": [
{
"note": "Initial integration of neon-router (behind feature flag)",
"pr": 295
}
],
"timestamp": 1633350101
},
{
"version": "16.28.0",
"changes": [
{
"note": "Update ExchangeProxySwapQuoteConsumer for Multiplex V2 and friends",
"pr": 282
}
],
"timestamp": 1632957537
},
{
"version": "16.27.5",
"changes": [
{
"note": "Remove protocol fees by setting `PROTOCOL_FEE_MULTIPLIER` to 0",
"pr": 333
}
]
},
{
"timestamp": 1631710679,
"version": "16.27.4",
"changes": [
{
"note": "Dependencies updated"
}
]
},
{
"timestamp": 1631646242,
"version": "16.27.3",
"changes": [
{
"note": "Dependencies updated"
}
]
},
{
"timestamp": 1631639620,
"version": "16.27.2",
"changes": [
{
"note": "Dependencies updated"
}
]
},
{
"version": "16.27.1",
"changes": [
{
"note": "Fix ApproximateBuys sampler to terminate if the buy amount is not met",
"pr": 319
}
],
"timestamp": 1631120757
},
{
"version": "16.27.0",
"changes": [

View File

@@ -5,6 +5,51 @@ Edit the package's CHANGELOG.json file only.
CHANGELOG
## v16.30.0 - _October 19, 2021_
* Fantom deployment (#347)
## v16.29.3 - _October 18, 2021_
* Update neon-router version and address breaking changes (#344)
## v16.29.2 - _October 13, 2021_
* Check MAX_IN_RATIO in sampleBuysFromBalancer (#338)
* Go back to using transformERC20 (instead of transformERC20Staging) (#343)
## v16.29.1 - _October 4, 2021_
* Remove `Clipper` as a custom liquidity source (#335)
## v16.29.0 - _October 4, 2021_
* Initial integration of neon-router (behind feature flag) (#295)
## v16.28.0 - _September 29, 2021_
* Update ExchangeProxySwapQuoteConsumer for Multiplex V2 and friends (#282)
## v16.27.5 - _Invalid date_
* Remove protocol fees by setting `PROTOCOL_FEE_MULTIPLIER` to 0 (#333)
## v16.27.4 - _September 15, 2021_
* Dependencies updated
## v16.27.3 - _September 14, 2021_
* Dependencies updated
## v16.27.2 - _September 14, 2021_
* Dependencies updated
## v16.27.1 - _September 8, 2021_
* Fix ApproximateBuys sampler to terminate if the buy amount is not met (#319)
## v16.27.0 - _September 1, 2021_
* Avalanche deployment (#312)

View File

@@ -1,144 +0,0 @@
// SPDX-License-Identifier: Apache-2.0
/*
Copyright 2020 ZeroEx Intl.
Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at
http://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
*/
pragma solidity ^0.6;
pragma experimental ABIEncoderV2;
import "@0x/contracts-utils/contracts/src/v06/LibMathV06.sol";
contract ApproximateBuys {
/// @dev Information computing buy quotes for sources that do not have native
/// buy quote support.
struct ApproximateBuyQuoteOpts {
// Arbitrary maker token data to pass to `getSellQuoteCallback`.
bytes makerTokenData;
// Arbitrary taker token data to pass to `getSellQuoteCallback`.
bytes takerTokenData;
// Callback to retrieve a sell quote.
function (bytes memory, bytes memory, uint256)
internal
view
returns (uint256) getSellQuoteCallback;
}
uint256 private constant ONE_HUNDED_PERCENT_BPS = 1e4;
/// @dev Maximum approximate (positive) error rate when approximating a buy quote.
uint256 private constant APPROXIMATE_BUY_TARGET_EPSILON_BPS = 0.0005e4;
/// @dev Maximum iterations to perform when approximating a buy quote.
uint256 private constant APPROXIMATE_BUY_MAX_ITERATIONS = 5;
function _sampleApproximateBuys(
ApproximateBuyQuoteOpts memory opts,
uint256[] memory makerTokenAmounts
)
internal
view
returns (uint256[] memory takerTokenAmounts)
{
takerTokenAmounts = new uint256[](makerTokenAmounts.length);
if (makerTokenAmounts.length == 0) {
return takerTokenAmounts;
}
uint256 sellAmount = opts.getSellQuoteCallback(
opts.makerTokenData,
opts.takerTokenData,
makerTokenAmounts[0]
);
if (sellAmount == 0) {
return takerTokenAmounts;
}
uint256 buyAmount = opts.getSellQuoteCallback(
opts.takerTokenData,
opts.makerTokenData,
sellAmount
);
if (buyAmount == 0) {
return takerTokenAmounts;
}
for (uint256 i = 0; i < makerTokenAmounts.length; i++) {
for (uint256 iter = 0; iter < APPROXIMATE_BUY_MAX_ITERATIONS; iter++) {
// adjustedSellAmount = previousSellAmount * (target/actual) * JUMP_MULTIPLIER
sellAmount = _safeGetPartialAmountCeil(
makerTokenAmounts[i],
buyAmount,
sellAmount
);
if (sellAmount == 0) {
break;
}
sellAmount = _safeGetPartialAmountCeil(
(ONE_HUNDED_PERCENT_BPS + APPROXIMATE_BUY_TARGET_EPSILON_BPS),
ONE_HUNDED_PERCENT_BPS,
sellAmount
);
if (sellAmount == 0) {
break;
}
uint256 _buyAmount = opts.getSellQuoteCallback(
opts.takerTokenData,
opts.makerTokenData,
sellAmount
);
if (_buyAmount == 0) {
break;
}
// We re-use buyAmount next iteration, only assign if it is
// non zero
buyAmount = _buyAmount;
// If we've reached our goal, exit early
if (buyAmount >= makerTokenAmounts[i]) {
uint256 eps =
(buyAmount - makerTokenAmounts[i]) * ONE_HUNDED_PERCENT_BPS /
makerTokenAmounts[i];
if (eps <= APPROXIMATE_BUY_TARGET_EPSILON_BPS) {
break;
}
}
}
// We do our best to close in on the requested amount, but we can either over buy or under buy and exit
// if we hit a max iteration limit
// We scale the sell amount to get the approximate target
takerTokenAmounts[i] = _safeGetPartialAmountCeil(
makerTokenAmounts[i],
buyAmount,
sellAmount
);
}
}
function _safeGetPartialAmountCeil(
uint256 numerator,
uint256 denominator,
uint256 target
)
internal
view
returns (uint256 partialAmount)
{
if (numerator == 0 || target == 0 || denominator == 0) return 0;
uint256 c = numerator * target;
if (c / numerator != target) return 0;
return (c + (denominator - 1)) / denominator;
}
}

View File

@@ -1,191 +0,0 @@
// SPDX-License-Identifier: Apache-2.0
/*
Copyright 2020 ZeroEx Intl.
Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at
http://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
*/
pragma solidity ^0.6;
pragma experimental ABIEncoderV2;
import "./interfaces/IBalancer.sol";
contract BalancerSampler {
/// @dev Base gas limit for Balancer calls.
uint256 constant private BALANCER_CALL_GAS = 300e3; // 300k
// Balancer math constants
// https://github.com/balancer-labs/balancer-core/blob/master/contracts/BConst.sol
uint256 constant private BONE = 10 ** 18;
uint256 constant private MAX_IN_RATIO = BONE / 2;
uint256 constant private MAX_OUT_RATIO = (BONE / 3) + 1 wei;
struct BalancerState {
uint256 takerTokenBalance;
uint256 makerTokenBalance;
uint256 takerTokenWeight;
uint256 makerTokenWeight;
uint256 swapFee;
}
/// @dev Sample sell quotes from Balancer.
/// @param poolAddress Address of the Balancer pool to query.
/// @param takerToken Address of the taker token (what to sell).
/// @param makerToken Address of the maker token (what to buy).
/// @param takerTokenAmounts Taker token sell amount for each sample.
/// @return makerTokenAmounts Maker amounts bought at each taker token
/// amount.
function sampleSellsFromBalancer(
address poolAddress,
address takerToken,
address makerToken,
uint256[] memory takerTokenAmounts
)
public
view
returns (uint256[] memory makerTokenAmounts)
{
IBalancer pool = IBalancer(poolAddress);
uint256 numSamples = takerTokenAmounts.length;
makerTokenAmounts = new uint256[](numSamples);
if (!pool.isBound(takerToken) || !pool.isBound(makerToken)) {
return makerTokenAmounts;
}
BalancerState memory poolState;
poolState.takerTokenBalance = pool.getBalance(takerToken);
poolState.makerTokenBalance = pool.getBalance(makerToken);
poolState.takerTokenWeight = pool.getDenormalizedWeight(takerToken);
poolState.makerTokenWeight = pool.getDenormalizedWeight(makerToken);
poolState.swapFee = pool.getSwapFee();
for (uint256 i = 0; i < numSamples; i++) {
// Handles this revert scenario:
// https://github.com/balancer-labs/balancer-core/blob/master/contracts/BPool.sol#L443
if (takerTokenAmounts[i] > _bmul(poolState.takerTokenBalance, MAX_IN_RATIO)) {
break;
}
try
pool.calcOutGivenIn
{gas: BALANCER_CALL_GAS}
(
poolState.takerTokenBalance,
poolState.takerTokenWeight,
poolState.makerTokenBalance,
poolState.makerTokenWeight,
takerTokenAmounts[i],
poolState.swapFee
)
returns (uint256 amount)
{
makerTokenAmounts[i] = amount;
// Break early if there are 0 amounts
if (makerTokenAmounts[i] == 0) {
break;
}
} catch (bytes memory) {
// Swallow failures, leaving all results as zero.
break;
}
}
}
/// @dev Sample buy quotes from Balancer.
/// @param poolAddress Address of the Balancer pool to query.
/// @param takerToken Address of the taker token (what to sell).
/// @param makerToken Address of the maker token (what to buy).
/// @param makerTokenAmounts Maker token buy amount for each sample.
/// @return takerTokenAmounts Taker amounts sold at each maker token
/// amount.
function sampleBuysFromBalancer(
address poolAddress,
address takerToken,
address makerToken,
uint256[] memory makerTokenAmounts
)
public
view
returns (uint256[] memory takerTokenAmounts)
{
IBalancer pool = IBalancer(poolAddress);
uint256 numSamples = makerTokenAmounts.length;
takerTokenAmounts = new uint256[](numSamples);
if (!pool.isBound(takerToken) || !pool.isBound(makerToken)) {
return takerTokenAmounts;
}
BalancerState memory poolState;
poolState.takerTokenBalance = pool.getBalance(takerToken);
poolState.makerTokenBalance = pool.getBalance(makerToken);
poolState.takerTokenWeight = pool.getDenormalizedWeight(takerToken);
poolState.makerTokenWeight = pool.getDenormalizedWeight(makerToken);
poolState.swapFee = pool.getSwapFee();
for (uint256 i = 0; i < numSamples; i++) {
// Handles this revert scenario:
// https://github.com/balancer-labs/balancer-core/blob/master/contracts/BPool.sol#L505
if (makerTokenAmounts[i] > _bmul(poolState.makerTokenBalance, MAX_OUT_RATIO)) {
break;
}
try
pool.calcInGivenOut
{gas: BALANCER_CALL_GAS}
(
poolState.takerTokenBalance,
poolState.takerTokenWeight,
poolState.makerTokenBalance,
poolState.makerTokenWeight,
makerTokenAmounts[i],
poolState.swapFee
)
returns (uint256 amount)
{
takerTokenAmounts[i] = amount;
// Break early if there are 0 amounts
if (takerTokenAmounts[i] == 0) {
break;
}
} catch (bytes memory) {
// Swallow failures, leaving all results as zero.
break;
}
}
}
/// @dev Hacked version of Balancer's `bmul` function, returning 0 instead
/// of reverting.
/// https://github.com/balancer-labs/balancer-core/blob/master/contracts/BNum.sol#L63-L73
/// @param a The first operand.
/// @param b The second operand.
/// @param c The result of the multiplication, or 0 if `bmul` would've reverted.
function _bmul(uint256 a, uint256 b)
private
pure
returns (uint256 c)
{
uint c0 = a * b;
if (a != 0 && c0 / a != b) {
return 0;
}
uint c1 = c0 + (BONE / 2);
if (c1 < c0) {
return 0;
}
uint c2 = c1 / BONE;
return c2;
}
}

View File

@@ -1,189 +0,0 @@
// SPDX-License-Identifier: Apache-2.0
/*
Copyright 2021 ZeroEx Intl.
Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at
http://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
*/
pragma solidity ^0.6;
pragma experimental ABIEncoderV2;
import "./SamplerUtils.sol";
/// @dev Minimal Balancer V2 Vault interface
/// for documentation refer to https://github.com/balancer-labs/balancer-core-v2/blob/master/contracts/vault/interfaces/IVault.sol
interface IBalancerV2Vault {
enum SwapKind { GIVEN_IN, GIVEN_OUT }
struct BatchSwapStep {
bytes32 poolId;
uint256 assetInIndex;
uint256 assetOutIndex;
uint256 amount;
bytes userData;
}
struct FundManagement {
address sender;
bool fromInternalBalance;
address payable recipient;
bool toInternalBalance;
}
function queryBatchSwap(
SwapKind kind,
BatchSwapStep[] calldata swaps,
IAsset[] calldata assets,
FundManagement calldata funds
) external returns (int256[] memory assetDeltas);
}
interface IAsset {
// solhint-disable-previous-line no-empty-blocks
}
contract BalancerV2Sampler is SamplerUtils {
struct BalancerV2PoolInfo {
bytes32 poolId;
address vault;
}
/// @dev Sample sell quotes from Balancer V2.
/// @param poolInfo Struct with pool related data
/// @param takerToken Address of the taker token (what to sell).
/// @param makerToken Address of the maker token (what to buy).
/// @param takerTokenAmounts Taker token sell amount for each sample.
/// @return makerTokenAmounts Maker amounts bought at each taker token
/// amount.
function sampleSellsFromBalancerV2(
BalancerV2PoolInfo memory poolInfo,
address takerToken,
address makerToken,
uint256[] memory takerTokenAmounts
)
public
returns (uint256[] memory makerTokenAmounts)
{
_assertValidPair(makerToken, takerToken);
IBalancerV2Vault vault = IBalancerV2Vault(poolInfo.vault);
IAsset[] memory swapAssets = new IAsset[](2);
swapAssets[0] = IAsset(takerToken);
swapAssets[1] = IAsset(makerToken);
uint256 numSamples = takerTokenAmounts.length;
makerTokenAmounts = new uint256[](numSamples);
IBalancerV2Vault.FundManagement memory swapFunds =
_createSwapFunds();
for (uint256 i = 0; i < numSamples; i++) {
IBalancerV2Vault.BatchSwapStep[] memory swapSteps =
_createSwapSteps(poolInfo, takerTokenAmounts[i]);
try
// For sells we specify the takerToken which is what the vault will receive from the trade
vault.queryBatchSwap(IBalancerV2Vault.SwapKind.GIVEN_IN, swapSteps, swapAssets, swapFunds)
// amounts represent pool balance deltas from the swap (incoming balance, outgoing balance)
returns (int256[] memory amounts) {
// Outgoing balance is negative so we need to flip the sign
int256 amountOutFromPool = amounts[1] * -1;
if (amountOutFromPool <= 0) {
break;
}
makerTokenAmounts[i] = uint256(amountOutFromPool);
} catch (bytes memory) {
// Swallow failures, leaving all results as zero.
break;
}
}
}
/// @dev Sample buy quotes from Balancer V2.
/// @param poolInfo Struct with pool related data
/// @param takerToken Address of the taker token (what to sell).
/// @param makerToken Address of the maker token (what to buy).
/// @param makerTokenAmounts Maker token buy amount for each sample.
/// @return takerTokenAmounts Taker amounts sold at each maker token
/// amount.
function sampleBuysFromBalancerV2(
BalancerV2PoolInfo memory poolInfo,
address takerToken,
address makerToken,
uint256[] memory makerTokenAmounts
)
public
returns (uint256[] memory takerTokenAmounts)
{
_assertValidPair(makerToken, takerToken);
IBalancerV2Vault vault = IBalancerV2Vault(poolInfo.vault);
IAsset[] memory swapAssets = new IAsset[](2);
swapAssets[0] = IAsset(takerToken);
swapAssets[1] = IAsset(makerToken);
uint256 numSamples = makerTokenAmounts.length;
takerTokenAmounts = new uint256[](numSamples);
IBalancerV2Vault.FundManagement memory swapFunds =
_createSwapFunds();
for (uint256 i = 0; i < numSamples; i++) {
IBalancerV2Vault.BatchSwapStep[] memory swapSteps =
_createSwapSteps(poolInfo, makerTokenAmounts[i]);
try
// For buys we specify the makerToken which is what taker will receive from the trade
vault.queryBatchSwap(IBalancerV2Vault.SwapKind.GIVEN_OUT, swapSteps, swapAssets, swapFunds)
returns (int256[] memory amounts) {
int256 amountIntoPool = amounts[0];
if (amountIntoPool <= 0) {
break;
}
takerTokenAmounts[i] = uint256(amountIntoPool);
} catch (bytes memory) {
// Swallow failures, leaving all results as zero.
break;
}
}
}
function _createSwapSteps(
BalancerV2PoolInfo memory poolInfo,
uint256 amount
) private pure returns (IBalancerV2Vault.BatchSwapStep[] memory) {
IBalancerV2Vault.BatchSwapStep[] memory swapSteps =
new IBalancerV2Vault.BatchSwapStep[](1);
swapSteps[0] = IBalancerV2Vault.BatchSwapStep({
poolId: poolInfo.poolId,
assetInIndex: 0,
assetOutIndex: 1,
amount: amount,
userData: ""
});
return swapSteps;
}
function _createSwapFunds()
private
view
returns (IBalancerV2Vault.FundManagement memory)
{
return
IBalancerV2Vault.FundManagement({
sender: address(this),
fromInternalBalance: false,
recipient: payable(address(this)),
toInternalBalance: false
});
}
}

View File

@@ -1,142 +0,0 @@
// SPDX-License-Identifier: Apache-2.0
/*
Copyright 2020 ZeroEx Intl.
Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at
http://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
*/
pragma solidity ^0.6;
pragma experimental ABIEncoderV2;
import "./interfaces/IBancor.sol";
contract CompilerHack {}
contract BancorSampler is CompilerHack {
/// @dev Base gas limit for Bancor calls.
uint256 constant private BANCOR_CALL_GAS = 300e3; // 300k
struct BancorSamplerOpts {
IBancorRegistry registry;
address[][] paths;
}
/// @dev Sample sell quotes from Bancor.
/// @param opts BancorSamplerOpts The Bancor registry contract address and paths
/// @param takerToken Address of the taker token (what to sell).
/// @param makerToken Address of the maker token (what to buy).
/// @param takerTokenAmounts Taker token sell amount for each sample.
/// @return bancorNetwork the Bancor Network address
/// @return path the selected conversion path from bancor
/// @return makerTokenAmounts Maker amounts bought at each taker token
/// amount.
function sampleSellsFromBancor(
BancorSamplerOpts memory opts,
address takerToken,
address makerToken,
uint256[] memory takerTokenAmounts
)
public
view
returns (address bancorNetwork, address[] memory path, uint256[] memory makerTokenAmounts)
{
if (opts.paths.length == 0) {
return (bancorNetwork, path, makerTokenAmounts);
}
(bancorNetwork, path) = _findBestPath(opts, takerToken, makerToken, takerTokenAmounts);
makerTokenAmounts = new uint256[](takerTokenAmounts.length);
for (uint256 i = 0; i < makerTokenAmounts.length; i++) {
try
IBancorNetwork(bancorNetwork)
.rateByPath
{gas: BANCOR_CALL_GAS}
(path, takerTokenAmounts[i])
returns (uint256 amount)
{
makerTokenAmounts[i] = amount;
// Break early if there are 0 amounts
if (makerTokenAmounts[i] == 0) {
break;
}
} catch {
// Swallow failures, leaving all results as zero.
break;
}
}
return (bancorNetwork, path, makerTokenAmounts);
}
/// @dev Sample buy quotes from Bancor. Unimplemented
/// @param opts BancorSamplerOpts The Bancor registry contract address and paths
/// @param takerToken Address of the taker token (what to sell).
/// @param makerToken Address of the maker token (what to buy).
/// @param makerTokenAmounts Maker token buy amount for each sample.
/// @return bancorNetwork the Bancor Network address
/// @return path the selected conversion path from bancor
/// @return takerTokenAmounts Taker amounts sold at each maker token
/// amount.
function sampleBuysFromBancor(
BancorSamplerOpts memory opts,
address takerToken,
address makerToken,
uint256[] memory makerTokenAmounts
)
public
view
returns (address bancorNetwork, address[] memory path, uint256[] memory takerTokenAmounts)
{
}
function _findBestPath(
BancorSamplerOpts memory opts,
address takerToken,
address makerToken,
uint256[] memory takerTokenAmounts
)
internal
view
returns (address bancorNetwork, address[] memory path)
{
bancorNetwork = opts.registry.getAddress(opts.registry.BANCOR_NETWORK());
if (opts.paths.length == 0) {
return (bancorNetwork, path);
}
uint256 maxBoughtAmount = 0;
// Find the best path by selling the largest taker amount
for (uint256 i = 0; i < opts.paths.length; i++) {
if (opts.paths[i].length < 2) {
continue;
}
try
IBancorNetwork(bancorNetwork)
.rateByPath
{gas: BANCOR_CALL_GAS}
(opts.paths[i], takerTokenAmounts[takerTokenAmounts.length-1])
returns (uint256 amount)
{
if (amount > maxBoughtAmount) {
maxBoughtAmount = amount;
path = opts.paths[i];
}
} catch {
// Swallow failures, leaving all results as zero.
continue;
}
}
}
}

View File

@@ -1,161 +0,0 @@
// SPDX-License-Identifier: Apache-2.0
/*
Copyright 2020 ZeroEx Intl.
Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at
http://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
*/
pragma solidity ^0.6;
pragma experimental ABIEncoderV2;
import "./interfaces/ICurve.sol";
import "./ApproximateBuys.sol";
import "./SamplerUtils.sol";
contract CurveSampler is
SamplerUtils,
ApproximateBuys
{
/// @dev Information for sampling from curve sources.
struct CurveInfo {
address poolAddress;
bytes4 sellQuoteFunctionSelector;
bytes4 buyQuoteFunctionSelector;
}
/// @dev Base gas limit for Curve calls. Some Curves have multiple tokens
/// So a reasonable ceil is 150k per token. Biggest Curve has 4 tokens.
uint256 constant private CURVE_CALL_GAS = 2000e3; // Was 600k for Curve but SnowSwap is using 1500k+
/// @dev Sample sell quotes from Curve.
/// @param curveInfo Curve information specific to this token pair.
/// @param fromTokenIdx Index of the taker token (what to sell).
/// @param toTokenIdx Index of the maker token (what to buy).
/// @param takerTokenAmounts Taker token sell amount for each sample.
/// @return makerTokenAmounts Maker amounts bought at each taker token
/// amount.
function sampleSellsFromCurve(
CurveInfo memory curveInfo,
int128 fromTokenIdx,
int128 toTokenIdx,
uint256[] memory takerTokenAmounts
)
public
view
returns (uint256[] memory makerTokenAmounts)
{
uint256 numSamples = takerTokenAmounts.length;
makerTokenAmounts = new uint256[](numSamples);
for (uint256 i = 0; i < numSamples; i++) {
(bool didSucceed, bytes memory resultData) =
curveInfo.poolAddress.staticcall.gas(CURVE_CALL_GAS)(
abi.encodeWithSelector(
curveInfo.sellQuoteFunctionSelector,
fromTokenIdx,
toTokenIdx,
takerTokenAmounts[i]
));
uint256 buyAmount = 0;
if (didSucceed) {
buyAmount = abi.decode(resultData, (uint256));
}
makerTokenAmounts[i] = buyAmount;
// Break early if there are 0 amounts
if (makerTokenAmounts[i] == 0) {
break;
}
}
}
/// @dev Sample buy quotes from Curve.
/// @param curveInfo Curve information specific to this token pair.
/// @param fromTokenIdx Index of the taker token (what to sell).
/// @param toTokenIdx Index of the maker token (what to buy).
/// @param makerTokenAmounts Maker token buy amount for each sample.
/// @return takerTokenAmounts Taker amounts sold at each maker token
/// amount.
function sampleBuysFromCurve(
CurveInfo memory curveInfo,
int128 fromTokenIdx,
int128 toTokenIdx,
uint256[] memory makerTokenAmounts
)
public
view
returns (uint256[] memory takerTokenAmounts)
{
if (curveInfo.buyQuoteFunctionSelector == bytes4(0)) {
// Buys not supported on this curve, so approximate it.
return _sampleApproximateBuys(
ApproximateBuyQuoteOpts({
makerTokenData: abi.encode(toTokenIdx, curveInfo),
takerTokenData: abi.encode(fromTokenIdx, curveInfo),
getSellQuoteCallback: _sampleSellForApproximateBuyFromCurve
}),
makerTokenAmounts
);
}
uint256 numSamples = makerTokenAmounts.length;
takerTokenAmounts = new uint256[](numSamples);
for (uint256 i = 0; i < numSamples; i++) {
(bool didSucceed, bytes memory resultData) =
curveInfo.poolAddress.staticcall.gas(CURVE_CALL_GAS)(
abi.encodeWithSelector(
curveInfo.buyQuoteFunctionSelector,
fromTokenIdx,
toTokenIdx,
makerTokenAmounts[i]
));
uint256 sellAmount = 0;
if (didSucceed) {
sellAmount = abi.decode(resultData, (uint256));
}
takerTokenAmounts[i] = sellAmount;
// Break early if there are 0 amounts
if (takerTokenAmounts[i] == 0) {
break;
}
}
}
function _sampleSellForApproximateBuyFromCurve(
bytes memory takerTokenData,
bytes memory makerTokenData,
uint256 sellAmount
)
private
view
returns (uint256 buyAmount)
{
(int128 takerTokenIdx, CurveInfo memory curveInfo) =
abi.decode(takerTokenData, (int128, CurveInfo));
(int128 makerTokenIdx) =
abi.decode(makerTokenData, (int128));
(bool success, bytes memory resultData) =
address(this).staticcall(abi.encodeWithSelector(
this.sampleSellsFromCurve.selector,
curveInfo,
takerTokenIdx,
makerTokenIdx,
_toSingleValueArray(sellAmount)
));
if (!success) {
return 0;
}
// solhint-disable-next-line indent
return abi.decode(resultData, (uint256[]))[0];
}
}

View File

@@ -1,211 +0,0 @@
// SPDX-License-Identifier: Apache-2.0
/*
Copyright 2020 ZeroEx Intl.
Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at
http://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
*/
pragma solidity ^0.6;
pragma experimental ABIEncoderV2;
import "./ApproximateBuys.sol";
import "./SamplerUtils.sol";
interface IDODOZoo {
function getDODO(address baseToken, address quoteToken) external view returns (address);
}
interface IDODOHelper {
function querySellQuoteToken(address dodo, uint256 amount) external view returns (uint256);
}
interface IDODO {
function querySellBaseToken(uint256 amount) external view returns (uint256);
function _TRADE_ALLOWED_() external view returns (bool);
}
contract DODOSampler is
SamplerUtils,
ApproximateBuys
{
/// @dev Gas limit for DODO calls.
uint256 constant private DODO_CALL_GAS = 300e3; // 300k
struct DODOSamplerOpts {
address registry;
address helper;
}
/// @dev Sample sell quotes from DODO.
/// @param opts DODOSamplerOpts DODO Registry and helper addresses
/// @param takerToken Address of the taker token (what to sell).
/// @param makerToken Address of the maker token (what to buy).
/// @param takerTokenAmounts Taker token sell amount for each sample.
/// @return sellBase whether the bridge needs to sell the base token
/// @return pool the DODO pool address
/// @return makerTokenAmounts Maker amounts bought at each taker token
/// amount.
function sampleSellsFromDODO(
DODOSamplerOpts memory opts,
address takerToken,
address makerToken,
uint256[] memory takerTokenAmounts
)
public
view
returns (bool sellBase, address pool, uint256[] memory makerTokenAmounts)
{
_assertValidPair(makerToken, takerToken);
uint256 numSamples = takerTokenAmounts.length;
makerTokenAmounts = new uint256[](numSamples);
pool = IDODOZoo(opts.registry).getDODO(takerToken, makerToken);
address baseToken;
// If pool exists we have the correct order of Base/Quote
if (pool != address(0)) {
baseToken = takerToken;
sellBase = true;
} else {
pool = IDODOZoo(opts.registry).getDODO(makerToken, takerToken);
// No pool either direction
if (address(pool) == address(0)) {
return (sellBase, pool, makerTokenAmounts);
}
baseToken = makerToken;
sellBase = false;
}
// DODO Pool has been disabled
if (!IDODO(pool)._TRADE_ALLOWED_()) {
return (sellBase, pool, makerTokenAmounts);
}
for (uint256 i = 0; i < numSamples; i++) {
uint256 buyAmount = _sampleSellForApproximateBuyFromDODO(
abi.encode(takerToken, pool, baseToken, opts.helper), // taker token data
abi.encode(makerToken, pool, baseToken, opts.helper), // maker token data
takerTokenAmounts[i]
);
makerTokenAmounts[i] = buyAmount;
// Break early if there are 0 amounts
if (makerTokenAmounts[i] == 0) {
break;
}
}
}
/// @dev Sample buy quotes from DODO.
/// @param opts DODOSamplerOpts DODO Registry and helper addresses
/// @param takerToken Address of the taker token (what to sell).
/// @param makerToken Address of the maker token (what to buy).
/// @param makerTokenAmounts Maker token sell amount for each sample.
/// @return sellBase whether the bridge needs to sell the base token
/// @return pool the DODO pool address
/// @return takerTokenAmounts Taker amounts sold at each maker token
/// amount.
function sampleBuysFromDODO(
DODOSamplerOpts memory opts,
address takerToken,
address makerToken,
uint256[] memory makerTokenAmounts
)
public
view
returns (bool sellBase, address pool, uint256[] memory takerTokenAmounts)
{
_assertValidPair(makerToken, takerToken);
uint256 numSamples = makerTokenAmounts.length;
takerTokenAmounts = new uint256[](numSamples);
// Pool is BASE/QUOTE
// Look up the pool from the taker/maker combination
pool = IDODOZoo(opts.registry).getDODO(takerToken, makerToken);
address baseToken;
// If pool exists we have the correct order of Base/Quote
if (pool != address(0)) {
baseToken = takerToken;
sellBase = true;
} else {
// Look up the pool from the maker/taker combination
pool = IDODOZoo(opts.registry).getDODO(makerToken, takerToken);
// No pool either direction
if (address(pool) == address(0)) {
return (sellBase, pool, takerTokenAmounts);
}
baseToken = makerToken;
sellBase = false;
}
// DODO Pool has been disabled
if (!IDODO(pool)._TRADE_ALLOWED_()) {
return (sellBase, pool, takerTokenAmounts);
}
takerTokenAmounts = _sampleApproximateBuys(
ApproximateBuyQuoteOpts({
makerTokenData: abi.encode(makerToken, pool, baseToken, opts.helper),
takerTokenData: abi.encode(takerToken, pool, baseToken, opts.helper),
getSellQuoteCallback: _sampleSellForApproximateBuyFromDODO
}),
makerTokenAmounts
);
}
function _sampleSellForApproximateBuyFromDODO(
bytes memory takerTokenData,
bytes memory /* makerTokenData */,
uint256 sellAmount
)
private
view
returns (uint256)
{
(address takerToken, address pool, address baseToken, address helper) = abi.decode(
takerTokenData,
(address, address, address, address)
);
// We will get called to sell both the taker token and also to sell the maker token
if (takerToken == baseToken) {
// If base token then use the original query on the pool
try
IDODO(pool).querySellBaseToken
{gas: DODO_CALL_GAS}
(sellAmount)
returns (uint256 amount)
{
return amount;
} catch (bytes memory) {
// Swallow failures, leaving all results as zero.
return 0;
}
} else {
// If quote token then use helper, this is less accurate
try
IDODOHelper(helper).querySellQuoteToken
{gas: DODO_CALL_GAS}
(pool, sellAmount)
returns (uint256 amount)
{
return amount;
} catch (bytes memory) {
// Swallow failures, leaving all results as zero.
return 0;
}
}
}
}

View File

@@ -1,204 +0,0 @@
// SPDX-License-Identifier: Apache-2.0
/*
Copyright 2021 ZeroEx Intl.
Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at
http://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
*/
pragma solidity ^0.6;
pragma experimental ABIEncoderV2;
import "./ApproximateBuys.sol";
import "./SamplerUtils.sol";
interface IDODOV2Registry {
function getDODOPool(address baseToken, address quoteToken)
external
view
returns (address[] memory machines);
}
interface IDODOV2Pool {
function querySellBase(address trader, uint256 payBaseAmount)
external
view
returns (uint256 receiveQuoteAmount, uint256 mtFee);
function querySellQuote(address trader, uint256 payQuoteAmount)
external
view
returns (uint256 receiveBaseAmount, uint256 mtFee);
}
contract DODOV2Sampler is
SamplerUtils,
ApproximateBuys
{
/// @dev Gas limit for DODO V2 calls.
uint256 constant private DODO_V2_CALL_GAS = 300e3; // 300k
/// @dev Sample sell quotes from DODO V2.
/// @param registry Address of the registry to look up.
/// @param offset offset index for the pool in the registry.
/// @param takerToken Address of the taker token (what to sell).
/// @param makerToken Address of the maker token (what to buy).
/// @param takerTokenAmounts Taker token sell amount for each sample.
/// @return sellBase whether the bridge needs to sell the base token
/// @return pool the DODO pool address
/// @return makerTokenAmounts Maker amounts bought at each taker token
/// amount.
function sampleSellsFromDODOV2(
address registry,
uint256 offset,
address takerToken,
address makerToken,
uint256[] memory takerTokenAmounts
)
public
view
returns (bool sellBase, address pool, uint256[] memory makerTokenAmounts)
{
_assertValidPair(makerToken, takerToken);
uint256 numSamples = takerTokenAmounts.length;
makerTokenAmounts = new uint256[](numSamples);
(pool, sellBase) = _getNextDODOV2Pool(registry, offset, takerToken, makerToken);
if (pool == address(0)) {
return (sellBase, pool, makerTokenAmounts);
}
for (uint256 i = 0; i < numSamples; i++) {
uint256 buyAmount = _sampleSellForApproximateBuyFromDODOV2(
abi.encode(takerToken, pool, sellBase), // taker token data
abi.encode(makerToken, pool, sellBase), // maker token data
takerTokenAmounts[i]
);
makerTokenAmounts[i] = buyAmount;
// Break early if there are 0 amounts
if (makerTokenAmounts[i] == 0) {
break;
}
}
}
/// @dev Sample buy quotes from DODO.
/// @param registry Address of the registry to look up.
/// @param offset offset index for the pool in the registry.
/// @param takerToken Address of the taker token (what to sell).
/// @param makerToken Address of the maker token (what to buy).
/// @param makerTokenAmounts Maker token sell amount for each sample.
/// @return sellBase whether the bridge needs to sell the base token
/// @return pool the DODO pool address
/// @return takerTokenAmounts Taker amounts sold at each maker token
/// amount.
function sampleBuysFromDODOV2(
address registry,
uint256 offset,
address takerToken,
address makerToken,
uint256[] memory makerTokenAmounts
)
public
view
returns (bool sellBase, address pool, uint256[] memory takerTokenAmounts)
{
_assertValidPair(makerToken, takerToken);
(pool, sellBase) = _getNextDODOV2Pool(registry, offset, takerToken, makerToken);
if (pool == address(0)) {
return (sellBase, pool, takerTokenAmounts);
}
uint256 numSamples = makerTokenAmounts.length;
takerTokenAmounts = new uint256[](numSamples);
takerTokenAmounts = _sampleApproximateBuys(
ApproximateBuyQuoteOpts({
makerTokenData: abi.encode(makerToken, pool, !sellBase),
takerTokenData: abi.encode(takerToken, pool, sellBase),
getSellQuoteCallback: _sampleSellForApproximateBuyFromDODOV2
}),
makerTokenAmounts
);
}
function _sampleSellForApproximateBuyFromDODOV2(
bytes memory takerTokenData,
bytes memory /* makerTokenData */,
uint256 sellAmount
)
private
view
returns (uint256)
{
(address takerToken, address pool, bool sellBase) = abi.decode(
takerTokenData,
(address, address, bool)
);
// We will get called to sell both the taker token and also to sell the maker token
// since we use approximate buy for sell and buy functions
if (sellBase) {
try
IDODOV2Pool(pool).querySellBase
{ gas: DODO_V2_CALL_GAS }
(address(0), sellAmount)
returns (uint256 amount, uint256)
{
return amount;
} catch {
return 0;
}
} else {
try
IDODOV2Pool(pool).querySellQuote
{ gas: DODO_V2_CALL_GAS }
(address(0), sellAmount)
returns (uint256 amount, uint256)
{
return amount;
} catch {
return 0;
}
}
}
function _getNextDODOV2Pool(
address registry,
uint256 offset,
address takerToken,
address makerToken
)
internal
view
returns (address machine, bool sellBase)
{
// Query in base -> quote direction, if a pool is found then we are selling the base
address[] memory machines = IDODOV2Registry(registry).getDODOPool(takerToken, makerToken);
sellBase = true;
if (machines.length == 0) {
// Query in quote -> base direction, if a pool is found then we are selling the quote
machines = IDODOV2Registry(registry).getDODOPool(makerToken, takerToken);
sellBase = false;
}
if (offset >= machines.length) {
return (address(0), false);
}
machine = machines[offset];
}
}

View File

@@ -1,93 +0,0 @@
// SPDX-License-Identifier: Apache-2.0
/*
Copyright 2020 ZeroEx Intl.
Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at
http://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
*/
pragma solidity ^0.6;
pragma experimental ABIEncoderV2;
import "./BalancerSampler.sol";
import "./BalancerV2Sampler.sol";
import "./BancorSampler.sol";
import "./CurveSampler.sol";
import "./DODOSampler.sol";
import "./DODOV2Sampler.sol";
import "./KyberSampler.sol";
import "./KyberDmmSampler.sol";
import "./LidoSampler.sol";
import "./LiquidityProviderSampler.sol";
import "./MakerPSMSampler.sol";
import "./MultiBridgeSampler.sol";
import "./MStableSampler.sol";
import "./MooniswapSampler.sol";
import "./NativeOrderSampler.sol";
import "./ShellSampler.sol";
import "./SmoothySampler.sol";
import "./TwoHopSampler.sol";
import "./UniswapSampler.sol";
import "./UniswapV2Sampler.sol";
import "./UniswapV3Sampler.sol";
import "./UtilitySampler.sol";
contract ERC20BridgeSampler is
BalancerSampler,
BalancerV2Sampler,
BancorSampler,
CurveSampler,
DODOSampler,
DODOV2Sampler,
KyberSampler,
KyberDmmSampler,
LidoSampler,
LiquidityProviderSampler,
MakerPSMSampler,
MStableSampler,
MooniswapSampler,
MultiBridgeSampler,
NativeOrderSampler,
ShellSampler,
SmoothySampler,
TwoHopSampler,
UniswapSampler,
UniswapV2Sampler,
UniswapV3Sampler,
UtilitySampler
{
struct CallResults {
bytes data;
bool success;
}
/// @dev Call multiple public functions on this contract in a single transaction.
/// @param callDatas ABI-encoded call data for each function call.
/// @return callResults ABI-encoded results data for each call.
function batchCall(bytes[] calldata callDatas)
external
returns (CallResults[] memory callResults)
{
callResults = new CallResults[](callDatas.length);
for (uint256 i = 0; i != callDatas.length; ++i) {
callResults[i].success = true;
if (callDatas[i].length == 0) {
continue;
}
(callResults[i].success, callResults[i].data) = address(this).call(callDatas[i]);
}
}
}

View File

@@ -1,176 +0,0 @@
// SPDX-License-Identifier: Apache-2.0
/*
Copyright 2020 ZeroEx Intl.
Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at
http://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
*/
pragma solidity ^0.6;
pragma experimental ABIEncoderV2;
interface IKyberDmmPool {
function totalSupply()
external
view
returns (uint256);
}
interface IKyberDmmFactory {
function getPools(address token0, address token1)
external
view
returns (address[] memory _tokenPools);
}
interface IKyberDmmRouter {
function factory() external view returns (address);
function getAmountsOut(uint256 amountIn, address[] calldata pools, address[] calldata path)
external
view
returns (uint256[] memory amounts);
function getAmountsIn(uint256 amountOut, address[] calldata pools, address[] calldata path)
external
view
returns (uint256[] memory amounts);
}
contract KyberDmmSampler
{
/// @dev Gas limit for KyberDmm calls.
uint256 constant private KYBER_DMM_CALL_GAS = 150e3; // 150k
/// @dev Sample sell quotes from KyberDmm.
/// @param router Router to look up tokens and amounts
/// @param path Token route. Should be takerToken -> makerToken
/// @param takerTokenAmounts Taker token sell amount for each sample.
/// @return pools The pool addresses involved in the multi path trade
/// @return makerTokenAmounts Maker amounts bought at each taker token
/// amount.
function sampleSellsFromKyberDmm(
address router,
address[] memory path,
uint256[] memory takerTokenAmounts
)
public
view
returns (address[] memory pools, uint256[] memory makerTokenAmounts)
{
uint256 numSamples = takerTokenAmounts.length;
makerTokenAmounts = new uint256[](numSamples);
pools = _getKyberDmmPools(router, path);
if (pools.length == 0) {
return (pools, makerTokenAmounts);
}
for (uint256 i = 0; i < numSamples; i++) {
try
IKyberDmmRouter(router).getAmountsOut
{gas: KYBER_DMM_CALL_GAS}
(takerTokenAmounts[i], pools, path)
returns (uint256[] memory amounts)
{
makerTokenAmounts[i] = amounts[path.length - 1];
// Break early if there are 0 amounts
if (makerTokenAmounts[i] == 0) {
break;
}
} catch (bytes memory) {
// Swallow failures, leaving all results as zero.
break;
}
}
}
/// @dev Sample buy quotes from KyberDmm.
/// @param router Router to look up tokens and amounts
/// @param path Token route. Should be takerToken -> makerToken.
/// @param makerTokenAmounts Maker token buy amount for each sample.
/// @return pools The pool addresses involved in the multi path trade
/// @return takerTokenAmounts Taker amounts sold at each maker token
/// amount.
function sampleBuysFromKyberDmm(
address router,
address[] memory path,
uint256[] memory makerTokenAmounts
)
public
view
returns (address[] memory pools, uint256[] memory takerTokenAmounts)
{
uint256 numSamples = makerTokenAmounts.length;
takerTokenAmounts = new uint256[](numSamples);
pools = _getKyberDmmPools(router, path);
if (pools.length == 0) {
return (pools, takerTokenAmounts);
}
for (uint256 i = 0; i < numSamples; i++) {
try
IKyberDmmRouter(router).getAmountsIn
{gas: KYBER_DMM_CALL_GAS}
(makerTokenAmounts[i], pools, path)
returns (uint256[] memory amounts)
{
takerTokenAmounts[i] = amounts[0];
// Break early if there are 0 amounts
if (takerTokenAmounts[i] == 0) {
break;
}
} catch (bytes memory) {
// Swallow failures, leaving all results as zero.
break;
}
}
}
function _getKyberDmmPools(
address router,
address[] memory path
)
private
view
returns (address[] memory pools)
{
IKyberDmmFactory factory = IKyberDmmFactory(IKyberDmmRouter(router).factory());
pools = new address[](path.length - 1);
for (uint256 i = 0; i < pools.length; i++) {
// find the best pool
address[] memory allPools;
try
factory.getPools
{gas: KYBER_DMM_CALL_GAS}
(path[i], path[i + 1])
returns (address[] memory allPools)
{
uint256 maxSupply = 0;
require(allPools.length >= 1, "KyberDMMSampler/NO_POOLS_FOUND");
for (uint256 j = 0; j < allPools.length; j++) {
uint256 totalSupply = IKyberDmmPool(allPools[j]).totalSupply();
if (totalSupply > maxSupply) {
maxSupply = totalSupply;
pools[i] = allPools[j];
}
}
} catch (bytes memory) {
return new address[](0);
}
}
}
}

View File

@@ -1,301 +0,0 @@
// SPDX-License-Identifier: Apache-2.0
/*
Copyright 2020 ZeroEx Intl.
Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at
http://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
*/
pragma solidity ^0.6;
pragma experimental ABIEncoderV2;
import "./interfaces/IKyberNetwork.sol";
import "./ApproximateBuys.sol";
import "./SamplerUtils.sol";
contract KyberSampler is
SamplerUtils,
ApproximateBuys
{
/// @dev Gas limit for Kyber calls.
uint256 constant private KYBER_CALL_GAS = 500e3; // 500k
/// @dev Kyber ETH pseudo-address.
address constant internal KYBER_ETH_ADDRESS = 0xEeeeeEeeeEeEeeEeEeEeeEEEeeeeEeeeeeeeEEeE;
struct KyberSamplerOpts {
uint256 reserveOffset;
address hintHandler;
address networkProxy;
address weth;
bytes hint;
}
/// @dev Sample sell quotes from Kyber.
/// @param opts KyberSamplerOpts The nth reserve
/// @param takerToken Address of the taker token (what to sell).
/// @param makerToken Address of the maker token (what to buy).
/// @param takerTokenAmounts Taker token sell amount for each sample.
/// @return reserveId The id of the reserve found at reserveOffset
/// @return hint The hint for the selected reserve
/// @return makerTokenAmounts Maker amounts bought at each taker token amount.
function sampleSellsFromKyberNetwork(
KyberSamplerOpts memory opts,
address takerToken,
address makerToken,
uint256[] memory takerTokenAmounts
)
public
view
returns (bytes32 reserveId, bytes memory hint, uint256[] memory makerTokenAmounts)
{
_assertValidPair(makerToken, takerToken);
reserveId = _getNextReserveId(opts, takerToken, makerToken);
if (reserveId == 0x0) {
return (reserveId, hint, makerTokenAmounts);
}
opts.hint = this.encodeKyberHint(opts, reserveId, takerToken, makerToken);
hint = opts.hint;
uint256 numSamples = takerTokenAmounts.length;
makerTokenAmounts = new uint256[](numSamples);
for (uint256 i = 0; i < numSamples; i++) {
uint256 value = this.sampleSellFromKyberNetwork(
opts,
takerToken,
makerToken,
takerTokenAmounts[i]
);
makerTokenAmounts[i] = value;
// Break early if there are 0 amounts
if (makerTokenAmounts[i] == 0) {
break;
}
}
}
/// @dev Sample buy quotes from Kyber.
/// @param opts KyberSamplerOpts The nth reserve
/// @param takerToken Address of the taker token (what to sell).
/// @param makerToken Address of the maker token (what to buy).
/// @param makerTokenAmounts Maker token buy amount for each sample.
/// @return reserveId The id of the reserve found at reserveOffset
/// @return hint The hint for the selected reserve
/// @return takerTokenAmounts Taker amounts sold at each maker token amount.
function sampleBuysFromKyberNetwork(
KyberSamplerOpts memory opts,
address takerToken,
address makerToken,
uint256[] memory makerTokenAmounts
)
public
view
returns (bytes32 reserveId, bytes memory hint, uint256[] memory takerTokenAmounts)
{
_assertValidPair(makerToken, takerToken);
reserveId = _getNextReserveId(opts, takerToken, makerToken);
if (reserveId == 0x0) {
return (reserveId, hint, takerTokenAmounts);
}
opts.hint = this.encodeKyberHint(opts, reserveId, takerToken, makerToken);
hint = opts.hint;
takerTokenAmounts = _sampleApproximateBuys(
ApproximateBuyQuoteOpts({
makerTokenData: abi.encode(makerToken, opts),
takerTokenData: abi.encode(takerToken, opts),
getSellQuoteCallback: _sampleSellForApproximateBuyFromKyber
}),
makerTokenAmounts
);
return (reserveId, hint, takerTokenAmounts);
}
function encodeKyberHint(
KyberSamplerOpts memory opts,
bytes32 reserveId,
address takerToken,
address makerToken
)
public
view
returns (bytes memory hint)
{
// Build a hint selecting the single reserve
IKyberHintHandler kyberHint = IKyberHintHandler(opts.hintHandler);
// All other reserves should be ignored with this hint
bytes32[] memory selectedReserves = new bytes32[](1);
selectedReserves[0] = reserveId;
uint256[] memory emptySplits = new uint256[](0);
if (takerToken == opts.weth) {
// ETH to Token
try
kyberHint.buildEthToTokenHint
{gas: KYBER_CALL_GAS}
(
makerToken,
IKyberHintHandler.TradeType.MaskIn,
selectedReserves,
emptySplits
)
returns (bytes memory result)
{
return result;
} catch (bytes memory) {
// Swallow failures, leaving all results as zero.
}
} else if (makerToken == opts.weth) {
// Token to ETH
try
kyberHint.buildTokenToEthHint
{gas: KYBER_CALL_GAS}
(
takerToken,
IKyberHintHandler.TradeType.MaskIn,
selectedReserves,
emptySplits
)
returns (bytes memory result)
{
return result;
} catch (bytes memory) {
// Swallow failures, leaving all results as zero.
}
} else {
// Token to Token
// We use the same reserve both ways
try
kyberHint.buildTokenToTokenHint
{gas: KYBER_CALL_GAS}
(
takerToken,
IKyberHintHandler.TradeType.MaskIn,
selectedReserves,
emptySplits,
makerToken,
IKyberHintHandler.TradeType.MaskIn,
selectedReserves,
emptySplits
)
returns (bytes memory result)
{
return result;
} catch (bytes memory) {
// Swallow failures, leaving all results as zero.
}
}
}
function _sampleSellForApproximateBuyFromKyber(
bytes memory takerTokenData,
bytes memory makerTokenData,
uint256 sellAmount
)
private
view
returns (uint256)
{
(address makerToken, KyberSamplerOpts memory opts) =
abi.decode(makerTokenData, (address, KyberSamplerOpts));
(address takerToken, ) =
abi.decode(takerTokenData, (address, KyberSamplerOpts));
try
this.sampleSellFromKyberNetwork
(opts, takerToken, makerToken, sellAmount)
returns (uint256 amount)
{
return amount;
} catch (bytes memory) {
// Swallow failures, leaving all results as zero.
return 0;
}
}
function sampleSellFromKyberNetwork(
KyberSamplerOpts memory opts,
address takerToken,
address makerToken,
uint256 takerTokenAmount
)
public
view
returns (uint256 makerTokenAmount)
{
// If there is no hint do not continue
if (opts.hint.length == 0) {
return 0;
}
try
IKyberNetworkProxy(opts.networkProxy).getExpectedRateAfterFee
{gas: KYBER_CALL_GAS}
(
takerToken == opts.weth ? KYBER_ETH_ADDRESS : takerToken,
makerToken == opts.weth ? KYBER_ETH_ADDRESS : makerToken,
takerTokenAmount,
0, // fee
opts.hint
)
returns (uint256 rate)
{
uint256 makerTokenDecimals = _getTokenDecimals(makerToken);
uint256 takerTokenDecimals = _getTokenDecimals(takerToken);
makerTokenAmount =
rate *
takerTokenAmount *
10 ** makerTokenDecimals /
10 ** takerTokenDecimals /
10 ** 18;
return makerTokenAmount;
} catch (bytes memory) {
// Swallow failures, leaving all results as zero.
return 0;
}
}
function _getNextReserveId(
KyberSamplerOpts memory opts,
address takerToken,
address makerToken
)
internal
view
returns (bytes32 reserveId)
{
// Fetch the registered reserves for this pair
IKyberHintHandler kyberHint = IKyberHintHandler(opts.hintHandler);
(bytes32[] memory reserveIds, ,) = kyberHint.getTradingReserves(
takerToken == opts.weth ? KYBER_ETH_ADDRESS : takerToken,
makerToken == opts.weth ? KYBER_ETH_ADDRESS : makerToken,
true,
new bytes(0) // empty hint
);
if (opts.reserveOffset >= reserveIds.length) {
return 0x0;
}
reserveId = reserveIds[opts.reserveOffset];
// Ignore Kyber Bridged Reserves (0xbb)
if (uint256(reserveId >> 248) == 0xbb) {
return 0x0;
}
return reserveId;
}
}

View File

@@ -1,91 +0,0 @@
// SPDX-License-Identifier: Apache-2.0
/*
Copyright 2021 ZeroEx Intl.
Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at
http://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
*/
pragma solidity ^0.6;
pragma experimental ABIEncoderV2;
import "./SamplerUtils.sol";
contract LidoSampler is SamplerUtils {
struct LidoInfo {
address stEthToken;
address wethToken;
}
/// @dev Sample sell quotes from Lido
/// @param lidoInfo Info regarding a specific Lido deployment
/// @param takerToken Address of the taker token (what to sell).
/// @param makerToken Address of the maker token (what to buy).
/// @param takerTokenAmounts Taker token sell amount for each sample.
/// @return makerTokenAmounts Maker amounts bought at each taker token
/// amount.
function sampleSellsFromLido(
LidoInfo memory lidoInfo,
address takerToken,
address makerToken,
uint256[] memory takerTokenAmounts
)
public
pure
returns (uint256[] memory)
{
_assertValidPair(makerToken, takerToken);
if (takerToken != lidoInfo.wethToken || makerToken != address(lidoInfo.stEthToken)) {
// Return 0 values if not selling WETH for stETH
uint256 numSamples = takerTokenAmounts.length;
uint256[] memory makerTokenAmounts = new uint256[](numSamples);
return makerTokenAmounts;
}
// Minting stETH is always 1:1 therefore we can just return the same amounts back
return takerTokenAmounts;
}
/// @dev Sample buy quotes from Lido.
/// @param lidoInfo Info regarding a specific Lido deployment
/// @param takerToken Address of the taker token (what to sell).
/// @param makerToken Address of the maker token (what to buy).
/// @param makerTokenAmounts Maker token buy amount for each sample.
/// @return takerTokenAmounts Taker amounts sold at each maker token
/// amount.
function sampleBuysFromLido(
LidoInfo memory lidoInfo,
address takerToken,
address makerToken,
uint256[] memory makerTokenAmounts
)
public
pure
returns (uint256[] memory)
{
_assertValidPair(makerToken, takerToken);
if (takerToken != lidoInfo.wethToken || makerToken != address(lidoInfo.stEthToken)) {
// Return 0 values if not buying stETH for WETH
uint256 numSamples = makerTokenAmounts.length;
uint256[] memory takerTokenAmounts = new uint256[](numSamples);
return takerTokenAmounts;
}
// Minting stETH is always 1:1 therefore we can just return the same amounts back
return makerTokenAmounts;
}
}

View File

@@ -1,132 +0,0 @@
// SPDX-License-Identifier: Apache-2.0
/*
Copyright 2020 ZeroEx Intl.
Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at
http://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
*/
pragma solidity ^0.6;
pragma experimental ABIEncoderV2;
import "@0x/contracts-utils/contracts/src/v06/LibBytesV06.sol";
import "@0x/contracts-zero-ex/contracts/src/vendor/ILiquidityProvider.sol";
import "./ApproximateBuys.sol";
import "./SamplerUtils.sol";
contract LiquidityProviderSampler is
SamplerUtils,
ApproximateBuys
{
/// @dev Default gas limit for liquidity provider calls.
uint256 constant private DEFAULT_CALL_GAS = 400e3; // 400k
/// @dev Sample sell quotes from an arbitrary on-chain liquidity provider.
/// @param providerAddress Address of the liquidity provider.
/// @param takerToken Address of the taker token (what to sell).
/// @param makerToken Address of the maker token (what to buy).
/// @param takerTokenAmounts Taker token sell amount for each sample.
/// @return makerTokenAmounts Maker amounts bought at each taker token
/// amount.
function sampleSellsFromLiquidityProvider(
address providerAddress,
address takerToken,
address makerToken,
uint256[] memory takerTokenAmounts
)
public
view
returns (uint256[] memory makerTokenAmounts)
{
// Initialize array of maker token amounts.
uint256 numSamples = takerTokenAmounts.length;
makerTokenAmounts = new uint256[](numSamples);
for (uint256 i = 0; i < numSamples; i++) {
try
ILiquidityProvider(providerAddress).getSellQuote
{gas: DEFAULT_CALL_GAS}
(
IERC20TokenV06(takerToken),
IERC20TokenV06(makerToken),
takerTokenAmounts[i]
)
returns (uint256 amount)
{
makerTokenAmounts[i] = amount;
// Break early if there are 0 amounts
if (makerTokenAmounts[i] == 0) {
break;
}
} catch (bytes memory) {
// Swallow failures, leaving all results as zero.
break;
}
}
}
/// @dev Sample buy quotes from an arbitrary on-chain liquidity provider.
/// @param providerAddress Address of the liquidity provider.
/// @param takerToken Address of the taker token (what to sell).
/// @param makerToken Address of the maker token (what to buy).
/// @param makerTokenAmounts Maker token buy amount for each sample.
/// @return takerTokenAmounts Taker amounts sold at each maker token
/// amount.
function sampleBuysFromLiquidityProvider(
address providerAddress,
address takerToken,
address makerToken,
uint256[] memory makerTokenAmounts
)
public
view
returns (uint256[] memory takerTokenAmounts)
{
takerTokenAmounts = _sampleApproximateBuys(
ApproximateBuyQuoteOpts({
makerTokenData: abi.encode(makerToken, providerAddress),
takerTokenData: abi.encode(takerToken, providerAddress),
getSellQuoteCallback: _sampleSellForApproximateBuyFromLiquidityProvider
}),
makerTokenAmounts
);
}
function _sampleSellForApproximateBuyFromLiquidityProvider(
bytes memory takerTokenData,
bytes memory makerTokenData,
uint256 sellAmount
)
private
view
returns (uint256 buyAmount)
{
(address takerToken, address providerAddress) =
abi.decode(takerTokenData, (address, address));
(address makerToken) =
abi.decode(makerTokenData, (address));
try
this.sampleSellsFromLiquidityProvider
{gas: DEFAULT_CALL_GAS}
(providerAddress, takerToken, makerToken, _toSingleValueArray(sellAmount))
returns (uint256[] memory amounts)
{
return amounts[0];
} catch (bytes memory) {
// Swallow failures, leaving all results as zero.
return 0;
}
}
}

View File

@@ -1,127 +0,0 @@
// SPDX-License-Identifier: Apache-2.0
/*
Copyright 2020 ZeroEx Intl.
Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at
http://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
*/
pragma solidity ^0.6;
pragma experimental ABIEncoderV2;
import "./interfaces/IMStable.sol";
import "./ApproximateBuys.sol";
import "./SamplerUtils.sol";
contract MStableSampler is
SamplerUtils,
ApproximateBuys
{
/// @dev Default gas limit for mStable calls.
uint256 constant private DEFAULT_CALL_GAS = 800e3; // 800k
/// @dev Sample sell quotes from the mStable contract
/// @param router Address of the mStable contract
/// @param takerToken Address of the taker token (what to sell).
/// @param makerToken Address of the maker token (what to buy).
/// @param takerTokenAmounts Taker token sell amount for each sample.
/// @return makerTokenAmounts Maker amounts bought at each taker token
/// amount.
function sampleSellsFromMStable(
address router,
address takerToken,
address makerToken,
uint256[] memory takerTokenAmounts
)
public
view
returns (uint256[] memory makerTokenAmounts)
{
_assertValidPair(makerToken, takerToken);
// Initialize array of maker token amounts.
uint256 numSamples = takerTokenAmounts.length;
makerTokenAmounts = new uint256[](numSamples);
for (uint256 i = 0; i < numSamples; i++) {
try
IMStable(router).getSwapOutput
{gas: DEFAULT_CALL_GAS}
(takerToken, makerToken, takerTokenAmounts[i])
returns (uint256 amount)
{
makerTokenAmounts[i] = amount;
// Break early if there are 0 amounts
if (makerTokenAmounts[i] == 0) {
break;
}
} catch (bytes memory) {
// Swallow failures, leaving all results as zero.
break;
}
}
}
/// @dev Sample buy quotes from MStable contract
/// @param router Address of the mStable contract
/// @param takerToken Address of the taker token (what to sell).
/// @param makerToken Address of the maker token (what to buy).
/// @param makerTokenAmounts Maker token buy amount for each sample.
/// @return takerTokenAmounts Taker amounts sold at each maker token
/// amount.
function sampleBuysFromMStable(
address router,
address takerToken,
address makerToken,
uint256[] memory makerTokenAmounts
)
public
view
returns (uint256[] memory takerTokenAmounts)
{
return _sampleApproximateBuys(
ApproximateBuyQuoteOpts({
makerTokenData: abi.encode(makerToken, router),
takerTokenData: abi.encode(takerToken, router),
getSellQuoteCallback: _sampleSellForApproximateBuyFromMStable
}),
makerTokenAmounts
);
}
function _sampleSellForApproximateBuyFromMStable(
bytes memory takerTokenData,
bytes memory makerTokenData,
uint256 sellAmount
)
private
view
returns (uint256 buyAmount)
{
(address takerToken, address router) =
abi.decode(takerTokenData, (address, address));
(address makerToken) =
abi.decode(makerTokenData, (address));
try
this.sampleSellsFromMStable
(router, takerToken, makerToken, _toSingleValueArray(sellAmount))
returns (uint256[] memory amounts)
{
return amounts[0];
} catch (bytes memory) {
// Swallow failures, leaving all results as zero.
return 0;
}
}
}

View File

@@ -1,267 +0,0 @@
// SPDX-License-Identifier: Apache-2.0
/*
Copyright 2021 ZeroEx Intl.
Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at
http://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
*/
pragma solidity ^0.6;
pragma experimental ABIEncoderV2;
import "./SamplerUtils.sol";
import "@0x/contracts-utils/contracts/src/v06/LibMathV06.sol";
interface IPSM {
// @dev Get the fee for selling USDC to DAI in PSM
// @return tin toll in [wad]
function tin() external view returns (uint256);
// @dev Get the fee for selling DAI to USDC in PSM
// @return tout toll out [wad]
function tout() external view returns (uint256);
// @dev Get the address of the PSM state Vat
// @return address of the Vat
function vat() external view returns (address);
// @dev Get the address of the underlying vault powering PSM
// @return address of gemJoin contract
function gemJoin() external view returns (address);
// @dev Get the address of DAI
// @return address of DAI contract
function dai() external view returns (address);
// @dev Sell USDC for DAI
// @param usr The address of the account trading USDC for DAI.
// @param gemAmt The amount of USDC to sell in USDC base units
function sellGem(
address usr,
uint256 gemAmt
) external;
// @dev Buy USDC for DAI
// @param usr The address of the account trading DAI for USDC
// @param gemAmt The amount of USDC to buy in USDC base units
function buyGem(
address usr,
uint256 gemAmt
) external;
}
interface IVAT {
// @dev Get a collateral type by identifier
// @param ilkIdentifier bytes32 identifier. Example: ethers.utils.formatBytes32String("PSM-USDC-A")
// @return ilk
// @return ilk.Art Total Normalised Debt in wad
// @return ilk.rate Accumulated Rates in ray
// @return ilk.spot Price with Safety Margin in ray
// @return ilk.line Debt Ceiling in rad
// @return ilk.dust Urn Debt Floor in rad
function ilks(
bytes32 ilkIdentifier
) external view returns (
uint256 Art,
uint256 rate,
uint256 spot,
uint256 line,
uint256 dust
);
}
contract MakerPSMSampler is
SamplerUtils
{
using LibSafeMathV06 for uint256;
/// @dev Information about which PSM module to use
struct MakerPsmInfo {
address psmAddress;
bytes32 ilkIdentifier;
address gemTokenAddress;
}
/// @dev Gas limit for MakerPsm calls.
uint256 constant private MAKER_PSM_CALL_GAS = 300e3; // 300k
// Maker units
// wad: fixed point decimal with 18 decimals (for basic quantities, e.g. balances)
uint256 constant private WAD = 10 ** 18;
// ray: fixed point decimal with 27 decimals (for precise quantites, e.g. ratios)
uint256 constant private RAY = 10 ** 27;
// rad: fixed point decimal with 45 decimals (result of integer multiplication with a wad and a ray)
uint256 constant private RAD = 10 ** 45;
// See https://github.com/makerdao/dss/blob/master/DEVELOPING.m
/// @dev Sample sell quotes from Maker PSM
function sampleSellsFromMakerPsm(
MakerPsmInfo memory psmInfo,
address takerToken,
address makerToken,
uint256[] memory takerTokenAmounts
)
public
view
returns (uint256[] memory makerTokenAmounts)
{
_assertValidPair(makerToken, takerToken);
IPSM psm = IPSM(psmInfo.psmAddress);
IVAT vat = IVAT(psm.vat());
uint256 numSamples = takerTokenAmounts.length;
makerTokenAmounts = new uint256[](numSamples);
if (makerToken != psm.dai() && takerToken != psm.dai()) {
return makerTokenAmounts;
}
for (uint256 i = 0; i < numSamples; i++) {
uint256 buyAmount = _samplePSMSell(psmInfo, makerToken, takerToken, takerTokenAmounts[i], psm, vat);
if (buyAmount == 0) {
break;
}
makerTokenAmounts[i] = buyAmount;
}
}
function sampleBuysFromMakerPsm(
MakerPsmInfo memory psmInfo,
address takerToken,
address makerToken,
uint256[] memory makerTokenAmounts
)
public
view
returns (uint256[] memory takerTokenAmounts)
{
_assertValidPair(makerToken, takerToken);
IPSM psm = IPSM(psmInfo.psmAddress);
IVAT vat = IVAT(psm.vat());
uint256 numSamples = makerTokenAmounts.length;
takerTokenAmounts = new uint256[](numSamples);
if (makerToken != psm.dai() && takerToken != psm.dai()) {
return takerTokenAmounts;
}
for (uint256 i = 0; i < numSamples; i++) {
uint256 sellAmount = _samplePSMBuy(psmInfo, makerToken, takerToken, makerTokenAmounts[i], psm, vat);
if (sellAmount == 0) {
break;
}
takerTokenAmounts[i] = sellAmount;
}
}
function _samplePSMSell(MakerPsmInfo memory psmInfo, address makerToken, address takerToken, uint256 takerTokenAmount, IPSM psm, IVAT vat)
private
view
returns (uint256)
{
(uint256 totalDebtInWad,,, uint256 debtCeilingInRad, uint256 debtFloorInRad) = vat.ilks(psmInfo.ilkIdentifier);
uint256 gemTokenBaseUnit = uint256(1e6);
if (takerToken == psmInfo.gemTokenAddress) {
// Simulate sellGem
// Selling USDC to the PSM, increasing the total debt
// Convert USDC 6 decimals to 18 decimals [wad]
uint256 takerTokenAmountInWad = takerTokenAmount.safeMul(1e12);
uint256 newTotalDebtInRad = totalDebtInWad.safeAdd(takerTokenAmountInWad).safeMul(RAY);
// PSM is too full to fit
if (newTotalDebtInRad >= debtCeilingInRad) {
return 0;
}
uint256 feeInWad = takerTokenAmountInWad.safeMul(psm.tin()).safeDiv(WAD);
uint256 makerTokenAmountInWad = takerTokenAmountInWad.safeSub(feeInWad);
return makerTokenAmountInWad;
} else if (makerToken == psmInfo.gemTokenAddress) {
// Simulate buyGem
// Buying USDC from the PSM, decreasing the total debt
// Selling DAI for USDC, already in 18 decimals [wad]
uint256 takerTokenAmountInWad = takerTokenAmount;
if (takerTokenAmountInWad > totalDebtInWad) {
return 0;
}
uint256 newTotalDebtInRad = totalDebtInWad.safeSub(takerTokenAmountInWad).safeMul(RAY);
// PSM is empty, not enough USDC to buy from it
if (newTotalDebtInRad <= debtFloorInRad) {
return 0;
}
uint256 feeDivisorInWad = WAD.safeAdd(psm.tout()); // eg. 1.001 * 10 ** 18 with 0.1% tout;
uint256 makerTokenAmountInGemTokenBaseUnits = takerTokenAmountInWad.safeMul(gemTokenBaseUnit).safeDiv(feeDivisorInWad);
return makerTokenAmountInGemTokenBaseUnits;
}
return 0;
}
function _samplePSMBuy(MakerPsmInfo memory psmInfo, address makerToken, address takerToken, uint256 makerTokenAmount, IPSM psm, IVAT vat)
private
view
returns (uint256)
{
(uint256 totalDebtInWad,,, uint256 debtCeilingInRad, uint256 debtFloorInRad) = vat.ilks(psmInfo.ilkIdentifier);
if (takerToken == psmInfo.gemTokenAddress) {
// Simulate sellGem
// Selling USDC to the PSM, increasing the total debt
uint256 makerTokenAmountInWad = makerTokenAmount;
uint256 feeDivisorInWad = WAD.safeSub(psm.tin()); // eg. 0.999 * 10 ** 18 with 0.1% tin;
uint256 takerTokenAmountInWad = makerTokenAmountInWad.safeMul(WAD).safeDiv(feeDivisorInWad);
uint256 newTotalDebtInRad = totalDebtInWad.safeAdd(takerTokenAmountInWad).safeMul(RAY);
// PSM is too full to fit
if (newTotalDebtInRad >= debtCeilingInRad) {
return 0;
}
uint256 takerTokenAmountInGemInGemBaseUnits = (takerTokenAmountInWad.safeDiv(1e12)).safeAdd(1); // Add 1 to deal with cut off decimals converting to lower decimals
return takerTokenAmountInGemInGemBaseUnits;
} else if (makerToken == psmInfo.gemTokenAddress) {
// Simulate buyGem
// Buying USDC from the PSM, decreasing the total debt
uint256 makerTokenAmountInWad = makerTokenAmount.safeMul(1e12);
uint256 feeMultiplierInWad = WAD.safeAdd(psm.tout()); // eg. 1.001 * 10 ** 18 with 0.1% tout;
uint256 takerTokenAmountInWad = makerTokenAmountInWad.safeMul(feeMultiplierInWad).safeDiv(WAD);
if (takerTokenAmountInWad > totalDebtInWad) {
return 0;
}
uint256 newTotalDebtInRad = totalDebtInWad.safeSub(takerTokenAmountInWad).safeMul(RAY);
// PSM is empty, not enough USDC to buy
if (newTotalDebtInRad <= debtFloorInRad) {
return 0;
}
return takerTokenAmountInWad;
}
return 0;
}
}

View File

@@ -1,169 +0,0 @@
// SPDX-License-Identifier: Apache-2.0
/*
Copyright 2020 ZeroEx Intl.
Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at
http://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
*/
pragma solidity ^0.6;
pragma experimental ABIEncoderV2;
import "./interfaces/IMooniswap.sol";
import "./ApproximateBuys.sol";
import "./SamplerUtils.sol";
contract MooniswapSampler is
SamplerUtils,
ApproximateBuys
{
/// @dev Gas limit for Mooniswap calls.
uint256 constant private MOONISWAP_CALL_GAS = 150e3; // 150k
/// @dev Sample sell quotes from Mooniswap.
/// @param registry Address of the Mooniswap Registry.
/// @param takerToken Address of the taker token (what to sell).
/// @param makerToken Address of the maker token (what to buy).
/// @param takerTokenAmounts Taker token sell amount for each sample.
/// @return pool The contract address for the pool
/// @return makerTokenAmounts Maker amounts bought at each taker token
/// amount.
function sampleSellsFromMooniswap(
address registry,
address takerToken,
address makerToken,
uint256[] memory takerTokenAmounts
)
public
view
returns (IMooniswap pool, uint256[] memory makerTokenAmounts)
{
_assertValidPair(makerToken, takerToken);
uint256 numSamples = takerTokenAmounts.length;
makerTokenAmounts = new uint256[](numSamples);
for (uint256 i = 0; i < numSamples; i++) {
uint256 buyAmount = sampleSingleSellFromMooniswapPool(
registry,
takerToken,
makerToken,
takerTokenAmounts[i]
);
makerTokenAmounts[i] = buyAmount;
// Break early if there are 0 amounts
if (makerTokenAmounts[i] == 0) {
break;
}
}
pool = IMooniswap(
IMooniswapRegistry(registry).pools(takerToken, makerToken)
);
}
function sampleSingleSellFromMooniswapPool(
address registry,
address mooniswapTakerToken,
address mooniswapMakerToken,
uint256 takerTokenAmount
)
public
view
returns (uint256)
{
// Find the pool for the pair.
IMooniswap pool = IMooniswap(
IMooniswapRegistry(registry).pools(mooniswapTakerToken, mooniswapMakerToken)
);
// If there is no pool then return early
if (address(pool) == address(0)) {
return 0;
}
uint256 poolBalance = mooniswapTakerToken == address(0)
? address(pool).balance
: IERC20TokenV06(mooniswapTakerToken).balanceOf(address(pool));
// If the pool balance is smaller than the sell amount
// don't sample to avoid multiplication overflow in buys
if (poolBalance < takerTokenAmount) {
return 0;
}
try
pool.getReturn
{gas: MOONISWAP_CALL_GAS}
(mooniswapTakerToken, mooniswapMakerToken, takerTokenAmount)
returns (uint256 amount)
{
return amount;
} catch (bytes memory) {
// Swallow failures, leaving all results as zero.
return 0;
}
}
/// @dev Sample buy quotes from Mooniswap.
/// @param registry Address of the Mooniswap Registry.
/// @param takerToken Address of the taker token (what to sell).
/// @param makerToken Address of the maker token (what to buy).
/// @param makerTokenAmounts Maker token sell amount for each sample.
/// @return pool The contract address for the pool
/// @return takerTokenAmounts Taker amounts sold at each maker token
/// amount.
function sampleBuysFromMooniswap(
address registry,
address takerToken,
address makerToken,
uint256[] memory makerTokenAmounts
)
public
view
returns (IMooniswap pool, uint256[] memory takerTokenAmounts)
{
_assertValidPair(makerToken, takerToken);
uint256 numSamples = makerTokenAmounts.length;
takerTokenAmounts = new uint256[](numSamples);
takerTokenAmounts = _sampleApproximateBuys(
ApproximateBuyQuoteOpts({
makerTokenData: abi.encode(registry, makerToken),
takerTokenData: abi.encode(registry, takerToken),
getSellQuoteCallback: _sampleSellForApproximateBuyFromMooniswap
}),
makerTokenAmounts
);
pool = IMooniswap(
IMooniswapRegistry(registry).pools(takerToken, makerToken)
);
}
function _sampleSellForApproximateBuyFromMooniswap(
bytes memory takerTokenData,
bytes memory makerTokenData,
uint256 sellAmount
)
private
view
returns (uint256 buyAmount)
{
(address registry, address mooniswapTakerToken) = abi.decode(takerTokenData, (address, address));
(address _registry, address mooniswapMakerToken) = abi.decode(makerTokenData, (address, address));
return sampleSingleSellFromMooniswapPool(
registry,
mooniswapTakerToken,
mooniswapMakerToken,
sellAmount
);
}
}

View File

@@ -1,82 +0,0 @@
// SPDX-License-Identifier: Apache-2.0
/*
Copyright 2020 ZeroEx Intl.
Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at
http://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
*/
pragma solidity ^0.6;
pragma experimental ABIEncoderV2;
import "./interfaces/IMultiBridge.sol";
contract MultiBridgeSampler {
/// @dev Default gas limit for multibridge calls.
uint256 constant private DEFAULT_CALL_GAS = 400e3; // 400k
/// @dev Sample sell quotes from MultiBridge.
/// @param multibridge Address of the MultiBridge contract.
/// @param takerToken Address of the taker token (what to sell).
/// @param intermediateToken The address of the intermediate token to
/// use in an indirect route.
/// @param makerToken Address of the maker token (what to buy).
/// @param takerTokenAmounts Taker token sell amount for each sample.
/// @return makerTokenAmounts Maker amounts bought at each taker token
/// amount.
function sampleSellsFromMultiBridge(
address multibridge,
address takerToken,
address intermediateToken,
address makerToken,
uint256[] memory takerTokenAmounts
)
public
view
returns (uint256[] memory makerTokenAmounts)
{
// Initialize array of maker token amounts.
uint256 numSamples = takerTokenAmounts.length;
makerTokenAmounts = new uint256[](numSamples);
// If no address provided, return all zeros.
if (multibridge == address(0)) {
return makerTokenAmounts;
}
for (uint256 i = 0; i < numSamples; i++) {
(bool didSucceed, bytes memory resultData) =
multibridge.staticcall.gas(DEFAULT_CALL_GAS)(
abi.encodeWithSelector(
IMultiBridge(0).getSellQuote.selector,
takerToken,
intermediateToken,
makerToken,
takerTokenAmounts[i]
));
uint256 buyAmount = 0;
if (didSucceed) {
buyAmount = abi.decode(resultData, (uint256));
}
// Exit early if the amount is too high for the source to serve
if (buyAmount == 0) {
break;
}
makerTokenAmounts[i] = buyAmount;
}
}
}

View File

@@ -1,239 +0,0 @@
// SPDX-License-Identifier: Apache-2.0
/*
Copyright 2021 ZeroEx Intl.
Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at
http://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
*/
pragma solidity ^0.6;
pragma experimental ABIEncoderV2;
import "@0x/contracts-erc20/contracts/src/v06/LibERC20TokenV06.sol";
import "@0x/contracts-utils/contracts/src/v06/LibMathV06.sol";
import "@0x/contracts-utils/contracts/src/v06/LibBytesV06.sol";
import "@0x/contracts-utils/contracts/src/v06/LibSafeMathV06.sol";
interface IExchange {
enum OrderStatus {
INVALID,
FILLABLE,
FILLED,
CANCELLED,
EXPIRED
}
/// @dev A standard OTC or OO limit order.
struct LimitOrder {
IERC20TokenV06 makerToken;
IERC20TokenV06 takerToken;
uint128 makerAmount;
uint128 takerAmount;
uint128 takerTokenFeeAmount;
address maker;
address taker;
address sender;
address feeRecipient;
bytes32 pool;
uint64 expiry;
uint256 salt;
}
/// @dev An RFQ limit order.
struct RfqOrder {
IERC20TokenV06 makerToken;
IERC20TokenV06 takerToken;
uint128 makerAmount;
uint128 takerAmount;
address maker;
address taker;
address txOrigin;
bytes32 pool;
uint64 expiry;
uint256 salt;
}
/// @dev Info on a limit or RFQ order.
struct OrderInfo {
bytes32 orderHash;
OrderStatus status;
uint128 takerTokenFilledAmount;
}
/// @dev Allowed signature types.
enum SignatureType {
ILLEGAL,
INVALID,
EIP712,
ETHSIGN
}
/// @dev Encoded EC signature.
struct Signature {
// How to validate the signature.
SignatureType signatureType;
// EC Signature data.
uint8 v;
// EC Signature data.
bytes32 r;
// EC Signature data.
bytes32 s;
}
/// @dev Get the order info for a limit order.
/// @param order The limit order.
/// @return orderInfo Info about the order.
function getLimitOrderInfo(LimitOrder memory order)
external
view
returns (OrderInfo memory orderInfo);
/// @dev Get order info, fillable amount, and signature validity for a limit order.
/// Fillable amount is determined using balances and allowances of the maker.
/// @param order The limit order.
/// @param signature The order signature.
/// @return orderInfo Info about the order.
/// @return actualFillableTakerTokenAmount How much of the order is fillable
/// based on maker funds, in taker tokens.
/// @return isSignatureValid Whether the signature is valid.
function getLimitOrderRelevantState(
LimitOrder memory order,
Signature calldata signature
)
external
view
returns (
OrderInfo memory orderInfo,
uint128 actualFillableTakerTokenAmount,
bool isSignatureValid
);
}
contract NativeOrderSampler {
using LibSafeMathV06 for uint256;
using LibBytesV06 for bytes;
/// @dev Gas limit for calls to `getOrderFillableTakerAmount()`.
uint256 constant internal DEFAULT_CALL_GAS = 200e3; // 200k
/// @dev Queries the fillable taker asset amounts of native orders.
/// Effectively ignores orders that have empty signatures or
/// maker/taker asset amounts (returning 0).
/// @param orders Native limit orders to query.
/// @param orderSignatures Signatures for each respective order in `orders`.
/// @param exchange The V4 exchange.
/// @return orderFillableTakerAssetAmounts How much taker asset can be filled
/// by each order in `orders`.
function getLimitOrderFillableTakerAssetAmounts(
IExchange.LimitOrder[] memory orders,
IExchange.Signature[] memory orderSignatures,
IExchange exchange
)
public
view
returns (uint256[] memory orderFillableTakerAssetAmounts)
{
orderFillableTakerAssetAmounts = new uint256[](orders.length);
for (uint256 i = 0; i != orders.length; i++) {
try
this.getLimitOrderFillableTakerAmount
{gas: DEFAULT_CALL_GAS}
(
orders[i],
orderSignatures[i],
exchange
)
returns (uint256 amount)
{
orderFillableTakerAssetAmounts[i] = amount;
} catch (bytes memory) {
// Swallow failures, leaving all results as zero.
orderFillableTakerAssetAmounts[i] = 0;
}
}
}
/// @dev Queries the fillable taker asset amounts of native orders.
/// Effectively ignores orders that have empty signatures or
/// @param orders Native orders to query.
/// @param orderSignatures Signatures for each respective order in `orders`.
/// @param exchange The V4 exchange.
/// @return orderFillableMakerAssetAmounts How much maker asset can be filled
/// by each order in `orders`.
function getLimitOrderFillableMakerAssetAmounts(
IExchange.LimitOrder[] memory orders,
IExchange.Signature[] memory orderSignatures,
IExchange exchange
)
public
view
returns (uint256[] memory orderFillableMakerAssetAmounts)
{
orderFillableMakerAssetAmounts = getLimitOrderFillableTakerAssetAmounts(
orders,
orderSignatures,
exchange
);
// `orderFillableMakerAssetAmounts` now holds taker asset amounts, so
// convert them to maker asset amounts.
for (uint256 i = 0; i < orders.length; ++i) {
if (orderFillableMakerAssetAmounts[i] != 0) {
orderFillableMakerAssetAmounts[i] = LibMathV06.getPartialAmountCeil(
orderFillableMakerAssetAmounts[i],
orders[i].takerAmount,
orders[i].makerAmount
);
}
}
}
/// @dev Get the fillable taker amount of an order, taking into account
/// order state, maker fees, and maker balances.
function getLimitOrderFillableTakerAmount(
IExchange.LimitOrder memory order,
IExchange.Signature memory signature,
IExchange exchange
)
virtual
public
view
returns (uint256 fillableTakerAmount)
{
if (signature.signatureType == IExchange.SignatureType.ILLEGAL ||
signature.signatureType == IExchange.SignatureType.INVALID ||
order.makerAmount == 0 ||
order.takerAmount == 0)
{
return 0;
}
(
IExchange.OrderInfo memory orderInfo,
uint128 remainingFillableTakerAmount,
bool isSignatureValid
) = exchange.getLimitOrderRelevantState(order, signature);
if (
orderInfo.status != IExchange.OrderStatus.FILLABLE ||
!isSignatureValid ||
order.makerToken == IERC20TokenV06(0)
) {
return 0;
}
fillableTakerAmount = uint256(remainingFillableTakerAmount);
}
}

View File

@@ -1,58 +0,0 @@
// SPDX-License-Identifier: Apache-2.0
/*
Copyright 2020 ZeroEx Intl.
Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at
http://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
*/
pragma solidity ^0.6;
pragma experimental ABIEncoderV2;
import "@0x/contracts-erc20/contracts/src/v06/LibERC20TokenV06.sol";
contract SamplerUtils {
/// @dev Overridable way to get token decimals.
/// @param tokenAddress Address of the token.
/// @return decimals The decimal places for the token.
function _getTokenDecimals(address tokenAddress)
virtual
internal
view
returns (uint8 decimals)
{
return LibERC20TokenV06.compatDecimals(IERC20TokenV06(tokenAddress));
}
function _toSingleValueArray(uint256 v)
internal
pure
returns (uint256[] memory arr)
{
arr = new uint256[](1);
arr[0] = v;
}
/// @dev Assert that the tokens in a trade pair are valid.
/// @param makerToken Address of the maker token.
/// @param takerToken Address of the taker token.
function _assertValidPair(address makerToken, address takerToken)
internal
pure
{
require(makerToken != takerToken, "ERC20BridgeSampler/INVALID_TOKEN_PAIR");
}
}

View File

@@ -1,126 +0,0 @@
// SPDX-License-Identifier: Apache-2.0
/*
Copyright 2020 ZeroEx Intl.
Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at
http://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
*/
pragma solidity ^0.6;
pragma experimental ABIEncoderV2;
import "./ApproximateBuys.sol";
import "./interfaces/IShell.sol";
import "./SamplerUtils.sol";
contract ShellSampler is
SamplerUtils,
ApproximateBuys
{
struct ShellInfo {
address poolAddress;
}
/// @dev Default gas limit for Shell calls.
uint256 constant private DEFAULT_CALL_GAS = 300e3; // 300k
/// @dev Sample sell quotes from the Shell pool contract
/// @param pool Address of the Shell pool contract
/// @param takerToken Address of the taker token (what to sell).
/// @param makerToken Address of the maker token (what to buy).
/// @param takerTokenAmounts Taker token sell amount for each sample.
/// @return makerTokenAmounts Maker amounts bought at each taker token
/// amount.
function sampleSellsFromShell(
address pool,
address takerToken,
address makerToken,
uint256[] memory takerTokenAmounts
)
public
view
returns (uint256[] memory makerTokenAmounts)
{
// Initialize array of maker token amounts.
uint256 numSamples = takerTokenAmounts.length;
makerTokenAmounts = new uint256[](numSamples);
for (uint256 i = 0; i < numSamples; i++) {
try
IShell(pool).viewOriginSwap
{gas: DEFAULT_CALL_GAS}
(takerToken, makerToken, takerTokenAmounts[i])
returns (uint256 amount)
{
makerTokenAmounts[i] = amount;
} catch (bytes memory) {
// Swallow failures, leaving all results as zero.
break;
}
}
}
/// @dev Sample buy quotes from Shell pool contract
/// @param pool Address of the Shell pool contract
/// @param takerToken Address of the taker token (what to sell).
/// @param makerToken Address of the maker token (what to buy).
/// @param makerTokenAmounts Maker token buy amount for each sample.
/// @return takerTokenAmounts Taker amounts sold at each maker token
/// amount.
function sampleBuysFromShell(
address pool,
address takerToken,
address makerToken,
uint256[] memory makerTokenAmounts
)
public
view
returns (uint256[] memory takerTokenAmounts)
{
return _sampleApproximateBuys(
ApproximateBuyQuoteOpts({
makerTokenData: abi.encode(makerToken, pool),
takerTokenData: abi.encode(takerToken, pool),
getSellQuoteCallback: _sampleSellForApproximateBuyFromShell
}),
makerTokenAmounts
);
}
function _sampleSellForApproximateBuyFromShell(
bytes memory takerTokenData,
bytes memory makerTokenData,
uint256 sellAmount
)
private
view
returns (uint256 buyAmount)
{
(address takerToken, address pool) = abi.decode(takerTokenData, (address, address));
(address makerToken) = abi.decode(makerTokenData, (address));
try
this.sampleSellsFromShell
(pool, takerToken, makerToken, _toSingleValueArray(sellAmount))
returns (uint256[] memory amounts)
{
return amounts[0];
} catch (bytes memory) {
// Swallow failures, leaving all results as zero.
return 0;
}
}
}

View File

@@ -1,156 +0,0 @@
// SPDX-License-Identifier: Apache-2.0
/*
Copyright 2020 ZeroEx Intl.
Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at
http://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
*/
pragma solidity ^0.6;
pragma experimental ABIEncoderV2;
// import "./interfaces/ISmoothy.sol";
import "./ApproximateBuys.sol";
import "./SamplerUtils.sol";
import "./interfaces/ISmoothy.sol";
contract SmoothySampler is
SamplerUtils,
ApproximateBuys
{
/// @dev Information for sampling from smoothy sources.
struct SmoothyInfo {
address poolAddress;
bytes4 sellQuoteFunctionSelector;
bytes4 buyQuoteFunctionSelector;
}
/// @dev Base gas limit for Smoothy calls.
uint256 constant private SMOOTHY_CALL_GAS = 600e3;
/// @dev Sample sell quotes from Smoothy.
/// @param smoothyInfo Smoothy information specific to this token pair.
/// @param fromTokenIdx Index of the taker token (what to sell).
/// @param toTokenIdx Index of the maker token (what to buy).
/// @param takerTokenAmounts Taker token sell amount for each sample.
/// @return makerTokenAmounts Maker amounts bought at each taker token
/// amount.
function sampleSellsFromSmoothy(
SmoothyInfo memory smoothyInfo,
int128 fromTokenIdx,
int128 toTokenIdx,
uint256[] memory takerTokenAmounts
)
public
view
returns (uint256[] memory makerTokenAmounts)
{
// Basically a Curve fork
// Smoothy only keep a percentage of its tokens available in reserve
uint256 poolReserveMakerAmount = ISmoothy(smoothyInfo.poolAddress).getBalance(uint256(toTokenIdx)) -
ISmoothy(smoothyInfo.poolAddress)._yBalances(uint256(toTokenIdx));
(, , , uint256 decimals) = ISmoothy(smoothyInfo.poolAddress).getTokenStats(uint256(toTokenIdx));
poolReserveMakerAmount = poolReserveMakerAmount/(10**(18-decimals));
uint256 numSamples = takerTokenAmounts.length;
makerTokenAmounts = new uint256[](numSamples);
for (uint256 i = 0; i < numSamples; i++) {
(bool didSucceed, bytes memory resultData) =
smoothyInfo.poolAddress.staticcall.gas(SMOOTHY_CALL_GAS)(
abi.encodeWithSelector(
smoothyInfo.sellQuoteFunctionSelector,
fromTokenIdx,
toTokenIdx,
takerTokenAmounts[i]
));
uint256 buyAmount = 0;
if (didSucceed) {
buyAmount = abi.decode(resultData, (uint256));
}
// Make sure the quoted buyAmount is available in the pool reserve
if (buyAmount >= poolReserveMakerAmount) {
// Assign pool reserve amount for all higher samples to break early
for (uint256 j = i; j < numSamples; j++) {
makerTokenAmounts[j] = poolReserveMakerAmount;
}
break;
} else {
makerTokenAmounts[i] = buyAmount;
}
// Break early if there are 0 amounts
if (makerTokenAmounts[i] == 0) {
break;
}
}
}
/// @dev Sample buy quotes from Smoothy.
/// @param smoothyInfo Smoothy information specific to this token pair.
/// @param fromTokenIdx Index of the taker token (what to sell).
/// @param toTokenIdx Index of the maker token (what to buy).
/// @param makerTokenAmounts Maker token buy amount for each sample.
/// @return takerTokenAmounts Taker amounts sold at each maker token
/// amount.
function sampleBuysFromSmoothy(
SmoothyInfo memory smoothyInfo,
int128 fromTokenIdx,
int128 toTokenIdx,
uint256[] memory makerTokenAmounts
)
public
view
returns (uint256[] memory takerTokenAmounts)
{
// Buys not supported so approximate it.
return _sampleApproximateBuys(
ApproximateBuyQuoteOpts({
makerTokenData: abi.encode(toTokenIdx, smoothyInfo),
takerTokenData: abi.encode(fromTokenIdx, smoothyInfo),
getSellQuoteCallback: _sampleSellForApproximateBuyFromSmoothy
}),
makerTokenAmounts
);
}
function _sampleSellForApproximateBuyFromSmoothy(
bytes memory takerTokenData,
bytes memory makerTokenData,
uint256 sellAmount
)
private
view
returns (uint256 buyAmount)
{
(int128 takerTokenIdx, SmoothyInfo memory smoothyInfo) =
abi.decode(takerTokenData, (int128, SmoothyInfo));
(int128 makerTokenIdx) =
abi.decode(makerTokenData, (int128));
(bool success, bytes memory resultData) =
address(this).staticcall(abi.encodeWithSelector(
this.sampleSellsFromSmoothy.selector,
smoothyInfo,
takerTokenIdx,
makerTokenIdx,
_toSingleValueArray(sellAmount)
));
if (!success) {
return 0;
}
// solhint-disable-next-line indent
return abi.decode(resultData, (uint256[]))[0];
}
}

View File

@@ -1,124 +0,0 @@
// SPDX-License-Identifier: Apache-2.0
/*
Copyright 2020 ZeroEx Intl.
Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at
http://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
*/
pragma solidity ^0.6;
pragma experimental ABIEncoderV2;
import "@0x/contracts-utils/contracts/src/v06/LibBytesV06.sol";
contract TwoHopSampler {
using LibBytesV06 for bytes;
struct HopInfo {
uint256 sourceIndex;
bytes returnData;
}
function sampleTwoHopSell(
bytes[] memory firstHopCalls,
bytes[] memory secondHopCalls,
uint256 sellAmount
)
public
returns (
HopInfo memory firstHop,
HopInfo memory secondHop,
uint256 buyAmount
)
{
uint256 intermediateAssetAmount = 0;
for (uint256 i = 0; i != firstHopCalls.length; ++i) {
firstHopCalls[i].writeUint256(firstHopCalls[i].length - 32, sellAmount);
(bool didSucceed, bytes memory returnData) = address(this).call(firstHopCalls[i]);
if (didSucceed) {
uint256 amount = returnData.readUint256(returnData.length - 32);
if (amount > intermediateAssetAmount) {
intermediateAssetAmount = amount;
firstHop.sourceIndex = i;
firstHop.returnData = returnData;
}
}
}
if (intermediateAssetAmount == 0) {
return (firstHop, secondHop, buyAmount);
}
for (uint256 j = 0; j != secondHopCalls.length; ++j) {
secondHopCalls[j].writeUint256(secondHopCalls[j].length - 32, intermediateAssetAmount);
(bool didSucceed, bytes memory returnData) = address(this).call(secondHopCalls[j]);
if (didSucceed) {
uint256 amount = returnData.readUint256(returnData.length - 32);
if (amount > buyAmount) {
buyAmount = amount;
secondHop.sourceIndex = j;
secondHop.returnData = returnData;
}
}
}
}
function sampleTwoHopBuy(
bytes[] memory firstHopCalls,
bytes[] memory secondHopCalls,
uint256 buyAmount
)
public
returns (
HopInfo memory firstHop,
HopInfo memory secondHop,
uint256 sellAmount
)
{
sellAmount = uint256(-1);
uint256 intermediateAssetAmount = uint256(-1);
for (uint256 j = 0; j != secondHopCalls.length; ++j) {
secondHopCalls[j].writeUint256(secondHopCalls[j].length - 32, buyAmount);
(bool didSucceed, bytes memory returnData) = address(this).call(secondHopCalls[j]);
if (didSucceed) {
uint256 amount = returnData.readUint256(returnData.length - 32);
if (
amount > 0 &&
amount < intermediateAssetAmount
) {
intermediateAssetAmount = amount;
secondHop.sourceIndex = j;
secondHop.returnData = returnData;
}
}
}
if (intermediateAssetAmount == uint256(-1)) {
return (firstHop, secondHop, sellAmount);
}
for (uint256 i = 0; i != firstHopCalls.length; ++i) {
firstHopCalls[i].writeUint256(firstHopCalls[i].length - 32, intermediateAssetAmount);
(bool didSucceed, bytes memory returnData) = address(this).call(firstHopCalls[i]);
if (didSucceed) {
uint256 amount = returnData.readUint256(returnData.length - 32);
if (
amount > 0 &&
amount < sellAmount
) {
sellAmount = amount;
firstHop.sourceIndex = i;
firstHop.returnData = returnData;
}
}
}
}
}

View File

@@ -1,214 +0,0 @@
// SPDX-License-Identifier: Apache-2.0
/*
Copyright 2020 ZeroEx Intl.
Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at
http://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
*/
pragma solidity ^0.6;
pragma experimental ABIEncoderV2;
import "./interfaces/IUniswapExchangeQuotes.sol";
import "./SamplerUtils.sol";
interface IUniswapExchangeFactory {
/// @dev Get the exchange for a token.
/// @param tokenAddress The address of the token contract.
function getExchange(address tokenAddress)
external
view
returns (address);
}
contract UniswapSampler is
SamplerUtils
{
/// @dev Gas limit for Uniswap calls.
uint256 constant private UNISWAP_CALL_GAS = 150e3; // 150k
/// @dev Sample sell quotes from Uniswap.
/// @param router Address of the Uniswap Router
/// @param takerToken Address of the taker token (what to sell).
/// @param makerToken Address of the maker token (what to buy).
/// @param takerTokenAmounts Taker token sell amount for each sample.
/// @return makerTokenAmounts Maker amounts bought at each taker token
/// amount.
function sampleSellsFromUniswap(
address router,
address takerToken,
address makerToken,
uint256[] memory takerTokenAmounts
)
public
view
returns (uint256[] memory makerTokenAmounts)
{
_assertValidPair(makerToken, takerToken);
uint256 numSamples = takerTokenAmounts.length;
makerTokenAmounts = new uint256[](numSamples);
IUniswapExchangeQuotes takerTokenExchange = takerToken == address(0) ?
IUniswapExchangeQuotes(0) : _getUniswapExchange(router, takerToken);
IUniswapExchangeQuotes makerTokenExchange = makerToken == address(0) ?
IUniswapExchangeQuotes(0) : _getUniswapExchange(router, makerToken);
for (uint256 i = 0; i < numSamples; i++) {
bool didSucceed = true;
if (makerToken == address(0)) {
(makerTokenAmounts[i], didSucceed) = _callUniswapExchangePriceFunction(
address(takerTokenExchange),
takerTokenExchange.getTokenToEthInputPrice.selector,
takerTokenAmounts[i]
);
} else if (takerToken == address(0)) {
(makerTokenAmounts[i], didSucceed) = _callUniswapExchangePriceFunction(
address(makerTokenExchange),
makerTokenExchange.getEthToTokenInputPrice.selector,
takerTokenAmounts[i]
);
} else {
uint256 ethBought;
(ethBought, didSucceed) = _callUniswapExchangePriceFunction(
address(takerTokenExchange),
takerTokenExchange.getTokenToEthInputPrice.selector,
takerTokenAmounts[i]
);
if (ethBought != 0) {
(makerTokenAmounts[i], didSucceed) = _callUniswapExchangePriceFunction(
address(makerTokenExchange),
makerTokenExchange.getEthToTokenInputPrice.selector,
ethBought
);
} else {
makerTokenAmounts[i] = 0;
}
}
// Break early if amounts are 0
if (!didSucceed || makerTokenAmounts[i] == 0) {
break;
}
}
}
/// @dev Sample buy quotes from Uniswap.
/// @param takerToken Address of the taker token (what to sell).
/// @param makerToken Address of the maker token (what to buy).
/// @param makerTokenAmounts Maker token sell amount for each sample.
/// @return takerTokenAmounts Taker amounts sold at each maker token
/// amount.
function sampleBuysFromUniswap(
address router,
address takerToken,
address makerToken,
uint256[] memory makerTokenAmounts
)
public
view
returns (uint256[] memory takerTokenAmounts)
{
_assertValidPair(makerToken, takerToken);
uint256 numSamples = makerTokenAmounts.length;
takerTokenAmounts = new uint256[](numSamples);
IUniswapExchangeQuotes takerTokenExchange = takerToken == address(0) ?
IUniswapExchangeQuotes(0) : _getUniswapExchange(router, takerToken);
IUniswapExchangeQuotes makerTokenExchange = makerToken == address(0) ?
IUniswapExchangeQuotes(0) : _getUniswapExchange(router, makerToken);
for (uint256 i = 0; i < numSamples; i++) {
bool didSucceed = true;
if (makerToken == address(0)) {
(takerTokenAmounts[i], didSucceed) = _callUniswapExchangePriceFunction(
address(takerTokenExchange),
takerTokenExchange.getTokenToEthOutputPrice.selector,
makerTokenAmounts[i]
);
} else if (takerToken == address(0)) {
(takerTokenAmounts[i], didSucceed) = _callUniswapExchangePriceFunction(
address(makerTokenExchange),
makerTokenExchange.getEthToTokenOutputPrice.selector,
makerTokenAmounts[i]
);
} else {
uint256 ethSold;
(ethSold, didSucceed) = _callUniswapExchangePriceFunction(
address(makerTokenExchange),
makerTokenExchange.getEthToTokenOutputPrice.selector,
makerTokenAmounts[i]
);
if (ethSold != 0) {
(takerTokenAmounts[i], didSucceed) = _callUniswapExchangePriceFunction(
address(takerTokenExchange),
takerTokenExchange.getTokenToEthOutputPrice.selector,
ethSold
);
} else {
takerTokenAmounts[i] = 0;
}
}
// Break early if amounts are 0
if (!didSucceed || takerTokenAmounts[i] == 0) {
break;
}
}
}
/// @dev Gracefully calls a Uniswap pricing function.
/// @param uniswapExchangeAddress Address of an `IUniswapExchangeQuotes` exchange.
/// @param functionSelector Selector of the target function.
/// @param inputAmount Quantity parameter particular to the pricing function.
/// @return outputAmount The returned amount from the function call. Will be
/// zero if the call fails or if `uniswapExchangeAddress` is zero.
function _callUniswapExchangePriceFunction(
address uniswapExchangeAddress,
bytes4 functionSelector,
uint256 inputAmount
)
private
view
returns (uint256 outputAmount, bool didSucceed)
{
if (uniswapExchangeAddress == address(0)) {
return (outputAmount, didSucceed);
}
bytes memory resultData;
(didSucceed, resultData) =
uniswapExchangeAddress.staticcall.gas(UNISWAP_CALL_GAS)(
abi.encodeWithSelector(
functionSelector,
inputAmount
));
if (didSucceed) {
outputAmount = abi.decode(resultData, (uint256));
}
}
/// @dev Retrive an existing Uniswap exchange contract.
/// Throws if the exchange does not exist.
/// @param router Address of the Uniswap router.
/// @param tokenAddress Address of the token contract.
/// @return exchange `IUniswapExchangeQuotes` for the token.
function _getUniswapExchange(address router, address tokenAddress)
private
view
returns (IUniswapExchangeQuotes exchange)
{
exchange = IUniswapExchangeQuotes(
address(IUniswapExchangeFactory(router)
.getExchange(tokenAddress))
);
}
}

View File

@@ -1,102 +0,0 @@
// SPDX-License-Identifier: Apache-2.0
/*
Copyright 2020 ZeroEx Intl.
Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at
http://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
*/
pragma solidity ^0.6;
pragma experimental ABIEncoderV2;
import "./interfaces/IUniswapV2Router01.sol";
contract UniswapV2Sampler
{
/// @dev Gas limit for UniswapV2 calls.
uint256 constant private UNISWAPV2_CALL_GAS = 150e3; // 150k
/// @dev Sample sell quotes from UniswapV2.
/// @param router Router to look up tokens and amounts
/// @param path Token route. Should be takerToken -> makerToken
/// @param takerTokenAmounts Taker token sell amount for each sample.
/// @return makerTokenAmounts Maker amounts bought at each taker token
/// amount.
function sampleSellsFromUniswapV2(
address router,
address[] memory path,
uint256[] memory takerTokenAmounts
)
public
view
returns (uint256[] memory makerTokenAmounts)
{
uint256 numSamples = takerTokenAmounts.length;
makerTokenAmounts = new uint256[](numSamples);
for (uint256 i = 0; i < numSamples; i++) {
try
IUniswapV2Router01(router).getAmountsOut
{gas: UNISWAPV2_CALL_GAS}
(takerTokenAmounts[i], path)
returns (uint256[] memory amounts)
{
makerTokenAmounts[i] = amounts[path.length - 1];
// Break early if there are 0 amounts
if (makerTokenAmounts[i] == 0) {
break;
}
} catch (bytes memory) {
// Swallow failures, leaving all results as zero.
break;
}
}
}
/// @dev Sample buy quotes from UniswapV2.
/// @param router Router to look up tokens and amounts
/// @param path Token route. Should be takerToken -> makerToken.
/// @param makerTokenAmounts Maker token buy amount for each sample.
/// @return takerTokenAmounts Taker amounts sold at each maker token
/// amount.
function sampleBuysFromUniswapV2(
address router,
address[] memory path,
uint256[] memory makerTokenAmounts
)
public
view
returns (uint256[] memory takerTokenAmounts)
{
uint256 numSamples = makerTokenAmounts.length;
takerTokenAmounts = new uint256[](numSamples);
for (uint256 i = 0; i < numSamples; i++) {
try
IUniswapV2Router01(router).getAmountsIn
{gas: UNISWAPV2_CALL_GAS}
(makerTokenAmounts[i], path)
returns (uint256[] memory amounts)
{
takerTokenAmounts[i] = amounts[0];
// Break early if there are 0 amounts
if (takerTokenAmounts[i] == 0) {
break;
}
} catch (bytes memory) {
// Swallow failures, leaving all results as zero.
break;
}
}
}
}

View File

@@ -1,313 +0,0 @@
// SPDX-License-Identifier: Apache-2.0
/*
Copyright 2021 ZeroEx Intl.
Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at
http://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
*/
pragma solidity ^0.6;
pragma experimental ABIEncoderV2;
import "@0x/contracts-erc20/contracts/src/v06/IERC20TokenV06.sol";
interface IUniswapV3Quoter {
function factory()
external
view
returns (IUniswapV3Factory factory);
function quoteExactInput(bytes memory path, uint256 amountIn)
external
returns (uint256 amountOut);
function quoteExactOutput(bytes memory path, uint256 amountOut)
external
returns (uint256 amountIn);
}
interface IUniswapV3Factory {
function getPool(IERC20TokenV06 a, IERC20TokenV06 b, uint24 fee)
external
view
returns (IUniswapV3Pool pool);
}
interface IUniswapV3Pool {
function token0() external view returns (IERC20TokenV06);
function token1() external view returns (IERC20TokenV06);
function fee() external view returns (uint24);
}
contract UniswapV3Sampler
{
/// @dev Gas limit for UniswapV3 calls. This is 100% a guess.
uint256 constant private QUOTE_GAS = 300e3;
/// @dev Sample sell quotes from UniswapV3.
/// @param quoter UniswapV3 Quoter contract.
/// @param path Token route. Should be takerToken -> makerToken
/// @param takerTokenAmounts Taker token sell amount for each sample.
/// @return uniswapPaths The encoded uniswap path for each sample.
/// @return makerTokenAmounts Maker amounts bought at each taker token
/// amount.
function sampleSellsFromUniswapV3(
IUniswapV3Quoter quoter,
IERC20TokenV06[] memory path,
uint256[] memory takerTokenAmounts
)
public
returns (
bytes[] memory uniswapPaths,
uint256[] memory makerTokenAmounts
)
{
IUniswapV3Pool[][] memory poolPaths =
_getValidPoolPaths(quoter.factory(), path, 0);
makerTokenAmounts = new uint256[](takerTokenAmounts.length);
uniswapPaths = new bytes[](takerTokenAmounts.length);
for (uint256 i = 0; i < takerTokenAmounts.length; ++i) {
// Pick the best result from all the paths.
bytes memory topUniswapPath;
uint256 topBuyAmount = 0;
for (uint256 j = 0; j < poolPaths.length; ++j) {
bytes memory uniswapPath = _toUniswapPath(path, poolPaths[j]);
try
quoter.quoteExactInput
{ gas: QUOTE_GAS }
(uniswapPath, takerTokenAmounts[i])
returns (uint256 buyAmount)
{
if (topBuyAmount <= buyAmount) {
topBuyAmount = buyAmount;
topUniswapPath = uniswapPath;
}
} catch { }
}
// Break early if we can't complete the buys.
if (topBuyAmount == 0) {
break;
}
makerTokenAmounts[i] = topBuyAmount;
uniswapPaths[i] = topUniswapPath;
}
}
/// @dev Sample buy quotes from UniswapV3.
/// @param quoter UniswapV3 Quoter contract.
/// @param path Token route. Should be takerToken -> makerToken.
/// @param makerTokenAmounts Maker token buy amount for each sample.
/// @return uniswapPaths The encoded uniswap path for each sample.
/// @return takerTokenAmounts Taker amounts sold at each maker token
/// amount.
function sampleBuysFromUniswapV3(
IUniswapV3Quoter quoter,
IERC20TokenV06[] memory path,
uint256[] memory makerTokenAmounts
)
public
returns (
bytes[] memory uniswapPaths,
uint256[] memory takerTokenAmounts
)
{
IUniswapV3Pool[][] memory poolPaths =
_getValidPoolPaths(quoter.factory(), path, 0);
IERC20TokenV06[] memory reversedPath = _reverseTokenPath(path);
takerTokenAmounts = new uint256[](makerTokenAmounts.length);
uniswapPaths = new bytes[](makerTokenAmounts.length);
for (uint256 i = 0; i < makerTokenAmounts.length; ++i) {
// Pick the best result from all the paths.
bytes memory topUniswapPath;
uint256 topSellAmount = 0;
for (uint256 j = 0; j < poolPaths.length; ++j) {
// quoter requires path to be reversed for buys.
bytes memory uniswapPath = _toUniswapPath(
reversedPath,
_reversePoolPath(poolPaths[j])
);
try
quoter.quoteExactOutput
{ gas: QUOTE_GAS }
(uniswapPath, makerTokenAmounts[i])
returns (uint256 sellAmount)
{
if (topSellAmount == 0 || topSellAmount >= sellAmount) {
topSellAmount = sellAmount;
// But the output path should still be encoded for sells.
topUniswapPath = _toUniswapPath(path, poolPaths[j]);
}
} catch {}
}
// Break early if we can't complete the buys.
if (topSellAmount == 0) {
break;
}
takerTokenAmounts[i] = topSellAmount;
uniswapPaths[i] = topUniswapPath;
}
}
function _getValidPoolPaths(
IUniswapV3Factory factory,
IERC20TokenV06[] memory tokenPath,
uint256 startIndex
)
private
view
returns (IUniswapV3Pool[][] memory poolPaths)
{
require(
tokenPath.length - startIndex >= 2,
"UniswapV3Sampler/tokenPath too short"
);
uint24[3] memory validPoolFees = [
// The launch pool fees. Could get hairier if they add more.
uint24(0.0005e6),
uint24(0.003e6),
uint24(0.01e6)
];
IUniswapV3Pool[] memory validPools =
new IUniswapV3Pool[](validPoolFees.length);
uint256 numValidPools = 0;
{
IERC20TokenV06 inputToken = tokenPath[startIndex];
IERC20TokenV06 outputToken = tokenPath[startIndex + 1];
for (uint256 i = 0; i < validPoolFees.length; ++i) {
IUniswapV3Pool pool =
factory.getPool(inputToken, outputToken, validPoolFees[i]);
if (_isValidPool(pool)) {
validPools[numValidPools++] = pool;
}
}
}
if (numValidPools == 0) {
// No valid pools for this hop.
return poolPaths;
}
if (startIndex + 2 == tokenPath.length) {
// End of path.
poolPaths = new IUniswapV3Pool[][](numValidPools);
for (uint256 i = 0; i < numValidPools; ++i) {
poolPaths[i] = new IUniswapV3Pool[](1);
poolPaths[i][0] = validPools[i];
}
return poolPaths;
}
// Get paths for subsequent hops.
IUniswapV3Pool[][] memory subsequentPoolPaths =
_getValidPoolPaths(factory, tokenPath, startIndex + 1);
if (subsequentPoolPaths.length == 0) {
// Could not complete the path.
return poolPaths;
}
// Combine our pools with the next hop paths.
poolPaths = new IUniswapV3Pool[][](
numValidPools * subsequentPoolPaths.length
);
for (uint256 i = 0; i < numValidPools; ++i) {
for (uint256 j = 0; j < subsequentPoolPaths.length; ++j) {
uint256 o = i * subsequentPoolPaths.length + j;
// Prepend pool to the subsequent path.
poolPaths[o] =
new IUniswapV3Pool[](1 + subsequentPoolPaths[j].length);
poolPaths[o][0] = validPools[i];
for (uint256 k = 0; k < subsequentPoolPaths[j].length; ++k) {
poolPaths[o][1 + k] = subsequentPoolPaths[j][k];
}
}
}
return poolPaths;
}
function _reverseTokenPath(IERC20TokenV06[] memory tokenPath)
private
returns (IERC20TokenV06[] memory reversed)
{
reversed = new IERC20TokenV06[](tokenPath.length);
for (uint256 i = 0; i < tokenPath.length; ++i) {
reversed[i] = tokenPath[tokenPath.length - i - 1];
}
}
function _reversePoolPath(IUniswapV3Pool[] memory poolPath)
private
returns (IUniswapV3Pool[] memory reversed)
{
reversed = new IUniswapV3Pool[](poolPath.length);
for (uint256 i = 0; i < poolPath.length; ++i) {
reversed[i] = poolPath[poolPath.length - i - 1];
}
}
function _isValidPool(IUniswapV3Pool pool)
private
view
returns (bool isValid)
{
// Check if it has been deployed.
{
uint256 codeSize;
assembly {
codeSize := extcodesize(pool)
}
if (codeSize == 0) {
return false;
}
}
// Must have a balance of both tokens.
if (pool.token0().balanceOf(address(pool)) == 0) {
return false;
}
if (pool.token1().balanceOf(address(pool)) == 0) {
return false;
}
return true;
}
function _toUniswapPath(
IERC20TokenV06[] memory tokenPath,
IUniswapV3Pool[] memory poolPath
)
private
view
returns (bytes memory uniswapPath)
{
require(
tokenPath.length >= 2 && tokenPath.length == poolPath.length + 1,
"UniswapV3Sampler/invalid path lengths"
);
// Uniswap paths are tightly packed as:
// [token0, token0token1PairFee, token1, token1Token2PairFee, token2, ...]
uniswapPath = new bytes(tokenPath.length * 20 + poolPath.length * 3);
uint256 o;
assembly { o := add(uniswapPath, 32) }
for (uint256 i = 0; i < tokenPath.length; ++i) {
if (i > 0) {
uint24 poolFee = poolPath[i - 1].fee();
assembly {
mstore(o, shl(232, poolFee))
o := add(o, 3)
}
}
IERC20TokenV06 token = tokenPath[i];
assembly {
mstore(o, shl(96, token))
o := add(o, 20)
}
}
}
}

View File

@@ -1,80 +0,0 @@
// SPDX-License-Identifier: Apache-2.0
/*
Copyright 2021 ZeroEx Intl.
Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at
http://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
*/
pragma solidity ^0.6;
pragma experimental ABIEncoderV2;
import "@0x/contracts-erc20/contracts/src/v06/LibERC20TokenV06.sol";
contract UtilitySampler {
using LibERC20TokenV06 for IERC20TokenV06;
IERC20TokenV06 private immutable UTILITY_ETH_ADDRESS = IERC20TokenV06(0xEeeeeEeeeEeEeeEeEeEeeEEEeeeeEeeeeeeeEEeE);
function getTokenDecimals(IERC20TokenV06[] memory tokens)
public
view
returns (uint256[] memory decimals)
{
decimals = new uint256[](tokens.length);
for (uint256 i = 0; i != tokens.length; i++) {
decimals[i] = tokens[i] == UTILITY_ETH_ADDRESS
? 18
: tokens[i].compatDecimals();
}
}
function getBalanceOf(IERC20TokenV06[] memory tokens, address account)
public
view
returns (uint256[] memory balances)
{
balances = new uint256[](tokens.length);
for (uint256 i = 0; i != tokens.length; i++) {
balances[i] = tokens[i] == UTILITY_ETH_ADDRESS
? account.balance
: tokens[i].compatBalanceOf(account);
}
}
function getAllowanceOf(IERC20TokenV06[] memory tokens, address account, address spender)
public
view
returns (uint256[] memory allowances)
{
allowances = new uint256[](tokens.length);
for (uint256 i = 0; i != tokens.length; i++) {
allowances[i] = tokens[i] == UTILITY_ETH_ADDRESS
? 0
: tokens[i].compatAllowance(account, spender);
}
}
function isContract(address account)
public
view
returns (bool)
{
uint256 size;
assembly { size := extcodesize(account) }
return size > 0;
}
}

View File

@@ -1,44 +0,0 @@
// SPDX-License-Identifier: Apache-2.0
/*
Copyright 2020 ZeroEx Intl.
Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at
http://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
*/
pragma solidity ^0.6;
interface IBalancer {
function isBound(address t) external view returns (bool);
function getDenormalizedWeight(address token) external view returns (uint256);
function getBalance(address token) external view returns (uint256);
function getSwapFee() external view returns (uint256);
function calcOutGivenIn(
uint256 tokenBalanceIn,
uint256 tokenWeightIn,
uint256 tokenBalanceOut,
uint256 tokenWeightOut,
uint256 tokenAmountIn,
uint256 swapFee
) external pure returns (uint256 tokenAmountOut);
function calcInGivenOut(
uint256 tokenBalanceIn,
uint256 tokenWeightIn,
uint256 tokenBalanceOut,
uint256 tokenWeightOut,
uint256 tokenAmountOut,
uint256 swapFee
) external pure returns (uint256 tokenAmountIn);
}

View File

@@ -1,33 +0,0 @@
// SPDX-License-Identifier: Apache-2.0
/*
Copyright 2020 ZeroEx Intl.
Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at
http://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
*/
pragma solidity ^0.6;
interface IBancor {}
interface IBancorNetwork {
function conversionPath(address _sourceToken, address _targetToken) external view returns (address[] memory);
function rateByPath(address[] memory _path, uint256 _amount) external view returns (uint256);
}
interface IBancorRegistry {
function getAddress(bytes32 _contractName) external view returns (address);
function BANCOR_NETWORK() external view returns (bytes32);
}

View File

@@ -1,72 +0,0 @@
// SPDX-License-Identifier: Apache-2.0
/*
Copyright 2020 ZeroEx Intl.
Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at
http://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
*/
pragma solidity ^0.6;
// solhint-disable func-name-mixedcase
interface ICurve {
/// @dev Sell `sellAmount` of `fromToken` token and receive `toToken` token.
/// This function exists on later versions of Curve (USDC/DAI/USDT)
/// @param i The token index being sold.
/// @param j The token index being bought.
/// @param sellAmount The amount of token being bought.
/// @param minBuyAmount The minimum buy amount of the token being bought.
function exchange_underlying(
int128 i,
int128 j,
uint256 sellAmount,
uint256 minBuyAmount
)
external;
/// @dev Get the amount of `toToken` by selling `sellAmount` of `fromToken`
/// @param i The token index being sold.
/// @param j The token index being bought.
/// @param sellAmount The amount of token being bought.
function get_dy_underlying(
int128 i,
int128 j,
uint256 sellAmount
)
external
returns (uint256 dy);
/// @dev Get the amount of `fromToken` by buying `buyAmount` of `toToken`
/// This function exists on later versions of Curve (USDC/DAI/USDT)
/// @param i The token index being sold.
/// @param j The token index being bought.
/// @param buyAmount The amount of token being bought.
function get_dx_underlying(
int128 i,
int128 j,
uint256 buyAmount
)
external
returns (uint256 dx);
/// @dev Get the underlying token address from the token index
/// @param i The token index.
function underlying_coins(
int128 i
)
external
returns (address tokenAddress);
}

View File

@@ -1,96 +0,0 @@
// SPDX-License-Identifier: Apache-2.0
/*
Copyright 2020 ZeroEx Intl.
Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at
http://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
*/
pragma solidity ^0.6;
// Keepin everything together
interface IKyberNetwork {
}
interface IKyberNetworkProxy {
function getExpectedRateAfterFee(
address src,
address dest,
uint256 srcQty,
uint256 platformFeeBps,
bytes calldata hint
)
external
view
returns (uint256 expectedRate);
}
interface IKyberHintHandler {
enum TradeType {BestOfAll, MaskIn, MaskOut, Split}
enum ProcessWithRate {NotRequired, Required}
function getTradingReserves(
address tokenSrc,
address tokenDest,
bool isTokenToToken,
bytes calldata hint
)
external
view
returns (
bytes32[] memory reserveIds,
uint256[] memory splitValuesBps,
ProcessWithRate processWithRate
);
function buildTokenToEthHint(
address tokenSrc,
TradeType tokenToEthType,
bytes32[] calldata tokenToEthReserveIds,
uint256[] calldata tokenToEthSplits
)
external
view
returns (bytes memory hint);
function buildEthToTokenHint(
address tokenDest,
TradeType ethToTokenType,
bytes32[] calldata ethToTokenReserveIds,
uint256[] calldata ethToTokenSplits
)
external
view
returns (bytes memory hint);
function buildTokenToTokenHint(
address tokenSrc,
TradeType tokenToEthType,
bytes32[] calldata tokenToEthReserveIds,
uint256[] calldata tokenToEthSplits,
address tokenDest,
TradeType ethToTokenType,
bytes32[] calldata ethToTokenReserveIds,
uint256[] calldata ethToTokenSplits
)
external
view
returns (bytes memory hint);
}

View File

@@ -1,33 +0,0 @@
// SPDX-License-Identifier: Apache-2.0
/*
Copyright 2020 ZeroEx Intl.
Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at
http://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
*/
pragma solidity ^0.6;
interface IMStable {
function getSwapOutput(
address _input,
address _output,
uint256 _quantity
)
external
view
returns (uint256 swapOutput);
}

View File

@@ -1,38 +0,0 @@
// SPDX-License-Identifier: Apache-2.0
/*
Copyright 2020 ZeroEx Intl.
Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at
http://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
*/
pragma solidity ^0.6;
interface IMooniswapRegistry {
function pools(address token1, address token2) external view returns(address);
}
interface IMooniswap {
function getReturn(
address fromToken,
address destToken,
uint256 amount
)
external
view
returns(uint256 returnAmount);
}

View File

@@ -1,59 +0,0 @@
// SPDX-License-Identifier: Apache-2.0
/*
Copyright 2020 ZeroEx Intl.
Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at
http://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
*/
pragma solidity ^0.6;
interface IMultiBridge {
/// @dev Transfers `amount` of the ERC20 `tokenAddress` from `from` to `to`.
/// @param tokenAddress The address of the ERC20 token to transfer.
/// @param from Address to transfer asset from.
/// @param to Address to transfer asset to.
/// @param amount Amount of asset to transfer.
/// @param bridgeData Arbitrary asset data needed by the bridge contract.
/// @return success The magic bytes `0xdc1600f3` if successful.
function bridgeTransferFrom(
address tokenAddress,
address from,
address to,
uint256 amount,
bytes calldata bridgeData
)
external
returns (bytes4 success);
/// @dev Quotes the amount of `makerToken` that would be obtained by
/// selling `sellAmount` of `takerToken`.
/// @param takerToken Address of the taker token (what to sell).
/// @param intermediateToken The address of the intermediate token to
/// use in an indirect route.
/// @param makerToken Address of the maker token (what to buy).
/// @param sellAmount Amount of `takerToken` to sell.
/// @return makerTokenAmount Amount of `makerToken` that would be obtained.
function getSellQuote(
address takerToken,
address intermediateToken,
address makerToken,
uint256 sellAmount
)
external
view
returns (uint256 makerTokenAmount);
}

View File

@@ -1,43 +0,0 @@
// SPDX-License-Identifier: Apache-2.0
/*
Copyright 2020 ZeroEx Intl.
Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at
http://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
*/
pragma solidity ^0.6;
interface IShell {
function viewOriginSwap (
address from,
address to,
uint256 fromAmount
)
external
view
returns (uint256 toAmount);
function viewTargetSwap (
address from,
address to,
uint256 toAmount
)
external
view
returns (uint256 fromAmount);
}

View File

@@ -1,45 +0,0 @@
// SPDX-License-Identifier: Apache-2.0
/*
Copyright 2021 ZeroEx Intl.
Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at
http://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
*/
pragma solidity ^0.6;
interface ISmoothy {
function getBalance (
uint256 tid
)
external
view
returns (uint256 balance);
function _yBalances (
uint256 tid
)
external
view
returns (uint256 balance);
function getTokenStats (
uint256 tid
)
external
view
returns (uint256 softWeight, uint256 hardWeight, uint256 balance, uint256 decimals);
}

View File

@@ -1,52 +0,0 @@
// SPDX-License-Identifier: Apache-2.0
/*
Copyright 2020 ZeroEx Intl.
Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at
http://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
*/
pragma solidity ^0.6;
interface IUniswapExchangeQuotes {
function getEthToTokenInputPrice(
uint256 ethSold
)
external
view
returns (uint256 tokensBought);
function getEthToTokenOutputPrice(
uint256 tokensBought
)
external
view
returns (uint256 ethSold);
function getTokenToEthInputPrice(
uint256 tokensSold
)
external
view
returns (uint256 ethBought);
function getTokenToEthOutputPrice(
uint256 ethBought
)
external
view
returns (uint256 tokensSold);
}

View File

@@ -1,34 +0,0 @@
// SPDX-License-Identifier: Apache-2.0
/*
Copyright 2020 ZeroEx Intl.
Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at
http://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
*/
pragma solidity ^0.6;
interface IUniswapV2Router01 {
function getAmountsOut(uint256 amountIn, address[] calldata path)
external
view
returns (uint256[] memory amounts);
function getAmountsIn(uint256 amountOut, address[] calldata path)
external
view
returns (uint256[] memory amounts);
}

View File

@@ -1,39 +0,0 @@
// SPDX-License-Identifier: Apache-2.0
pragma solidity ^0.6;
pragma experimental ABIEncoderV2;
contract DummyLiquidityProvider
{
/// @dev Quotes the amount of `makerToken` that would be obtained by
/// selling `sellAmount` of `takerToken`.
/// @param sellAmount Amount of `takerToken` to sell.
/// @return makerTokenAmount Amount of `makerToken` that would be obtained.
function getSellQuote(
address, /* takerToken */
address, /* makerToken */
uint256 sellAmount
)
external
view
returns (uint256 makerTokenAmount)
{
makerTokenAmount = sellAmount - 1;
}
/// @dev Quotes the amount of `takerToken` that would need to be sold in
/// order to obtain `buyAmount` of `makerToken`.
/// @param buyAmount Amount of `makerToken` to buy.
/// @return takerTokenAmount Amount of `takerToken` that would need to be sold.
function getBuyQuote(
address, /* takerToken */
address, /* makerToken */
uint256 buyAmount
)
external
view
returns (uint256 takerTokenAmount)
{
takerTokenAmount = buyAmount + 1;
}
}

View File

@@ -1,455 +0,0 @@
// SPDX-License-Identifier: Apache-2.0
/*
Copyright 2020 ZeroEx Intl.
Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at
http://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
*/
pragma solidity ^0.6;
pragma experimental ABIEncoderV2;
import "../src/ERC20BridgeSampler.sol";
import "../src/interfaces/IKyberNetwork.sol";
import "../src/interfaces/IUniswapV2Router01.sol";
library LibDeterministicQuotes {
address private constant WETH_ADDRESS = 0xC02aaA39b223FE8D0A0e5C4F27eAD9083C756Cc2;
uint256 private constant RATE_DENOMINATOR = 1 ether;
uint256 private constant MIN_RATE = RATE_DENOMINATOR / 100;
uint256 private constant MAX_RATE = 100 * RATE_DENOMINATOR;
uint8 private constant MIN_DECIMALS = 4;
uint8 private constant MAX_DECIMALS = 20;
function getDeterministicSellQuote(
bytes32 salt,
address sellToken,
address buyToken,
uint256 sellAmount
)
internal
pure
returns (uint256 buyAmount)
{
uint256 sellBase = uint256(10) ** getDeterministicTokenDecimals(sellToken);
uint256 buyBase = uint256(10) ** getDeterministicTokenDecimals(buyToken);
uint256 rate = getDeterministicRate(salt, sellToken, buyToken);
return sellAmount * rate * buyBase / sellBase / RATE_DENOMINATOR;
}
function getDeterministicBuyQuote(
bytes32 salt,
address sellToken,
address buyToken,
uint256 buyAmount
)
internal
pure
returns (uint256 sellAmount)
{
uint256 sellBase = uint256(10) ** getDeterministicTokenDecimals(sellToken);
uint256 buyBase = uint256(10) ** getDeterministicTokenDecimals(buyToken);
uint256 rate = getDeterministicRate(salt, sellToken, buyToken);
return buyAmount * RATE_DENOMINATOR * sellBase / rate / buyBase;
}
function getDeterministicTokenDecimals(address token)
internal
pure
returns (uint8 decimals)
{
if (token == WETH_ADDRESS) {
return 18;
}
bytes32 seed = keccak256(abi.encodePacked(token));
return uint8(uint256(seed) % (MAX_DECIMALS - MIN_DECIMALS)) + MIN_DECIMALS;
}
function getDeterministicRate(bytes32 salt, address sellToken, address buyToken)
internal
pure
returns (uint256 rate)
{
bytes32 seed = keccak256(abi.encodePacked(salt, sellToken, buyToken));
return uint256(seed) % (MAX_RATE - MIN_RATE) + MIN_RATE;
}
}
contract TestDeploymentConstants {
// solhint-disable separate-by-one-line-in-contract
// Mainnet addresses ///////////////////////////////////////////////////////
/// @dev Mainnet address of the WETH contract.
address constant private WETH_ADDRESS = 0xC02aaA39b223FE8D0A0e5C4F27eAD9083C756Cc2;
/// @dev Overridable way to get the WETH address.
/// @return wethAddress The WETH address.
function _getWethAddress()
internal
view
returns (address wethAddress)
{
return WETH_ADDRESS;
}
}
contract FailTrigger {
// Give this address a balance to force operations to fail.
address payable constant public FAILURE_ADDRESS = 0xe9dB8717BC5DFB20aaf538b4a5a02B7791FF430C;
// Funds `FAILURE_ADDRESS`.
function enableFailTrigger() external payable {
FAILURE_ADDRESS.transfer(msg.value);
}
function _revertIfShouldFail() internal view {
if (FAILURE_ADDRESS.balance != 0) {
revert("FAIL_TRIGGERED");
}
}
}
contract TestERC20BridgeSamplerUniswapExchange is
IUniswapExchangeQuotes,
TestDeploymentConstants,
FailTrigger
{
bytes32 constant private BASE_SALT = 0x1d6a6a0506b0b4a554b907a4c29d9f4674e461989d9c1921feb17b26716385ab;
address public tokenAddress;
bytes32 public salt;
constructor(address _tokenAddress) public {
tokenAddress = _tokenAddress;
salt = keccak256(abi.encodePacked(BASE_SALT, _tokenAddress));
}
// Deterministic `IUniswapExchangeQuotes.getEthToTokenInputPrice()`.
function getEthToTokenInputPrice(
uint256 ethSold
)
override
external
view
returns (uint256 tokensBought)
{
_revertIfShouldFail();
return LibDeterministicQuotes.getDeterministicSellQuote(
salt,
tokenAddress,
_getWethAddress(),
ethSold
);
}
// Deterministic `IUniswapExchangeQuotes.getEthToTokenOutputPrice()`.
function getEthToTokenOutputPrice(
uint256 tokensBought
)
override
external
view
returns (uint256 ethSold)
{
_revertIfShouldFail();
return LibDeterministicQuotes.getDeterministicBuyQuote(
salt,
_getWethAddress(),
tokenAddress,
tokensBought
);
}
// Deterministic `IUniswapExchangeQuotes.getTokenToEthInputPrice()`.
function getTokenToEthInputPrice(
uint256 tokensSold
)
override
external
view
returns (uint256 ethBought)
{
_revertIfShouldFail();
return LibDeterministicQuotes.getDeterministicSellQuote(
salt,
tokenAddress,
_getWethAddress(),
tokensSold
);
}
// Deterministic `IUniswapExchangeQuotes.getTokenToEthOutputPrice()`.
function getTokenToEthOutputPrice(
uint256 ethBought
)
override
external
view
returns (uint256 tokensSold)
{
_revertIfShouldFail();
return LibDeterministicQuotes.getDeterministicBuyQuote(
salt,
_getWethAddress(),
tokenAddress,
ethBought
);
}
}
contract TestERC20BridgeSamplerUniswapV2Router01 is
IUniswapV2Router01,
TestDeploymentConstants,
FailTrigger
{
bytes32 constant private SALT = 0xadc7fcb33c735913b8635927e66896b356a53a912ab2ceff929e60a04b53b3c1;
// Deterministic `IUniswapV2Router01.getAmountsOut()`.
function getAmountsOut(uint256 amountIn, address[] calldata path)
override
external
view
returns (uint256[] memory amounts)
{
require(path.length >= 2, "PATH_TOO_SHORT");
_revertIfShouldFail();
amounts = new uint256[](path.length);
amounts[0] = amountIn;
for (uint256 i = 0; i < path.length - 1; ++i) {
amounts[i + 1] = LibDeterministicQuotes.getDeterministicSellQuote(
SALT,
path[i],
path[i + 1],
amounts[i]
);
}
}
// Deterministic `IUniswapV2Router01.getAmountsInt()`.
function getAmountsIn(uint256 amountOut, address[] calldata path)
override
external
view
returns (uint256[] memory amounts)
{
require(path.length >= 2, "PATH_TOO_SHORT");
_revertIfShouldFail();
amounts = new uint256[](path.length);
amounts[path.length - 1] = amountOut;
for (uint256 i = path.length - 1; i > 0; --i) {
amounts[i - 1] = LibDeterministicQuotes.getDeterministicBuyQuote(
SALT,
path[i - 1],
path[i],
amounts[i]
);
}
}
}
// solhint-disable space-after-comma
contract TestERC20BridgeSamplerKyberNetwork is
TestDeploymentConstants,
FailTrigger
{
bytes32 constant private SALT = 0x0ff3ca9d46195c39f9a12afb74207b4970349fb3cfb1e459bbf170298d326bc7;
address constant public ETH_ADDRESS = 0xEeeeeEeeeEeEeeEeEeEeeEEEeeeeEeeeeeeeEEeE;
enum TradeType {BestOfAll, MaskIn, MaskOut, Split}
enum ProcessWithRate {NotRequired, Required}
// IKyberHintHandler
function buildTokenToEthHint(
address tokenSrc,
TradeType /* tokenToEthType */,
bytes32[] calldata /* tokenToEthReserveIds */,
uint256[] calldata /* tokenToEthSplits */
) external view returns (bytes memory hint)
{
return abi.encode(tokenSrc);
}
function buildEthToTokenHint(
address tokenDest,
TradeType /* ethToTokenType */,
bytes32[] calldata /* ethToTokenReserveIds */,
uint256[] calldata /* ethToTokenSplits */
) external view returns (bytes memory hint)
{
return abi.encode(tokenDest);
}
// IKyberHintHandler
function buildTokenToTokenHint(
address tokenSrc,
TradeType /* tokenToEthType */,
bytes32[] calldata /* tokenToEthReserveIds */,
uint256[] calldata /* tokenToEthSplits */,
address /* tokenDest */,
TradeType /* EthToTokenType */,
bytes32[] calldata /* EthToTokenReserveIds */,
uint256[] calldata /* EthToTokenSplits */
) external view returns (bytes memory hint)
{
return abi.encode(tokenSrc);
}
// IKyberHintHandler
function getTradingReserves(
address tokenSrc,
address tokenDest,
bool isTokenToToken,
bytes calldata hint
)
external
view
returns (
bytes32[] memory reserveIds,
uint256[] memory splitValuesBps,
ProcessWithRate processWithRate
)
{
reserveIds = new bytes32[](1);
reserveIds[0] = bytes32(uint256(1));
splitValuesBps = new uint256[](0);
processWithRate = ProcessWithRate.NotRequired;
}
// Deterministic `IKyberNetworkProxy.getExpectedRateAfterFee()`.
function getExpectedRateAfterFee(
address fromToken,
address toToken,
uint256 /* srcQty */,
uint256 /* fee */,
bytes calldata /* hint */
)
external
view
returns
(uint256 expectedRate)
{
_revertIfShouldFail();
fromToken = fromToken == ETH_ADDRESS ? _getWethAddress() : fromToken;
toToken = toToken == ETH_ADDRESS ? _getWethAddress() : toToken;
expectedRate = LibDeterministicQuotes.getDeterministicRate(
SALT,
fromToken,
toToken
);
}
// Deterministic `IKyberNetworkProxy.getExpectedRate()`.
function getExpectedRate(
address fromToken,
address toToken,
uint256
)
external
view
returns (uint256 expectedRate, uint256)
{
_revertIfShouldFail();
fromToken = fromToken == ETH_ADDRESS ? _getWethAddress() : fromToken;
toToken = toToken == ETH_ADDRESS ? _getWethAddress() : toToken;
expectedRate = LibDeterministicQuotes.getDeterministicRate(
SALT,
fromToken,
toToken
);
}
}
contract TestERC20BridgeSamplerUniswapExchangeFactory is
IUniswapExchangeFactory
{
mapping (address => IUniswapExchangeQuotes) private _exchangesByToken;
// Creates Uniswap exchange contracts for tokens.
function createTokenExchanges(address[] calldata tokenAddresses)
external
{
for (uint256 i = 0; i < tokenAddresses.length; i++) {
address tokenAddress = tokenAddresses[i];
_exchangesByToken[tokenAddress] =
new TestERC20BridgeSamplerUniswapExchange(tokenAddress);
}
}
// `IUniswapExchangeFactory.getExchange()`.
function getExchange(address tokenAddress)
override
external
view
returns (address)
{
return address(_exchangesByToken[tokenAddress]);
}
}
contract TestERC20BridgeSampler is
ERC20BridgeSampler,
FailTrigger
{
TestERC20BridgeSamplerUniswapExchangeFactory public uniswap;
TestERC20BridgeSamplerUniswapV2Router01 public uniswapV2Router;
TestERC20BridgeSamplerKyberNetwork public kyber;
uint8 private constant MAX_ORDER_STATUS = uint8(IExchange.OrderStatus.CANCELLED) + 1;
constructor() public ERC20BridgeSampler() {
uniswap = new TestERC20BridgeSamplerUniswapExchangeFactory();
uniswapV2Router = new TestERC20BridgeSamplerUniswapV2Router01();
kyber = new TestERC20BridgeSamplerKyberNetwork();
}
// Creates Uniswap exchange contracts for tokens.
function createTokenExchanges(address[] calldata tokenAddresses)
external
{
uniswap.createTokenExchanges(tokenAddresses);
}
// Overridden to return deterministic states.
function getLimitOrderFillableTakerAmount(
IExchange.LimitOrder memory order,
IExchange.Signature memory,
IExchange
)
override
public
view
returns (uint256 fillableTakerAmount)
{
return uint256(keccak256(abi.encode(order.salt))) % order.takerAmount;
}
// Overriden to return deterministic decimals.
function _getTokenDecimals(address tokenAddress)
override
internal
view
returns (uint8 decimals)
{
return LibDeterministicQuotes.getDeterministicTokenDecimals(tokenAddress);
}
}

View File

@@ -1,135 +0,0 @@
// SPDX-License-Identifier: Apache-2.0
/*
Copyright 2019 ZeroEx Intl.
Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at
http://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
*/
pragma solidity ^0.6;
pragma experimental ABIEncoderV2;
import "../src/NativeOrderSampler.sol";
import "../src/UtilitySampler.sol";
contract TestNativeOrderSamplerToken {
mapping (address => uint256) public balanceOf;
mapping (address => mapping(address => uint256)) public allowance;
function setBalanceAndAllowance(
address owner,
address spender,
uint256 balance,
uint256 allowance_
)
external
{
balanceOf[owner] = balance;
allowance[owner][spender] = allowance_;
}
}
contract TestNativeOrderSampler is
NativeOrderSampler,
UtilitySampler
{
uint8 private constant MAX_ORDER_STATUS = uint8(IExchange.OrderStatus.CANCELLED) + 1;
bytes32 private constant VALID_SIGNATURE_HASH = bytes32(hex"01");
function createTokens(uint256 count)
external
returns (TestNativeOrderSamplerToken[] memory tokens)
{
tokens = new TestNativeOrderSamplerToken[](count);
for (uint256 i = 0; i < count; ++i) {
tokens[i] = new TestNativeOrderSamplerToken();
}
}
function setTokenBalanceAndAllowance(
TestNativeOrderSamplerToken token,
address owner,
address spender,
uint256 balance,
uint256 allowance
)
external
{
token.setBalanceAndAllowance(owner, spender, balance, allowance);
}
// IExchange.getLimitOrderRelevantState()
function getLimitOrderRelevantState(
IExchange.LimitOrder memory order,
IExchange.Signature calldata signature
)
external
view
returns (
IExchange.OrderInfo memory orderInfo,
uint128 actualFillableTakerTokenAmount,
bool isSignatureValid
)
{
// The order salt determines everything.
orderInfo.orderHash = keccak256(abi.encode(order.salt));
if (uint8(order.salt) == 0xFF) {
orderInfo.status = IExchange.OrderStatus.FILLED;
} else {
orderInfo.status = IExchange.OrderStatus.FILLABLE;
}
isSignatureValid = signature.r == VALID_SIGNATURE_HASH;
// The expiration time is the filled taker asset amount.
orderInfo.takerTokenFilledAmount = uint128(order.expiry);
// Calculate how much is fillable in maker terms given the filled taker amount
uint256 fillableMakerTokenAmount = LibMathV06.getPartialAmountFloor(
uint256(
order.takerAmount
- orderInfo.takerTokenFilledAmount
),
uint256(order.takerAmount),
uint256(order.makerAmount)
);
// Take the min of the balance/allowance and the fillable maker amount
fillableMakerTokenAmount = LibSafeMathV06.min256(
fillableMakerTokenAmount,
_getSpendableERC20BalanceOf(order.makerToken, order.maker)
);
// Convert to taker terms
actualFillableTakerTokenAmount = LibMathV06.getPartialAmountCeil(
fillableMakerTokenAmount,
uint256(order.makerAmount),
uint256(order.takerAmount)
).safeDowncastToUint128();
}
function _getSpendableERC20BalanceOf(
IERC20TokenV06 token,
address owner
)
internal
view
returns (uint256)
{
return LibSafeMathV06.min256(
token.allowance(owner, address(this)),
token.balanceOf(owner)
);
}
}

View File

@@ -1,6 +1,6 @@
{
"name": "@0x/asset-swapper",
"version": "16.27.0",
"version": "16.30.0",
"engines": {
"node": ">=6.12"
},
@@ -39,7 +39,7 @@
"config": {
"publicInterfaceContracts": "ERC20BridgeSampler,BalanceChecker,FakeTaker",
"abis:comment": "This list is auto-generated by contracts-gen. Don't edit manually.",
"abis": "./test/generated-artifacts/@(ApproximateBuys|BalanceChecker|BalancerSampler|BalancerV2Sampler|BancorSampler|CurveSampler|DODOSampler|DODOV2Sampler|DummyLiquidityProvider|ERC20BridgeSampler|FakeTaker|IBalancer|IBancor|ICurve|IKyberNetwork|IMStable|IMooniswap|IMultiBridge|IShell|ISmoothy|IUniswapExchangeQuotes|IUniswapV2Router01|KyberDmmSampler|KyberSampler|LidoSampler|LiquidityProviderSampler|MStableSampler|MakerPSMSampler|MooniswapSampler|MultiBridgeSampler|NativeOrderSampler|SamplerUtils|ShellSampler|SmoothySampler|TestERC20BridgeSampler|TestNativeOrderSampler|TwoHopSampler|UniswapSampler|UniswapV2Sampler|UniswapV3Sampler|UtilitySampler).json",
"abis": "./test/generated-artifacts/@(BalanceChecker|FakeTaker).json",
"postpublish": {
"assets": []
}
@@ -58,20 +58,21 @@
"registry": "git@github.com:0xProject/gitpkg-registry.git"
},
"dependencies": {
"@0x/assert": "^3.0.27",
"@0x/base-contract": "^6.4.0",
"@0x/contract-addresses": "^6.7.0",
"@0x/contract-wrappers": "^13.17.6",
"@0x/contracts-erc20": "^3.3.18",
"@0x/contracts-zero-ex": "^0.27.0",
"@0x/dev-utils": "^4.2.7",
"@0x/json-schemas": "^6.1.3",
"@0x/protocol-utils": "^1.8.4",
"@0x/quote-server": "^6.0.2",
"@0x/types": "^3.3.3",
"@0x/typescript-typings": "^5.2.0",
"@0x/utils": "^6.4.3",
"@0x/web3-wrapper": "^7.5.3",
"@0x/assert": "^3.0.29",
"@0x/base-contract": "^6.4.2",
"@0x/contract-addresses": "^6.8.0",
"@0x/contract-wrappers": "^13.18.1",
"@0x/contracts-erc20": "^3.3.21",
"@0x/contracts-zero-ex": "^0.29.2",
"@0x/dev-utils": "^4.2.9",
"@0x/json-schemas": "^6.3.0",
"@0x/neon-router": "^0.2.1",
"@0x/protocol-utils": "^1.9.3",
"@0x/quote-server": "^6.0.6",
"@0x/types": "^3.3.4",
"@0x/typescript-typings": "^5.2.1",
"@0x/utils": "^6.4.4",
"@0x/web3-wrapper": "^7.6.0",
"@balancer-labs/sor": "0.3.2",
"@bancor/sdk": "0.2.9",
"@ethersproject/abi": "^5.0.1",
@@ -79,11 +80,12 @@
"@ethersproject/contracts": "^5.0.1",
"@ethersproject/providers": "^5.0.4",
"@ethersproject/strings": "^5.0.10",
"axios": "^0.21.1",
"axios-mock-adapter": "^1.19.0",
"@open-rpc/client-js": "^1.7.1",
"axios": "^0.24.0",
"axios-mock-adapter": "^1.20.0",
"cream-sor": "^0.3.3",
"decimal.js": "^10.2.0",
"ethereum-types": "^3.5.0",
"ethereum-types": "^3.6.0",
"ethereumjs-util": "^7.0.10",
"fast-abi": "^0.0.2",
"graphql": "^15.4.0",
@@ -92,20 +94,20 @@
"lodash": "^4.17.11"
},
"devDependencies": {
"@0x/abi-gen": "^5.6.0",
"@0x/abi-gen": "^5.6.2",
"@0x/contracts-asset-proxy": "^3.7.19",
"@0x/contracts-exchange": "^3.2.38",
"@0x/contracts-exchange-libs": "^4.3.37",
"@0x/contracts-gen": "^2.0.38",
"@0x/contracts-test-utils": "^5.4.10",
"@0x/contracts-utils": "^4.7.18",
"@0x/contracts-gen": "^2.0.40",
"@0x/contracts-test-utils": "^5.4.12",
"@0x/contracts-utils": "^4.8.2",
"@0x/mesh-rpc-client": "^9.4.2",
"@0x/migrations": "^8.1.4",
"@0x/sol-compiler": "^4.7.3",
"@0x/subproviders": "^6.5.3",
"@0x/migrations": "^8.1.9",
"@0x/sol-compiler": "^4.7.5",
"@0x/subproviders": "^6.6.0",
"@0x/ts-doc-gen": "^0.0.28",
"@0x/tslint-config": "^4.1.4",
"@0x/types": "^3.3.3",
"@0x/types": "^3.3.4",
"@types/lodash": "4.14.104",
"@types/mocha": "^5.2.7",
"@types/node": "12.12.54",

View File

@@ -1,4 +1,3 @@
import { ChainId } from '@0x/contract-addresses';
import { SignatureType } from '@0x/protocol-utils';
import { BigNumber, logUtils } from '@0x/utils';
@@ -11,12 +10,9 @@ import {
RfqRequestOpts,
SwapQuoteGetOutputOpts,
SwapQuoteRequestOpts,
SwapQuoterOpts,
} from './types';
import {
DEFAULT_GET_MARKET_ORDERS_OPTS,
DEFAULT_INTERMEDIATE_TOKENS_BY_CHAIN_ID,
DEFAULT_TOKEN_ADJACENCY_GRAPH_BY_CHAIN_ID,
} from './utils/market_operation_utils/constants';
const ETH_GAS_STATION_API_URL = 'https://ethgasstation.info/api/ethgasAPI.json';
@@ -38,25 +34,11 @@ const DEFAULT_ORDER_PRUNER_OPTS: OrderPrunerOpts = {
// 6 seconds polling interval
const PROTOCOL_FEE_UTILS_POLLING_INTERVAL_IN_MS = 6000;
const PROTOCOL_FEE_MULTIPLIER = new BigNumber(70000);
const PROTOCOL_FEE_MULTIPLIER = new BigNumber(0);
// default 50% buffer for selecting native orders to be aggregated with other sources
const MARKET_UTILS_AMOUNT_BUFFER_PERCENTAGE = 0.5;
const DEFAULT_SWAP_QUOTER_OPTS: SwapQuoterOpts = {
chainId: ChainId.Mainnet,
orderRefreshIntervalMs: 10000, // 10 seconds
...DEFAULT_ORDER_PRUNER_OPTS,
samplerGasLimit: 500e6,
ethGasStationUrl: ETH_GAS_STATION_API_URL,
rfqt: {
takerApiKeyWhitelist: [],
makerAssetOfferings: {},
txOriginBlacklist: new Set(),
},
tokenAdjacencyGraph: DEFAULT_TOKEN_ADJACENCY_GRAPH_BY_CHAIN_ID[ChainId.Mainnet],
};
const DEFAULT_EXCHANGE_PROXY_EXTENSION_CONTRACT_OPTS: ExchangeProxyContractOpts = {
isFromETH: false,
isToETH: false,
@@ -91,8 +73,6 @@ export const DEFAULT_WARNING_LOGGER: LogFunction = (obj, msg) =>
const EMPTY_BYTES32 = '0x0000000000000000000000000000000000000000000000000000000000000000';
export const INVALID_SIGNATURE = { signatureType: SignatureType.Invalid, v: 1, r: EMPTY_BYTES32, s: EMPTY_BYTES32 };
export { DEFAULT_FEE_SCHEDULE, DEFAULT_GAS_SCHEDULE } from './utils/market_operation_utils/constants';
export const POSITIVE_SLIPPAGE_FEE_TRANSFORMER_GAS = new BigNumber(30000);
// tslint:disable-next-line: custom-no-magic-numbers
@@ -111,8 +91,6 @@ export const constants = {
ONE_AMOUNT: new BigNumber(1),
ONE_SECOND_MS,
ONE_MINUTE_MS,
DEFAULT_SWAP_QUOTER_OPTS,
DEFAULT_INTERMEDIATE_TOKENS_BY_CHAIN_ID,
DEFAULT_SWAP_QUOTE_REQUEST_OPTS,
DEFAULT_EXCHANGE_PROXY_SWAP_QUOTE_GET_OPTS,
DEFAULT_EXCHANGE_PROXY_EXTENSION_CONTRACT_OPTS,

View File

@@ -88,6 +88,7 @@ export {
ExchangeProxyContractOpts,
ExchangeProxyRefundReceiver,
GetExtensionContractTypeOpts,
Integrator,
LogFunction,
MarketBuySwapQuote,
MarketOperation,
@@ -108,6 +109,7 @@ export {
SwapQuoteGetOutputOpts,
SwapQuoteInfo,
SwapQuoteOrdersBreakdown,
SwapQuoteMultiHopBreakdown,
SwapQuoteRequestOpts,
SwapQuoterError,
SwapQuoterOpts,
@@ -115,48 +117,24 @@ export {
} from './types';
export { affiliateFeeUtils } from './utils/affiliate_fee_utils';
export {
DEFAULT_TOKEN_ADJACENCY_GRAPH_BY_CHAIN_ID,
DEFAULT_GAS_SCHEDULE,
SOURCE_FLAGS,
BUY_SOURCE_FILTER_BY_CHAIN_ID,
SELL_SOURCE_FILTER_BY_CHAIN_ID,
NATIVE_FEE_TOKEN_BY_CHAIN_ID,
} from './utils/market_operation_utils/constants';
export {
Parameters,
SamplerContractCall,
SamplerContractOperation,
} from './utils/market_operation_utils/sampler_contract_operation';
export {
BalancerFillData,
BancorFillData,
CollapsedFill,
CurveFillData,
CurveFunctionSelectors,
CurveInfo,
DexSample,
DODOFillData,
ERC20BridgeSource,
ExchangeProxyOverhead,
FeeSchedule,
Fill,
FillData,
GetMarketOrdersRfqOpts,
KyberFillData,
LiquidityProviderFillData,
LiquidityProviderRegistry,
MarketDepth,
MarketDepthSide,
MooniswapFillData,
MultiHopFillData,
NativeCollapsedFill,
NativeRfqOrderFillData,
NativeLimitOrderFillData,
NativeFillData,
OptimizedMarketOrder,
SourceQuoteOperation,
TokenAdjacencyGraph,
UniswapV2FillData,
} from './utils/market_operation_utils/types';
export { ProtocolFeeUtils } from './utils/protocol_fee_utils';
export {

View File

@@ -1,6 +1,5 @@
import { ChainId, ContractAddresses } from '@0x/contract-addresses';
import { IZeroExContract, WETH9Contract } from '@0x/contract-wrappers';
import { MultiplexFeatureContract } from '@0x/contracts-zero-ex';
import { IZeroExContract } from '@0x/contract-wrappers';
import {
encodeAffiliateFeeTransformerData,
encodeCurveLiquidityProviderData,
@@ -13,12 +12,14 @@ import {
FillQuoteTransformerSide,
findTransformerNonce,
} from '@0x/protocol-utils';
import { BigNumber } from '@0x/utils';
import { BigNumber, hexUtils } from '@0x/utils';
import * as _ from 'lodash';
import { constants, POSITIVE_SLIPPAGE_FEE_TRANSFORMER_GAS } from '../constants';
import {
Address,
AffiliateFeeType,
Bytes,
CalldataInfo,
ExchangeProxyContractOpts,
MarketBuySwapQuote,
@@ -29,30 +30,31 @@ import {
SwapQuoteConsumerOpts,
SwapQuoteExecutionOpts,
SwapQuoteGetOutputOpts,
SwapQuoteLiquidityProviderBridgeOrder,
SwapQuoteUniswapV2BridgeOrder,
SwapQuoteUniswapV3BridgeOrder,
SwapQuoteCurveBridgeOrder,
SwapQuoteMooniswapBridgeOrder,
SwapQuoteHop,
SwapQuoteGenericBridgeOrder,
SwapQuoteOrder,
} from '../types';
import { assert } from '../utils/assert';
import { valueByChainId } from '../utils/utils';
import {
CURVE_LIQUIDITY_PROVIDER_BY_CHAIN_ID,
MOONISWAP_LIQUIDITY_PROVIDER_BY_CHAIN_ID,
NATIVE_FEE_TOKEN_BY_CHAIN_ID,
} from '../utils/market_operation_utils/constants';
import { poolEncoder } from '../utils/market_operation_utils/orders';
import {
CurveFillData,
ERC20BridgeSource,
FinalUniswapV3FillData,
LiquidityProviderFillData,
MooniswapFillData,
OptimizedMarketBridgeOrder,
OptimizedMarketOrder,
UniswapV2FillData,
} from '../utils/market_operation_utils/types';
import {
multiplexPlpEncoder,
multiplexRfqEncoder,
MultiplexSubcall,
multiplexTransformERC20Encoder,
multiplexUniswapEncoder,
multiplexBatchSellEncoder,
} from './multiplex_encoders';
import {
getFQTTransformerDataFromOptimizedOrders,
@@ -77,12 +79,29 @@ const PANCAKE_SWAP_FORKS = [
ERC20BridgeSource.CheeseSwap,
ERC20BridgeSource.JulSwap,
];
const FAKE_PROVIDER: any = {
sendAsync(): void {
return;
},
};
const DUMMY_WETH_CONTRACT = new WETH9Contract(NULL_ADDRESS, FAKE_PROVIDER);
const CURVE_LIQUIDITY_PROVIDER_BY_CHAIN_ID = valueByChainId<string>(
{
[ChainId.Mainnet]: '0x561b94454b65614ae3db0897b74303f4acf7cc75',
[ChainId.Ropsten]: '0xae241c6fc7f28f6dc0cb58b4112ba7f63fcaf5e2',
},
NULL_ADDRESS,
);
const MOONISWAP_LIQUIDITY_PROVIDER_BY_CHAIN_ID = valueByChainId<string>(
{
[ChainId.Mainnet]: '0xa2033d6ba88756ce6a87584d69dc87bda9a4f889',
[ChainId.Ropsten]: '0x87e0393aee0fb8c10b8653c6507c182264fe5a34',
},
NULL_ADDRESS,
);
export class ExchangeProxySwapQuoteConsumer implements SwapQuoteConsumerBase {
public readonly chainId: ChainId;
@@ -95,15 +114,12 @@ export class ExchangeProxySwapQuoteConsumer implements SwapQuoteConsumerBase {
};
private readonly _exchangeProxy: IZeroExContract;
private readonly _multiplex: MultiplexFeatureContract;
constructor(public readonly contractAddresses: ContractAddresses, options: Partial<SwapQuoteConsumerOpts> = {}) {
const { chainId } = _.merge({}, constants.DEFAULT_SWAP_QUOTER_OPTS, options);
assert.isNumber('chainId', chainId);
constructor(public readonly contractAddresses: ContractAddresses, options: SwapQuoteConsumerOpts) {
const { chainId } = options;
this.chainId = chainId;
this.contractAddresses = contractAddresses;
this._exchangeProxy = new IZeroExContract(contractAddresses.exchangeProxy, FAKE_PROVIDER);
this._multiplex = new MultiplexFeatureContract(contractAddresses.exchangeProxy, FAKE_PROVIDER);
this.transformerNonces = {
wethTransformer: findTransformerNonce(
contractAddresses.transformers.wethTransformer,
@@ -154,15 +170,14 @@ export class ExchangeProxySwapQuoteConsumer implements SwapQuoteConsumerBase {
ethAmount = ethAmount.plus(sellAmount);
}
const slippedOrders = slipNonNativeOrders(quote);
// VIP routes.
if (
this.chainId === ChainId.Mainnet &&
isDirectSwapCompatible(quote, optsWithDefaults, [ERC20BridgeSource.UniswapV2, ERC20BridgeSource.SushiSwap])
) {
const source = slippedOrders[0].source;
const fillData = (slippedOrders[0] as OptimizedMarketBridgeOrder<UniswapV2FillData>).fillData;
const order = quote.hops[0].orders[0] as SwapQuoteUniswapV2BridgeOrder;
const { source } = order;
const { fillData } = order;
return {
calldataHexString: this._exchangeProxy
.sellToUniswap(
@@ -191,19 +206,20 @@ export class ExchangeProxySwapQuoteConsumer implements SwapQuoteConsumerBase {
this.chainId === ChainId.Mainnet &&
isDirectSwapCompatible(quote, optsWithDefaults, [ERC20BridgeSource.UniswapV3])
) {
const fillData = (slippedOrders[0] as OptimizedMarketBridgeOrder<FinalUniswapV3FillData>).fillData;
const order = quote.hops[0].orders[0] as SwapQuoteUniswapV3BridgeOrder;
const { fillData } = order;
let _calldataHexString;
if (isFromETH) {
_calldataHexString = this._exchangeProxy
.sellEthForTokenToUniswapV3(fillData.uniswapPath, minBuyAmount, NULL_ADDRESS)
.sellEthForTokenToUniswapV3(fillData.encodedPath, minBuyAmount, NULL_ADDRESS)
.getABIEncodedTransactionData();
} else if (isToETH) {
_calldataHexString = this._exchangeProxy
.sellTokenForEthToUniswapV3(fillData.uniswapPath, sellAmount, minBuyAmount, NULL_ADDRESS)
.sellTokenForEthToUniswapV3(fillData.encodedPath, sellAmount, minBuyAmount, NULL_ADDRESS)
.getABIEncodedTransactionData();
} else {
_calldataHexString = this._exchangeProxy
.sellTokenForTokenToUniswapV3(fillData.uniswapPath, sellAmount, minBuyAmount, NULL_ADDRESS)
.sellTokenForTokenToUniswapV3(fillData.encodedPath, sellAmount, minBuyAmount, NULL_ADDRESS)
.getABIEncodedTransactionData();
}
return {
@@ -228,8 +244,8 @@ export class ExchangeProxySwapQuoteConsumer implements SwapQuoteConsumerBase {
ERC20BridgeSource.JulSwap,
])
) {
const source = slippedOrders[0].source;
const fillData = (slippedOrders[0] as OptimizedMarketBridgeOrder<UniswapV2FillData>).fillData;
const order = quote.hops[0].orders[0] as SwapQuoteUniswapV2BridgeOrder;
const { source, fillData } = order;
return {
calldataHexString: this._exchangeProxy
.sellToPancakeSwap(
@@ -258,14 +274,13 @@ export class ExchangeProxySwapQuoteConsumer implements SwapQuoteConsumerBase {
[ChainId.Mainnet, ChainId.BSC].includes(this.chainId) &&
isDirectSwapCompatible(quote, optsWithDefaults, [ERC20BridgeSource.LiquidityProvider])
) {
const fillData = (slippedOrders[0] as OptimizedMarketBridgeOrder<LiquidityProviderFillData>).fillData;
const target = fillData.poolAddress;
const { fillData } = quote.hops[0].orders[0] as SwapQuoteLiquidityProviderBridgeOrder;
return {
calldataHexString: this._exchangeProxy
.sellToLiquidityProvider(
isFromETH ? ETH_TOKEN_ADDRESS : sellToken,
isToETH ? ETH_TOKEN_ADDRESS : buyToken,
target,
fillData.poolAddress,
NULL_ADDRESS,
sellAmount,
minBuyAmount,
@@ -287,7 +302,7 @@ export class ExchangeProxySwapQuoteConsumer implements SwapQuoteConsumerBase {
// ETH buy/sell is supported
![sellToken, buyToken].includes(NATIVE_FEE_TOKEN_BY_CHAIN_ID[ChainId.Mainnet])
) {
const fillData = slippedOrders[0].fills[0].fillData as CurveFillData;
const { fillData } = quote.hops[0].orders[0] as SwapQuoteCurveBridgeOrder;
return {
calldataHexString: this._exchangeProxy
.sellToLiquidityProvider(
@@ -298,8 +313,8 @@ export class ExchangeProxySwapQuoteConsumer implements SwapQuoteConsumerBase {
sellAmount,
minBuyAmount,
encodeCurveLiquidityProviderData({
curveAddress: fillData.pool.poolAddress,
exchangeFunctionSelector: fillData.pool.exchangeFunctionSelector,
curveAddress: fillData.poolAddress,
exchangeFunctionSelector: fillData.exchangeFunctionSelector,
fromCoinIdx: new BigNumber(fillData.fromTokenIdx),
toCoinIdx: new BigNumber(fillData.toTokenIdx),
}),
@@ -316,7 +331,7 @@ export class ExchangeProxySwapQuoteConsumer implements SwapQuoteConsumerBase {
this.chainId === ChainId.Mainnet &&
isDirectSwapCompatible(quote, optsWithDefaults, [ERC20BridgeSource.Mooniswap])
) {
const fillData = slippedOrders[0].fills[0].fillData as MooniswapFillData;
const { fillData } = quote.hops[0].orders[0] as SwapQuoteMooniswapBridgeOrder;
return {
calldataHexString: this._exchangeProxy
.sellToLiquidityProvider(
@@ -326,7 +341,7 @@ export class ExchangeProxySwapQuoteConsumer implements SwapQuoteConsumerBase {
NULL_ADDRESS,
sellAmount,
minBuyAmount,
poolEncoder.encode([fillData.poolAddress]),
encodeAddress(fillData.poolAddress),
)
.getABIEncodedTransactionData(),
ethAmount: isFromETH ? sellAmount : ZERO_AMOUNT,
@@ -338,7 +353,10 @@ export class ExchangeProxySwapQuoteConsumer implements SwapQuoteConsumerBase {
if (this.chainId === ChainId.Mainnet && isMultiplexBatchFillCompatible(quote, optsWithDefaults)) {
return {
calldataHexString: this._encodeMultiplexBatchFillCalldata({ ...quote, orders: slippedOrders }),
calldataHexString: this._encodeMultiplexBatchFillCalldata(
quote.hops[0],
optsWithDefaults,
),
ethAmount,
toAddress: this._exchangeProxy.address,
allowanceTarget: this._exchangeProxy.address,
@@ -348,7 +366,7 @@ export class ExchangeProxySwapQuoteConsumer implements SwapQuoteConsumerBase {
if (this.chainId === ChainId.Mainnet && isMultiplexMultiHopFillCompatible(quote, optsWithDefaults)) {
return {
calldataHexString: this._encodeMultiplexMultiHopFillCalldata(
{ ...quote, orders: slippedOrders },
quote.hops,
optsWithDefaults,
),
ethAmount,
@@ -371,47 +389,28 @@ export class ExchangeProxySwapQuoteConsumer implements SwapQuoteConsumerBase {
});
}
// If it's two hop we have an intermediate token this is needed to encode the individual FQT
// and we also want to ensure no dust amount is left in the flash wallet
const intermediateToken = quote.isTwoHop ? slippedOrders[0].makerToken : NULL_ADDRESS;
// This transformer will fill the quote.
if (quote.isTwoHop) {
const [firstHopOrder, secondHopOrder] = slippedOrders;
for (const [i, hop] of quote.hops.entries()) {
let fillAmount = !isBuyQuote(quote)
? shouldSellEntireBalance ? MAX_UINT256 : hop.takerAmount
: hop.makerAmount;
let side = !isBuyQuote(quote) ? FillQuoteTransformerSide.Sell : FillQuoteTransformerSide.Buy;
if (quote.hops.length > 1) { // Multi-hop.
// Multi-hop is always a sell.
side = FillQuoteTransformerSide.Sell;
// Subsequent multi-hops always sell entire balance.
fillAmount = i > 0 ? MAX_UINT256 : hop.takerAmount;
}
transforms.push({
deploymentNonce: this.transformerNonces.fillQuoteTransformer,
data: encodeFillQuoteTransformerData({
side: FillQuoteTransformerSide.Sell,
sellToken,
buyToken: intermediateToken,
...getFQTTransformerDataFromOptimizedOrders([firstHopOrder]),
side,
fillAmount,
sellToken: hop.takerToken,
buyToken: hop.makerToken,
...getFQTTransformerDataFromOptimizedOrders(hop.orders),
refundReceiver: refundReceiver || NULL_ADDRESS,
fillAmount: shouldSellEntireBalance ? MAX_UINT256 : firstHopOrder.takerAmount,
}),
});
transforms.push({
deploymentNonce: this.transformerNonces.fillQuoteTransformer,
data: encodeFillQuoteTransformerData({
side: FillQuoteTransformerSide.Sell,
buyToken,
sellToken: intermediateToken,
...getFQTTransformerDataFromOptimizedOrders([secondHopOrder]),
refundReceiver: refundReceiver || NULL_ADDRESS,
fillAmount: MAX_UINT256,
}),
});
} else {
const fillAmount = isBuyQuote(quote) ? quote.makerTokenFillAmount : quote.takerTokenFillAmount;
transforms.push({
deploymentNonce: this.transformerNonces.fillQuoteTransformer,
data: encodeFillQuoteTransformerData({
side: isBuyQuote(quote) ? FillQuoteTransformerSide.Buy : FillQuoteTransformerSide.Sell,
sellToken,
buyToken,
...getFQTTransformerDataFromOptimizedOrders(slippedOrders),
refundReceiver: refundReceiver || NULL_ADDRESS,
fillAmount: !isBuyQuote(quote) && shouldSellEntireBalance ? MAX_UINT256 : fillAmount,
}),
});
})
}
if (isToETH) {
@@ -473,15 +472,21 @@ export class ExchangeProxySwapQuoteConsumer implements SwapQuoteConsumerBase {
}
}
// Return any unspent sell tokens.
const payTakerTokens = [sellToken];
// Return any unspent ETH. If ETH is the buy token, it will
// be returned in TransformERC20Feature rather than PayTakerTransformer.
if (!isToETH) {
payTakerTokens.push(ETH_TOKEN_ADDRESS);
}
// The final transformer will send all funds to the taker.
transforms.push({
deploymentNonce: this.transformerNonces.payTakerTransformer,
data: encodePayTakerTransformerData({
tokens: [sellToken, buyToken, ETH_TOKEN_ADDRESS].concat(quote.isTwoHop ? intermediateToken : []),
tokens: payTakerTokens,
amounts: [],
}),
});
const calldataHexString = this._exchangeProxy
.transformERC20(
isFromETH ? ETH_TOKEN_ADDRESS : sellToken,
@@ -509,9 +514,109 @@ export class ExchangeProxySwapQuoteConsumer implements SwapQuoteConsumerBase {
throw new Error('Execution not supported for Exchange Proxy quotes');
}
private _encodeMultiplexBatchFillCalldata(quote: SwapQuote): string {
const wrappedBatchCalls = [];
for_loop: for (const [i, order] of quote.orders.entries()) {
private _encodeMultiplexBatchFillCalldata(hop: SwapQuoteHop, opts: ExchangeProxyContractOpts): string {
const subcalls = this._getMultiplexBatchSellSubcalls(hop.orders);
if (opts.isFromETH) {
return this._exchangeProxy
.multiplexBatchSellEthForToken(hop.makerToken, subcalls, hop.minMakerAmount)
.getABIEncodedTransactionData();
} else if (opts.isToETH) {
return this._exchangeProxy
.multiplexBatchSellTokenForEth(
hop.takerToken,
subcalls,
hop.maxTakerAmount,
hop.minMakerAmount,
)
.getABIEncodedTransactionData();
} else {
return this._exchangeProxy
.multiplexBatchSellTokenForToken(
hop.takerToken,
hop.makerToken,
subcalls,
hop.maxTakerAmount,
hop.minMakerAmount,
)
.getABIEncodedTransactionData();
}
}
private _encodeMultiplexMultiHopFillCalldata(hops: SwapQuoteHop[], opts: ExchangeProxyContractOpts): string {
const subcalls = [];
for (const hop of hops) {
if (hop.orders.length !== 1) {
subcalls.push({
id: MultiplexSubcall.BatchSell,
sellAmount: hop.maxTakerAmount,
data: multiplexBatchSellEncoder.encode(this._getMultiplexBatchSellSubcalls(hop.orders)),
});
continue;
}
const order = hop.orders[0] as SwapQuoteGenericBridgeOrder;
switch (order.source) {
case ERC20BridgeSource.UniswapV2:
case ERC20BridgeSource.SushiSwap:
subcalls.push({
id: MultiplexSubcall.UniswapV2,
data: multiplexUniswapEncoder.encode({
tokens: (order as SwapQuoteUniswapV2BridgeOrder).fillData.tokenAddressPath,
isSushi: order.source === ERC20BridgeSource.SushiSwap,
}),
});
break;
case ERC20BridgeSource.LiquidityProvider:
subcalls.push({
id: MultiplexSubcall.LiquidityProvider,
data: multiplexPlpEncoder.encode({
provider: (order as SwapQuoteLiquidityProviderBridgeOrder).fillData.poolAddress,
auxiliaryData: NULL_BYTES,
}),
});
break;
case ERC20BridgeSource.UniswapV3:
subcalls.push({
id: MultiplexSubcall.UniswapV3,
data: (order as SwapQuoteUniswapV3BridgeOrder).fillData.encodedPath,
});
break;
default:
// Should never happen because we check `isMultiplexMultiHopFillCompatible`
// before calling this function.
throw new Error(`Multiplex multi-hop unsupported source: ${order.source}`);
}
}
const tokenPath = getTokenPathFromHops(hops);
const firstHop = hops[0];
const lastHop = hops[hops.length - 1];
if (opts.isFromETH) {
return this._exchangeProxy
.multiplexMultiHopSellEthForToken(tokenPath, subcalls, lastHop.minMakerAmount)
.getABIEncodedTransactionData();
} else if (opts.isToETH) {
return this._exchangeProxy
.multiplexMultiHopSellTokenForEth(
tokenPath,
subcalls,
firstHop.maxTakerAmount,
lastHop.minMakerAmount,
)
.getABIEncodedTransactionData();
} else {
return this._exchangeProxy
.multiplexMultiHopSellTokenForToken(
tokenPath,
subcalls,
firstHop.maxTakerAmount,
lastHop.minMakerAmount,
)
.getABIEncodedTransactionData();
}
}
private _getMultiplexBatchSellSubcalls(orders: SwapQuoteOrder[]): any[] {
const subcalls = [];
for_loop: for (const [i, order] of orders.entries()) {
switch_statement: switch (order.source) {
case ERC20BridgeSource.Native:
if (order.type !== FillQuoteTransformerOrderType.Rfq) {
@@ -519,8 +624,8 @@ export class ExchangeProxySwapQuoteConsumer implements SwapQuoteConsumerBase {
// before calling this function.
throw new Error('Multiplex batch fill only supported for RFQ native orders');
}
wrappedBatchCalls.push({
selector: this._exchangeProxy.getSelector('_fillRfqOrder'),
subcalls.push({
id: MultiplexSubcall.Rfq,
sellAmount: order.takerAmount,
data: multiplexRfqEncoder.encode({
order: order.fillData.order,
@@ -530,169 +635,80 @@ export class ExchangeProxySwapQuoteConsumer implements SwapQuoteConsumerBase {
break switch_statement;
case ERC20BridgeSource.UniswapV2:
case ERC20BridgeSource.SushiSwap:
wrappedBatchCalls.push({
selector: this._multiplex.getSelector('_sellToUniswap'),
sellAmount: order.takerAmount,
subcalls.push({
id: MultiplexSubcall.UniswapV2,
sellAmount: (order as SwapQuoteUniswapV2BridgeOrder).maxTakerAmount,
data: multiplexUniswapEncoder.encode({
tokens: (order.fillData as UniswapV2FillData).tokenAddressPath,
tokens: (order as SwapQuoteUniswapV2BridgeOrder).fillData.tokenAddressPath,
isSushi: order.source === ERC20BridgeSource.SushiSwap,
}),
});
break switch_statement;
case ERC20BridgeSource.LiquidityProvider:
wrappedBatchCalls.push({
selector: this._multiplex.getSelector('_sellToLiquidityProvider'),
sellAmount: order.takerAmount,
subcalls.push({
id: MultiplexSubcall.LiquidityProvider,
sellAmount: (order as SwapQuoteLiquidityProviderBridgeOrder).maxTakerAmount,
data: multiplexPlpEncoder.encode({
provider: (order.fillData as LiquidityProviderFillData).poolAddress,
provider: (order as SwapQuoteLiquidityProviderBridgeOrder).fillData.poolAddress,
auxiliaryData: NULL_BYTES,
}),
});
break switch_statement;
case ERC20BridgeSource.UniswapV3:
const fillData = (order as OptimizedMarketBridgeOrder<FinalUniswapV3FillData>).fillData;
wrappedBatchCalls.push({
selector: this._exchangeProxy.getSelector('sellTokenForTokenToUniswapV3'),
sellAmount: order.takerAmount,
data: fillData.uniswapPath,
subcalls.push({
id: MultiplexSubcall.UniswapV3,
sellAmount: (order as SwapQuoteUniswapV3BridgeOrder).maxTakerAmount,
data: (order as SwapQuoteUniswapV3BridgeOrder).fillData.encodedPath,
});
break switch_statement;
default:
const fqtData = encodeFillQuoteTransformerData({
side: FillQuoteTransformerSide.Sell,
sellToken: quote.takerToken,
buyToken: quote.makerToken,
...getFQTTransformerDataFromOptimizedOrders(quote.orders.slice(i)),
sellToken: order.takerToken,
buyToken: order.makerToken,
...getFQTTransformerDataFromOptimizedOrders(orders.slice(i)),
refundReceiver: NULL_ADDRESS,
fillAmount: MAX_UINT256,
});
const transformations = [
{ deploymentNonce: this.transformerNonces.fillQuoteTransformer, data: fqtData },
{
deploymentNonce: this.transformerNonces.payTakerTransformer,
data: encodePayTakerTransformerData({
tokens: [quote.takerToken, quote.makerToken],
amounts: [],
}),
},
// TODO(lawrence): needed?
// {
// deploymentNonce: this.transformerNonces.payTakerTransformer,
// data: encodePayTakerTransformerData({
// tokens: [hop.takerToken],
// amounts: [],
// }),
// },
];
wrappedBatchCalls.push({
selector: this._exchangeProxy.getSelector('_transformERC20'),
sellAmount: BigNumber.sum(...quote.orders.slice(i).map(o => o.takerAmount)),
subcalls.push({
id: MultiplexSubcall.TransformERC20,
sellAmount: BigNumber.sum(
...orders.slice(i)
.map(o => (o as SwapQuoteGenericBridgeOrder).maxTakerAmount),
),
data: multiplexTransformERC20Encoder.encode({
transformations,
ethValue: constants.ZERO_AMOUNT,
}),
});
break for_loop;
}
}
return this._exchangeProxy
.batchFill(
{
inputToken: quote.takerToken,
outputToken: quote.makerToken,
sellAmount: quote.worstCaseQuoteInfo.totalTakerAmount,
calls: wrappedBatchCalls,
},
quote.worstCaseQuoteInfo.makerAmount,
)
.getABIEncodedTransactionData();
}
private _encodeMultiplexMultiHopFillCalldata(quote: SwapQuote, opts: ExchangeProxyContractOpts): string {
const wrappedMultiHopCalls = [];
const tokens: string[] = [];
if (opts.isFromETH) {
wrappedMultiHopCalls.push({
selector: DUMMY_WETH_CONTRACT.getSelector('deposit'),
data: NULL_BYTES,
});
tokens.push(ETH_TOKEN_ADDRESS);
}
const [firstHopOrder, secondHopOrder] = quote.orders;
const intermediateToken = firstHopOrder.makerToken;
tokens.push(quote.takerToken, intermediateToken, quote.makerToken);
for (const order of [firstHopOrder, secondHopOrder]) {
switch (order.source) {
case ERC20BridgeSource.UniswapV2:
case ERC20BridgeSource.SushiSwap:
wrappedMultiHopCalls.push({
selector: this._multiplex.getSelector('_sellToUniswap'),
data: multiplexUniswapEncoder.encode({
tokens: (order.fillData as UniswapV2FillData).tokenAddressPath,
isSushi: order.source === ERC20BridgeSource.SushiSwap,
}),
});
break;
case ERC20BridgeSource.LiquidityProvider:
wrappedMultiHopCalls.push({
selector: this._multiplex.getSelector('_sellToLiquidityProvider'),
data: multiplexPlpEncoder.encode({
provider: (order.fillData as LiquidityProviderFillData).poolAddress,
auxiliaryData: NULL_BYTES,
}),
});
break;
default:
// Note: we'll need to redeploy TransformERC20Feature before we can
// use other sources
// Should never happen because we check `isMultiplexMultiHopFillCompatible`
// before calling this function.
throw new Error(`Multiplex multi-hop unsupported source: ${order.source}`);
}
}
if (opts.isToETH) {
wrappedMultiHopCalls.push({
selector: DUMMY_WETH_CONTRACT.getSelector('withdraw'),
data: NULL_BYTES,
});
tokens.push(ETH_TOKEN_ADDRESS);
}
return this._exchangeProxy
.multiHopFill(
{
tokens,
sellAmount: quote.worstCaseQuoteInfo.totalTakerAmount,
calls: wrappedMultiHopCalls,
},
quote.worstCaseQuoteInfo.makerAmount,
)
.getABIEncodedTransactionData();
return subcalls;
}
}
function slipNonNativeOrders(quote: MarketSellSwapQuote | MarketBuySwapQuote): OptimizedMarketOrder[] {
const slippage = getMaxQuoteSlippageRate(quote);
if (!slippage) {
return quote.orders;
}
return quote.orders.map(o => {
if (o.source === ERC20BridgeSource.Native) {
return o;
function getTokenPathFromHops(hops: SwapQuoteHop[]): Address[] {
const path = [];
for (const [i, hop] of hops.entries()) {
path.push(hop.takerToken);
if (i === path.length - 1) {
path.push(hop.makerToken);
}
return {
...o,
...(quote.type === MarketOperation.Sell
? { makerAmount: o.makerAmount.times(1 - slippage).integerValue(BigNumber.ROUND_DOWN) }
: { takerAmount: o.takerAmount.times(1 + slippage).integerValue(BigNumber.ROUND_UP) }),
};
});
}
return path;
}
function getMaxQuoteSlippageRate(quote: MarketBuySwapQuote | MarketSellSwapQuote): number {
if (quote.type === MarketOperation.Buy) {
// (worstCaseTaker - bestCaseTaker) / bestCaseTaker
// where worstCaseTaker >= bestCaseTaker
return quote.worstCaseQuoteInfo.takerAmount
.minus(quote.bestCaseQuoteInfo.takerAmount)
.div(quote.bestCaseQuoteInfo.takerAmount)
.toNumber();
}
// (bestCaseMaker - worstCaseMaker) / bestCaseMaker
// where bestCaseMaker >= worstCaseMaker
return quote.bestCaseQuoteInfo.makerAmount
.minus(quote.worstCaseQuoteInfo.makerAmount)
.div(quote.bestCaseQuoteInfo.makerAmount)
.toNumber();
function encodeAddress(address: Address): Bytes {
return hexUtils.leftPad(hexUtils.slice(address, 0, 20));
}

View File

@@ -1,6 +1,18 @@
import { RfqOrder, SIGNATURE_ABI } from '@0x/protocol-utils';
import { AbiEncoder } from '@0x/utils';
export enum MultiplexSubcall {
Invalid,
Rfq,
Otc,
UniswapV2,
UniswapV3,
LiquidityProvider,
TransformERC20,
BatchSell,
MultiHopSell,
}
export const multiplexTransformERC20Encoder = AbiEncoder.create([
{
name: 'transformations',
@@ -10,17 +22,31 @@ export const multiplexTransformERC20Encoder = AbiEncoder.create([
{ name: 'data', type: 'bytes' },
],
},
{ name: 'ethValue', type: 'uint256' },
]);
export const multiplexRfqEncoder = AbiEncoder.create([
{ name: 'order', type: 'tuple', components: RfqOrder.STRUCT_ABI },
{ name: 'signature', type: 'tuple', components: SIGNATURE_ABI },
]);
export const multiplexUniswapEncoder = AbiEncoder.create([
{ name: 'tokens', type: 'address[]' },
{ name: 'isSushi', type: 'bool' },
]);
export const multiplexPlpEncoder = AbiEncoder.create([
{ name: 'provider', type: 'address' },
{ name: 'auxiliaryData', type: 'bytes' },
]);
export const multiplexBatchSellEncoder = AbiEncoder.create([
{
name: 'subcalls',
type: 'tuple[]',
components: [
{ name: 'id', type: 'uint8' },
{ name: 'sellAmount', type: 'uint256' },
{ name: 'data', type: 'bytes' },
],
},
]);

View File

@@ -2,17 +2,17 @@ import { FillQuoteTransformerData, FillQuoteTransformerOrderType } from '@0x/pro
import { ExchangeProxyContractOpts, MarketBuySwapQuote, MarketOperation, SwapQuote } from '../types';
import {
createBridgeDataForBridgeOrder,
getErc20BridgeSourceToBridgeSource,
} from '../utils/market_operation_utils/orders';
import {
ERC20BridgeSource,
NativeLimitOrderFillData,
NativeRfqOrderFillData,
OptimizedMarketBridgeOrder,
OptimizedMarketOrder,
OptimizedMarketOrderBase,
} from '../utils/market_operation_utils/types';
import {
SwapQuoteGenericBridgeOrder,
SwapQuoteOrder,
SwapQuoteLimitOrder,
SwapQuoteRfqOrder,
} from '../types';
const MULTIPLEX_BATCH_FILL_SOURCES = [
ERC20BridgeSource.UniswapV2,
@@ -29,26 +29,26 @@ export function isMultiplexBatchFillCompatible(quote: SwapQuote, opts: ExchangeP
if (requiresTransformERC20(opts)) {
return false;
}
if (quote.isTwoHop) {
// Must not be multi-hop.
if (quote.hops.length > 1) {
return false;
}
// batchFill does not support WETH wrapping/unwrapping at the moment
if (opts.isFromETH || opts.isToETH) {
return false;
}
if (quote.orders.map(o => o.type).includes(FillQuoteTransformerOrderType.Limit)) {
// Must not contain limit orders.
const allOrderTypes = quote.hops.map(h => h.orders.map(o => o.type)).flat(2);
if (allOrderTypes.includes(FillQuoteTransformerOrderType.Limit)) {
return false;
}
// Use Multiplex if the non-fallback sources are a subset of
// {UniswapV2, Sushiswap, RFQ, PLP, UniswapV3}
const nonFallbackSources = Object.keys(quote.sourceBreakdown);
return nonFallbackSources.every(source => MULTIPLEX_BATCH_FILL_SOURCES.includes(source as ERC20BridgeSource));
const nonFallbackSources = quote.hops.map(h => h.orders.filter(o => !o.isFallback).map(o => o.source)).flat(2);
return nonFallbackSources.every(s => MULTIPLEX_BATCH_FILL_SOURCES.includes(s));
}
const MULTIPLEX_MULTIHOP_FILL_SOURCES = [
ERC20BridgeSource.UniswapV2,
ERC20BridgeSource.SushiSwap,
ERC20BridgeSource.LiquidityProvider,
ERC20BridgeSource.UniswapV3,
];
/**
@@ -58,14 +58,12 @@ export function isMultiplexMultiHopFillCompatible(quote: SwapQuote, opts: Exchan
if (requiresTransformERC20(opts)) {
return false;
}
if (!quote.isTwoHop) {
// Must be multi-hop.
if (quote.hops.length < 2) {
return false;
}
const [firstHopOrder, secondHopOrder] = quote.orders;
return (
MULTIPLEX_MULTIHOP_FILL_SOURCES.includes(firstHopOrder.source) &&
MULTIPLEX_MULTIHOP_FILL_SOURCES.includes(secondHopOrder.source)
);
const sources = quote.hops.map(h => h.orders.map(o => o.source)).flat(2);
return sources.every(s => MULTIPLEX_MULTIHOP_FILL_SOURCES.includes(s));
}
/**
@@ -80,11 +78,11 @@ export function isDirectSwapCompatible(
if (requiresTransformERC20(opts)) {
return false;
}
// Must be a single order.
if (quote.orders.length !== 1) {
// Must be a single hop with a single order.
if (quote.hops.length !== 1 || quote.hops[0].orders.length !== 1) {
return false;
}
const order = quote.orders[0];
const order = quote.hops[0].orders[0];
if (!directSources.includes(order.source)) {
return false;
}
@@ -98,24 +96,24 @@ export function isBuyQuote(quote: SwapQuote): quote is MarketBuySwapQuote {
return quote.type === MarketOperation.Buy;
}
function isOptimizedBridgeOrder(x: OptimizedMarketOrder): x is OptimizedMarketBridgeOrder {
function isBridgeOrder(x: SwapQuoteOrder): x is SwapQuoteGenericBridgeOrder {
return x.type === FillQuoteTransformerOrderType.Bridge;
}
function isOptimizedLimitOrder(x: OptimizedMarketOrder): x is OptimizedMarketOrderBase<NativeLimitOrderFillData> {
return x.type === FillQuoteTransformerOrderType.Limit;
}
function isOptimizedRfqOrder(x: OptimizedMarketOrder): x is OptimizedMarketOrderBase<NativeRfqOrderFillData> {
return x.type === FillQuoteTransformerOrderType.Rfq;
}
// function isOptimizedLimitOrder(x: OptimizedMarketOrder): x is OptimizedMarketOrderBase<NativeLimitOrderFillData> {
// return x.type === FillQuoteTransformerOrderType.Limit;
// }
//
// function isOptimizedRfqOrder(x: OptimizedMarketOrder): x is OptimizedMarketOrderBase<NativeRfqOrderFillData> {
// return x.type === FillQuoteTransformerOrderType.Rfq;
// }
/**
* Converts the given `OptimizedMarketOrder`s into bridge, limit, and RFQ orders for
* FillQuoteTransformer.
*/
export function getFQTTransformerDataFromOptimizedOrders(
orders: OptimizedMarketOrder[],
orders: SwapQuoteOrder[],
): Pick<FillQuoteTransformerData, 'bridgeOrders' | 'limitOrders' | 'rfqOrders' | 'fillSequence'> {
const fqtData: Pick<FillQuoteTransformerData, 'bridgeOrders' | 'limitOrders' | 'rfqOrders' | 'fillSequence'> = {
bridgeOrders: [],
@@ -125,25 +123,25 @@ export function getFQTTransformerDataFromOptimizedOrders(
};
for (const order of orders) {
if (isOptimizedBridgeOrder(order)) {
if (isBridgeOrder(order)) {
fqtData.bridgeOrders.push({
bridgeData: createBridgeDataForBridgeOrder(order),
makerTokenAmount: order.makerAmount,
takerTokenAmount: order.takerAmount,
bridgeData: order.fillData.encodedFillData,
makerTokenAmount: order.minMakerAmount,
takerTokenAmount: order.maxTakerAmount,
source: getErc20BridgeSourceToBridgeSource(order.source),
});
} else if (isOptimizedLimitOrder(order)) {
fqtData.limitOrders.push({
order: order.fillData.order,
signature: order.fillData.signature,
maxTakerTokenFillAmount: order.takerAmount,
});
} else if (isOptimizedRfqOrder(order)) {
fqtData.rfqOrders.push({
order: order.fillData.order,
signature: order.fillData.signature,
maxTakerTokenFillAmount: order.takerAmount,
});
// } else if (isOptimizedLimitOrder(order)) {
// fqtData.limitOrders.push({
// order: order.fillData.order,
// signature: order.fillData.signature,
// maxTakerTokenFillAmount: order.takerAmount,
// });
// } else if (isOptimizedRfqOrder(order)) {
// fqtData.rfqOrders.push({
// order: order.fillData.order,
// signature: order.fillData.signature,
// maxTakerTokenFillAmount: order.takerAmount,
// });
} else {
// Should never happen
throw new Error('Unknown Order type');

View File

@@ -20,13 +20,12 @@ export class SwapQuoteConsumer implements SwapQuoteConsumerBase {
private readonly _contractAddresses: ContractAddresses;
private readonly _exchangeProxyConsumer: ExchangeProxySwapQuoteConsumer;
public static getSwapQuoteConsumer(options: Partial<SwapQuoteConsumerOpts> = {}): SwapQuoteConsumer {
public static getSwapQuoteConsumer(options: SwapQuoteConsumerOpts): SwapQuoteConsumer {
return new SwapQuoteConsumer(options);
}
constructor(options: Partial<SwapQuoteConsumerOpts> = {}) {
const { chainId } = _.merge({}, constants.DEFAULT_SWAP_QUOTER_OPTS, options);
assert.isNumber('chainId', chainId);
constructor(options: SwapQuoteConsumerOpts) {
const { chainId } = options;
this.chainId = chainId;
this._contractAddresses = options.contractAddresses || getContractAddressesForChainOrThrow(chainId);

View File

@@ -1,16 +1,15 @@
import { ChainId, getContractAddressesForChainOrThrow } from '@0x/contract-addresses';
import { getContractAddressesForChainOrThrow } from '@0x/contract-addresses';
import { FillQuoteTransformerOrderType, LimitOrder } from '@0x/protocol-utils';
import { BigNumber, providerUtils } from '@0x/utils';
import Axios, { AxiosInstance } from 'axios';
import { BlockParamLiteral, MethodAbi, SupportedProvider, ZeroExProvider } from 'ethereum-types';
import { FastABI } from 'fast-abi';
import { SupportedProvider, ZeroExProvider } from 'ethereum-types';
import { Agent as HttpAgent } from 'http';
import { Agent as HttpsAgent } from 'https';
import * as _ from 'lodash';
import { artifacts } from './artifacts';
import { constants, INVALID_SIGNATURE, KEEP_ALIVE_TTL } from './constants';
import {
Address,
AssetSwapperContractAddresses,
MarketBuySwapQuote,
MarketOperation,
@@ -19,6 +18,10 @@ import {
SignedNativeOrder,
SwapQuote,
SwapQuoteInfo,
SwapQuoteHop,
SwapQuoteOrder,
SwapQuoteGenericBridgeOrder,
SwapQuoteNativeOrder,
SwapQuoteOrdersBreakdown,
SwapQuoteRequestOpts,
SwapQuoterOpts,
@@ -26,25 +29,26 @@ import {
} from './types';
import { assert } from './utils/assert';
import { MarketOperationUtils } from './utils/market_operation_utils';
import { BancorService } from './utils/market_operation_utils/bancor_service';
import { SAMPLER_ADDRESS, SOURCE_FLAGS, ZERO_AMOUNT } from './utils/market_operation_utils/constants';
import { DexOrderSampler } from './utils/market_operation_utils/sampler';
import { ZERO_AMOUNT } from './utils/market_operation_utils/constants';
import { SamplerClient } from './utils/market_operation_utils/sampler';
import { SourceFilters } from './utils/market_operation_utils/source_filters';
import {
ERC20BridgeSource,
FeeSchedule,
FillData,
GetMarketOrdersOpts,
MarketDepth,
MarketDepthSide,
MarketSideLiquidity,
OptimizedMarketOrder,
OptimizedHop,
OptimizedOrder,
OptimizedBridgeOrder,
OptimizedLimitOrder,
OptimizedRfqOrder,
OptimizedGenericBridgeOrder,
OptimizerResultWithReport,
} from './utils/market_operation_utils/types';
import { ProtocolFeeUtils } from './utils/protocol_fee_utils';
import { QuoteRequestor } from './utils/quote_requestor';
import { QuoteFillResult, simulateBestCaseFill, simulateWorstCaseFill } from './utils/quote_simulation';
import { ERC20BridgeSamplerContract } from './wrappers';
export abstract class Orderbook {
public abstract getOrdersAsync(
@@ -75,6 +79,7 @@ export class SwapQuoter {
private readonly _marketOperationUtils: MarketOperationUtils;
private readonly _rfqtOptions?: SwapQuoterRfqOpts;
private readonly _quoteRequestorHttpClient: AxiosInstance;
private readonly _integratorIdsSet: Set<string>;
/**
* Instantiates a new SwapQuoter instance
@@ -84,20 +89,15 @@ export class SwapQuoter {
*
* @return An instance of SwapQuoter
*/
constructor(supportedProvider: SupportedProvider, orderbook: Orderbook, options: Partial<SwapQuoterOpts> = {}) {
constructor(supportedProvider: SupportedProvider, orderbook: Orderbook, options: SwapQuoterOpts) {
const {
chainId,
expiryBufferMs,
permittedOrderFeeTypes,
samplerGasLimit,
rfqt,
tokenAdjacencyGraph,
liquidityProviderRegistry,
} = { ...constants.DEFAULT_SWAP_QUOTER_OPTS, ...options };
} = options;
const provider = providerUtils.standardizeOrThrow(supportedProvider);
assert.isValidOrderbook('orderbook', orderbook);
assert.isNumber('chainId', chainId);
assert.isNumber('expiryBufferMs', expiryBufferMs);
this.chainId = chainId;
this.provider = provider;
this.orderbook = orderbook;
@@ -112,45 +112,11 @@ export class SwapQuoter {
constants.PROTOCOL_FEE_UTILS_POLLING_INTERVAL_IN_MS,
options.ethGasStationUrl,
);
// Allow the sampler bytecode to be overwritten using geths override functionality
const samplerBytecode = _.get(artifacts.ERC20BridgeSampler, 'compilerOutput.evm.deployedBytecode.object');
// Allow address of the Sampler to be overridden, i.e in Ganache where overrides do not work
const samplerAddress = (options.samplerOverrides && options.samplerOverrides.to) || SAMPLER_ADDRESS;
const defaultCodeOverrides = samplerBytecode
? {
[samplerAddress]: { code: samplerBytecode },
}
: {};
const samplerOverrides = _.assign(
{ block: BlockParamLiteral.Latest, overrides: defaultCodeOverrides },
options.samplerOverrides,
);
const fastAbi = new FastABI(ERC20BridgeSamplerContract.ABI() as MethodAbi[], { BigNumber });
const samplerContract = new ERC20BridgeSamplerContract(
samplerAddress,
this.provider,
{
gas: samplerGasLimit,
},
{},
undefined,
{
encodeInput: (fnName: string, values: any) => fastAbi.encodeInput(fnName, values),
decodeOutput: (fnName: string, data: string) => fastAbi.decodeOutput(fnName, data),
},
);
this._marketOperationUtils = new MarketOperationUtils(
new DexOrderSampler(
SamplerClient.createFromChainIdAndEndpoint(
this.chainId,
samplerContract,
samplerOverrides,
undefined, // pools caches for balancer and cream
tokenAdjacencyGraph,
liquidityProviderRegistry,
this.chainId === ChainId.Mainnet // Enable Bancor only on Mainnet
? async () => BancorService.createAsync(provider)
: async () => undefined,
options.samplerServiceUrl,
),
this._contractAddresses,
{
@@ -164,6 +130,9 @@ export class SwapQuoter {
httpsAgent: new HttpsAgent({ keepAlive: true, timeout: KEEP_ALIVE_TTL }),
...(rfqt ? rfqt.axiosInstanceOpts : {}),
});
const integratorIds = this._rfqtOptions?.integratorsWhitelist.map(integrator => integrator.integratorId) || [];
this._integratorIdsSet = new Set(integratorIds);
}
public async getBatchMarketBuySwapQuoteAsync(
@@ -212,7 +181,6 @@ export class SwapQuoter {
MarketOperation.Buy,
makerTokenBuyAmounts[i],
gasPrice,
opts.gasSchedule,
opts.bridgeSlippage,
);
} else {
@@ -239,49 +207,50 @@ export class SwapQuoter {
takerAssetAmount: BigNumber,
options: Partial<SwapQuoteRequestOpts> = {},
): Promise<MarketDepth> {
assert.isString('makerToken', makerToken);
assert.isString('takerToken', takerToken);
const sourceFilters = new SourceFilters([], options.excludedSources, options.includedSources);
let [sellOrders, buyOrders] = !sourceFilters.isAllowed(ERC20BridgeSource.Native)
? [[], []]
: await Promise.all([
this.orderbook.getOrdersAsync(makerToken, takerToken),
this.orderbook.getOrdersAsync(takerToken, makerToken),
]);
if (!sellOrders || sellOrders.length === 0) {
sellOrders = [createDummyOrder(makerToken, takerToken)];
}
if (!buyOrders || buyOrders.length === 0) {
buyOrders = [createDummyOrder(takerToken, makerToken)];
}
const getMarketDepthSide = (marketSideLiquidity: MarketSideLiquidity): MarketDepthSide => {
const { dexQuotes, nativeOrders } = marketSideLiquidity.quotes;
const { side } = marketSideLiquidity;
return [
...dexQuotes,
nativeOrders.map(o => {
return {
input: side === MarketOperation.Sell ? o.fillableTakerAmount : o.fillableMakerAmount,
output: side === MarketOperation.Sell ? o.fillableMakerAmount : o.fillableTakerAmount,
fillData: o,
source: ERC20BridgeSource.Native,
};
}),
];
};
const [bids, asks] = await Promise.all([
this._marketOperationUtils.getMarketBuyLiquidityAsync(buyOrders, takerAssetAmount, options),
this._marketOperationUtils.getMarketSellLiquidityAsync(sellOrders, takerAssetAmount, options),
]);
return {
bids: getMarketDepthSide(bids),
asks: getMarketDepthSide(asks),
makerTokenDecimals: asks.makerTokenDecimals,
takerTokenDecimals: asks.takerTokenDecimals,
};
throw new Error(`Not implemented`);
// assert.isString('makerToken', makerToken);
// assert.isString('takerToken', takerToken);
// const sourceFilters = new SourceFilters([], options.excludedSources, options.includedSources);
//
// let [sellOrders, buyOrders] = !sourceFilters.isAllowed(ERC20BridgeSource.Native)
// ? [[], []]
// : await Promise.all([
// this.orderbook.getOrdersAsync(makerToken, takerToken),
// this.orderbook.getOrdersAsync(takerToken, makerToken),
// ]);
// if (!sellOrders || sellOrders.length === 0) {
// sellOrders = [createDummyOrder(makerToken, takerToken)];
// }
// if (!buyOrders || buyOrders.length === 0) {
// buyOrders = [createDummyOrder(takerToken, makerToken)];
// }
//
// const getMarketDepthSide = (marketSideLiquidity: MarketSideLiquidity): MarketDepthSide => {
// const { dexQuotes, nativeOrders } = marketSideLiquidity.quotes;
// const { side } = marketSideLiquidity;
//
// return [
// ...dexQuotes,
// nativeOrders.map(o => {
// return {
// input: side === MarketOperation.Sell ? o.fillableTakerAmount : o.fillableMakerAmount,
// output: side === MarketOperation.Sell ? o.fillableMakerAmount : o.fillableTakerAmount,
// fillData: o,
// source: ERC20BridgeSource.Native,
// };
// }),
// ];
// };
// const [bids, asks] = await Promise.all([
// this._marketOperationUtils.getMarketBuyLiquidityAsync(buyOrders, takerAssetAmount, options),
// this._marketOperationUtils.getMarketSellLiquidityAsync(sellOrders, takerAssetAmount, options),
// ]);
// return {
// bids: getMarketDepthSide(bids),
// asks: getMarketDepthSide(asks),
// makerTokenDecimals: asks.makerTokenDecimals,
// takerTokenDecimals: asks.takerTokenDecimals,
// };
}
/**
@@ -359,9 +328,7 @@ export class SwapQuoter {
const cloneOpts = _.omit(opts, 'gasPrice') as GetMarketOrdersOpts;
const calcOpts: GetMarketOrdersOpts = {
...cloneOpts,
feeSchedule: _.mapValues(opts.feeSchedule, gasCost => (fillData: FillData) =>
gasCost === undefined ? 0 : gasPrice.times(gasCost(fillData)),
),
gasPrice,
exchangeProxyOverhead: flags => gasPrice.times(opts.exchangeProxyOverhead(flags)),
};
// pass the QuoteRequestor on if rfqt enabled
@@ -392,12 +359,13 @@ export class SwapQuoter {
marketOperation,
assetFillAmount,
gasPrice,
opts.gasSchedule,
opts.bridgeSlippage,
);
// Use the raw gas, not scaled by gas price
const exchangeProxyOverhead = opts.exchangeProxyOverhead(result.sourceFlags).toNumber();
const exchangeProxyOverhead = BigNumber.sum(
...result.hops.map(h => opts.exchangeProxyOverhead(h.sourceFlags)),
).toNumber();
swapQuote.bestCaseQuoteInfo.gas += exchangeProxyOverhead;
swapQuote.worstCaseQuoteInfo.gas += exchangeProxyOverhead;
@@ -414,12 +382,11 @@ export class SwapQuoter {
return isOpenOrder && !willOrderExpire && isFeeTypeAllowed;
}; // tslint:disable-line:semicolon
private _isApiKeyWhitelisted(apiKey: string | undefined): boolean {
if (!apiKey) {
private _isIntegratorIdWhitelisted(integratorId: string | undefined): boolean {
if (!integratorId) {
return false;
}
const whitelistedApiKeys = this._rfqtOptions ? this._rfqtOptions.takerApiKeyWhitelist : [];
return whitelistedApiKeys.includes(apiKey);
return this._integratorIdsSet.has(integratorId);
}
private _isTxOriginBlacklisted(txOrigin: string | undefined): boolean {
@@ -438,19 +405,19 @@ export class SwapQuoter {
return rfqt;
}
// tslint:disable-next-line: boolean-naming
const { apiKey, nativeExclusivelyRFQ, intentOnFilling, txOrigin } = rfqt;
const { integrator, nativeExclusivelyRFQ, intentOnFilling, txOrigin } = rfqt;
// If RFQ-T is enabled and `nativeExclusivelyRFQ` is set, then `ERC20BridgeSource.Native` should
// never be excluded.
if (nativeExclusivelyRFQ === true && !sourceFilters.isAllowed(ERC20BridgeSource.Native)) {
throw new Error('Native liquidity cannot be excluded if "rfqt.nativeExclusivelyRFQ" is set');
}
// If an API key was provided, but the key is not whitelisted, raise a warning and disable RFQ
if (!this._isApiKeyWhitelisted(apiKey)) {
// If an integrator ID was provided, but the ID is not whitelisted, raise a warning and disable RFQ
if (!this._isIntegratorIdWhitelisted(integrator.integratorId)) {
if (this._rfqtOptions && this._rfqtOptions.warningLogger) {
this._rfqtOptions.warningLogger(
{
apiKey,
...integrator,
},
'Attempt at using an RFQ API key that is not whitelisted. Disabling RFQ for the request lifetime.',
);
@@ -474,7 +441,7 @@ export class SwapQuoter {
// Otherwise check other RFQ options
if (
intentOnFilling && // The requestor is asking for a firm quote
this._isApiKeyWhitelisted(apiKey) && // A valid API key was provided
this._isIntegratorIdWhitelisted(integrator.integratorId) && // A valid API key was provided
sourceFilters.isAllowed(ERC20BridgeSource.Native) // Native liquidity is not excluded
) {
if (!txOrigin || txOrigin === constants.NULL_ADDRESS) {
@@ -492,26 +459,22 @@ function createSwapQuote(
optimizerResult: OptimizerResultWithReport,
makerToken: string,
takerToken: string,
operation: MarketOperation,
side: MarketOperation,
assetFillAmount: BigNumber,
gasPrice: BigNumber,
gasSchedule: FeeSchedule,
slippage: number,
): SwapQuote {
const {
optimizedOrders,
hops,
quoteReport,
sourceFlags,
takerAmountPerEth,
makerAmountPerEth,
priceComparisonsReport,
} = optimizerResult;
const isTwoHop = sourceFlags === SOURCE_FLAGS[ERC20BridgeSource.MultiHop];
// Calculate quote info
const { bestCaseQuoteInfo, worstCaseQuoteInfo, sourceBreakdown } = isTwoHop
? calculateTwoHopQuoteInfo(optimizedOrders, operation, gasSchedule, slippage)
: calculateQuoteInfo(optimizedOrders, operation, assetFillAmount, gasPrice, gasSchedule, slippage);
const quoteHops = hops.map(hop => toSwapQuoteHop(hop, side, slippage));
const { bestCaseQuoteInfo, worstCaseQuoteInfo, sourceBreakdown } =
calculateQuoteInfo(quoteHops, side, assetFillAmount, gasPrice, slippage);
// Put together the swap quote
const { makerTokenDecimals, takerTokenDecimals } = optimizerResult.marketSideLiquidity;
@@ -519,7 +482,7 @@ function createSwapQuote(
makerToken,
takerToken,
gasPrice,
orders: optimizedOrders,
orders: hops.map(h => h.orders).flat(1),
bestCaseQuoteInfo,
worstCaseQuoteInfo,
sourceBreakdown,
@@ -528,116 +491,216 @@ function createSwapQuote(
takerAmountPerEth,
makerAmountPerEth,
quoteReport,
isTwoHop,
priceComparisonsReport,
};
if (operation === MarketOperation.Buy) {
if (side === MarketOperation.Buy) {
return {
...swapQuote,
type: MarketOperation.Buy,
makerTokenFillAmount: assetFillAmount,
maxSlippage: slippage,
hops: quoteHops,
};
} else {
return {
...swapQuote,
type: MarketOperation.Sell,
takerTokenFillAmount: assetFillAmount,
maxSlippage: slippage,
hops: quoteHops,
};
}
}
function toSwapQuoteHop(hop: OptimizedHop, side: MarketOperation, slippage: number): SwapQuoteHop {
const orders = hop.orders.map(o => toSwapQuoteOrder(o, side, slippage));
const takerAmount = side === MarketOperation.Sell ? hop.inputAmount : hop.outputAmount;
const makerAmount = side === MarketOperation.Sell ? hop.outputAmount : hop.inputAmount;
return {
orders,
makerAmount: roundMakerAmount(side, makerAmount),
takerAmount: roundTakerAmount(side, takerAmount),
makerToken: side === MarketOperation.Sell ? hop.outputToken : hop.inputToken,
takerToken: side === MarketOperation.Sell ? hop.inputToken : hop.outputToken,
minMakerAmount: slipMakerAmount(side, makerAmount, slippage),
maxTakerAmount: slipTakerAmount(side, takerAmount, slippage),
sourceFlags: hop.sourceFlags,
};
}
function roundMakerAmount(side: MarketOperation, makerAmount: BigNumber): BigNumber {
const rm = side === MarketOperation.Sell ? BigNumber.ROUND_DOWN : BigNumber.ROUND_UP;
return makerAmount.integerValue(rm);
}
function roundTakerAmount(side: MarketOperation, takerAmount: BigNumber): BigNumber {
const rm = side === MarketOperation.Sell ? BigNumber.ROUND_UP : BigNumber.ROUND_UP;
return takerAmount.integerValue(rm);
}
function slipMakerAmount(side: MarketOperation, makerAmount: BigNumber, slippage: number): BigNumber {
return roundMakerAmount(
side,
side === MarketOperation.Sell ? makerAmount.times(1 - slippage) : makerAmount,
);
}
function slipTakerAmount(side: MarketOperation, takerAmount: BigNumber, slippage: number): BigNumber {
return roundTakerAmount(
side,
side === MarketOperation.Sell ? takerAmount : takerAmount.times(1 + slippage),
);
}
function toSwapQuoteOrder(order: OptimizedOrder, side: MarketOperation, slippage: number): SwapQuoteGenericBridgeOrder | SwapQuoteNativeOrder {
const { inputToken, outputToken, inputAmount, outputAmount, ...rest } = order;
const common = {
...rest,
takerToken: side === MarketOperation.Sell ? inputToken : outputToken,
makerToken: side === MarketOperation.Sell ? outputToken : inputToken,
takerAmount: side === MarketOperation.Sell ? inputAmount : outputAmount,
makerAmount: side === MarketOperation.Sell ? outputAmount : inputAmount,
};
if (isBridgeOrder(order)) {
return {
...common,
minMakerAmount: slipMakerAmount(
side,
side === MarketOperation.Sell
? order.outputAmount
: order.inputAmount,
slippage,
),
maxTakerAmount: slipTakerAmount(
side,
side === MarketOperation.Sell
? order.inputAmount
: order.outputAmount,
slippage,
),
};
}
return common as SwapQuoteNativeOrder;
}
function isBridgeOrder(order: OptimizedOrder): order is OptimizedGenericBridgeOrder {
return order.type === FillQuoteTransformerOrderType.Bridge;
}
function calculateQuoteInfo(
optimizedOrders: OptimizedMarketOrder[],
operation: MarketOperation,
assetFillAmount: BigNumber,
hops: SwapQuoteHop[],
side: MarketOperation,
fillAmount: BigNumber,
gasPrice: BigNumber,
gasSchedule: FeeSchedule,
slippage: number,
): { bestCaseQuoteInfo: SwapQuoteInfo; worstCaseQuoteInfo: SwapQuoteInfo; sourceBreakdown: SwapQuoteOrdersBreakdown } {
const bestCaseFillResult = simulateBestCaseFill({
gasPrice,
orders: optimizedOrders,
side: operation,
fillAmount: assetFillAmount,
opts: { gasSchedule },
});
const worstCaseFillResult = simulateWorstCaseFill({
gasPrice,
orders: optimizedOrders,
side: operation,
fillAmount: assetFillAmount,
opts: { gasSchedule, slippage },
});
return {
bestCaseQuoteInfo: fillResultsToQuoteInfo(bestCaseFillResult),
worstCaseQuoteInfo: fillResultsToQuoteInfo(worstCaseFillResult),
sourceBreakdown: getSwapQuoteOrdersBreakdown(bestCaseFillResult.fillAmountBySource),
};
}
function calculateTwoHopQuoteInfo(
optimizedOrders: OptimizedMarketOrder[],
operation: MarketOperation,
gasSchedule: FeeSchedule,
slippage: number,
): { bestCaseQuoteInfo: SwapQuoteInfo; worstCaseQuoteInfo: SwapQuoteInfo; sourceBreakdown: SwapQuoteOrdersBreakdown } {
const [firstHopOrder, secondHopOrder] = optimizedOrders;
const [firstHopFill] = firstHopOrder.fills;
const [secondHopFill] = secondHopOrder.fills;
const gas = new BigNumber(
gasSchedule[ERC20BridgeSource.MultiHop]!({
firstHopSource: _.pick(firstHopFill, 'source', 'fillData'),
secondHopSource: _.pick(secondHopFill, 'source', 'fillData'),
}),
).toNumber();
return {
bestCaseQuoteInfo: {
makerAmount: operation === MarketOperation.Sell ? secondHopFill.output : secondHopFill.input,
takerAmount: operation === MarketOperation.Sell ? firstHopFill.input : firstHopFill.output,
totalTakerAmount: operation === MarketOperation.Sell ? firstHopFill.input : firstHopFill.output,
feeTakerTokenAmount: constants.ZERO_AMOUNT,
protocolFeeInWeiAmount: constants.ZERO_AMOUNT,
gas,
},
// TODO jacob consolidate this with quote simulation worstCase
worstCaseQuoteInfo: {
makerAmount: MarketOperation.Sell
? secondHopOrder.makerAmount.times(1 - slippage).integerValue()
: secondHopOrder.makerAmount,
takerAmount: MarketOperation.Sell
? firstHopOrder.takerAmount
: firstHopOrder.takerAmount.times(1 + slippage).integerValue(),
totalTakerAmount: MarketOperation.Sell
? firstHopOrder.takerAmount
: firstHopOrder.takerAmount.times(1 + slippage).integerValue(),
feeTakerTokenAmount: constants.ZERO_AMOUNT,
protocolFeeInWeiAmount: constants.ZERO_AMOUNT,
gas,
},
sourceBreakdown: {
[ERC20BridgeSource.MultiHop]: {
proportion: new BigNumber(1),
intermediateToken: secondHopOrder.takerToken,
hops: [firstHopFill.source, secondHopFill.source],
},
},
};
}
function getSwapQuoteOrdersBreakdown(fillAmountBySource: { [source: string]: BigNumber }): SwapQuoteOrdersBreakdown {
const totalFillAmount = BigNumber.sum(...Object.values(fillAmountBySource));
const breakdown: SwapQuoteOrdersBreakdown = {};
Object.entries(fillAmountBySource).forEach(([s, fillAmount]) => {
const source = s as keyof SwapQuoteOrdersBreakdown;
if (source === ERC20BridgeSource.MultiHop) {
// TODO jacob has a different breakdown
} else {
breakdown[source] = fillAmount.div(totalFillAmount);
const getNextFillAmount = (fillResults: QuoteFillResult[]) => {
if (fillResults.length === 0) {
return fillAmount;
}
const lastFillResult = fillResults[fillResults.length - 1];
const { totalTakerAssetAmount, makerAssetAmount } = lastFillResult;
return side === MarketOperation.Sell
? makerAssetAmount : totalTakerAssetAmount;
};
const bestCaseFillResults = [];
const worstCaseFillResults = [];
const tokenPath = [];
for (const [i, hop] of hops.entries()) {
if (i === 0 || i < hops.length - 1) {
tokenPath.push(hop.takerToken);
}
if (i === tokenPath.length - 1) {
tokenPath.push(hop.makerToken);
}
const bestCaseFillResult = simulateBestCaseFill({
gasPrice,
side,
orders: hop.orders,
fillAmount: getNextFillAmount(bestCaseFillResults),
opts: {},
});
bestCaseFillResults.push(bestCaseFillResult);
const worstCaseFillResult = simulateWorstCaseFill({
gasPrice,
side,
orders: hop.orders,
fillAmount: getNextFillAmount(worstCaseFillResults),
opts: { slippage },
});
worstCaseFillResults.push(worstCaseFillResult);
}
const combinedBestCaseFillResult = combineQuoteFillResults(bestCaseFillResults);
const combinedWorstCaseFillResult = combineQuoteFillResults(worstCaseFillResults);
const sourceBreakdown = getSwapQuoteOrdersBreakdown(side, tokenPath, bestCaseFillResults);
return {
sourceBreakdown,
bestCaseQuoteInfo: fillResultsToQuoteInfo(combinedBestCaseFillResult),
worstCaseQuoteInfo: fillResultsToQuoteInfo(combinedWorstCaseFillResult),
};
}
function combineQuoteFillResults(fillResults: QuoteFillResult[]): QuoteFillResult {
if (fillResults.length === 0) {
throw new Error(`Empty fillResults array`);
}
const lastResult = fillResults[fillResults.length - 1];
const r = {
...fillResults[0],
makerAssetAmount: lastResult.makerAssetAmount,
totalMakerAssetAmount: lastResult.totalMakerAssetAmount,
};
for (const fr of fillResults.slice(1)) {
r.gas += fr.gas + 30e3;
r.protocolFeeAmount = r.protocolFeeAmount.plus(fr.protocolFeeAmount);
}
return r;
}
function getSwapQuoteOrdersBreakdown(side: MarketOperation, tokenPath: Address[], hopFillResults: QuoteFillResult[]): SwapQuoteOrdersBreakdown {
const cumulativeFillRatioBySource: Partial<{ [key in ERC20BridgeSource]: number }> = {};
for (const hop of hopFillResults) {
const hopTotalFillAmount = side === MarketOperation.Sell
? hop.totalTakerAssetAmount
: hop.totalMakerAssetAmount;
for (const [source, sourceFillAmount] of Object.entries(hop.fillAmountBySource)) {
cumulativeFillRatioBySource[source as ERC20BridgeSource] =
(cumulativeFillRatioBySource[source as ERC20BridgeSource] || 0)
+ sourceFillAmount.div(hopTotalFillAmount).toNumber();
}
}
const globalFillRatiosSum = Object.values(cumulativeFillRatioBySource).reduce((a, v) => a! + v!, 0);
if (!globalFillRatiosSum) {
return {};
}
const breakdown: SwapQuoteOrdersBreakdown = {};
for (const [source, fillRatio] of Object.entries(cumulativeFillRatioBySource)) {
(breakdown as any)[source] = fillRatio! / globalFillRatiosSum;
}
const hopBreakdowns = hopFillResults.map(hop => {
const hopTotalFillAmount = side === MarketOperation.Sell
? hop.totalTakerAssetAmount
: hop.totalMakerAssetAmount;
return Object.assign(
{},
...Object.entries(hop.fillAmountBySource).map(([source, sourceFillAmount]) => ({
[source as ERC20BridgeSource]: sourceFillAmount.div(hopTotalFillAmount).toNumber(),
})),
);
});
if (hopFillResults.length > 1) {
return {
[ERC20BridgeSource.MultiHop]: {
proportion: 1,
tokenPath: tokenPath,
breakdowns: hopBreakdowns,
},
};
}
return breakdown;
}

View File

@@ -1,7 +1,9 @@
import { ChainId } from '@0x/contract-addresses';
import { BlockParam, ContractAddresses, GethCallOverrides } from '@0x/contract-wrappers';
import {
FillQuoteTransformerLimitOrderInfo,
FillQuoteTransformerOrderType,
FillQuoteTransformerRfqOrderInfo,
LimitOrderFields,
RfqOrder,
RfqOrderFields,
@@ -16,12 +18,21 @@ import {
ERC20BridgeSource,
GetMarketOrdersOpts,
LiquidityProviderRegistry,
OptimizedMarketOrder,
LiquidityProviderFillData,
TokenAdjacencyGraph,
BridgeFillData,
CurveFillData,
UniswapV2FillData,
UniswapV3FillData,
NativeOrderFillData,
MooniswapFillData,
} from './utils/market_operation_utils/types';
import { PriceComparisonsReport, QuoteReport } from './utils/quote_report_generator';
import { MetricsProxy } from './utils/quote_requestor';
export type Address = string;
export type Bytes = string;
/**
* expiryBufferMs: The number of seconds to add when calculating whether an order is expired or not. Defaults to 300s (5m).
* permittedOrderFeeTypes: A set of all the takerFee types that OrderPruner will filter for
@@ -37,19 +48,9 @@ export interface SignedOrder<T> {
signature: Signature;
}
export type SignedNativeOrder = SignedOrder<LimitOrderFields> | SignedOrder<RfqOrderFields>;
export type NativeOrderWithFillableAmounts = SignedNativeOrder & NativeOrderFillableAmountFields;
/**
* fillableMakerAmount: Amount of makerAsset that is fillable
* fillableTakerAmount: Amount of takerAsset that is fillable
* fillableTakerFeeAmount: Amount of takerFee paid to fill fillableTakerAmount
*/
export interface NativeOrderFillableAmountFields {
fillableMakerAmount: BigNumber;
fillableTakerAmount: BigNumber;
fillableTakerFeeAmount: BigNumber;
}
export type SignedRfqOrder = SignedOrder<RfqOrderFields>;
export type SignedLimitOrder = SignedOrder<LimitOrderFields>;
export type SignedNativeOrder = SignedLimitOrder | SignedRfqOrder;
/**
* Represents the metadata to call a smart contract with calldata.
@@ -166,19 +167,72 @@ export interface SwapQuoteBase {
takerToken: string;
makerToken: string;
gasPrice: BigNumber;
orders: OptimizedMarketOrder[];
hops: SwapQuoteHop[];
bestCaseQuoteInfo: SwapQuoteInfo;
worstCaseQuoteInfo: SwapQuoteInfo;
sourceBreakdown: SwapQuoteOrdersBreakdown;
quoteReport?: QuoteReport;
priceComparisonsReport?: PriceComparisonsReport;
isTwoHop: boolean;
makerTokenDecimals: number;
takerTokenDecimals: number;
takerAmountPerEth: BigNumber;
makerAmountPerEth: BigNumber;
maxSlippage: number;
}
export interface SwapQuoteHop {
takerToken: Address;
makerToken: Address;
makerAmount: BigNumber;
takerAmount: BigNumber;
minMakerAmount: BigNumber;
maxTakerAmount: BigNumber;
sourceFlags: bigint;
orders: SwapQuoteOrder[];
}
export interface SwapQuoteOrder {
type: FillQuoteTransformerOrderType; // should correspond with TFillData
source: ERC20BridgeSource;
makerToken: string;
takerToken: string;
gasCost: number;
makerAmount: BigNumber;
takerAmount: BigNumber;
isFallback: boolean;
fillData?: any;
}
export interface SwapQuoteBridgeOrder<TFillData extends BridgeFillData> extends SwapQuoteOrder {
fillData: TFillData;
minMakerAmount: BigNumber;
maxTakerAmount: BigNumber;
}
export interface SwapQuoteGenericBridgeOrder extends SwapQuoteBridgeOrder<BridgeFillData> {}
export interface SwapQuoteUniswapV2BridgeOrder extends SwapQuoteBridgeOrder<UniswapV2FillData> {}
export interface SwapQuoteUniswapV3BridgeOrder extends SwapQuoteBridgeOrder<UniswapV3FillData> {}
export interface SwapQuoteLiquidityProviderBridgeOrder extends SwapQuoteBridgeOrder<LiquidityProviderFillData> {}
export interface SwapQuoteMooniswapBridgeOrder extends SwapQuoteBridgeOrder<MooniswapFillData> {}
export interface SwapQuoteCurveBridgeOrder extends SwapQuoteBridgeOrder<CurveFillData> {}
export interface SwapQuoteLimitOrder extends SwapQuoteOrder {
type: FillQuoteTransformerOrderType.Limit;
fillData: NativeOrderFillData;
}
export interface SwapQuoteRfqOrder extends SwapQuoteOrder {
type: FillQuoteTransformerOrderType.Rfq;
fillData: NativeOrderFillData;
}
export type SwapQuoteNativeOrder = SwapQuoteLimitOrder | SwapQuoteRfqOrder;
/**
* takerAssetFillAmount: The amount of takerAsset sold for makerAsset.
* type: Specified MarketOperation the SwapQuote is provided for
@@ -220,15 +274,17 @@ export interface SwapQuoteInfo {
* percentage breakdown of each liquidity source used in quote
*/
export type SwapQuoteOrdersBreakdown = Partial<
{ [key in Exclude<ERC20BridgeSource, typeof ERC20BridgeSource.MultiHop>]: BigNumber } & {
[ERC20BridgeSource.MultiHop]: {
proportion: BigNumber;
intermediateToken: string;
hops: ERC20BridgeSource[];
};
{ [key in Exclude<ERC20BridgeSource, typeof ERC20BridgeSource.MultiHop>]: number } & {
[ERC20BridgeSource.MultiHop]: SwapQuoteMultiHopBreakdown;
}
>;
export interface SwapQuoteMultiHopBreakdown {
proportion: number;
tokenPath: Address[];
breakdowns: Partial<{ [key in ERC20BridgeSource]: number }>[];
};
/**
* nativeExclusivelyRFQ: if set to `true`, Swap quote will exclude Open Orderbook liquidity.
* If set to `true` and `ERC20BridgeSource.Native` is part of the `excludedSources`
@@ -243,7 +299,7 @@ export interface RfqmRequestOptions extends RfqRequestOpts {
export interface RfqRequestOpts {
takerAddress: string;
txOrigin: string;
apiKey: string;
integrator: Integrator;
intentOnFilling: boolean;
isIndicative?: boolean;
makerEndpointMaxResponseTimeMs?: number;
@@ -256,7 +312,7 @@ export interface RfqRequestOpts {
/**
* gasPrice: gas price to determine protocolFee amount, default to ethGasStation fast amount
*/
export interface SwapQuoteRequestOpts extends GetMarketOrdersOpts {
export interface SwapQuoteRequestOpts extends Omit<GetMarketOrdersOpts, 'gasPrice'> {
gasPrice?: BigNumber;
rfqt?: RfqRequestOpts;
}
@@ -293,8 +349,14 @@ export interface RfqFirmQuoteValidator {
getRfqtTakerFillableAmountsAsync(quotes: RfqOrder[]): Promise<BigNumber[]>;
}
export interface Integrator {
integratorId: string;
label: string;
whitelistIntegratorUrls?: string[];
}
export interface SwapQuoterRfqOpts {
takerApiKeyWhitelist: string[];
integratorsWhitelist: Integrator[];
makerAssetOfferings: RfqMakerAssetOfferings;
txOriginBlacklist: Set<string>;
altRfqCreds?: {
@@ -320,15 +382,16 @@ export interface SwapQuoterOpts extends OrderPrunerOpts {
chainId: ChainId;
orderRefreshIntervalMs: number;
expiryBufferMs: number;
ethereumRpcUrl?: string;
// ethereumRpcUrl?: string;
contractAddresses?: AssetSwapperContractAddresses;
samplerGasLimit?: number;
multiBridgeAddress?: string;
// multiBridgeAddress?: string;
ethGasStationUrl?: string;
rfqt?: SwapQuoterRfqOpts;
samplerOverrides?: SamplerOverrides;
tokenAdjacencyGraph?: TokenAdjacencyGraph;
liquidityProviderRegistry?: LiquidityProviderRegistry;
// samplerOverrides?: SamplerOverrides;
// tokenAdjacencyGraph?: TokenAdjacencyGraph;
// liquidityProviderRegistry?: LiquidityProviderRegistry;
samplerServiceUrl: string;
}
/**
@@ -404,8 +467,6 @@ export interface SamplerCallResult {
data: string;
}
export type Omit<T, K extends keyof T> = Pick<T, Exclude<keyof T, K>>;
export enum AltQuoteModel {
Firm = 'firm',
Indicative = 'indicative',

View File

@@ -1,34 +0,0 @@
import { SupportedProvider } from '@0x/dev-utils';
import { SDK } from '@bancor/sdk';
import { Ethereum } from '@bancor/sdk/dist/blockchains/ethereum';
import { BlockchainType } from '@bancor/sdk/dist/types';
import { MAINNET_TOKENS } from './constants';
const findToken = (tokenAddress: string, graph: object): string =>
// If we're looking for WETH it is stored by Bancor as the 0xeee address
tokenAddress.toLowerCase() === MAINNET_TOKENS.WETH.toLowerCase()
? '0xEeeeeEeeeEeEeeEeEeEeeEEEeeeeEeeeeeeeEEeE'
: Object.keys(graph).filter(k => k.toLowerCase() === tokenAddress.toLowerCase())[0];
export class BancorService {
public static async createAsync(provider: SupportedProvider): Promise<BancorService> {
const sdk = await SDK.create({ ethereumNodeEndpoint: provider });
const service = new BancorService(sdk);
return service;
}
constructor(public sdk: SDK) {}
public getPaths(_fromToken: string, _toToken: string): string[][] {
// HACK: We reach into the blockchain object and pull in it's cache of tokens
// and we use it's internal non-async getPathsFunc
try {
const blockchain = this.sdk._core.blockchains[BlockchainType.Ethereum] as Ethereum;
const fromToken = findToken(_fromToken, blockchain.graph);
const toToken = findToken(_toToken, blockchain.graph);
return blockchain.getPathsFunc.bind(blockchain)(fromToken, toToken);
} catch (e) {
return [];
}
}
}

View File

@@ -1,500 +0,0 @@
import { ChainId } from '@0x/contract-addresses';
import { BigNumber, NULL_BYTES } from '@0x/utils';
import {
ACRYPTOS_BSC_INFOS,
APESWAP_ROUTER_BY_CHAIN_ID,
BAKERYSWAP_ROUTER_BY_CHAIN_ID,
BELT_BSC_INFOS,
CAFESWAP_ROUTER_BY_CHAIN_ID,
CHEESESWAP_ROUTER_BY_CHAIN_ID,
COMETHSWAP_ROUTER_BY_CHAIN_ID,
COMPONENT_POOLS_BY_CHAIN_ID,
CRYPTO_COM_ROUTER_BY_CHAIN_ID,
CURVE_MAINNET_INFOS,
CURVE_POLYGON_INFOS,
CURVE_V2_MAINNET_INFOS,
CURVE_V2_POLYGON_INFOS,
DFYN_ROUTER_BY_CHAIN_ID,
ELLIPSIS_BSC_INFOS,
FIREBIRDONESWAP_BSC_INFOS,
FIREBIRDONESWAP_POLYGON_INFOS,
IRONSWAP_POLYGON_INFOS,
JETSWAP_ROUTER_BY_CHAIN_ID,
JULSWAP_ROUTER_BY_CHAIN_ID,
KYBER_BANNED_RESERVES,
KYBER_BRIDGED_LIQUIDITY_PREFIX,
MAX_DODOV2_POOLS_QUERIED,
MAX_KYBER_RESERVES_QUERIED,
MSTABLE_POOLS_BY_CHAIN_ID,
NERVE_BSC_INFOS,
NULL_ADDRESS,
PANCAKESWAP_ROUTER_BY_CHAIN_ID,
PANCAKESWAPV2_ROUTER_BY_CHAIN_ID,
PANGOLIN_ROUTER_BY_CHAIN_ID,
POLYDEX_ROUTER_BY_CHAIN_ID,
QUICKSWAP_ROUTER_BY_CHAIN_ID,
SADDLE_MAINNET_INFOS,
SHELL_POOLS_BY_CHAIN_ID,
SHIBASWAP_ROUTER_BY_CHAIN_ID,
SMOOTHY_BSC_INFOS,
SMOOTHY_MAINNET_INFOS,
SNOWSWAP_MAINNET_INFOS,
SUSHISWAP_ROUTER_BY_CHAIN_ID,
SWERVE_MAINNET_INFOS,
TRADER_JOE_ROUTER_BY_CHAIN_ID,
UNISWAPV2_ROUTER_BY_CHAIN_ID,
WAULTSWAP_ROUTER_BY_CHAIN_ID,
XSIGMA_MAINNET_INFOS,
} from './constants';
import { CurveInfo, ERC20BridgeSource } from './types';
/**
* Filter Kyber reserves which should not be used (0xbb bridged reserves)
* @param reserveId Kyber reserveId
*/
export function isAllowedKyberReserveId(reserveId: string): boolean {
return (
reserveId !== NULL_BYTES &&
!reserveId.startsWith(KYBER_BRIDGED_LIQUIDITY_PREFIX) &&
!KYBER_BANNED_RESERVES.includes(reserveId)
);
}
// tslint:disable-next-line: completed-docs ban-types
export function isValidAddress(address: string | String): address is string {
return (typeof address === 'string' || address instanceof String) && address.toString() !== NULL_ADDRESS;
}
/**
* Returns the offsets to be used to discover Kyber reserves
*/
export function getKyberOffsets(): BigNumber[] {
return Array(MAX_KYBER_RESERVES_QUERIED)
.fill(0)
.map((_v, i) => new BigNumber(i));
}
// tslint:disable completed-docs
export function getDodoV2Offsets(): BigNumber[] {
return Array(MAX_DODOV2_POOLS_QUERIED)
.fill(0)
.map((_v, i) => new BigNumber(i));
}
// tslint:disable completed-docs
export function getShellsForPair(chainId: ChainId, takerToken: string, makerToken: string): string[] {
if (chainId !== ChainId.Mainnet) {
return [];
}
return Object.values(SHELL_POOLS_BY_CHAIN_ID[chainId])
.filter(c => [makerToken, takerToken].every(t => c.tokens.includes(t)))
.map(i => i.poolAddress);
}
// tslint:disable completed-docs
export function getComponentForPair(chainId: ChainId, takerToken: string, makerToken: string): string[] {
if (chainId !== ChainId.Mainnet) {
return [];
}
return Object.values(COMPONENT_POOLS_BY_CHAIN_ID[chainId])
.filter(c => [makerToken, takerToken].every(t => c.tokens.includes(t)))
.map(i => i.poolAddress);
}
// tslint:disable completed-docs
export function getMStableForPair(chainId: ChainId, takerToken: string, makerToken: string): string[] {
if (chainId !== ChainId.Mainnet && chainId !== ChainId.Polygon) {
return [];
}
return Object.values(MSTABLE_POOLS_BY_CHAIN_ID[chainId])
.filter(c => [makerToken, takerToken].every(t => c.tokens.includes(t)))
.map(i => i.poolAddress);
}
// tslint:disable completed-docs
export function getCurveInfosForPair(chainId: ChainId, takerToken: string, makerToken: string): CurveInfo[] {
switch (chainId) {
case ChainId.Mainnet:
return Object.values(CURVE_MAINNET_INFOS).filter(c =>
[makerToken, takerToken].every(
t =>
(c.tokens.includes(t) && c.metaTokens === undefined) ||
(c.tokens.includes(t) &&
[makerToken, takerToken].filter(v => c.metaTokens?.includes(v)).length > 0),
),
);
case ChainId.Polygon:
return Object.values(CURVE_POLYGON_INFOS).filter(c =>
[makerToken, takerToken].every(
t =>
(c.tokens.includes(t) && c.metaTokens === undefined) ||
(c.tokens.includes(t) &&
[makerToken, takerToken].filter(v => c.metaTokens?.includes(v)).length > 0),
),
);
default:
return [];
}
}
// tslint:disable completed-docs
export function getCurveV2InfosForPair(chainId: ChainId, takerToken: string, makerToken: string): CurveInfo[] {
switch (chainId) {
case ChainId.Mainnet:
return Object.values(CURVE_V2_MAINNET_INFOS).filter(c =>
[makerToken, takerToken].every(
t =>
(c.tokens.includes(t) && c.metaTokens === undefined) ||
(c.tokens.includes(t) &&
[makerToken, takerToken].filter(v => c.metaTokens?.includes(v)).length > 0),
),
);
case ChainId.Polygon:
return Object.values(CURVE_V2_POLYGON_INFOS).filter(c =>
[makerToken, takerToken].every(
t =>
(c.tokens.includes(t) && c.metaTokens === undefined) ||
(c.tokens.includes(t) &&
[makerToken, takerToken].filter(v => c.metaTokens?.includes(v)).length > 0),
),
);
default:
return [];
}
}
export function getSwerveInfosForPair(chainId: ChainId, takerToken: string, makerToken: string): CurveInfo[] {
if (chainId !== ChainId.Mainnet) {
return [];
}
return Object.values(SWERVE_MAINNET_INFOS).filter(c =>
[makerToken, takerToken].every(
t =>
(c.tokens.includes(t) && c.metaTokens === undefined) ||
(c.tokens.includes(t) && [makerToken, takerToken].filter(v => c.metaTokens?.includes(v)).length > 0),
),
);
}
export function getSnowSwapInfosForPair(chainId: ChainId, takerToken: string, makerToken: string): CurveInfo[] {
if (chainId !== ChainId.Mainnet) {
return [];
}
return Object.values(SNOWSWAP_MAINNET_INFOS).filter(c =>
[makerToken, takerToken].every(
t =>
(c.tokens.includes(t) && c.metaTokens === undefined) ||
(c.tokens.includes(t) && [makerToken, takerToken].filter(v => c.metaTokens?.includes(v)).length > 0),
),
);
}
export function getNerveInfosForPair(chainId: ChainId, takerToken: string, makerToken: string): CurveInfo[] {
if (chainId !== ChainId.BSC) {
return [];
}
return Object.values(NERVE_BSC_INFOS).filter(c =>
[makerToken, takerToken].every(
t =>
(c.tokens.includes(t) && c.metaTokens === undefined) ||
(c.tokens.includes(t) && [makerToken, takerToken].filter(v => c.metaTokens?.includes(v)).length > 0),
),
);
}
export function getFirebirdOneSwapInfosForPair(chainId: ChainId, takerToken: string, makerToken: string): CurveInfo[] {
if (chainId === ChainId.BSC) {
return Object.values(FIREBIRDONESWAP_BSC_INFOS).filter(c =>
[makerToken, takerToken].every(
t =>
(c.tokens.includes(t) && c.metaTokens === undefined) ||
(c.tokens.includes(t) &&
[makerToken, takerToken].filter(v => c.metaTokens?.includes(v)).length > 0),
),
);
} else if (chainId === ChainId.Polygon) {
return Object.values(FIREBIRDONESWAP_POLYGON_INFOS).filter(c =>
[makerToken, takerToken].every(
t =>
(c.tokens.includes(t) && c.metaTokens === undefined) ||
(c.tokens.includes(t) &&
[makerToken, takerToken].filter(v => c.metaTokens?.includes(v)).length > 0),
),
);
} else {
return [];
}
}
export function getBeltInfosForPair(chainId: ChainId, takerToken: string, makerToken: string): CurveInfo[] {
if (chainId !== ChainId.BSC) {
return [];
}
return Object.values(BELT_BSC_INFOS).filter(c =>
[makerToken, takerToken].every(
t =>
(c.tokens.includes(t) && c.metaTokens === undefined) ||
(c.tokens.includes(t) && [makerToken, takerToken].filter(v => c.metaTokens?.includes(v)).length > 0),
),
);
}
export function getEllipsisInfosForPair(chainId: ChainId, takerToken: string, makerToken: string): CurveInfo[] {
if (chainId !== ChainId.BSC) {
return [];
}
return Object.values(ELLIPSIS_BSC_INFOS).filter(c =>
[makerToken, takerToken].every(
t =>
(c.tokens.includes(t) && c.metaTokens === undefined) ||
(c.tokens.includes(t) && [makerToken, takerToken].filter(v => c.metaTokens?.includes(v)).length > 0),
),
);
}
export function getSmoothyInfosForPair(chainId: ChainId, takerToken: string, makerToken: string): CurveInfo[] {
if (chainId === ChainId.BSC) {
return Object.values(SMOOTHY_BSC_INFOS).filter(c =>
[makerToken, takerToken].every(
t =>
(c.tokens.includes(t) && c.metaTokens === undefined) ||
(c.tokens.includes(t) &&
[makerToken, takerToken].filter(v => c.metaTokens?.includes(v)).length > 0),
),
);
} else if (chainId === ChainId.Mainnet) {
return Object.values(SMOOTHY_MAINNET_INFOS).filter(c =>
[makerToken, takerToken].every(
t =>
(c.tokens.includes(t) && c.metaTokens === undefined) ||
(c.tokens.includes(t) &&
[makerToken, takerToken].filter(v => c.metaTokens?.includes(v)).length > 0),
),
);
} else {
return [];
}
}
export function getSaddleInfosForPair(chainId: ChainId, takerToken: string, makerToken: string): CurveInfo[] {
if (chainId !== ChainId.Mainnet) {
return [];
}
return Object.values(SADDLE_MAINNET_INFOS).filter(c =>
[makerToken, takerToken].every(
t =>
(c.tokens.includes(t) && c.metaTokens === undefined) ||
(c.tokens.includes(t) && [makerToken, takerToken].filter(v => c.metaTokens?.includes(v)).length > 0),
),
);
}
export function getIronSwapInfosForPair(chainId: ChainId, takerToken: string, makerToken: string): CurveInfo[] {
if (chainId !== ChainId.Polygon) {
return [];
}
return Object.values(IRONSWAP_POLYGON_INFOS).filter(c =>
[makerToken, takerToken].every(
t =>
(c.tokens.includes(t) && c.metaTokens === undefined) ||
(c.tokens.includes(t) && [makerToken, takerToken].filter(v => c.metaTokens?.includes(v)).length > 0),
),
);
}
export function getXSigmaInfosForPair(chainId: ChainId, takerToken: string, makerToken: string): CurveInfo[] {
if (chainId !== ChainId.Mainnet) {
return [];
}
return Object.values(XSIGMA_MAINNET_INFOS).filter(c =>
[makerToken, takerToken].every(
t =>
(c.tokens.includes(t) && c.metaTokens === undefined) ||
(c.tokens.includes(t) && [makerToken, takerToken].filter(v => c.metaTokens?.includes(v)).length > 0),
),
);
}
export function getAcryptosInfosForPair(chainId: ChainId, takerToken: string, makerToken: string): CurveInfo[] {
if (chainId !== ChainId.BSC) {
return [];
}
return Object.values(ACRYPTOS_BSC_INFOS).filter(c =>
[makerToken, takerToken].every(
t =>
(c.tokens.includes(t) && c.metaTokens === undefined) ||
(c.tokens.includes(t) && [makerToken, takerToken].filter(v => c.metaTokens?.includes(v)).length > 0),
),
);
}
export function getShellLikeInfosForPair(
chainId: ChainId,
takerToken: string,
makerToken: string,
source: ERC20BridgeSource.Shell | ERC20BridgeSource.Component | ERC20BridgeSource.MStable,
): string[] {
switch (source) {
case ERC20BridgeSource.Shell:
return getShellsForPair(chainId, takerToken, makerToken);
case ERC20BridgeSource.Component:
return getComponentForPair(chainId, takerToken, makerToken);
case ERC20BridgeSource.MStable:
return getMStableForPair(chainId, takerToken, makerToken);
default:
throw new Error(`Unknown Shell like source ${source}`);
}
}
export interface CurveDetailedInfo extends CurveInfo {
makerTokenIdx: number;
takerTokenIdx: number;
}
export function getCurveLikeInfosForPair(
chainId: ChainId,
takerToken: string,
makerToken: string,
source:
| ERC20BridgeSource.Curve
| ERC20BridgeSource.CurveV2
| ERC20BridgeSource.Swerve
| ERC20BridgeSource.SnowSwap
| ERC20BridgeSource.Nerve
| ERC20BridgeSource.Belt
| ERC20BridgeSource.Ellipsis
| ERC20BridgeSource.Smoothy
| ERC20BridgeSource.Saddle
| ERC20BridgeSource.IronSwap
| ERC20BridgeSource.XSigma
| ERC20BridgeSource.FirebirdOneSwap
| ERC20BridgeSource.ACryptos,
): CurveDetailedInfo[] {
let pools: CurveInfo[] = [];
switch (source) {
case ERC20BridgeSource.Curve:
pools = getCurveInfosForPair(chainId, takerToken, makerToken);
break;
case ERC20BridgeSource.CurveV2:
pools = getCurveV2InfosForPair(chainId, takerToken, makerToken);
break;
case ERC20BridgeSource.Swerve:
pools = getSwerveInfosForPair(chainId, takerToken, makerToken);
break;
case ERC20BridgeSource.SnowSwap:
pools = getSnowSwapInfosForPair(chainId, takerToken, makerToken);
break;
case ERC20BridgeSource.Nerve:
pools = getNerveInfosForPair(chainId, takerToken, makerToken);
break;
case ERC20BridgeSource.Belt:
pools = getBeltInfosForPair(chainId, takerToken, makerToken);
break;
case ERC20BridgeSource.Ellipsis:
pools = getEllipsisInfosForPair(chainId, takerToken, makerToken);
break;
case ERC20BridgeSource.Smoothy:
pools = getSmoothyInfosForPair(chainId, takerToken, makerToken);
break;
case ERC20BridgeSource.Saddle:
pools = getSaddleInfosForPair(chainId, takerToken, makerToken);
break;
case ERC20BridgeSource.XSigma:
pools = getXSigmaInfosForPair(chainId, takerToken, makerToken);
break;
case ERC20BridgeSource.FirebirdOneSwap:
pools = getFirebirdOneSwapInfosForPair(chainId, takerToken, makerToken);
break;
case ERC20BridgeSource.IronSwap:
pools = getIronSwapInfosForPair(chainId, takerToken, makerToken);
break;
case ERC20BridgeSource.ACryptos:
pools = getAcryptosInfosForPair(chainId, takerToken, makerToken);
break;
default:
throw new Error(`Unknown Curve like source ${source}`);
}
return pools.map(pool => ({
...pool,
makerTokenIdx: pool.tokens.indexOf(makerToken),
takerTokenIdx: pool.tokens.indexOf(takerToken),
}));
}
export function uniswapV2LikeRouterAddress(
chainId: ChainId,
source:
| ERC20BridgeSource.UniswapV2
| ERC20BridgeSource.SushiSwap
| ERC20BridgeSource.CryptoCom
| ERC20BridgeSource.PancakeSwap
| ERC20BridgeSource.PancakeSwapV2
| ERC20BridgeSource.BakerySwap
| ERC20BridgeSource.ApeSwap
| ERC20BridgeSource.CafeSwap
| ERC20BridgeSource.CheeseSwap
| ERC20BridgeSource.JulSwap
| ERC20BridgeSource.QuickSwap
| ERC20BridgeSource.ComethSwap
| ERC20BridgeSource.Dfyn
| ERC20BridgeSource.WaultSwap
| ERC20BridgeSource.Polydex
| ERC20BridgeSource.ShibaSwap
| ERC20BridgeSource.JetSwap
| ERC20BridgeSource.TraderJoe
| ERC20BridgeSource.Pangolin,
): string {
switch (source) {
case ERC20BridgeSource.UniswapV2:
return UNISWAPV2_ROUTER_BY_CHAIN_ID[chainId];
case ERC20BridgeSource.SushiSwap:
return SUSHISWAP_ROUTER_BY_CHAIN_ID[chainId];
case ERC20BridgeSource.CryptoCom:
return CRYPTO_COM_ROUTER_BY_CHAIN_ID[chainId];
case ERC20BridgeSource.PancakeSwap:
return PANCAKESWAP_ROUTER_BY_CHAIN_ID[chainId];
case ERC20BridgeSource.PancakeSwapV2:
return PANCAKESWAPV2_ROUTER_BY_CHAIN_ID[chainId];
case ERC20BridgeSource.BakerySwap:
return BAKERYSWAP_ROUTER_BY_CHAIN_ID[chainId];
case ERC20BridgeSource.ApeSwap:
return APESWAP_ROUTER_BY_CHAIN_ID[chainId];
case ERC20BridgeSource.CafeSwap:
return CAFESWAP_ROUTER_BY_CHAIN_ID[chainId];
case ERC20BridgeSource.CheeseSwap:
return CHEESESWAP_ROUTER_BY_CHAIN_ID[chainId];
case ERC20BridgeSource.JulSwap:
return JULSWAP_ROUTER_BY_CHAIN_ID[chainId];
case ERC20BridgeSource.QuickSwap:
return QUICKSWAP_ROUTER_BY_CHAIN_ID[chainId];
case ERC20BridgeSource.ComethSwap:
return COMETHSWAP_ROUTER_BY_CHAIN_ID[chainId];
case ERC20BridgeSource.Dfyn:
return DFYN_ROUTER_BY_CHAIN_ID[chainId];
case ERC20BridgeSource.WaultSwap:
return WAULTSWAP_ROUTER_BY_CHAIN_ID[chainId];
case ERC20BridgeSource.Polydex:
return POLYDEX_ROUTER_BY_CHAIN_ID[chainId];
case ERC20BridgeSource.ShibaSwap:
return SHIBASWAP_ROUTER_BY_CHAIN_ID[chainId];
case ERC20BridgeSource.JetSwap:
return JETSWAP_ROUTER_BY_CHAIN_ID[chainId];
case ERC20BridgeSource.Pangolin:
return PANGOLIN_ROUTER_BY_CHAIN_ID[chainId];
case ERC20BridgeSource.TraderJoe:
return TRADER_JOE_ROUTER_BY_CHAIN_ID[chainId];
default:
throw new Error(`Unknown UniswapV2 like source ${source}`);
}
}
const BAD_TOKENS_BY_SOURCE: Partial<{ [key in ERC20BridgeSource]: string[] }> = {
[ERC20BridgeSource.Uniswap]: [
'0xb8c77482e45f1f44de1745f52c74426c631bdd52', // BNB
],
};
export function isBadTokenForSource(token: string, source: ERC20BridgeSource): boolean {
return (BAD_TOKENS_BY_SOURCE[source] || []).includes(token.toLowerCase());
}

View File

@@ -1,5 +1,4 @@
import { Web3Wrapper } from '@0x/dev-utils';
import { FillQuoteTransformerOrderType } from '@0x/protocol-utils';
import { BigNumber, logUtils } from '@0x/utils';
import * as _ from 'lodash';
@@ -8,10 +7,6 @@ import { MarketOperation } from '../../types';
import { COMPARISON_PRICE_DECIMALS, SOURCE_FLAGS } from './constants';
import {
ComparisonPrice,
ERC20BridgeSource,
ExchangeProxyOverhead,
FeeEstimate,
FeeSchedule,
MarketSideLiquidity,
} from './types';
@@ -29,41 +24,20 @@ export function getComparisonPrices(
adjustedRate: BigNumber,
amount: BigNumber,
marketSideLiquidity: MarketSideLiquidity,
feeSchedule: FeeSchedule,
exchangeProxyOverhead: ExchangeProxyOverhead,
gasPrice: BigNumber,
): ComparisonPrice {
let wholeOrder: BigNumber | undefined;
let feeInEth: BigNumber | number;
// HACK: get the fee penalty of a single 0x native order
// The FeeSchedule function takes in a `FillData` object and returns a fee estimate in ETH
// We don't have fill data here, we just want the cost of a single native order, so we pass in undefined
// This works because the feeSchedule returns a constant for Native orders, this will need
// to be tweaked if the feeSchedule for native orders uses the fillData passed in
// 2 potential issues: there is no native fee schedule or the fee schedule depends on fill data
if (feeSchedule[ERC20BridgeSource.Native] === undefined) {
logUtils.warn('ComparisonPrice function did not find native order fee schedule');
return { wholeOrder };
} else {
try {
const fillFeeInEth = new BigNumber(
(feeSchedule[ERC20BridgeSource.Native] as FeeEstimate)({ type: FillQuoteTransformerOrderType.Rfq }),
);
const exchangeProxyOverheadInEth = new BigNumber(exchangeProxyOverhead(SOURCE_FLAGS.RfqOrder));
feeInEth = fillFeeInEth.plus(exchangeProxyOverheadInEth);
} catch {
logUtils.warn('Native order fee schedule requires fill data');
return { wholeOrder };
}
}
let feeInEth = gasPrice.times(100e3);
const [inputAmountPerEth, outputAmountPerEth] = [
marketSideLiquidity.tokenAmountPerEth[marketSideLiquidity.inputToken],
marketSideLiquidity.tokenAmountPerEth[marketSideLiquidity.outputToken],
];
// Calc native order fee penalty in output unit (maker units for sells, taker unit for buys)
const feePenalty = !marketSideLiquidity.outputAmountPerEth.isZero()
? marketSideLiquidity.outputAmountPerEth.times(feeInEth)
const feePenalty = !outputAmountPerEth.isZero()
? outputAmountPerEth.times(feeInEth)
: // if it's a sell, the input token is the taker token
marketSideLiquidity.inputAmountPerEth
inputAmountPerEth
.times(feeInEth)
.times(marketSideLiquidity.side === MarketOperation.Sell ? adjustedRate : adjustedRate.pow(-1));

View File

@@ -1,10 +1,11 @@
import { FillQuoteTransformerOrderType } from '@0x/protocol-utils';
import { BigNumber, hexUtils } from '@0x/utils';
import { MarketOperation, NativeOrderWithFillableAmounts } from '../../types';
import { NativeOrderWithFillableAmounts } from '../native_orders';
import { MarketOperation } from '../../types';
import { POSITIVE_INF, SOURCE_FLAGS, ZERO_AMOUNT } from './constants';
import { DexSample, ERC20BridgeSource, FeeSchedule, Fill } from './types';
import { DexSample, ERC20BridgeSource, Fill, GenericBridgeFill, NativeOrderFill } from './types';
// tslint:disable: prefer-for-of no-bitwise completed-docs
@@ -18,12 +19,9 @@ export function createFills(opts: {
targetInput?: BigNumber;
outputAmountPerEth?: BigNumber;
inputAmountPerEth?: BigNumber;
excludedSources?: ERC20BridgeSource[];
feeSchedule?: FeeSchedule;
gasPrice: BigNumber;
}): Fill[][] {
const { side } = opts;
const excludedSources = opts.excludedSources || [];
const feeSchedule = opts.feeSchedule || {};
const orders = opts.orders || [];
const dexQuotes = opts.dexQuotes || [];
const outputAmountPerEth = opts.outputAmountPerEth || ZERO_AMOUNT;
@@ -35,15 +33,15 @@ export function createFills(opts: {
opts.targetInput,
outputAmountPerEth,
inputAmountPerEth,
feeSchedule,
opts.gasPrice,
);
// Create DEX fills.
const dexFills = dexQuotes.map(singleSourceSamples =>
dexSamplesToFills(side, singleSourceSamples, outputAmountPerEth, inputAmountPerEth, feeSchedule),
dexSamplesToFills(side, singleSourceSamples, outputAmountPerEth, inputAmountPerEth, opts.gasPrice),
);
return [...dexFills, nativeFills]
.map(p => clipFillsToInput(p, opts.targetInput))
.filter(fills => hasLiquidity(fills) && !excludedSources.includes(fills[0].source));
.filter(fills => hasLiquidity(fills));
}
function clipFillsToInput(fills: Fill[], targetInput: BigNumber = POSITIVE_INF): Fill[] {
@@ -71,27 +69,59 @@ function hasLiquidity(fills: Fill[]): boolean {
return true;
}
function nativeOrdersToFills(
export function ethToOutputAmount({
input,
output,
ethAmount,
inputAmountPerEth,
outputAmountPerEth,
}: {
input: BigNumber;
output: BigNumber;
inputAmountPerEth: BigNumber;
outputAmountPerEth: BigNumber;
ethAmount: BigNumber | number;
}): BigNumber {
return !outputAmountPerEth.isZero()
? outputAmountPerEth.times(ethAmount)
: inputAmountPerEth.times(ethAmount).times(output.dividedToIntegerBy(input));
}
export function nativeOrdersToFills(
side: MarketOperation,
orders: NativeOrderWithFillableAmounts[],
targetInput: BigNumber = POSITIVE_INF,
outputAmountPerEth: BigNumber,
inputAmountPerEth: BigNumber,
fees: FeeSchedule,
): Fill[] {
gasPrice: BigNumber,
): NativeOrderFill[] {
if (orders.length === 0) {
return [];
}
const sourcePathId = hexUtils.random();
// Create a single path from all orders.
let fills: Array<Fill & { adjustedRate: BigNumber }> = [];
let fills: Array<NativeOrderFill & { adjustedRate: BigNumber }> = [];
for (const o of orders) {
const { fillableTakerAmount, fillableTakerFeeAmount, fillableMakerAmount, type } = o;
const makerAmount = fillableMakerAmount;
const takerAmount = fillableTakerAmount.plus(fillableTakerFeeAmount);
const input = side === MarketOperation.Sell ? takerAmount : makerAmount;
const output = side === MarketOperation.Sell ? makerAmount : takerAmount;
const fee = fees[ERC20BridgeSource.Native] === undefined ? 0 : fees[ERC20BridgeSource.Native]!(o);
const outputPenalty = !outputAmountPerEth.isZero()
? outputAmountPerEth.times(fee)
: inputAmountPerEth.times(fee).times(output.dividedToIntegerBy(input));
const { fillableTakerAmount, fillableMakerAmount, type } = o;
// TODO(lawrence): handle taker fees.
if (o.fillableTakerFeeAmount.gt(0)) {
continue;
}
let input, output;
if (side === MarketOperation.Sell) {
input = fillableTakerAmount;
output = fillableMakerAmount;
} else {
input = fillableMakerAmount;
output = fillableTakerAmount;
}
const outputPenalty = ethToOutputAmount({
input,
output,
inputAmountPerEth,
outputAmountPerEth,
ethAmount: gasPrice.times(o.gasCost),
});
// targetInput can be less than the order size
// whilst the penalty is constant, it affects the adjusted output
// only up until the target has been exhausted.
@@ -109,17 +139,21 @@ function nativeOrdersToFills(
continue;
}
fills.push({
type,
sourcePathId,
adjustedRate,
adjustedOutput,
adjustedRate,
input: clippedInput,
output: clippedOutput,
flags: SOURCE_FLAGS[type === FillQuoteTransformerOrderType.Rfq ? 'RfqOrder' : 'LimitOrder'],
index: 0, // TBD
parent: undefined, // TBD
source: ERC20BridgeSource.Native,
type,
fillData: { ...o },
gasCost: o.gasCost,
data: {
order: o.order,
signature: o.signature,
},
});
}
// Sort by descending adjusted rate.
@@ -132,15 +166,15 @@ function nativeOrdersToFills(
return fills;
}
function dexSamplesToFills(
export function dexSamplesToFills(
side: MarketOperation,
samples: DexSample[],
outputAmountPerEth: BigNumber,
inputAmountPerEth: BigNumber,
fees: FeeSchedule,
): Fill[] {
gasPrice: BigNumber,
): GenericBridgeFill[] {
const sourcePathId = hexUtils.random();
const fills: Fill[] = [];
const fills: GenericBridgeFill[] = [];
// Drop any non-zero entries. This can occur if the any fills on Kyber were UniswapReserves
// We need not worry about Kyber fills going to UniswapReserve as the input amount
// we fill is the same as we sampled. I.e we received [0,20,30] output from [1,2,3] input
@@ -149,16 +183,20 @@ function dexSamplesToFills(
for (let i = 0; i < nonzeroSamples.length; i++) {
const sample = nonzeroSamples[i];
const prevSample = i === 0 ? undefined : nonzeroSamples[i - 1];
const { source, fillData } = sample;
const { source, encodedFillData, metadata } = sample;
const input = sample.input.minus(prevSample ? prevSample.input : 0);
const output = sample.output.minus(prevSample ? prevSample.output : 0);
const fee = fees[source] === undefined ? 0 : fees[source]!(sample.fillData) || 0;
const fee = gasPrice.times(sample.gasCost);
let penalty = ZERO_AMOUNT;
if (i === 0) {
// Only the first fill in a DEX path incurs a penalty.
penalty = !outputAmountPerEth.isZero()
? outputAmountPerEth.times(fee)
: inputAmountPerEth.times(fee).times(output.dividedToIntegerBy(input));
penalty = ethToOutputAmount({
input,
output,
inputAmountPerEth,
outputAmountPerEth,
ethAmount: fee,
});
}
const adjustedOutput = side === MarketOperation.Sell ? output.minus(penalty) : output.plus(penalty);
@@ -168,11 +206,15 @@ function dexSamplesToFills(
output,
adjustedOutput,
source,
fillData,
type: FillQuoteTransformerOrderType.Bridge,
gasCost: sample.gasCost,
index: i,
parent: i !== 0 ? fills[fills.length - 1] : undefined,
flags: SOURCE_FLAGS[source],
data: {
...metadata,
encodedFillData,
},
});
}
return fills;

View File

@@ -1,16 +1,6 @@
import { BigNumber } from '@0x/utils';
import * as _ from 'lodash';
import { Omit } from '../../types';
import { ZERO_AMOUNT } from './constants';
import { getTwoHopAdjustedRate } from './rate_utils';
import {
DexSample,
ExchangeProxyOverhead,
FeeSchedule,
MarketSideLiquidity,
MultiHopFillData,
TokenAdjacencyGraph,
} from './types';
@@ -30,49 +20,3 @@ export function getIntermediateTokens(
token => token.toLowerCase() !== makerToken.toLowerCase() && token.toLowerCase() !== takerToken.toLowerCase(),
);
}
/**
* Returns the best two-hop quote and the fee-adjusted rate of that quote.
*/
export function getBestTwoHopQuote(
marketSideLiquidity: Omit<MarketSideLiquidity, 'makerTokenDecimals' | 'takerTokenDecimals'>,
feeSchedule?: FeeSchedule,
exchangeProxyOverhead?: ExchangeProxyOverhead,
): { quote: DexSample<MultiHopFillData> | undefined; adjustedRate: BigNumber } {
const { side, inputAmount, outputAmountPerEth, quotes } = marketSideLiquidity;
const { twoHopQuotes } = quotes;
// Ensure the expected data we require exists. In the case where all hops reverted
// or there were no sources included that allowed for multi hop,
// we can end up with empty, but not undefined, fill data
const filteredQuotes = twoHopQuotes.filter(
quote =>
quote &&
quote.fillData &&
quote.fillData.firstHopSource &&
quote.fillData.secondHopSource &&
quote.output.isGreaterThan(ZERO_AMOUNT),
);
if (filteredQuotes.length === 0) {
return { quote: undefined, adjustedRate: ZERO_AMOUNT };
}
const best = filteredQuotes
.map(quote =>
getTwoHopAdjustedRate(side, quote, inputAmount, outputAmountPerEth, feeSchedule, exchangeProxyOverhead),
)
.reduce(
(prev, curr, i) =>
curr.isGreaterThan(prev.adjustedRate) ? { adjustedRate: curr, quote: filteredQuotes[i] } : prev,
{
adjustedRate: getTwoHopAdjustedRate(
side,
filteredQuotes[0],
inputAmount,
outputAmountPerEth,
feeSchedule,
exchangeProxyOverhead,
),
quote: filteredQuotes[0],
},
);
return best;
}

View File

@@ -1,82 +1,19 @@
import { BridgeProtocol, encodeBridgeSourceId, FillQuoteTransformerOrderType } from '@0x/protocol-utils';
import { AbiEncoder, BigNumber } from '@0x/utils';
import { AssetSwapperContractAddresses, MarketOperation } from '../../types';
import { Address, MarketOperation } from '../../types';
import { MAX_UINT256, NULL_BYTES, ZERO_AMOUNT } from './constants';
import {
AggregationError,
BalancerFillData,
BalancerV2FillData,
BancorFillData,
CollapsedFill,
CurveFillData,
DexSample,
DODOFillData,
CollapsedGenericBridgeFill,
ERC20BridgeSource,
FillData,
FinalUniswapV3FillData,
GenericRouterFillData,
KyberDmmFillData,
KyberFillData,
LidoFillData,
LiquidityProviderFillData,
MakerPsmFillData,
MooniswapFillData,
MultiHopFillData,
NativeCollapsedFill,
NativeLimitOrderFillData,
NativeRfqOrderFillData,
OptimizedMarketBridgeOrder,
OptimizedMarketOrder,
OptimizedMarketOrderBase,
OrderDomain,
ShellFillData,
UniswapV2FillData,
UniswapV3FillData,
CollapsedNativeOrderFill,
OptimizedGenericBridgeOrder,
OptimizedLimitOrder,
OptimizedRfqOrder,
} from './types';
// tslint:disable completed-docs
export interface CreateOrderFromPathOpts {
side: MarketOperation;
inputToken: string;
outputToken: string;
orderDomain: OrderDomain;
contractAddresses: AssetSwapperContractAddresses;
bridgeSlippage: number;
}
export function createOrdersFromTwoHopSample(
sample: DexSample<MultiHopFillData>,
opts: CreateOrderFromPathOpts,
): OptimizedMarketOrder[] {
const [makerToken, takerToken] = getMakerTakerTokens(opts);
const { firstHopSource, secondHopSource, intermediateToken } = sample.fillData;
const firstHopFill: CollapsedFill = {
sourcePathId: '',
source: firstHopSource.source,
type: FillQuoteTransformerOrderType.Bridge,
input: opts.side === MarketOperation.Sell ? sample.input : ZERO_AMOUNT,
output: opts.side === MarketOperation.Sell ? ZERO_AMOUNT : sample.output,
subFills: [],
fillData: firstHopSource.fillData,
};
const secondHopFill: CollapsedFill = {
sourcePathId: '',
source: secondHopSource.source,
type: FillQuoteTransformerOrderType.Bridge,
input: opts.side === MarketOperation.Sell ? MAX_UINT256 : sample.input,
output: opts.side === MarketOperation.Sell ? sample.output : MAX_UINT256,
subFills: [],
fillData: secondHopSource.fillData,
};
return [
createBridgeOrder(firstHopFill, intermediateToken, takerToken, opts.side),
createBridgeOrder(secondHopFill, makerToken, intermediateToken, opts.side),
];
}
export function getErc20BridgeSourceToBridgeSource(source: ERC20BridgeSource): string {
switch (source) {
case ERC20BridgeSource.Balancer:
@@ -180,347 +117,64 @@ export function getErc20BridgeSourceToBridgeSource(source: ERC20BridgeSource): s
return encodeBridgeSourceId(BridgeProtocol.Nerve, 'IronSwap');
case ERC20BridgeSource.ACryptos:
return encodeBridgeSourceId(BridgeProtocol.Curve, 'ACryptoS');
case ERC20BridgeSource.Clipper:
return encodeBridgeSourceId(BridgeProtocol.Clipper, 'Clipper');
case ERC20BridgeSource.Pangolin:
return encodeBridgeSourceId(BridgeProtocol.UniswapV2, 'Pangolin');
case ERC20BridgeSource.TraderJoe:
return encodeBridgeSourceId(BridgeProtocol.UniswapV2, 'TraderJoe');
case ERC20BridgeSource.SpiritSwap:
return encodeBridgeSourceId(BridgeProtocol.UniswapV2, 'SpiritSwap');
case ERC20BridgeSource.SpookySwap:
return encodeBridgeSourceId(BridgeProtocol.UniswapV2, 'SpookySwap');
default:
throw new Error(AggregationError.NoBridgeForSource);
}
}
export function createBridgeDataForBridgeOrder(order: OptimizedMarketBridgeOrder): string {
let bridgeData: string;
if (
order.source === ERC20BridgeSource.MultiHop ||
order.source === ERC20BridgeSource.MultiBridge ||
order.source === ERC20BridgeSource.Native
) {
throw new Error('Invalid order to encode for Bridge Data');
}
const encoder = BRIDGE_ENCODERS[order.source];
if (!encoder) {
throw new Error(AggregationError.NoBridgeForSource);
}
switch (order.source) {
case ERC20BridgeSource.Curve:
case ERC20BridgeSource.CurveV2:
case ERC20BridgeSource.Swerve:
case ERC20BridgeSource.SnowSwap:
case ERC20BridgeSource.Nerve:
case ERC20BridgeSource.Belt:
case ERC20BridgeSource.Ellipsis:
case ERC20BridgeSource.Smoothy:
case ERC20BridgeSource.Saddle:
case ERC20BridgeSource.XSigma:
case ERC20BridgeSource.FirebirdOneSwap:
case ERC20BridgeSource.IronSwap:
case ERC20BridgeSource.ACryptos:
const curveFillData = (order as OptimizedMarketBridgeOrder<CurveFillData>).fillData;
bridgeData = encoder.encode([
curveFillData.pool.poolAddress,
curveFillData.pool.exchangeFunctionSelector,
curveFillData.fromTokenIdx,
curveFillData.toTokenIdx,
]);
break;
case ERC20BridgeSource.Balancer:
case ERC20BridgeSource.Cream:
const balancerFillData = (order as OptimizedMarketBridgeOrder<BalancerFillData>).fillData;
bridgeData = encoder.encode([balancerFillData.poolAddress]);
break;
case ERC20BridgeSource.BalancerV2:
const balancerV2FillData = (order as OptimizedMarketBridgeOrder<BalancerV2FillData>).fillData;
const { vault, poolId } = balancerV2FillData;
bridgeData = encoder.encode([vault, poolId]);
break;
case ERC20BridgeSource.Bancor:
const bancorFillData = (order as OptimizedMarketBridgeOrder<BancorFillData>).fillData;
bridgeData = encoder.encode([bancorFillData.networkAddress, bancorFillData.path]);
break;
case ERC20BridgeSource.UniswapV2:
case ERC20BridgeSource.SushiSwap:
case ERC20BridgeSource.CryptoCom:
case ERC20BridgeSource.Linkswap:
case ERC20BridgeSource.PancakeSwap:
case ERC20BridgeSource.PancakeSwapV2:
case ERC20BridgeSource.BakerySwap:
case ERC20BridgeSource.ApeSwap:
case ERC20BridgeSource.CafeSwap:
case ERC20BridgeSource.CheeseSwap:
case ERC20BridgeSource.JulSwap:
case ERC20BridgeSource.QuickSwap:
case ERC20BridgeSource.ComethSwap:
case ERC20BridgeSource.Dfyn:
case ERC20BridgeSource.WaultSwap:
case ERC20BridgeSource.Polydex:
case ERC20BridgeSource.ShibaSwap:
case ERC20BridgeSource.JetSwap:
case ERC20BridgeSource.Pangolin:
case ERC20BridgeSource.TraderJoe:
const uniswapV2FillData = (order as OptimizedMarketBridgeOrder<UniswapV2FillData>).fillData;
bridgeData = encoder.encode([uniswapV2FillData.router, uniswapV2FillData.tokenAddressPath]);
break;
case ERC20BridgeSource.Kyber:
const kyberFillData = (order as OptimizedMarketBridgeOrder<KyberFillData>).fillData;
bridgeData = encoder.encode([kyberFillData.networkProxy, kyberFillData.hint]);
break;
case ERC20BridgeSource.Mooniswap:
const mooniswapFillData = (order as OptimizedMarketBridgeOrder<MooniswapFillData>).fillData;
bridgeData = encoder.encode([mooniswapFillData.poolAddress]);
break;
case ERC20BridgeSource.Dodo:
const dodoFillData = (order as OptimizedMarketBridgeOrder<DODOFillData>).fillData;
bridgeData = encoder.encode([
dodoFillData.helperAddress,
dodoFillData.poolAddress,
dodoFillData.isSellBase,
]);
break;
case ERC20BridgeSource.DodoV2:
const dodoV2FillData = (order as OptimizedMarketBridgeOrder<DODOFillData>).fillData;
bridgeData = encoder.encode([dodoV2FillData.poolAddress, dodoV2FillData.isSellBase]);
break;
case ERC20BridgeSource.Shell:
case ERC20BridgeSource.Component:
const shellFillData = (order as OptimizedMarketBridgeOrder<ShellFillData>).fillData;
bridgeData = encoder.encode([shellFillData.poolAddress]);
break;
case ERC20BridgeSource.LiquidityProvider:
const lpFillData = (order as OptimizedMarketBridgeOrder<LiquidityProviderFillData>).fillData;
bridgeData = encoder.encode([lpFillData.poolAddress, tokenAddressEncoder.encode([order.takerToken])]);
break;
case ERC20BridgeSource.Uniswap:
const uniFillData = (order as OptimizedMarketBridgeOrder<GenericRouterFillData>).fillData;
bridgeData = encoder.encode([uniFillData.router]);
break;
case ERC20BridgeSource.Eth2Dai:
const oasisFillData = (order as OptimizedMarketBridgeOrder<GenericRouterFillData>).fillData;
bridgeData = encoder.encode([oasisFillData.router]);
break;
case ERC20BridgeSource.MStable:
const mStableFillData = (order as OptimizedMarketBridgeOrder<GenericRouterFillData>).fillData;
bridgeData = encoder.encode([mStableFillData.router]);
break;
case ERC20BridgeSource.MakerPsm:
const psmFillData = (order as OptimizedMarketBridgeOrder<MakerPsmFillData>).fillData;
bridgeData = encoder.encode([psmFillData.psmAddress, psmFillData.gemTokenAddress]);
break;
case ERC20BridgeSource.UniswapV3:
const uniswapV3FillData = (order as OptimizedMarketBridgeOrder<FinalUniswapV3FillData>).fillData;
bridgeData = encoder.encode([uniswapV3FillData.router, uniswapV3FillData.uniswapPath]);
break;
case ERC20BridgeSource.KyberDmm:
const kyberDmmFillData = (order as OptimizedMarketBridgeOrder<KyberDmmFillData>).fillData;
bridgeData = encoder.encode([
kyberDmmFillData.router,
kyberDmmFillData.poolsPath,
kyberDmmFillData.tokenAddressPath,
]);
break;
case ERC20BridgeSource.Lido:
const lidoFillData = (order as OptimizedMarketBridgeOrder<LidoFillData>).fillData;
bridgeData = encoder.encode([lidoFillData.stEthTokenAddress]);
break;
case ERC20BridgeSource.Clipper:
const clipperFillData = (order as OptimizedMarketBridgeOrder<LiquidityProviderFillData>).fillData;
bridgeData = encoder.encode([clipperFillData.poolAddress, NULL_BYTES]);
break;
default:
throw new Error(AggregationError.NoBridgeForSource);
}
return bridgeData;
}
export function createBridgeOrder(
fill: CollapsedFill,
makerToken: string,
takerToken: string,
side: MarketOperation,
): OptimizedMarketBridgeOrder {
const [makerAmount, takerAmount] = getFillTokenAmounts(fill, side);
fill: CollapsedGenericBridgeFill,
inputToken: Address,
outputToken: Address,
): OptimizedGenericBridgeOrder {
return {
makerToken,
takerToken,
makerAmount,
takerAmount,
fillData: createFinalBridgeOrderFillDataFromCollapsedFill(fill),
inputToken,
outputToken,
inputAmount: fill.input,
outputAmount: fill.output,
fillData: fill.data,
source: fill.source,
sourcePathId: fill.sourcePathId,
type: FillQuoteTransformerOrderType.Bridge,
fills: [fill],
gasCost: fill.gasCost,
isFallback: fill.isFallback,
...((fill as any).metadata !== undefined ? { metadata: (fill as any).metadata } : {}),
};
}
function createFinalBridgeOrderFillDataFromCollapsedFill(fill: CollapsedFill): FillData {
switch (fill.source) {
case ERC20BridgeSource.UniswapV3: {
const fd = fill.fillData as UniswapV3FillData;
return {
router: fd.router,
tokenAddressPath: fd.tokenAddressPath,
uniswapPath: getBestUniswapV3PathForInputAmount(fd, fill.input),
};
}
default:
break;
}
return fill.fillData;
}
function getBestUniswapV3PathForInputAmount(fillData: UniswapV3FillData, inputAmount: BigNumber): string {
if (fillData.pathAmounts.length === 0) {
throw new Error(`No Uniswap V3 paths`);
}
// Find the best path that can satisfy `inputAmount`.
// Assumes `fillData.pathAmounts` is sorted ascending.
for (const { inputAmount: pathInputAmount, uniswapPath } of fillData.pathAmounts) {
if (pathInputAmount.gte(inputAmount)) {
return uniswapPath;
}
}
return fillData.pathAmounts[fillData.pathAmounts.length - 1].uniswapPath;
}
export function getMakerTakerTokens(opts: CreateOrderFromPathOpts): [string, string] {
const makerToken = opts.side === MarketOperation.Sell ? opts.outputToken : opts.inputToken;
const takerToken = opts.side === MarketOperation.Sell ? opts.inputToken : opts.outputToken;
export function getMakerTakerTokens(side: MarketOperation, inputToken: Address, outputToken: Address): [Address, Address] {
const makerToken = side === MarketOperation.Sell ? outputToken : inputToken;
const takerToken = side === MarketOperation.Sell ? inputToken : outputToken;
return [makerToken, takerToken];
}
export const poolEncoder = AbiEncoder.create([{ name: 'poolAddress', type: 'address' }]);
const curveEncoder = AbiEncoder.create([
{ name: 'curveAddress', type: 'address' },
{ name: 'exchangeFunctionSelector', type: 'bytes4' },
{ name: 'fromTokenIdx', type: 'int128' },
{ name: 'toTokenIdx', type: 'int128' },
]);
const makerPsmEncoder = AbiEncoder.create([
{ name: 'psmAddress', type: 'address' },
{ name: 'gemTokenAddress', type: 'address' },
]);
const balancerV2Encoder = AbiEncoder.create([
{ name: 'vault', type: 'address' },
{ name: 'poolId', type: 'bytes32' },
]);
const routerAddressPathEncoder = AbiEncoder.create('(address,address[])');
const tokenAddressEncoder = AbiEncoder.create([{ name: 'tokenAddress', type: 'address' }]);
export const BRIDGE_ENCODERS: {
[key in Exclude<
ERC20BridgeSource,
ERC20BridgeSource.Native | ERC20BridgeSource.MultiHop | ERC20BridgeSource.MultiBridge
>]: AbiEncoder.DataType;
} = {
[ERC20BridgeSource.LiquidityProvider]: AbiEncoder.create([
{ name: 'provider', type: 'address' },
{ name: 'data', type: 'bytes' },
]),
[ERC20BridgeSource.Kyber]: AbiEncoder.create([
{ name: 'kyberNetworkProxy', type: 'address' },
{ name: 'hint', type: 'bytes' },
]),
[ERC20BridgeSource.Dodo]: AbiEncoder.create([
{ name: 'helper', type: 'address' },
{ name: 'poolAddress', type: 'address' },
{ name: 'isSellBase', type: 'bool' },
]),
[ERC20BridgeSource.DodoV2]: AbiEncoder.create([
{ name: 'poolAddress', type: 'address' },
{ name: 'isSellBase', type: 'bool' },
]),
// Curve like
[ERC20BridgeSource.Curve]: curveEncoder,
[ERC20BridgeSource.CurveV2]: curveEncoder,
[ERC20BridgeSource.Swerve]: curveEncoder,
[ERC20BridgeSource.SnowSwap]: curveEncoder,
[ERC20BridgeSource.Nerve]: curveEncoder,
[ERC20BridgeSource.Belt]: curveEncoder,
[ERC20BridgeSource.Ellipsis]: curveEncoder,
[ERC20BridgeSource.Smoothy]: curveEncoder,
[ERC20BridgeSource.Saddle]: curveEncoder,
[ERC20BridgeSource.XSigma]: curveEncoder,
[ERC20BridgeSource.FirebirdOneSwap]: curveEncoder,
[ERC20BridgeSource.IronSwap]: curveEncoder,
[ERC20BridgeSource.ACryptos]: curveEncoder,
// UniswapV2 like, (router, address[])
[ERC20BridgeSource.Bancor]: routerAddressPathEncoder,
[ERC20BridgeSource.UniswapV2]: routerAddressPathEncoder,
[ERC20BridgeSource.SushiSwap]: routerAddressPathEncoder,
[ERC20BridgeSource.CryptoCom]: routerAddressPathEncoder,
[ERC20BridgeSource.Linkswap]: routerAddressPathEncoder,
[ERC20BridgeSource.ShibaSwap]: routerAddressPathEncoder,
[ERC20BridgeSource.Pangolin]: routerAddressPathEncoder,
[ERC20BridgeSource.TraderJoe]: routerAddressPathEncoder,
// BSC
[ERC20BridgeSource.PancakeSwap]: routerAddressPathEncoder,
[ERC20BridgeSource.PancakeSwapV2]: routerAddressPathEncoder,
[ERC20BridgeSource.BakerySwap]: routerAddressPathEncoder,
[ERC20BridgeSource.ApeSwap]: routerAddressPathEncoder,
[ERC20BridgeSource.CafeSwap]: routerAddressPathEncoder,
[ERC20BridgeSource.CheeseSwap]: routerAddressPathEncoder,
[ERC20BridgeSource.JulSwap]: routerAddressPathEncoder,
[ERC20BridgeSource.WaultSwap]: routerAddressPathEncoder,
// Polygon
[ERC20BridgeSource.QuickSwap]: routerAddressPathEncoder,
[ERC20BridgeSource.ComethSwap]: routerAddressPathEncoder,
[ERC20BridgeSource.Dfyn]: routerAddressPathEncoder,
[ERC20BridgeSource.Polydex]: routerAddressPathEncoder,
[ERC20BridgeSource.JetSwap]: routerAddressPathEncoder,
// Generic pools
[ERC20BridgeSource.Shell]: poolEncoder,
[ERC20BridgeSource.Component]: poolEncoder,
[ERC20BridgeSource.Mooniswap]: poolEncoder,
[ERC20BridgeSource.Eth2Dai]: poolEncoder,
[ERC20BridgeSource.MStable]: poolEncoder,
[ERC20BridgeSource.Balancer]: poolEncoder,
[ERC20BridgeSource.Cream]: poolEncoder,
[ERC20BridgeSource.Uniswap]: poolEncoder,
// Custom integrations
[ERC20BridgeSource.MakerPsm]: makerPsmEncoder,
[ERC20BridgeSource.BalancerV2]: balancerV2Encoder,
[ERC20BridgeSource.UniswapV3]: AbiEncoder.create([
{ name: 'router', type: 'address' },
{ name: 'path', type: 'bytes' },
]),
[ERC20BridgeSource.KyberDmm]: AbiEncoder.create('(address,address[],address[])'),
[ERC20BridgeSource.Lido]: AbiEncoder.create('(address)'),
[ERC20BridgeSource.Clipper]: AbiEncoder.create([
{ name: 'provider', type: 'address' },
{ name: 'data', type: 'bytes' },
]),
};
function getFillTokenAmounts(fill: CollapsedFill, side: MarketOperation): [BigNumber, BigNumber] {
return [
// Maker asset amount.
side === MarketOperation.Sell ? fill.output.integerValue(BigNumber.ROUND_DOWN) : fill.input,
// Taker asset amount.
side === MarketOperation.Sell ? fill.input : fill.output.integerValue(BigNumber.ROUND_UP),
];
}
export function createNativeOptimizedOrder(
fill: NativeCollapsedFill,
fill: CollapsedNativeOrderFill,
side: MarketOperation,
): OptimizedMarketOrderBase<NativeLimitOrderFillData> | OptimizedMarketOrderBase<NativeRfqOrderFillData> {
const fillData = fill.fillData;
const [makerAmount, takerAmount] = getFillTokenAmounts(fill, side);
const base = {
type: fill.type,
source: ERC20BridgeSource.Native,
makerToken: fillData.order.makerToken,
takerToken: fillData.order.takerToken,
makerAmount,
takerAmount,
fills: [fill],
fillData,
};
return fill.type === FillQuoteTransformerOrderType.Rfq
? { ...base, type: FillQuoteTransformerOrderType.Rfq, fillData: fillData as NativeRfqOrderFillData }
: { ...base, type: FillQuoteTransformerOrderType.Limit, fillData: fillData as NativeLimitOrderFillData };
): OptimizedLimitOrder | OptimizedRfqOrder {
throw new Error(`No implementado`);
// const fillData = fill.fillData;
// const [makerAmount, takerAmount] = getFillTokenAmounts(fill, side);
// const base = {
// type: fill.type,
// source: ERC20BridgeSource.Native,
// makerToken: fillData.order.makerToken,
// takerToken: fillData.order.takerToken,
// makerAmount,
// takerAmount,
// fills: [fill],
// fillData,
// };
// return fill.type === FillQuoteTransformerOrderType.Rfq
// ? { ...base, type: FillQuoteTransformerOrderType.Rfq, fillData: fillData as NativeRfqOrderFillData }
// : { ...base, type: FillQuoteTransformerOrderType.Limit, fillData: fillData as NativeLimitOrderFillData };
}

Some files were not shown because too many files have changed in this diff Show More