1inch 🛠Dive into 1inch Aggregation API and Limit Order Protocol
ETHGlobal·Sun, Oct 9, 2022, 12:00 AM
Together with Kirill, we will study how to integrate the 1inch Aggregation API for token exchange on 8 networks using the Pathfinder algorithm and consider the Limit Order Protocol, which allows you to create gasless orders taking advantage of arbitrary price discovery and conditional execution Join Kirill Kuznetsov of 1inch for a workshop titled, "Dive into 1inch Aggregation API and Limit Order Protocol." This workshop is part of ETHNewYork 2022, a three-day in person hackathon that will feature hackers, mentors, enthusiasts, sponsors and speakers who are all gathering in New York June 24-26, 2022 to talk about the future of money, finance, the web, ownership, the way software works and the way power is distributed. We're excited about a new frontier. ---------------------------------------------------- Follow us: https://twitter.com/ETHGlobal​ Check us out: https://ethglobal.co​m Email us at: hello@ethglobal.com
Transcript
[Music] hello everyone my name is kirill i am the lead backend engineer in one inch and today we're gonna talk about the 1 inch aggregation api and 1 inch limit order protocol let's start from aggregation one inch aggregation protocol incorporates pathfinder algorithm that search deals across multiple dexes uh among eight networks uh for example uh if you swap one adder to usdt you will get the simple path through and if you want to swap large amount of adder uh then uh finder algorithm will will find the complex path uh among different uh automatic market makers uh limit orders and also market makers called pmms uh and let's look at the api how to swap one token to another and get the best rate using one inch so you can go to the docs dot one inch dot io and see the swagger documentation quick start examples how to use the api on different networks with the wall flow of request quote request call data sign call data and uh broadcast signed transaction to the chain and also here you will be able to see uh swagger documentation and parameters of each api in point so let's start from the quoting point this endpoint is just like a view function that allows you to see the exchange rate the destination token amount here there are three required parameters the token that you would like to exchange destination token and the amount that you would like to sell uh for example uh in this uh default parameters we see the alias to ethereum address and the orange token address and the amount of ether uh in way that we want to sell let's try to execute this method and see that in response we'll have the all tokens details our destination token amount and the path that and the execution path uh that was built by pathfinder algorithm for example if we increase the value of adder we will see the more complex path that had that has different splits different routes and in this case we'll get better rate and swap on any single decks let's look at other parameters uh protocols parameter allows you to set the enabled protocols by default we enable all protocols supported by one inch pop finder and if you would like to set for example only uni swap or sushi swap you can enter these protocols uh by comma in this field in this query parameter and pathfinder will uh only look at the pulls of this protocols uh if you are an integrator and would like to charge fees from your customers you can set the fee parameter up to three percents and receive the quote by giving fee parameter and taking into account this fee when calculating crates and our parameter is gas limit for example if you know that you don't want to spend more than for example 500 000 of gas you can specify this limitation in this query param and a finder will be taking into account the gas limits uh also our api can work with the connector tokens by default we have about 20 or 12 to 20 connector tokens through which the profinder algorithm searches the path but if you would like to set custom connector tokens that you can enter by comma in this query parameter next parameters allows you to configure uh finder algorithm you can set the complexity level this is the number of connector tokens that can be in the path for example uh if we'll see at this path uh you'll be able to see that in this path uh we have usdc as a connector token and also die as a connector token and for and you can set any connector token if you know that this token will give you better price for some custom tokens than our default list next parameter it's a main road parts this also finder settings so the source token amount uh splits by the parts and searches different uh subroutes in the wall uh exchange path for example uh here we can see that uh 60 of azer goes through this path and other 40 percent goes for this path and if you set more uh main roads then you will be able to see the more pre more precise values and by the way the response time will be slower because algorithm should perform more actions and our parameter its parts this spread is the same the main road parts but it [Music] it works with the single hope for example uh here we have the swap use dc to use dt on different markets and each market has their own percent of distribution and you can set parts up to 100 and and search for the uh most precisely uh values of distribution among different taxes between two tokens by default it's 50 parts but you don't need to worry about these parts we always set the most useful parts to to find the best price with the best response time and the last parameter is gas price by default we use fast gas price from the network but you can specify your value in this field and uh finder will search for the best rate taking into account gas costs with your custom gas price setting okay let's go to the swapping point this endpoint allows you to build the call data that you can execute directly from your wallet or proxy through your smart contract and this endpoint has two more required methods it's from address and slippage uh from address this is the address that will call the one inch contract it can be your uh wallet address or maybe a smart contract address if you would like to proxy one inch call data through your contract to perform more actions uh and the slippage parameter this is the maximal percent uh with the which you are okay uh to to lose for example uh right now we see this amount of usd uh but when you send the transaction to the network your transaction should be mined and it takes time uh during this time the rates of on different exchanges can be changed by in our participants on of the market and you said the safety uh you can set the safety slippage percent if the rate drops uh more than 0.1 for example then uh the exchange will not be performed and your transaction will be reverted and you will just pay for the gas fees of for reverted transaction but you can it's better to pay gas fees in some cases than lose a big amount of destination token okay let's try uh this method uh for uh estimation we need to use some address with the uh with the big amount of header and let's try to execute and here we are here we can add additional takes property in the response object here you can see the from address from which we call uh like from what we send the transaction the wallet address if you use the smart contract that you need to replace this address to your wallet address that will broadcast the message to the blockchain uh two parameter it's two properties it's a one inch contract address the date of this the call data that executes all swaps all the path in one atomic transaction the value this is the amount of header that we need to send to the contract in this transaction to perform swaps from other and the gas this is the estimated gas limit with some additional uh gas uh multiplier to be sure that your transaction will be completed successfully and you can use this value for example to show the gas costs to look at the gas costs of this transaction and gas price and you can take this object and just sign it and broadcast to the node and perform one inch swap next let's look at in our parameters uh one more new parameter it's a deskt receiver this is the address that will receive the funds after the exchange by default we use from address that you specified but if you would like to send tokens after the exchange to another destination address you can specify it in this parameter as i said before you can specify a fee parameter and set the address that will receive this fee this is the referral address query parameter another interesting option is disable estimate if you don't want to perform balance allowance called data checks and you need to set this value to true for example if you would like to proxy through your smart contract that don't have um that doesn't have enough allowance or balance before this before the swap then you need to set this flag to true and perform the transaction estimation on your own site also you can provide the permit to the parameters to allow some tokens support the way to approve um uh amount of tokens on behalf of the smart contract and during the swap this permit will execute it and you don't need to do x extra transaction to set allowance for specific token to one inch contract one more flag it's a low partial fill this is the interesting feature there are some cases when you need to swap tokens anyway for example sell one token or buy one talk in any way and when a market moves fast your transaction may be reverted but if you set a low partial fuel flag to true then in case of price drops on some main roads this main road will reverted for example the first one but if the if uh other roads uh will execute successfully and these roads will be okay with the price slippage uh that you set in uh your response uh then uh you'll get back uh for example fifty percent of either and receive another percent of usdt to your wallet yeah uh this all about the swap api you can also look at the supported liquidity sources look the white listed token list and some helpers for approve and also help check you can always go to the one inch documentation portal and see all descriptions and examples how to communicate with the one inch api okay uh let's go to the limit order protocol and see how to set the gas less limit order uh for example uh what is limit order this is the structure that you will sign by your private key and this is not the transaction this is just uh ellip the signature on elliptic curve of your of the hash of your data and this data includes talking addresses amounts and other parameters and you can set the you can create the limit order sign it and uh broadcast to some uh relayer yeah and let's look uh how it works what interesting feature limit order protocol has okay uh one inch limit uh order protocol uh has three interesting features this is the conditional execution you can check is your order valid or not during the execution when the taker will execute your order and also limit order protocol supports arbitrary price discovery with this feature you can use your custom formula for exchange rate by default we use uh simple proportion but you can fetch prices for example from chain link oracle or maybe build your some custom option options but it's up to you and also you can specify the custom callback during the execution that will be performed before uh before the transfer from from your maker address let's look at the order structure uh the first field that itself is just to uh protect uh orders against double spending uh make your asset uh this is the address that you want to sell take a rest is the token that you want to buy this is your maker address this is the receiver address that will uh receive um takers funds allowed sender allows you to to do private orders to be specified for specific uh executor taker making command this is the amount you would like to sell and taking amount the amount you would like to buy makers data and takers data allows you perform transfers with the with more parameters by uh finding the selector of transfer from uh function uh get maker amount and get taken amount allows you to provide the custom formula for execution api encoded call data that will be executed on chain during the order filling uh predicate this is the abi encoded function that returns boolean that checks is your order valid or not when taker uh try to to fill this order also you can specify permit to not set allowance before the order making and interaction this is the bytes uh bi encoded bytes of your uh custom callback that will be performed before the transfer funds from your address to execute the order you need to call fill order function that accepts order the order structure the signature of your wallet or any bytes in terms of you operate with the smart contract at the maker and you would like to check the not the signature itself but some other details you can provide here any data if you operate via through small smart contract and you can specify one of the amounts if you would like to set the amount that you would like to buy you can set making amount and if you would like to set the amount that you want to sell then taking amount and that is halt amount to protect you against the front running if you use not fixed rate but the order contains custom formulas okay let's look at the predicate there are two built-in predicates this is the non-sqls and timestamp below you can combine different predicates by the end function that accepts the array of target addresses where you will call the data like the uh by sorry of data on each target for example we have two default checks uh first check performs nonce validation like if you cancel all orders on chain that we increase your nons and if the order contains of the previous uh nouns that don't equal that doesn't equal to uh actual nouns on smart contract then this order can be executed and if you would like to set the expiration time to your order you can use the timestamp below predicate that check the expiration time of the order uh here the default proportional uh formulas for calculating maker amount or taker amount and these actions executes by the on on the limit order address by calling function static call and if you would like to uh define some custom formulas with the custom smart contracts you need to wrap your uh your functions by the arbitrary static call function here you specify the target as your smart contract address and the data is the bytes of abi encoded call dia call data that should be called to execute your formula on your contract if you would like to operate with the smart contract as the maker of limit order for example you have the funds on smart contract that you would like to trade then you need to implement is valid signature function on your smart contract that should returns uh the selector of itself as i said before you can specify any bytes to the signature param and validate for example order parse the order from this param and validate that this order is okay for your logic but you can also provide a signature you can set to your contract some eoa address like the basic wallet address and sign for the private key that belongs to this wallet address the order structure and provide the signature and validate the signature by the wallet in the storage on your smart contract and the last thing is the interaction if you would like to perform for example some collateral borrowing or repay deposits whatever you can call the nutsify fill order function on some target the interaction field contains of address the first 20 bytes and some additional data that can be empty or not empty and when the order will be executed your contact will be notified by the limit order contract address and you'll be able to see who executes this order the makers take reset and avoid the field amount this is all about the limit orders you can always check the limit order documentation and some typescript hotels by this core codes or just simply go to our docs portal and see the limit order section and here you will be able to see the guide how to start how to create limit order how to fill limit order you can also cancel limit order build your custom predicates operate with nonsense validate limit orders uh and set the the cheapest orders called tariff q order thank you wish you good luck on the hackathon
Automatic transcript — names and jargon may be misspelled.