Skip to main content

Swap.sol

An embedded solidity library for performing swaps using the AMM of your choice, with integration options with Balancer, Curve, Sushiswap, Uniswap V2 and Uniswap V3.

Under the hood, the AMM is chosen either by providing its unique identifier registered in the Mosaic SDK or automatically by the chain you are using it on.

Usage

function someRandomMethodFromYourContract(    address _tokenIn,    address _tokenOut,    uint256 _amountIn) public view returns (uint256) {    return Swap.getAmountsOut(_tokenIn, _tokenOut, _amountIn, "");}
function methodThatPerformsAnExchangeBetweenTwoTokens(    address _tokenIn,    address _tokenOut,    uint256 _amountIn,    uint256 _deadline) public {    Swap.swap(_tokenIn, _tokenOut, _amountIn, 0, msg.sender, abi.encodePacked(_deadline));}

Methods


// Returns Config contract address; config holds information about the available addresses from your current layerfunction getConfigAddress() internal pure returns (address)
// Get the available exchanges per layerfunction getAvailableExchanges() internal view returns (bytes32[] memory) 
// Get the amount returned by an exchange; exchange is automatically selected based on the layer you are using it fromfunction getAmountsOut(    address _tokenIn,    address _tokenOut,    uint256 _amountIn,    bytes memory _data) internal view returns (uint256) 
// Get the amount returned by a specific exchange, identified by `_exchangeId`function getAmountsOut(    bytes32 _exchangeId,    address _tokenIn,    address _tokenOut,    uint256 _amountIn,    bytes memory _data) internal view returns (uint256) 
// Performs a swap by selecting the exchange automatically based on the layer you are using it fromfunction swap(    address _tokenIn,    address _tokenOut,    uint256 _amountIn,    uint256 _amountOut,    address to,    bytes memory _data) internal returns (uint256)
// Performs a swap using a specific exchange, identified by `_exchangeId`function swap(    bytes32 _exchangeId,    address _tokenIn,    address _tokenOut,    uint256 _amountIn,    uint256 _amountOut,    address to,    bytes memory _data) internal returns (uint256) 

All data like: chain id, exchange id and addresses can be retrieved from the Config contract