πŸ› 
Custom Integrations
This page will show you how to create a new integration to connect Babylon to a new DeFI protocol.
Integrations are adapters that Babylon uses to connect with other DeFi & Web3 protocols.
So far, Babylon has developed all these adapters in-house. You can see the whole list here.​
Starting today, anyone can develop an adapter, test it on a private garden and then submit it to be verified and audited by Babylon governance.

Requirements

  • Only gardens with the flag customIntegrationsEnabled can create strategies that use custom integrations.
  • The protocol you are connecting with must return an ERC-20 token in an amount proportional to the capital provided by the strategy.
  • The resulting ERC-20 tokens need to be priced on-chain in a safe and reliable way. Enough liquidity, resistant to manipulation...

Architecture Recap

Babylon has 1-N gardens or investment clubs.
Every Garden has 0-N active strategies at any single moment.
Every strategy has 1-N DeFi operations it executes to start and exit a strategy.
Every operation uses one adapter to connect to an specific DeFi protocol.

How to build a custom integration?

In order to facilitate the process as much as possible, we have created a template called CustomIntegrationTemplate.sol that you can copy and paste and rename to start working on your custom integration.
You can also look at the example CustomIntegrationYearn to see how to connect with Yearn using the template.

Steps to build your integration

To complete the integrations, there are a few functions that need to be filled. You can read more about their signatures and parameters on their API page.
  1. 1.
    Copy CustomIntegrationTemplate.sol and rename it.
  2. 2.
    Add any state variables you would need.
  3. 3.
    Modify the constructor to receive any extra parameters you need on deployment. Change the name test_sample_change_me
1
constructor(IBabController _controller) CustomIntegration('**test_sample_change_me**', _controller) {
2
require(address(_controller) != address(0), 'invalid address');
3
}
Copied!
4. Fill the function that checks whether the parameters passed on execution to this operation are valid. These are different than the constructor parameters. These are the configurable parameters. For example, which yearn vault to enter.
1
function _isValid(
2
bytes memory /* _data */
3
) internal pure override returns (bool) {
4
/**FILL THIS*/
5
return true;
6
}
Copied!
5. Fill _getSpender, this function returns the address where the input tokens need to be approved to before execution. This address will be called with ERC-20 approve and the input tokens before execution.
1
function _getSpender(
2
bytes calldata, /* _data */
3
uint8 /* _opType */
4
) internal pure override returns (address) {
5
/** FILL THIS */
6
return address(0);
7
}
Copied!
6. Implement _getResultToken. This function must return the result token (ERC-20) to be received after executing the enter operation.
1
function _getResultToken(address _token) internal pure override returns (address) {
2
/** FILL THIS */
3
return _token;
4
}
Copied!
7. Provide the input tokens and the weights of these tokens. The strategy will automatically buy these tokens with the capital before entering the strategy according to these weights.
1
function getInputTokensAndWeights(
2
bytes calldata /* _data */
3
) external pure override returns (address[] memory _inputTokens, uint256[] memory _inputWeights) {
4
/** FILL THIS */
5
return (new address[](1), new uint256[](1));
6
}
Copied!
8. Provide the output tokens and the minimum amounts to receive for each upon exit based on _liquidity amount of the result token defined above.
1
function getOutputTokensAndMinAmountOut(
2
bytes calldata, /* _data */
3
uint256 /* _liquidity */
4
) external pure override returns (address[] memory exitTokens, uint256[] memory _minAmountsOut) {
5
/** FILL THIS */
6
return (new address[](1), new uint256[](1));
7
}
Copied!
9. Provide the enter calldata. You must return the calldata required for the strategy to allocate capital to this integration.
1
function _getEnterCalldata(
2
address, /* _strategy */
3
bytes calldata, /* _data */
4
uint256, /* _resultTokensOut */
5
address[] calldata, /* _tokensIn */
6
uint256[] calldata /* _maxAmountsIn */
7
)
8
internal
9
pure
10
override
11
returns (
12
address,
13
uint256,
14
bytes memory
15
)
16
{
17
/** FILL THIS */
18
return (address(0), 0, bytes(''));
19
}
Copied!
10. Provide the exit calldata. You must return the calldata required for the strategy to exit and recover the capital allocated through this integration.
1
function _getExitCalldata(
2
address, /* _strategy */
3
bytes calldata, /* _data */
4
uint256, /* _resultTokensIn */
5
address[] calldata, /* _tokensOut */
6
uint256[] calldata /* _minAmountsOut */
7
)
8
internal
9
pure
10
override
11
returns (
12
address,
13
uint256,
14
bytes memory
15
)
16
{
17
/** FILL THIS */
18
return (address(0), 0, bytes(''));
19
}
Copied!
11. (Optional). Provide the list of reward tokens that will be received upon exit. List of ERC-20 addresses.
1
/**
2
* The list of addresses of the IERC-20 tokens mined as rewards during the strategy
3
*
4
* hparam _data Address provided as param
5
* @return address[] memory List of reward token addresses
6
*/
7
function _getRewardTokens(
8
address /* _data */
9
) internal pure override returns (address[] memory) {
10
return new address[](1);
11
}
Copied!
12. (Optional) If the result tokens cannot be priced through Babylon Price Oracle, you need to implement the following function that returns the price of the result token in the _tokenDenominator
1
function getPriceResultToken(
2
bytes calldata, /* _data */
3
address /* _tokenDenominator */
4
) external pure override returns (uint256) {
5
/** FILL THIS */
6
return 0;
7
}
8
​
Copied!
13. (Optional) If you need to execute another custom action before entry or exit, you can define a pre-action hook. _customOp will be 0 for enter and 1 for exit. If this action needs to execute an ERC approval you can fill _preActionNeedsApproval.
1
/**
2
* (OPTIONAL). Return pre action calldata
3
*
4
* hparam _strategy Address of the strategy
5
* hparam _asset Address param
6
* hparam _amount Amount
7
* hparam _customOp Type of Custom op
8
*
9
* @return address Target contract address
10
* @return uint256 Call value
11
* @return bytes Trade calldata
12
*/
13
function _getPreActionCallData(
14
address, /* _strategy */
15
address, /* _asset */
16
uint256, /* _amount */
17
uint256 /* _customOp */
18
)
19
internal
20
view
21
override
22
returns (
23
address,
24
uint256,
25
bytes memory
26
)
27
{
28
return (address(0), 0, bytes(''));
29
}
Copied!
14. (Optional) Similarly, if you need to execute another custom action after entering or exiting, you can define a pre-action hook. _customOp will be 0 for enter and 1 for exit. If this action needs to execute an ERC approval you can fill _postActionNeedsApproval.
1
/**
2
* (OPTIONAL) Return post action calldata
3
*
4
* hparam _strategy Address of the strategy
5
* hparam _asset Address param
6
* hparam _amount Amount
7
* hparam _customOp Type of op
8
*
9
* @return address Target contract address
10
* @return uint256 Call value
11
* @return bytes Trade calldata
12
*/
13
function _getPostActionCallData(
14
address, /* _strategy */
15
address, /* _asset */
16
uint256, /* _amount */
17
uint256 /* _customOp */
18
)
19
internal
20
view
21
override
22
returns (
23
address,
24
uint256,
25
bytes memory
26
)
27
{
28
return (address(0), 0, bytes(''));
29
}
Copied!

Utils

You can use the following functions directly from your custom integration:

_getPrice(address _tokenIn, address _tokenOut) view returns (uint256)

It gets you the price of _tokenIn denominated in _tokenOut from our price oracle (1e18). It will return an exception if it doesn't know how to price a specific token.
_getDurationStrategy(address _strategy) view returns (uint256)
It returns the duration of the strategy in seconds.

_getTokenOrETHBalance(address _strategy, address _token) view returns (uint256)

Returns the balance of the strategy of the _token. If token is 0x or 0xEEEE..EEE, it will return the ETH balance of the strategy.

_getTradeCallData(address _strategy, address _tokenIn, address _tokenOut, uint256 _amountIn, uint256 _minAmountOut) view returns (address, uint, bytes memory)

Receives the trade call data needed to execute a swap of _tokenIn to _tokenOut. The number of _tokenIn supplied is specified by_amountIn. The call will revert if you don't receive at least _minAmountOut. It returns the address, callvalue, and calldata to pass directly to a preaction hook, postaction hook, enter or exit functions

Testing the custom integration

In the contracts repository there is a test file that executes the yearn custom integration example. In order to test your integration, follow these steps:
  1. 1.
    Change the integration to deploy to the name of your custom integration. Pass the appropriate params depending on your constructor
2. Run yarn run test. That's it, you can start debugging πŸŽ‰
The test suite will automatically create a strategy with your integration, approve it, allocate capital to it, execute it and finalize it.

Testing on mainnet with a live garden

You can also test it directly on mainnet using the Test WETH garden on the Babylon website or creating your own garden. Make sure you enable custom integrations in the garden parameters.
  • Deploy your custom integration to mainnet.
  • Deposit the min amount to join the test garden.
  • Submit a new strategy and select your custom integration as one of the operations. Set the address of your deployed integration as the integration address. You can also add two optional params that the integration will receive on execution, an address and a number.
It should execute automatically in two hours given that strategies automatically reach quorum in this garden and do not need voters.

Submitting it for approval

After the tests above pass for your integration, please follow the next steps:
  • Clone the repo and submit a PR against the contracts repo.
  • Head to our Discord and post a message explaining your integration and adding the link to your PR in the #development channel.
  • We'll review your PR and follow up to get it audited and eventually incorporated into our list of verified integrations.
  • We may award a bounty if the integration is approved and it is one of the integrations on our wishlist.