Deep dive into 1inch Swap and Limit order API with Nick Kozlov

ETH Seoul 2022 · August 2022 · 27:49

Nick Kozlov, a software developer at 1inch, gives a deep dive into the 1inch aggregation API and limit order protocol. The talk explains how the aggregation protocol routes swaps across many exchanges in one transaction, walks through the quote and swap methods and their parameters, including protocols, connector tokens, complexity, slippage, fees, partial fills and gas limits, then covers limit orders, predicates, custom price logic and callbacks for developers building on EVM chains.

Speakers

Topics

From ETH Seoul 2022, hosted by Kryptoplanet.

Transcript

hi everyone um my name is nick i'm software developer from one inch and right now i want to talk about our apis about our limit order api and about our aggregation api so but firstly i want to talk about our project a bit so vanish network is actually um consists multiple protocols the main protocol of varnish network is aggregation protocol and currently we have more than two and seven for 2.7 million users in our across multiple evm compatible chains we have billions of volume uh on our exchange on our aggregation protocol

we also do support um most of the evm chains and we also support most of the daxes across all the evm chains aggregation protocol is basically the idea how we can aggregate all the liquidity across all the daxes and allow users to swap with the best rate on the market uh with the same experience with the same user experience as uh on the uni swap for example so basically this is like short overview of all our protocols we do have our one inch wallet we do have dao we do have token currently we also have limit order protocol and liquidity protocol and we're also working on one inch pro that has basically

regulated solution for the institutionals that allows you to swap with kyc with all the support with all the regulations uh let's talk a bit about aggregation protocol so basically aggregation protocol as i said before allows to get the best rate on the market across any chain let's just think that you need to swap for example like 100 film to die and basically you'll firstly will go to uni swap and try to swap there and what if i can tell you that you will always have the best the better rate on one inch exchange because we we aggregate multiple providers also

uni-swap curve almost all the daxes that you have on any chain and allows you to get the better get the best rate we also have very optimized gas router so basically um it's always the best way to swap is always to aggregate multiple daxes and we do all of this stuff on the same transaction so you have the same user experience as uh swapping just on uniswap so like the the main idea of aggregation protocol that you have multiple patches here basically we allow you to use our api to get the rate as a developer you just need to ask for the quote you need to pass something like to build

some data for the quote you ask for the quote on our api if you are agreed with the amount of the return amount of the quote you need to ask for the call data for the transaction after you build the transaction using our swap method you basically just need to pass this transaction to the for contact so you just after swap you need to go to the smart contract manage route of one of an inch it's called one inch router and just pass the data that we gave you on the previous uh previously uh our managed routers the poll supports actually multiple swaps and we do it on like actually i mean that it can be executed parallely we can execute

uh we can execute swaps between any token to any token and even if some of the part of the swap will fail we still allows user to continue to swap it so some part of some percent of the swap can go back to the to the user to the executor and the main part will be swapped with this with this rate we also support mean return amounts so basically one shorter allows you to actually to execute any type of the transaction for the user whatever user wants and uh the most interesting stuff about winning show router that is much more optimized than a unit swap router even if you do just simple swap the uni it will be much more cheaper using just one inch router for that

so you will always save on gas more than on uniswap with us basically if you want to integrate one inch aggregation protocol api you just need these two methods these two methods are available on our one inch docs site so basically the first one is one each voltage code as i said before the second one is a swap i just want to talk about all the params that we have on the quote on on the swap all the old query params basically you just need free query params to make a swap to get a quote on one inch it's just from token to token address and also amount that is basically amount of the from token address

so this is example of uh some these examples are of the examples of swapping ethereum to one inch or five ethereum to one inch basically all these params allows you to get the quote you don't need to think a lot about which params you need to pass just to tokens and amount but we do also support multiple other additional parameters that allows you to be to do more custom logic for your client um so i want i want to talk about that we also firstly we support uh protocols param this program allows you to specify specific daxes that you need for exchange for the swap sometimes you want to swap only on specific daxes

for a specific pair and it can be useful for low liquidity tokens usually we do support all the protocols but you can turn off some of them and also have a bit more faster response um if you also support fee fee we allows any protocol any integration to have some reward fee for each positive slippage from the user side this fee can be up to three percent per swap it depends on the execution of the transaction if there is no positive slippage you will not get the fee from this transaction but if it will be you will can you can earn up to 50 from the user

so by default the fee is zero percent but if you want to specify something not differ from zero you just need to pass a number from zero to three uh we also have a parameter called connector tokens this parameter is pretty important because that's one of the most base how we build path for the swap sometimes if you have no low liquidity token and you know specific pool for example on uniswap exchange where there is a lot of liquidity for this token if you will pass this connector token to our tower api we will give you much more better return rate than if it will be just default connector

tokens so um it's pretty important parameter for the very low liquidity of tokens when you need to get to get but the best return we also have basic parameter is just gas price if you will have pretty high gas price it will be much more easier to get a good return so you can it depends on the how much gas price you will pay you will you will and you will get a different return amount and we can adjust your to your to the client sometimes users needs to pay a lot for the for the good swap because users can swap hundreds of ethereum sometimes you need just to pay a couple of dollars so it's almost like nothing and we can optimize to each user

we can adjust to each of them this is basically example of connector tokens here um on this on this path is usdc and ethereum i just want to visualize the idea of connected tokens so for example the main part the main swap is actually between die to eighth and we have udc and ethereum as connector targets so if you will specify something else for example like one inch talking vanish connector token we will try to find the path to swap from die to one inch and after that from one inch to eighth for example so and there is another parameter called complexity level this parameter is pretty important because we can specify how much

how much parts how much we can go to the to the roads sometimes we can make just one connector token uh here the plasticity level is here because we use small two connector tokens between swap of uh die to eighth you can have up to three complexity levels on the on the swap uh usually it's not very important to have uh free the competition level of free usually two is pretty optimal solution sometimes he needs a complexity level of zero or one if it's very low gas amount low gas optimized swap another very important parameter is main

road parts basically this this is max amount of splits that you can make for the each sub path from the from amount it can be up to 50 usually it's 10 but in current example is just five basically that means that we can split your die amount on each path to five parts for to swap out the multiple daxes to the usdc for example we'd also have another parameter called parts so we can split your subparts to another subparts during each swap that allow and you can also specify this parameter you also can by default it's like 50 but sometimes you can specify it lower so

the response response time will be much more lower and faster basically after you can specify all the parameters for the quote you need to do the same for the swap and if you're talking about this farm swap method is pretty the same as quote actually you have all the same parameters except one more one more some news one more new parameter is slippage and it's very important parameter because sometimes when you do a swap during transaction execution the rates can be differ and you can get much more lower amount of ethereum for example in this example than you expected so we can specify how much um

percentage of ethereum how much of the return amount you can lose is and you are okay with that for example in this in this example i just specify 39 that means that i can lose up to 49 percent of the ethereum on the return rate and the transaction will be still executed it's very important to understand that if you will have very low sleepage you will also can not ex will also have fail of your transaction because the price is going up and down too fast sometimes and if the slippage is too low it will be always reported so it's like always balance between very high slippage and very low sleepage it depends on the token pair and depends on the market conditions sometimes always it's usually if it's very hard to swap

with the low slippage you need to adjust it to authorize it a bit and try again and again it's pretty okay if you are using for example flash swaps like flash bots for that in this case you just don't pay for gas and it will be easier for you just to adjust slippage of your swap another important parameters is destination receiver and refer address basically we do allow to specify return addre the other address that will be used to receive the the funds from the swap for example you want to swap from one wallet and get funds to another wallet it's possible via our router it's also possible to specify the refer address you need to specify this address

if you want to get some fee something emission for your integration so for example if you are a wallet and you want to integrate one inch api and you will you want to get some commissions you always need to specify address that will get some rewards and also specify the fiamma percent as well our router also supports pyramid so basically if token supports pyramid you can swap transaction you can just swap without any approve and you don't need to spend gas on the approve you can do it in the same transaction as this as a swap and also you do specify gas limit you can you can specify gas limit depends on the gas limit amount we can adjust the road of the path so if gas limit is very

low it will be pretty simple swap and it's also pretty useful for low liquidity swaps and low and for small amount swaps um there is also one important parameter called allow partial fill that allows you to for example to fill specific parts of the road of the swap not the whole swap just for example 70 of the swap so basically sometimes it's very important for you to swap at least something and you are okay to swap at least 70 percent of the whole amount that you are going to swap and at least that all the other parts will go back to your account in the this in the source currency for example in

this example for if i will swap die to prop ethereum for example um like uh like up to 70 up to six seven eight ninety percent of the swap goes to wrapped ethereum and the other ten percent of die goes back to the my address but i will swap at least ninety percent of my amount to develop the theorem with the very pretty good rate it's very hard to swap big amounts of big amounts of tokens with low sleepage but you can always try it at least to feel small percent of it so it's always it's also possible with our api uh it's also a very important parameter

called disable estimate it allows you to specify sometimes you need to estimate transaction on the wallet and sometimes it's impossible to do this because the destination the wallet is not ready for that for example you need to estimate that you need to build cool data for the swap but this wallet doesn't have allowance for this token in this case you need to use disable estimate flag so we will not estimate the transaction execution from the from this wallet and you can just use this call data later for the swap it's also important for the smart contracts if you you can always use another smart contract as a from from token address as a from address for the

swap so basically that's it about aggregation protocol and i want to talk a bit about limit order protocol as well um aggregation protocol is more about a user side experience when you need to instantly swap some tokens a limit order protocol allows you to specify the destination or the rate how you which the rate of the swap and the swap will be executed only with this trade we do we do also support multiple additional features over limit orders it's pretty the same user experience as on any centralized exchange but is absolutely decentralized uh what does it mean uh for example if you create a limit

order you will not pay for the gas from the user side you just need to create this limit order and sign it from with your wallet with the specified rate and any user can get your order and feel it so basically it's just agreement between someone and you that you you are okay to swap some specific amount of the tokens with this rate and anyone else can just fill this uh this execution disagreement and just provides you these tokens with this trade with this expected trade and there is just smart contract that allows you to check all these conditions the it can be multiple conditions for each uh swap so basically with our limit order protocol it's

possible to implement multiple features like stop losses like rfq also we can have dynamic prices we can use arbitrary price discovery for each swap the best example of the eve of it is that for each amount of of tokens that you swap it can be it the price will can be varies depends on the amount of the swap it can it rise depends on the market condition for example i can create a limit order that is that the pro and the rate of the limit order will be different depends on the price of chin link for example so i want to swap for example wrapped ethereum to one inch with the rate like one one inch for like 0.08 0.0.1 ethereum but if the price of the shin link will be higher than 10

i want to have different rate it's also possible with limit order so basically it's almost possible to implement any any execution any condition using our limit order protocol there are two parties during of swap during limit order protocol it's market maker and market taker so market maker is the person who basically creates an order you just create with some condition and you need to sign it via your wallet after that you just need to share this order this information with uh we have a niche api with the whole network and also you don't pay for gas during this

moment if you don't have a pro allowance for the token you don't need to pay for gas you just create any order with any amount that is available on your wallet and anyone else actually can fill your word fill your order after that another party that is responsible for actually filling disorders is market taker it's anyone who can search for any available orders on our api and basically if this person is okay to fill this order this person can just execute the swap and fill uh the order why it's why it's important why market taker exists and why do they need this basically sometimes there is some limit orders with the market rate that is pretty

profitable for the market taker so market takers usually just earn some profits based on the limit orders for example the current market price of the ethereum for example will be 2000 but there will be limit order for 2050. so it will be profitable for market taker just to execute such limit order and earn some earn some profits on it so basically market taker also needs to calculate amount of gas that mark attacker needs to pay to execute this limit order basically it's always market taker who pays for the gas of the limit order market maker don't need to pay for that it looks pretty like this firstly maker

like market maker just need to sign and send order to our storage of limit orders to our api it also can be absolutely decentralized you can not use any api for that you can just use p2p exchange for for the limit orders after that a market taker search for suitable limit orders on our storage after that market taker can fill this limit order via our smart contract on chain paying some gas for it and after that basically market market maker receives these tokens like needed tokens based on this order so it works pretty simple actually let's talk a bit about limit order

structure if you want to integrate him with others we do support we have sdk we have javascript sdk for limited orders so it's pretty simple actually to integrate and to use them on your site but if you want to have additional conditions and if you want to go deep you need to understand this information basically uh this structure is like that and i want to say about mark make your iso data and take your asset data parameters here basically with our limit orders you can we support not only gear c20 tokens you can specify any other asset that can be implemented via make your asset data and take care of the data parameters this this data allows you to actually

support any asset that will have their own method of transfer so basically you can for example specify uh nft tokens like irish 721 tokens for limit order and implement for example market nfc marketplace based on the limit order protocol you just need to redefine make your asset data and take care as the data fields also get maker amount and take your amount fields allows you to specify the rates the exchange rate of the assets so you can it can be pretty dynamic rates depends on the additional parameters that you can also specify we do also support predicate and permit so payment is pretty basic it's pretty

the same it's also the same parameters we have on aggregation protocol so you can swap without any fees if you just if your token supports permit and interaction field is pretty important field that allows you to make some callbacks actually we do we use this call we use this callbacks before transfer from of the makers other makers tokens to the taker address so basically interaction allows you for example to implement such logic as uh for example to store all your tokens on the compound and then the limit order will be executed these tokens will be removed from the compound stake they will be unstaked and will be going to the taker amount to the taker

so basically for example with limit order you can create an order and use your funds to earn some earn some profits on the compound for example on the same time and when it will be executed they will just withdraw this uh tokens from the compound and the same transaction so let's talk a bit about order execution if you need to fill this order you need to use function called field order on the smart contract and for that you need to implement you need to pass such parameters as the old order structure as i described before and also you need to pass a signature we do support sign type data v4 signature for that

and if you have a case when you have smart contract as a maker address signature must be not a sign type data signature but specific data that you will be that then this signature will be sent to your smart contract and your smart contract need to verify this data on its side on this it's a site basically we do so we actually support any address that can be executed that can be a market maker for the limit order and basically making amount and taking amount adjust amounts that you need for the swap based on them you actually have a rate and fiscal amount is mean return that you can get for the taking amount so if

you are not okay to get at least like if you are okay only to get at least like 50 percent of the limit order you can specify that using first called amount uh let's talk about predicate actually it's conditional execution what i said before we are using predicates for example to allows users to cancel the orders if you if you will use predicates you can allow users to cancel limit order during time during during some time uh you can also specify timestamp for the limit order so it will be alive for only for example for five minutes or like for 30 minutes we do use predicate for that we have basic implementations of some functions for limit orders and

you can use all of them and actually you can build your own predicate based on our basic functions so it's pretty customizable for example if you want some additional condition uh you just need to deploy smart contract to the ethereum and implement specific functions there and after that you can just build limit order that will you just goes to the your smart contract on the film and checks some conditions there so you can also as i said before you can already find get maker and take your amount so basically this is default implementations of get maker and can take your amount but if you are not okay with that you can always specify your own

actually that allows you to may implement your own price auction so the price can goes up and down depends on your rules for each token and for each additional condition on the market so that's actually how we implement execution of the element order so basically that's how actually we do calculate our return amount and it's pretty important to understand that to build your your custom implementation of the get maker and get the care amount if you have a case when you your maker address is smart contract is just not a simple word for example user but it's

some other smart contract in this case you we will actually will call this function this we will call this function it's called is valid signature only on the address of the smart contract so you need to implement this function on your site basically we pass a hash of the smart of the order of the limit order and also if you pass the signature which is basically any data that you can specify so you can you need to verify this data on the smart contract site uh all the time that you create any new any new order so if you will not have this signature it will be not working or actually all the signal all the functions will be working by default uh it's also as i said before there is

an interaction parameter that allows you to um somehow to interact with the user funds with the maker funds before transfer from to that to the taker address so basically using this uh inter function called notify field order you can implement any custom callback to any other protocol and somehow manage the major maker funds so it's pretty useful as i said before for example for the case of the compounds so basically that's all what i want to say if you have any questions we'll be happy to answer about that uh it's they're just two important links

that i see that they put the first one is actually quick start for the limit orders if you use a javascript sdk or if you use javascript on the client side and if you want to go deeper and you need for example to modify smart contracts for your pro and specific to specify specific conditions for the limit order you need to go to the limit order protocol v2 github and actually to try to use it on the solidity side we have all the tests there all the information that you need to somehow use additional callbacks or something like that so we'll be pretty happy to answer any your questions and thank you