Skip to content

Commit

Permalink
cleanup interface
Browse files Browse the repository at this point in the history
  • Loading branch information
z0r0z committed Sep 15, 2022
1 parent fe7157b commit bd62683
Show file tree
Hide file tree
Showing 4 changed files with 22 additions and 16 deletions.
2 changes: 1 addition & 1 deletion .gas-snapshot
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
HeliosTest:testHeliosCreation() (gas: 2862619)
HeliosTest:testHeliosCreation() (gas: 2862816)
HeliosTest:testXYKpairCreation() (gas: 233762)
HeliosTest:testXYKpairMultiHop(uint256) (runs: 256, μ: 56713, ~: 22944)
HeliosTest:testXYKpairNoFeeInvariance(uint256) (runs: 256, μ: 74197, ~: 7765)
Expand Down
8 changes: 4 additions & 4 deletions src/Helios.sol
Original file line number Diff line number Diff line change
@@ -1,19 +1,19 @@
// SPDX-License-Identifier: MIT
pragma solidity ^0.8.4;

import {ERC1155, ERC1155TokenReceiver} from "@solbase/tokens/ERC1155.sol";
import {IHelios} from "./interfaces/IHelios.sol";
import {OwnedThreeStep} from "@solbase/auth/OwnedThreeStep.sol";
import {SafeTransferLib} from "@solbase/utils/SafeTransferLib.sol";
import {SafeMulticallable} from "@solbase/utils/SafeMulticallable.sol";
import {ERC1155, ERC1155TokenReceiver} from "@solbase/tokens/ERC1155.sol";

/// @notice ERC1155 vault with router and liquidity pools.
/// @author z0r0z.eth (SolDAO)
contract Helios is
ERC1155,
ERC1155TokenReceiver,
OwnedThreeStep(tx.origin),
SafeMulticallable
SafeMulticallable,
ERC1155,
ERC1155TokenReceiver
{
constructor() payable {} // Clean deployment.

Expand Down
8 changes: 3 additions & 5 deletions src/interfaces/IHelios.sol
Original file line number Diff line number Diff line change
@@ -1,13 +1,11 @@
// SPDX-License-Identifier: MIT
pragma solidity ^0.8.4;

interface ERC20 {}

/// @notice Swapper interface for Helios
/// @notice Interface for Helios.
interface IHelios {
struct Pair {
ERC20 token0;
ERC20 token1;
address token0;
address token1;
IHelios swapper;
uint112 reserve0;
uint112 reserve1;
Expand Down
20 changes: 14 additions & 6 deletions src/swappers/XYKswapper.sol
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ pragma solidity ^0.8.4;

import {FixedPointMathLib} from "@solbase/utils/FixedPointMathLib.sol";
import {ReentrancyGuard} from "@solbase/utils/ReentrancyGuard.sol";
import {ERC20, IHelios} from "../interfaces/IHelios.sol";
import {IHelios} from "../interfaces/IHelios.sol";

/// @notice XYK swapper for Helios.
/// @author Modified from UniswapV2Pair (https://github.com/Uniswap/v2-core/blob/master/contracts/UniswapV2Pair.sol)
Expand All @@ -18,14 +18,15 @@ contract XYKswapper is ReentrancyGuard {
/// Constants
/// -----------------------------------------------------------------------

uint224 private constant Q112 = 2**112;
uint256 private constant MIN_LP = 10**3;
uint224 internal constant Q112 = 2**112;

uint256 internal constant MIN_LP = 10**3;

/// -----------------------------------------------------------------------
/// Math
/// -----------------------------------------------------------------------

function min(uint256 x, uint256 y) private pure returns (uint256 z) {
function min(uint256 x, uint256 y) internal pure returns (uint256 z) {
z = x < y ? x : y;
}

Expand All @@ -41,6 +42,7 @@ contract XYKswapper is ReentrancyGuard {
IHelios.Pair memory pair = IHelios(msg.sender).pairs(id);

uint256 reserve0 = pair.reserve0;

uint256 reserve1 = pair.reserve1;

uint256 totalSupply = IHelios(msg.sender).totalSupplyForId(id);
Expand All @@ -65,11 +67,13 @@ contract XYKswapper is ReentrancyGuard {
IHelios.Pair memory pair = IHelios(msg.sender).pairs(id);

uint256 reserve0 = pair.reserve0;

uint256 reserve1 = pair.reserve1;

uint256 totalSupply = IHelios(msg.sender).totalSupplyForId(id);

amount0out = (lp * reserve0) / totalSupply;

amount1out = (lp * reserve1) / totalSupply;

require(
Expand All @@ -84,12 +88,13 @@ contract XYKswapper is ReentrancyGuard {

function swap(
uint256 id,
ERC20 tokenIn,
address tokenIn,
uint256 amountIn
) external nonReentrant returns (uint256 amountOut) {
IHelios.Pair memory pair = IHelios(msg.sender).pairs(id);

uint256 reserve0 = pair.reserve0;

uint256 reserve1 = pair.reserve1;

uint256 fee = pair.fee;
Expand All @@ -98,6 +103,7 @@ contract XYKswapper is ReentrancyGuard {
amountOut = _getAmountOut(amountIn, reserve0, reserve1, fee);
} else {
require(tokenIn == pair.token1, "XYKswapper: INVALID_INPUT_TOKEN");

amountOut = _getAmountOut(amountIn, reserve1, reserve0, fee);
}
}
Expand All @@ -107,9 +113,11 @@ contract XYKswapper is ReentrancyGuard {
uint256 reserveAmountIn,
uint256 reserveAmountOut,
uint256 fee
) private pure returns (uint256 amountOut) {
) internal pure returns (uint256 amountOut) {
uint256 amountInWithFee = amountIn * (10000 - fee);

uint256 newReserveIn = reserveAmountIn * 10000 + amountInWithFee;

amountOut =
(amountInWithFee * reserveAmountOut + (newReserveIn >> 1)) /
newReserveIn;
Expand Down

0 comments on commit bd62683

Please sign in to comment.