Contract Fillable Liquidity

What is Contract Fillable Liquidity?

Contract Fillable Liquidity (CFL) is a term in Decentralized Finance (DeFi) used to describe the ability for smart contracts or decentralized applications to tap into existing on-chain pools of liquidity automatically and programmatically. This helps decentralized applications to run more efficiently and allows for more complex integrations between projects to be built as projects have access to faster and cheaper asset swaps. An example of CFL could be something as simple as a contract that allows a MakerDAO CDP owner to pay back their generated debt in ETH, trading the ETH into MKR and paying back the debt all in one transaction. reducing the number of assets one needs to own in order to manage a CDP. Another more complex example would be a project like Set Protocol rebalancing their Sets and making these trades automatically on chain, sourcing the liquidity from various Decentralized Exchanges.

Using DEX.AG for Contract Fillable Liquidity

Developers can now use the DEX.AG API and Proxy Contracts to implement CFL in their own projects quickly and easily. By using the DEX.AG API developers have access to a large range of liquidity including exchanges like Radar Relay, Uniswap, and Kyber. Furthermore, they will be able to use existing infrastructure to find the best trades among these sources, and utilize DEX.AG's trade aggregation feature to split their trade among various exchanges, reducing slippage more than could be done by only trading on one exchange. If interested in utilizing DEX.AG for CFL, please continue reading the below steps to integrate this into your project.

Overview of DEX.AG and CFL

The first step in using DEX.AG for CFL is having access to a DEX.AG Trading Proxy contract, we will not go over this part here as this is covered in the Proxy Contract Deployment section of the docs. Once you have access to a Proxy contract you are able get trade data from the DEX.AG API and send this data from your smart contract that needs liquidity to your Proxy contract to execute the trade. To see an example of a smart contract using DEX.AG for CFL, view this Github Repo.

Getting trade data from the DEX.AG API is covered in this section of the docs but we will quickly go over it here as well. You can either call the API directly with a call like:

/trade
https://api.dex.ag/trade?from=ETH&to=DAI&fromAmount=1&dex=best&proxy=0xD3BeD3A8E3e6b24b740EAD108bA776e0Ad298588

Or use the DEX.AG SDK to get a trade like this:

getTrade
sdk.getTrade({
to:'DAI',
from:'ETH',
toAmount:1,
dex:'Best',
proxy:'0xA540fb50288cc31639305B1675c70763C334953b'
});

The response will be a JSON object structured like this:

{
"trade": {
"to": "0xA540fb50288cc31639305B1675c70763C334953b",
"data": "0x821d3bae........",
"value": "1000000000000000000"
},
"metadata": {
"source": {
"dex": "uniswap",
"price": "165.378577246317035305"
},
"query": {
"from": "ETH",
"to": "DAI",
"fromAmount": "1",
"dex": "best",
"proxy": "0xA540fb50288cc31639305B1675c70763C334953b"
}
}
}

The data that we will use to execute the trade in our contract is located under the trade field. data contains the actual calldata we will send to our contract, and value is the amount of ETH needed (if any, will be zero if the from token is not ETH). You pass this data into your contract function that needs liquidity just like you would any other type of data. An example of what this would look like on the smart contract side is:

contract Liquidity {
address public tradingProxy; //address of Proxy Contract
function assetSwap(bytes calldata data, uint256 value) {
tradingProxy.call(data).value(value);
// continue function here
}
}

This call will execute the trade you received from the API using whichever dex or dexes were selected. DEX approvals are handled automatically in the Proxy Contract, however you will need to approve the Proxy Contract's Approval Handler contract (see Proxy Contract Spec for more details on the Approval Handler) beforehand to allow ERC20 tokens to be taken from your smart contract and sent to the Proxy Contract.