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

🔂 Liquidity Provider Asset Swapper integration #2505

Merged
merged 33 commits into from
Mar 6, 2020
Merged
Show file tree
Hide file tree
Changes from 18 commits
Commits
Show all changes
33 commits
Select commit Hold shift + click to select a range
77d7afe
created DummyPLPRegistry and DummyPLP + generated wrappers for these …
Feb 27, 2020
3c0fd54
generated wrappers for ERC20 sampler
Feb 27, 2020
8be60e2
initial unit tests for DEX sampler
Feb 27, 2020
9608d8f
added more unit tests for PLP DEX sampling
Feb 28, 2020
7181be8
added more unit tests, reinforced existing tests, added more implemen…
Feb 28, 2020
e5df51a
tidy up tests, add other tests
Feb 28, 2020
bcc3e5e
refactor tests, and add more tests
Feb 28, 2020
9d2aef5
Added linting and prettifying
Feb 28, 2020
5d4bbd5
Merge branch 'development' of github.com:0xProject/0x-monorepo into f…
Feb 28, 2020
08619e2
re-generate ERC20BridgeSampler artifacts
Feb 28, 2020
7495ac8
performed a rename
Feb 29, 2020
fa8e8ad
Update artifacts after rename
Feb 29, 2020
82b0f85
transformed the new artifacts
Mar 2, 2020
82de5ad
refactored sampler operations into a single external file
Mar 2, 2020
599af2b
factored out interfaces in `types.ts`
Mar 2, 2020
8186d62
fixed a bug with imports
Mar 2, 2020
b0fd78d
added linting to the contracts
Mar 2, 2020
18ce19a
refactored imports
Mar 2, 2020
49b7c9c
update sampler address
Mar 3, 2020
807904b
addressed PR comments
Mar 4, 2020
99dc4b8
refactor more code
Mar 4, 2020
bc7504c
refresh wrappers and artifacts
Mar 4, 2020
3eb1429
adds possibility to exclude PLP
Mar 4, 2020
5909189
Merge branch 'development' of github.com:0xProject/0x-monorepo into f…
Mar 4, 2020
d0d7d27
upgrades contracts-erc20-bridge-sampler dependency
Mar 4, 2020
61f03b0
invert maker and taker token variable
Mar 6, 2020
32e1ae2
added unit tests to avoid regression due to variable order
Mar 6, 2020
fa886aa
prettify and lint
Mar 6, 2020
dbc5c0d
moved unit tests to the appropriate sections
Mar 6, 2020
36c457f
added unit tests for the Liquidity Provider
Mar 6, 2020
17b2320
remove unit tests that were ported to `erc20-bridge-sampler` package
Mar 6, 2020
d849813
Merge branch 'development' of github.com:0xProject/0x-monorepo into f…
Mar 6, 2020
659e899
Completed feedback
Mar 6, 2020
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
pragma solidity ^0.5.9;
pragma experimental ABIEncoderV2;


contract DummyLiquidityProvider
{
constructor()
public
// solhint-disable-next-line no-empty-blocks
{}
Copy link
Contributor

Choose a reason for hiding this comment

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

no constructor needed

Copy link
Contributor Author

Choose a reason for hiding this comment

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

done


/// @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(
Copy link
Contributor

Choose a reason for hiding this comment

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

Verified that this matches the spec ✅

address, /* takerToken */
address, /* makerToken */
uint256 sellAmount
)
external
view
returns (uint256 makerTokenAmount) {
makerTokenAmount = sellAmount - 1;
Copy link
Contributor

Choose a reason for hiding this comment

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

style nit:

Suggested change
returns (uint256 makerTokenAmount) {
makerTokenAmount = sellAmount - 1;
returns (uint256 makerTokenAmount)
{
makerTokenAmount = sellAmount - 1;

Copy link
Contributor Author

Choose a reason for hiding this comment

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

done

}

/// @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(
Copy link
Contributor

Choose a reason for hiding this comment

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

Verified that this matches the spec ✅

address, /* takerToken */
address, /* makerToken */
uint256 buyAmount
)
external
view
returns (uint256 takerTokenAmount) {
takerTokenAmount = buyAmount + 1;
Copy link
Contributor

Choose a reason for hiding this comment

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

same

Copy link
Contributor Author

Choose a reason for hiding this comment

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

done

}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,48 @@
pragma solidity ^0.5.9;
pragma experimental ABIEncoderV2;


contract DummyLiquidityProviderRegistry
{
address private constant NULL_ADDRESS = address(0x0);

constructor()
public
// solhint-disable-next-line no-empty-blocks
{}
Copy link
Contributor

Choose a reason for hiding this comment

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

no constructor needed

Copy link
Contributor Author

Choose a reason for hiding this comment

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

done


mapping (address => mapping (address => address)) internal _gAddressBook;

/// @dev Sets address of pool for a market given market (xAsset, yAsset).
/// @param takerToken First asset managed by pool.
/// @param makerToken Second asset managed by pool.
/// @param poolAddress Address of pool.
function setLiquidityProviderForMarket(
address takerToken,
Copy link
Contributor

Choose a reason for hiding this comment

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

Nit - this naming may be confusing because either asset could be the taker/maker token on a given trade. Something like xToken / yToken or firstToken / secondToken may be better 🤷‍♂.

address makerToken,
address poolAddress
) external
Copy link
Contributor

Choose a reason for hiding this comment

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

Suggested change
) external
)
external

Copy link
Contributor Author

Choose a reason for hiding this comment

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

done

{
_gAddressBook[takerToken][makerToken] = poolAddress;
_gAddressBook[makerToken][takerToken] = poolAddress;
}

/// @dev Returns the address of pool for a market given market (xAsset, yAsset), or reverts if pool does not exist.
/// @param takerToken First asset managed by pool.
/// @param makerToken Second asset managed by pool.
/// @return Address of pool.
function getLiquidityProviderForMarket(
address takerToken,
address makerToken
)
external
view
returns (address poolAddress)
{
poolAddress = _gAddressBook[takerToken][makerToken];
require(
poolAddress != NULL_ADDRESS,
"Registry/MARKET_PAIR_NOT_SET"
);
}
}
4 changes: 2 additions & 2 deletions contracts/erc20-bridge-sampler/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -36,9 +36,9 @@
"compile:truffle": "truffle compile"
},
"config": {
"publicInterfaceContracts": "ERC20BridgeSampler,IERC20BridgeSampler,ILiquidityProvider,ILiquidityProviderRegistry",
"publicInterfaceContracts": "ERC20BridgeSampler,IERC20BridgeSampler,ILiquidityProvider,ILiquidityProviderRegistry,DummyLiquidityProviderRegistry,DummyLiquidityProvider",
"abis:comment": "This list is auto-generated by contracts-gen. Don't edit manually.",
"abis": "./test/generated-artifacts/@(ERC20BridgeSampler|ICurve|IDevUtils|IERC20BridgeSampler|IEth2Dai|IKyberNetwork|ILiquidityProvider|ILiquidityProviderRegistry|IUniswapExchangeQuotes|TestERC20BridgeSampler).json"
"abis": "./test/generated-artifacts/@(DummyLiquidityProvider|DummyLiquidityProviderRegistry|ERC20BridgeSampler|ICurve|IDevUtils|IERC20BridgeSampler|IEth2Dai|IKyberNetwork|ILiquidityProvider|ILiquidityProviderRegistry|IUniswapExchangeQuotes|TestERC20BridgeSampler).json"
},
"repository": {
"type": "git",
Expand Down
4 changes: 4 additions & 0 deletions contracts/erc20-bridge-sampler/src/artifacts.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,8 @@
*/
import { ContractArtifact } from 'ethereum-types';

import * as DummyLiquidityProvider from '../generated-artifacts/DummyLiquidityProvider.json';
import * as DummyLiquidityProviderRegistry from '../generated-artifacts/DummyLiquidityProviderRegistry.json';
import * as ERC20BridgeSampler from '../generated-artifacts/ERC20BridgeSampler.json';
import * as IERC20BridgeSampler from '../generated-artifacts/IERC20BridgeSampler.json';
import * as ILiquidityProvider from '../generated-artifacts/ILiquidityProvider.json';
Expand All @@ -14,4 +16,6 @@ export const artifacts = {
IERC20BridgeSampler: IERC20BridgeSampler as ContractArtifact,
ILiquidityProvider: ILiquidityProvider as ContractArtifact,
ILiquidityProviderRegistry: ILiquidityProviderRegistry as ContractArtifact,
DummyLiquidityProviderRegistry: DummyLiquidityProviderRegistry as ContractArtifact,
DummyLiquidityProvider: DummyLiquidityProvider as ContractArtifact,
};
2 changes: 2 additions & 0 deletions contracts/erc20-bridge-sampler/src/wrappers.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,8 @@
* Warning: This file is auto-generated by contracts-gen. Don't edit manually.
* -----------------------------------------------------------------------------
*/
export * from '../generated-wrappers/dummy_liquidity_provider';
export * from '../generated-wrappers/dummy_liquidity_provider_registry';
export * from '../generated-wrappers/erc20_bridge_sampler';
export * from '../generated-wrappers/i_erc20_bridge_sampler';
export * from '../generated-wrappers/i_liquidity_provider';
Expand Down
4 changes: 4 additions & 0 deletions contracts/erc20-bridge-sampler/test/artifacts.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,8 @@
*/
import { ContractArtifact } from 'ethereum-types';

import * as DummyLiquidityProvider from '../test/generated-artifacts/DummyLiquidityProvider.json';
import * as DummyLiquidityProviderRegistry from '../test/generated-artifacts/DummyLiquidityProviderRegistry.json';
import * as ERC20BridgeSampler from '../test/generated-artifacts/ERC20BridgeSampler.json';
import * as ICurve from '../test/generated-artifacts/ICurve.json';
import * as IDevUtils from '../test/generated-artifacts/IDevUtils.json';
Expand All @@ -16,6 +18,8 @@ import * as ILiquidityProviderRegistry from '../test/generated-artifacts/ILiquid
import * as IUniswapExchangeQuotes from '../test/generated-artifacts/IUniswapExchangeQuotes.json';
import * as TestERC20BridgeSampler from '../test/generated-artifacts/TestERC20BridgeSampler.json';
export const artifacts = {
DummyLiquidityProvider: DummyLiquidityProvider as ContractArtifact,
DummyLiquidityProviderRegistry: DummyLiquidityProviderRegistry as ContractArtifact,
ERC20BridgeSampler: ERC20BridgeSampler as ContractArtifact,
ICurve: ICurve as ContractArtifact,
IDevUtils: IDevUtils as ContractArtifact,
Expand Down
2 changes: 2 additions & 0 deletions contracts/erc20-bridge-sampler/test/wrappers.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,8 @@
* Warning: This file is auto-generated by contracts-gen. Don't edit manually.
* -----------------------------------------------------------------------------
*/
export * from '../test/generated-wrappers/dummy_liquidity_provider';
export * from '../test/generated-wrappers/dummy_liquidity_provider_registry';
export * from '../test/generated-wrappers/erc20_bridge_sampler';
export * from '../test/generated-wrappers/i_curve';
export * from '../test/generated-wrappers/i_dev_utils';
Expand Down
4 changes: 4 additions & 0 deletions contracts/erc20-bridge-sampler/tsconfig.json
Original file line number Diff line number Diff line change
Expand Up @@ -3,10 +3,14 @@
"compilerOptions": { "outDir": "lib", "rootDir": ".", "resolveJsonModule": true },
"include": ["./src/**/*", "./test/**/*", "./generated-wrappers/**/*"],
"files": [
"generated-artifacts/DummyLiquidityProvider.json",
"generated-artifacts/DummyLiquidityProviderRegistry.json",
"generated-artifacts/ERC20BridgeSampler.json",
"generated-artifacts/IERC20BridgeSampler.json",
"generated-artifacts/ILiquidityProvider.json",
"generated-artifacts/ILiquidityProviderRegistry.json",
"test/generated-artifacts/DummyLiquidityProvider.json",
"test/generated-artifacts/DummyLiquidityProviderRegistry.json",
"test/generated-artifacts/ERC20BridgeSampler.json",
"test/generated-artifacts/ICurve.json",
"test/generated-artifacts/IDevUtils.json",
Expand Down
1 change: 1 addition & 0 deletions packages/asset-swapper/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -55,6 +55,7 @@
"devDependencies": {
"@0x/base-contract": "^6.2.1",
"@0x/contracts-test-utils": "^5.3.1",
"@0x/contracts-erc20-bridge-sampler": "1.5.0",
"@0x/dev-utils": "^3.2.1",
"@0x/mesh-rpc-client": "^7.0.4-beta-0xv3",
"@0x/migrations": "^6.2.3",
Expand Down
28 changes: 18 additions & 10 deletions packages/asset-swapper/src/swap_quoter.ts
Original file line number Diff line number Diff line change
Expand Up @@ -21,8 +21,9 @@ import {
} from './types';
import { assert } from './utils/assert';
import { calculateLiquidity } from './utils/calculate_liquidity';
import { DexOrderSampler, MarketOperationUtils } from './utils/market_operation_utils';
import { MarketOperationUtils } from './utils/market_operation_utils';
import { dummyOrderUtils } from './utils/market_operation_utils/dummy_order_utils';
import { DexOrderSampler } from './utils/market_operation_utils/sampler';
import { orderPrunerUtils } from './utils/order_prune_utils';
import { OrderStateUtils } from './utils/order_state_utils';
import { ProtocolFeeUtils } from './utils/protocol_fee_utils';
Expand Down Expand Up @@ -144,11 +145,13 @@ export class SwapQuoter {
* @return An instance of SwapQuoter
*/
constructor(supportedProvider: SupportedProvider, orderbook: Orderbook, options: Partial<SwapQuoterOpts> = {}) {
const { chainId, expiryBufferMs, permittedOrderFeeTypes, samplerGasLimit } = _.merge(
{},
constants.DEFAULT_SWAP_QUOTER_OPTS,
options,
);
const {
chainId,
expiryBufferMs,
permittedOrderFeeTypes,
samplerGasLimit,
liquidityProviderRegistryAddress,
} = _.merge({}, constants.DEFAULT_SWAP_QUOTER_OPTS, options);
const provider = providerUtils.standardizeOrThrow(supportedProvider);
assert.isValidOrderbook('orderbook', orderbook);
assert.isNumber('chainId', chainId);
Expand All @@ -167,10 +170,15 @@ export class SwapQuoter {
gas: samplerGasLimit,
}),
);
this._marketOperationUtils = new MarketOperationUtils(sampler, this._contractAddresses, {
chainId,
exchangeAddress: this._contractAddresses.exchange,
});
this._marketOperationUtils = new MarketOperationUtils(
sampler,
this._contractAddresses,
{
chainId,
exchangeAddress: this._contractAddresses.exchange,
},
liquidityProviderRegistryAddress,
);
this._swapQuoteCalculator = new SwapQuoteCalculator(this._protocolFeeUtils, this._marketOperationUtils);
}

Expand Down
1 change: 1 addition & 0 deletions packages/asset-swapper/src/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -212,6 +212,7 @@ export interface SwapQuoterOpts extends OrderPrunerOpts {
expiryBufferMs: number;
contractAddresses?: ContractAddresses;
samplerGasLimit?: number;
liquidityProviderRegistryAddress?: string;
}

/**
Expand Down
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
import { assert } from '@0x/assert';
import { ContractAddresses } from '@0x/contract-addresses';
import { assetDataUtils, generatePseudoRandomSalt } from '@0x/order-utils';
import { AbiEncoder, BigNumber } from '@0x/utils';
Expand Down Expand Up @@ -31,6 +32,7 @@ export class CreateOrderUtils {
outputToken: string,
path: CollapsedFill[],
bridgeSlippage: number,
liquidityPoolAddress?: string,
): OptimizedMarketOrder[] {
const orders: OptimizedMarketOrder[] = [];
for (const fill of path) {
Expand All @@ -41,7 +43,7 @@ export class CreateOrderUtils {
createBridgeOrder(
orderDomain,
fill,
this._getBridgeAddressFromSource(fill.source),
this._getBridgeAddressFromSource(fill.source, liquidityPoolAddress),
outputToken,
inputToken,
bridgeSlippage,
Expand All @@ -59,6 +61,7 @@ export class CreateOrderUtils {
outputToken: string,
path: CollapsedFill[],
bridgeSlippage: number,
liquidityPoolAddress?: string,
): OptimizedMarketOrder[] {
const orders: OptimizedMarketOrder[] = [];
for (const fill of path) {
Expand All @@ -69,7 +72,7 @@ export class CreateOrderUtils {
createBridgeOrder(
orderDomain,
fill,
this._getBridgeAddressFromSource(fill.source),
this._getBridgeAddressFromSource(fill.source, liquidityPoolAddress),
inputToken,
outputToken,
bridgeSlippage,
Expand All @@ -81,7 +84,7 @@ export class CreateOrderUtils {
return orders;
}

private _getBridgeAddressFromSource(source: ERC20BridgeSource): string {
private _getBridgeAddressFromSource(source: ERC20BridgeSource, liquidityPoolAddress?: string): string {
switch (source) {
case ERC20BridgeSource.Eth2Dai:
return this._contractAddress.eth2DaiBridge;
Expand All @@ -93,6 +96,14 @@ export class CreateOrderUtils {
case ERC20BridgeSource.CurveUsdcDaiUsdt:
case ERC20BridgeSource.CurveUsdcDaiUsdtTusd:
return this._contractAddress.curveBridge;
case ERC20BridgeSource.LiquidityProvider:
if (liquidityPoolAddress === undefined) {
throw new Error(
'Cannot create a LiquidityProvider order without a LiquidityProvider pool address.',
);
}
assert.isETHAddressHex('liquidityPoolAddress', liquidityPoolAddress);
return liquidityPoolAddress;
default:
break;
}
Expand Down
Loading