Skip to content

Allocator: swap to another token#6

Open
simon-something wants to merge 35 commits intomainfrom
feature/allocator-swap
Open

Allocator: swap to another token#6
simon-something wants to merge 35 commits intomainfrom
feature/allocator-swap

Conversation

@simon-something
Copy link
Copy Markdown
Member

@simon-something simon-something commented Sep 28, 2022

Allocator swapping a given tokenIn (specified in the splits data received) to a tokenOut (set at deployment), using the best quote received across a set of dexes.

The allocator should avoid reverting (if no swap is possible, then the unswapped tokenIn is sent), avoiding freezing the whole split distribution.

There is no slippage as the quote and swap are atomic, therefore the only sandwich protection is getting a quote in multiple dexes (this shouldn't be used for market with a single pool/low liquidity, as sandwich becomes easy)

@simon-something simon-something marked this pull request as ready for review October 4, 2022 21:26
@simon-something simon-something added the WIP PR for follow up only label Nov 13, 2022
/**
@notice The token which should be distributed
*/
address immutable tokenOut;
Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

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

todo: public

For clarity, we suggest changing the contract's name to reflect the tokenOut (ie SwapAllocatorDai)
*/
contract SwapAllocator is ERC165, Ownable, IJBSplitAllocator {
event SwapAllocated(address indexed beneficiary, address tokenIn, address tokenOut, uint256 amountIn, uint256 amountOut);
Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

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

todo: natspec + ISwapAllocator

@param _tokenIn The token to swap
@param _newDexes The array of new pool wrappers to add
*/
function addDex(address _tokenIn, IPoolWrapper[] calldata _newDexes) external onlyOwner {
Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

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

todo: "addDexes "

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

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

  • override both add/remove with single/array

address _beneficiary = _data.split.beneficiary;

// Keep record of the best pool wrapper. The pool address is passed to avoid having
// to find it again in the wrapper
Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

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

// to find it again in the wrapper when calling swap()

}
}

function swap(
Copy link
Copy Markdown
Member Author

@simon-something simon-something Nov 15, 2022

Choose a reason for hiding this comment

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

adapt for eth (wrap if in, unwrap if out)

_amountOut = _getAmountOut(_amountIn, reserve0, reserve1);
}

function swap(
Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

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

adapt for eth (wrap/unwrap)

registry = abi.decode(_registry, (address));
}

function getQuote(
Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

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

adapt for eth: convert jbTokens.ETH() to weth address

factory = _factory; // uniV2 mainnet 0x5C69bEe701ef814a2B6a3EDD4B1652CB9cc5aA6f
}

function getQuote(
Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

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

adapt for eth: convert jbTokens.ETH() to weth address


@notice
*/
contract SwapAllocator {
Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

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

wrapper, todo (fee via? Gov? Just best quote accross all?)

import { Mainnet_UniswapV2 } from '../example/swapAllocator/poolWrappers/Mainnet_UniswapV2.sol';

contract SwapAllocator_UniV2_Test is Test {
address constant _uniV2Factory = address(0x5C69bEe701ef814a2B6a3EDD4B1652CB9cc5aA6f);
Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

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

add comment: needs to be hardcoded for the deterministic addressing

vm.expectCall(_poolWrapper1, abi.encodeCall(IPoolWrapper.getQuote, (amountIn, jbLibraries().ETHToken(), _tokenOut)));
vm.expectCall(_poolWrapper2, abi.encodeCall(IPoolWrapper.getQuote, (amountIn, jbLibraries().ETHToken(), _tokenOut)));

//else check eth balance (token out == token in)
Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

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

remove "else"

@simon-something simon-something added enhancement New feature or request and removed WIP PR for follow up only labels Nov 15, 2022
@viraj124
Copy link
Copy Markdown

great to see this coming to a finish line :)

// PoolAddress.getPoolKey(_token0, _token1, _fee)
// );
// (uint160 _sqrtPriceX96, , , , , , ) = IUniswapV3Pool(_pair).slot0();
// // Return amount out with "not so bad" precision (if not overflowing) : TODO: replace by uniV3 lib (there must be one)
Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

replace by uniV3 lib (there must be one)

https://github.com/slice-so/uniswap-v3-price-feed

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

enhancement New feature or request

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants