1inch / 1inchProtocol

1inch Protocol – fully on-chain DeFi aggregation protocol
MIT License
563 stars 310 forks source link

[DEPRECATED]

1inchProtocol was deprecated. We recommend to use API instead of 1inchProtocol.

1inch on-chain DeFi aggregation protocol

First ever fully on-chain DEX aggregator protocol by 1inch

Build Status Coverage Status Built-with openzeppelin

Integration

Latest version is always accessible at 1split.eth (beta on 1proto.eth)

Start with checking out solidity interface: IOneSplit.sol

How it works

This smart contract allows to get best price for tokens by aggregating prices from several DEXes.

So far the service works with 2 types of exchages: split and wrap.

List of split exchanges:

let splitExchanges = [
    "Uniswap",
    "Kyber",
    "Bancor",
    "Oasis",
    "Curve Compound",
    "Curve USDT",
    "Curve Y",
    "Curve Binance",
    "Curve Synthetix",
    "Uniswap Compound",
    "Uniswap CHAI",
    "Uniswap Aave",
    "Mooniswap",
    "Uniswap V2",
    "Uniswap V2 ETH",
    "Uniswap V2 DAI",
    "Uniswap V2 USDC",
    "Curve Pax",
    "Curve renBTC",
    "Curve tBTC",
    "Dforce XSwap",
    "Shell",
    "mStable mUSD",
    "Curve sBTC",
    "Balancer 1",
    "Balancer 2",
    "Balancer 3",
    "Kyber 1",
    "Kyber 2",
    "Kyber 3",
    "Kyber 4"
]

List of wrap exchanges:

let wrapExchanges = [
    "CHAI",
    "BDAI",
    "Aave",
    "Fulcrum",
    "Compound",
    "Iearn",
    "Idle",
    "WETH",
    "mUSD"
]

How it works

How to use it

To use this service you have to call methods at OneSplitAudit

How to use it

To swap tokens you have to figure out way from left to right points by one of paths on scheme above.

For example, first of all call method getExpectedReturn (see methods section), it returns distribution array. Each element of this array matches element of splitExchanges (see above) and represents fraction of trading volume.
Then call getExpectedReturnWithGas to take into account gas when splitting. This method returns more profitable distribution array for exchange.
Then call method swap or swapWithReferral (see methods section) with param distribution which was recieved earlier from method getExpectedReturn.

Swap may be customized by flags (see flags section). There are 2 types of swap: direct swap and swap over transitional token.

In case of direct swap each element of distribution array matches element of splitExchanges and represents fraction of trading off token as alerady described above.

In case of swap with transitional token each element of distribution (256 bits) matches 2 swaps: second bytes are equal to swap to transitional token, lowest bytes are equal to swap to the desired token.

Supported DEXes

Methods

If you need Ether instead of any token use address(0) or address(0xEeeeeEeeeEeEeeEeEeEeeEEEeeeeEeeeeeeeEEeE) as param fromToken/destToken

getExpectedReturn

function getExpectedReturn(
    IERC20 fromToken,
    IERC20 destToken,
    uint256 amount,
    uint256 parts,
    uint256 flags
)
    public
    view
    returns(
        uint256 returnAmount,
        uint256[] memory distribution
    )

Calculate expected returning amount of desired token

Params Type Description
fromToken IERC20 Address of trading off token
destToken IERC20 Address of desired token
amount uint256 Amount for fromToken
parts uint256 Number of pieces source volume could be splitted (Works like granularity, higly affects gas usage. Should be called offchain, but could be called onchain if user swaps not his own funds, but this is still considered as not safe)
flags uint256 Flags for enabling and disabling some features (default: 0), see flags description

Return values:

Params Type Description
returnAmount uint256 Expected returning amount of desired token
distribution uint256[] Array of weights for volume distribution

Notice: This method is equal to getExpectedReturnWithGas(fromToken, destToken, amount, parts, flags, 0)

Example:

let Web3 = require('web3')

let provider = new Web3.providers.WebsocketProvider('wss://mainnet.infura.io/ws/v3/YOUR_TOKEN')
let web3 = new Web3(provider)

let ABI = [{"inputs":[{"internalType":"contract IOneSplitMulti","name":"impl","type":"address"}],"payable":false,"stateMutability":"nonpayable","type":"constructor"},{"anonymous":false,"inputs":[{"indexed":true,"internalType":"address","name":"newImpl","type":"address"}],"name":"ImplementationUpdated","type":"event"},{"anonymous":false,"inputs":[{"indexed":true,"internalType":"address","name":"previousOwner","type":"address"},{"indexed":true,"internalType":"address","name":"newOwner","type":"address"}],"name":"OwnershipTransferred","type":"event"},{"anonymous":false,"inputs":[{"indexed":true,"internalType":"contract IERC20","name":"fromToken","type":"address"},{"indexed":true,"internalType":"contract IERC20","name":"destToken","type":"address"},{"indexed":false,"internalType":"uint256","name":"fromTokenAmount","type":"uint256"},{"indexed":false,"internalType":"uint256","name":"destTokenAmount","type":"uint256"},{"indexed":false,"internalType":"uint256","name":"minReturn","type":"uint256"},{"indexed":false,"internalType":"uint256[]","name":"distribution","type":"uint256[]"},{"indexed":false,"internalType":"uint256[]","name":"flags","type":"uint256[]"},{"indexed":false,"internalType":"address","name":"referral","type":"address"},{"indexed":false,"internalType":"uint256","name":"feePercent","type":"uint256"}],"name":"Swapped","type":"event"},{"payable":true,"stateMutability":"payable","type":"fallback"},{"constant":true,"inputs":[],"name":"chi","outputs":[{"internalType":"contract IFreeFromUpTo","name":"","type":"address"}],"payable":false,"stateMutability":"view","type":"function"},{"constant":false,"inputs":[{"internalType":"contract IERC20","name":"asset","type":"address"},{"internalType":"uint256","name":"amount","type":"uint256"}],"name":"claimAsset","outputs":[],"payable":false,"stateMutability":"nonpayable","type":"function"},{"constant":true,"inputs":[{"internalType":"contract IERC20","name":"fromToken","type":"address"},{"internalType":"contract IERC20","name":"destToken","type":"address"},{"internalType":"uint256","name":"amount","type":"uint256"},{"internalType":"uint256","name":"parts","type":"uint256"},{"internalType":"uint256","name":"flags","type":"uint256"}],"name":"getExpectedReturn","outputs":[{"internalType":"uint256","name":"returnAmount","type":"uint256"},{"internalType":"uint256[]","name":"distribution","type":"uint256[]"}],"payable":false,"stateMutability":"view","type":"function"},{"constant":true,"inputs":[{"internalType":"contract IERC20","name":"fromToken","type":"address"},{"internalType":"contract IERC20","name":"destToken","type":"address"},{"internalType":"uint256","name":"amount","type":"uint256"},{"internalType":"uint256","name":"parts","type":"uint256"},{"internalType":"uint256","name":"flags","type":"uint256"},{"internalType":"uint256","name":"destTokenEthPriceTimesGasPrice","type":"uint256"}],"name":"getExpectedReturnWithGas","outputs":[{"internalType":"uint256","name":"returnAmount","type":"uint256"},{"internalType":"uint256","name":"estimateGasAmount","type":"uint256"},{"internalType":"uint256[]","name":"distribution","type":"uint256[]"}],"payable":false,"stateMutability":"view","type":"function"},{"constant":true,"inputs":[{"internalType":"contract IERC20[]","name":"tokens","type":"address[]"},{"internalType":"uint256","name":"amount","type":"uint256"},{"internalType":"uint256[]","name":"parts","type":"uint256[]"},{"internalType":"uint256[]","name":"flags","type":"uint256[]"},{"internalType":"uint256[]","name":"destTokenEthPriceTimesGasPrices","type":"uint256[]"}],"name":"getExpectedReturnWithGasMulti","outputs":[{"internalType":"uint256[]","name":"returnAmounts","type":"uint256[]"},{"internalType":"uint256","name":"estimateGasAmount","type":"uint256"},{"internalType":"uint256[]","name":"distribution","type":"uint256[]"}],"payable":false,"stateMutability":"view","type":"function"},{"constant":true,"inputs":[],"name":"isOwner","outputs":[{"internalType":"bool","name":"","type":"bool"}],"payable":false,"stateMutability":"view","type":"function"},{"constant":true,"inputs":[],"name":"oneSplitImpl","outputs":[{"internalType":"contract IOneSplitMulti","name":"","type":"address"}],"payable":false,"stateMutability":"view","type":"function"},{"constant":true,"inputs":[],"name":"owner","outputs":[{"internalType":"address","name":"","type":"address"}],"payable":false,"stateMutability":"view","type":"function"},{"constant":false,"inputs":[],"name":"renounceOwnership","outputs":[],"payable":false,"stateMutability":"nonpayable","type":"function"},{"constant":false,"inputs":[{"internalType":"contract IOneSplitMulti","name":"impl","type":"address"}],"name":"setNewImpl","outputs":[],"payable":false,"stateMutability":"nonpayable","type":"function"},{"constant":false,"inputs":[{"internalType":"contract IERC20","name":"fromToken","type":"address"},{"internalType":"contract IERC20","name":"destToken","type":"address"},{"internalType":"uint256","name":"amount","type":"uint256"},{"internalType":"uint256","name":"minReturn","type":"uint256"},{"internalType":"uint256[]","name":"distribution","type":"uint256[]"},{"internalType":"uint256","name":"flags","type":"uint256"}],"name":"swap","outputs":[{"internalType":"uint256","name":"","type":"uint256"}],"payable":true,"stateMutability":"payable","type":"function"},{"constant":false,"inputs":[{"internalType":"contract IERC20[]","name":"tokens","type":"address[]"},{"internalType":"uint256","name":"amount","type":"uint256"},{"internalType":"uint256","name":"minReturn","type":"uint256"},{"internalType":"uint256[]","name":"distribution","type":"uint256[]"},{"internalType":"uint256[]","name":"flags","type":"uint256[]"}],"name":"swapMulti","outputs":[{"internalType":"uint256","name":"","type":"uint256"}],"payable":true,"stateMutability":"payable","type":"function"},{"constant":false,"inputs":[{"internalType":"contract IERC20","name":"fromToken","type":"address"},{"internalType":"contract IERC20","name":"destToken","type":"address"},{"internalType":"uint256","name":"amount","type":"uint256"},{"internalType":"uint256","name":"minReturn","type":"uint256"},{"internalType":"uint256[]","name":"distribution","type":"uint256[]"},{"internalType":"uint256","name":"flags","type":"uint256"},{"internalType":"address","name":"referral","type":"address"},{"internalType":"uint256","name":"feePercent","type":"uint256"}],"name":"swapWithReferral","outputs":[{"internalType":"uint256","name":"","type":"uint256"}],"payable":true,"stateMutability":"payable","type":"function"},{"constant":false,"inputs":[{"internalType":"contract IERC20[]","name":"tokens","type":"address[]"},{"internalType":"uint256","name":"amount","type":"uint256"},{"internalType":"uint256","name":"minReturn","type":"uint256"},{"internalType":"uint256[]","name":"distribution","type":"uint256[]"},{"internalType":"uint256[]","name":"flags","type":"uint256[]"},{"internalType":"address","name":"referral","type":"address"},{"internalType":"uint256","name":"feePercent","type":"uint256"}],"name":"swapWithReferralMulti","outputs":[{"internalType":"uint256","name":"returnAmount","type":"uint256"}],"payable":true,"stateMutability":"payable","type":"function"},{"constant":false,"inputs":[{"internalType":"address","name":"newOwner","type":"address"}],"name":"transferOwnership","outputs":[],"payable":false,"stateMutability":"nonpayable","type":"function"}]
let CONTRACT_ADDRESS = "0xC586BeF4a0992C495Cf22e1aeEE4E446CECDee0E"

let contract = new web3.eth.Contract(ABI, CONTRACT_ADDRESS)
contract.methods.getExpectedReturn(
    "0xEeeeeEeeeEeEeeEeEeEeeEEEeeeeEeeeeeeeEEeE",
    "0x89d24a6b4ccb1b6faa2625fe562bdd9a23260359",
    100,
    10, 
    0
).call().then(data => {
    console.log(`returnAmount: ${data.returnAmount.toString()}`)
    console.log(`distribution: ${JSON.stringify(data.distribution)}`)
}).catch(error => {
    // TO DO: ...
});

getExpectedReturnWithGas

function getExpectedReturnWithGas(
    IERC20 fromToken,
    IERC20 destToken,
    uint256 amount,
    uint256 parts,
    uint256 flags,
    uint256 destTokenEthPriceTimesGasPrice
)
    public
    view
    returns(
        uint256 returnAmount,
        uint256 estimateGasAmount,
        uint256[] memory distribution
    )

Calculate expected returning amount of desired token taking into account how gas protocols affect price

Params Type Description
fromToken IERC20 Address of trading off token
destToken IERC20 Address of desired token
amount uint256 Amount for fromToken
parts uint256 Number of pieces source volume could be splitted (Works like granularity, higly affects gas usage. Should be called offchain, but could be called onchain if user swaps not his own funds, but this is still considered as not safe)
flags uint256 Flags for enabling and disabling some features (default: 0), see flags description
destTokenEthPriceTimesGasPrice uint256 returnAmount * gas_price, where returnAmount is result of getExpectedReturn(fromToken, destToken, amount, parts, flags)

Return values:

Params Type Description
returnAmount uint256 Expected returning amount of desired token
estimateGasAmount uint256 Expected gas amount of exchange
distribution uint256[] Array of weights for volume distribution

Example:

 // TO DO: ...

getExpectedReturnWithGasMulti

function getExpectedReturnWithGasMulti(
    IERC20[] memory tokens,
    uint256 amount,
    uint256[] memory parts,
    uint256[] memory flags,
    uint256[] memory destTokenEthPriceTimesGasPrices
)
    public
    view
    returns(
        uint256[] memory returnAmounts,
        uint256 estimateGasAmount,
        uint256[] memory distribution
    )

Calculate expected returning amount of first tokens element to last tokens element through and the middle tokens with corresponding parts, flags and destTokenEthPriceTimesGasPrices array values of each step.
The length of each array (parts, flags and destTokenEthPriceTimesGasPrices) should be 1 element less than tokens array length. Each element from parts, flags and destTokenEthPriceTimesGasPrices corresponds to 2 neighboring elements from tokens array.

Params Type Description
tokens IERC20[] The sequence of tokens swaps (tokens[0] -> tokens[1] -> ...)
amount uint256 Amount for tokens[0]
parts uint256[] The sequence of number of pieces source volume could be splitted (Works like granularity, higly affects gas usage. Should be called offchain, but could be called onchain if user swaps not his own funds, but this is still considered as not safe)
flags uint256[] The sequence of flags for enabling and disabling some features (default: 0), see flags description
destTokenEthPriceTimesGasPrice uint256[] The sequence of numbers returnAmount * gas_price, where returnAmount is result of getExpectedReturn(fromToken, destToken, amount, parts, flags)

Return values:

Params Type Description
returnAmount uint256[] Expected returning amounts of desired tokens in tokens array
estimateGasAmount uint256 Expected gas amount of exchange
distribution uint256[] Array of weights for volume distribution

Example:

 // TO DO: ...

swap

function swap(
    IERC20 fromToken,
    IERC20 destToken,
    uint256 amount,
    uint256 minReturn,
    uint256[] memory distribution,
    uint256 flags
) public payable returns(uint256)

Swap amount of fromToken to destToken

Params Type Description
fromToken IERC20 Address of trading off token
destToken IERC20 Address of desired token
amount uint256 Amount for fromToken
minReturn uint256 Minimum expected return, else revert transaction
distribution uint256[] Array of weights for volume distribution (returned by getExpectedReturn)
flags uint256 Flags for enabling and disabling some features (default: 0), see flags description

Notice: Make sure the flags param coincides flags param in getExpectedReturn method if you want the same result

Notice: This method is equal to swapWithReferral(fromToken, destToken, amount, minReturn, distribution, flags, address(0), 0)

Return values:

Params Type Description
returnAmount uint256 Recieved amount of desired token

Example:

 // TO DO: ...

swapMulti

function swapMulti(
    IERC20[] memory tokens,
    uint256 amount,
    uint256 minReturn,
    uint256[] memory distribution,
    uint256[] memory flags
) public payable returns(uint256)

Swap amount of first element of tokens to the latest element.
The length of flags array should be 1 element less than tokens array length. Each element from flags array corresponds to 2 neighboring elements from tokens array.

Params Type Description
tokens IERC20[] Addresses of tokens or address(0) for Ether
amount uint256 Amount for tokens[0]
minReturn uint256 Minimum expected return, else revert transaction
distribution uint256[] Array of weights for volume distribution (returned by getExpectedReturn)
flags uint256[] The sequence of flags for enabling and disabling some features (default: 0), see flags description

Notice: Make sure the flags param coincides flags param in getExpectedReturnWithGasMulti method if you want the same result

Notice: This method is equal to swapWithReferralMulti(fromToken, destToken, amount, minReturn, distribution, flags, address(0), 0)

Return values:

Params Type Description
returnAmount uint256 Recieved amount of desired token

Example:

 // TO DO: ...

swapWithReferral

function swapWithReferral(
    IERC20 fromToken,
    IERC20 destToken,
    uint256 amount,
    uint256 minReturn,
    uint256[] memory distribution,
    uint256 flags,
    address referral,
    uint256 feePercent
) public payable returns(uint256)

Swap amount of fromToken to destToken

Params Type Description
fromToken IERC20 Address of trading off token
destToken IERC20 Address of desired token
amount uint256 Amount for fromToken
minReturn uint256 Minimum expected return, else revert transaction
distribution uint256[] Array of weights for volume distribution (returned by getExpectedReturn)
flags uint256 Flags for enabling and disabling some features (default: 0), see flags description
referral address Referrer's address (exception with flag FLAG_ENABLE_REFERRAL_GAS_SPONSORSHIP)
feePercent uint256 Fees percents normalized to 1e18, limited to 0.03e18 (3%)

Notice: Make sure the flags param coincides flags param in getExpectedReturn method if you want the same result

Return values:

Params Type Description
returnAmount uint256 Recieved amount of desired token

Example:

 // TO DO: ...

swapWithReferralMulti

function swapWithReferralMulti(
    IERC20[] memory tokens,
    uint256 amount,
    uint256 minReturn,
    uint256[] memory distribution,
    uint256[] memory flags,
    address referral,
    uint256 feePercent
) public payable returns(uint256 returnAmount)

Swap amount of first element of tokens to the latest element.
The length of flags array should be 1 element less than tokens array length. Each element from flags array corresponds to 2 neighboring elements from tokens array.

Params Type Description
tokens IERC20[] Addresses of tokens or address(0) for Ether
amount uint256 Amount for tokens[0]
minReturn uint256 Minimum expected return, else revert transaction
distribution uint256[] Array of weights for volume distribution (returned by getExpectedReturn)
flags uint256[] The sequence of flags for enabling and disabling some features (default: 0), see flags description
referral address Referrer's address (exception with flag FLAG_ENABLE_REFERRAL_GAS_SPONSORSHIP)
feePercent uint256 Fees percents normalized to 1e18, limited to 0.03e18 (3%)

Notice: Make sure the flags param coincides flags param in getExpectedReturnWithGasMulti method if you want the same result

Return values:

Params Type Description
returnAmount uint256 Recieved amount of desired token

Example:

 // TO DO: ...

makeGasDiscount

function makeGasDiscount(
    uint256 gasSpent,
    uint256 returnAmount,
    bytes calldata msgSenderCalldata
)

In case developer wants to manage burning GAS or CHI tokens with developer's own smartcontract one should implement this method and use FLAG_ENABLE_REFERRAL_GAS_SPONSORSHIP flag. 1proto.eth will call makeGasDiscount in developer's smartcontract.

Params Type Description
gasSpent uint256 How many gas was spent
returnAmount uint256 Recieved amount of desired token
msgSenderCalldata bytes Arguments from swap, swapWithReferral or swapWithReferralMulti method

Notice: There is no such method in 1proto.eth.

Flags

Flag types

There are basically 3 types of flags:

  1. Exchange switch
    This flags allow 1split.eth to enable or disable using exchange pools for swap. This can be applied for exchanges in genereral, for example: split, wrap, or this can be applied for a specific exchange type, for example: bancor, oasis.
    This flags may be used in any combination.

  2. Transitional token selector
    This flags provide to swap from fromToken to destToken using transitional token.
    This flags cann't be used in combination with the same type.

  3. Functional flags
    This flags provide some additional features.
    This flags may be used in any combination.

Flags description

flags param in 1split.eth methods is sum of flags values, for example:

flags = FLAG_DISABLE_UNISWAP + FLAG_DISABLE_KYBER + ...