Skip to main content

Simple Swap

The Swaap pools offers are two types of swaps:

  • swapExactAmountInMMM sells a fixed amount of tokenIn for a maximum amount of tokenOut
  • swapExactAmountOutMMM buys a fixed amount of tokenOut for a minimum amount of tokenIn

This guide will help you implement these two functions using Swaap's interface. The contract is not production ready and should be used only as a guide.

Install the Swaap Core V1 contracts

$ npm add @swaap-labs/swaap-core-v1

Sell a fixed amount of tokens

Before calling the function, the user must approve the usage of tokenIn by the contract (in this example weth).

pragma solidity =0.8.12;

// import Swaap pool interface
import "@swaap-labs/swaap-core-v1/contracts/interfaces/IPool.sol";
// import SafeERC20 wrapper
import "@openzeppelin/contracts/token/ERC20/utils/SafeERC20.sol";

/**
* @title Example guide to trade using Swaap's interface
*/
contract SimpleSellSwaap {

// Use wrapper library around ERC20 operations
using SafeERC20 for IERC20;

// For demonstration purposes we are using the tri-crypto pool of Swaap on polygon
address public constant pool = 0x7f5f7411c2c7eC60e2db946aBbe7DC354254870B;

// ERC20 addresses on polygon network
address public constant WETH = 0x7ceB23fD6bC0adD59E62ac25578270cFf1b9f619;
address public constant USDC = 0x2791Bca1f2de4661ED88A30C99A7a9449Aa84174;
address public constant WBTC = 0x1BFD67037B42Cf73acF2047067bd4F2C47D9BfD6;

/**
* @notice Sells a fixed amount of weth for some usdc with a Swaap pool
* @param amountIn The amount of weth
* @param maxPrice The maximum allowed spot price after the trade
*/
function simpleSellWETHToUSDC(
uint256 amountIn,
uint256 minAmountOut,
uint256 maxPrice
) public returns(uint256) {

// Transfer the funds from the caller to the contract
IERC20(WETH).safeTransferFrom(msg.sender, address(this), amountIn);

(uint256 amountOut, ) = IPool(pool).swapExactAmountInMMM({
tokenIn: WETH, // The address of tokenIn
tokenAmountIn: amountIn, // The sell amount of tokenIn
tokenOut: USDC, // The address of tokenOut
minAmountOut: minAmountOut, // The minimum buy amount of tokenOut
maxPrice: maxPrice // The maximum allowed spot price after the trade
});

// Transfer the bought tokenOut to caller
IERC20(USDC).safeTransfer(msg.sender, amountOut);

return amountOut;
}

}

Buy a fixed amount of tokens

Before calling the function, the user must approve the usage of tokenIn by the contract (in this example usdc).

pragma solidity =0.8.12;

// import Swaap pool interface
import "@swaap-labs/swaap-core-v1/contracts/interfaces/IPool.sol";
// import SafeERC20 wrapper
import "@openzeppelin/contracts/token/ERC20/utils/SafeERC20.sol";

/**
* @title Example guide to trade using Swaap's interface
*/
contract SimpleBuySwaap {

// Use wrapper library around ERC20 operations
using SafeERC20 for IERC20;

// For demonstration purposes we are using the tri-crypto pool of Swaap on polygon
address public constant pool = 0x7f5f7411c2c7eC60e2db946aBbe7DC354254870B;

// ERC20 addresses on polygon network
address public constant WETH = 0x7ceB23fD6bC0adD59E62ac25578270cFf1b9f619;
address public constant USDC = 0x2791Bca1f2de4661ED88A30C99A7a9449Aa84174;
address public constant WBTC = 0x1BFD67037B42Cf73acF2047067bd4F2C47D9BfD6;

/**
* @notice Buys a fixed amount of wbtc using usdc with a Swaap pool
* @param maxAmountIn The maxium amount of usdc
* @param amountOut The buy amount of wbtc
* @param maxPrice The maximum allowed spot price after the trade
*/
function simpleBuyWBTCWithUSDC(
uint256 maxAmountIn,
uint256 amountOut,
uint256 maxPrice
) public returns(uint256) {

// Transfer the funds from the caller to the contract
IERC20(WETH).safeTransferFrom(msg.sender, address(this), maxAmountIn);

(uint256 usedAmountIn, ) = IPool(pool).swapExactAmountOutMMM({
tokenIn: USDC, // The address of tokenIn
maxAmountIn: maxAmountIn, // The maximum sell amount of tokenIn
tokenOut: WBTC,// The address of tokenOut
tokenAmountOut: amountOut, // The buy amount of tokenOut
maxPrice: maxPrice // The maximum allowed spot price after the trade
});

// Transfer the remaining tokenIn to the caller
IERC20(USDC).safeTransfer(msg.sender, maxAmountIn - usedAmountIn);
// Transfer the bought tokenOut to caller
IERC20(WBTC).safeTransfer(msg.sender, amountOut);

return usedAmountIn;
}

}