@@ -7,6 +7,14 @@ export class OneInch extends Aggr {
77constructor ( ) {
88super ( `1Inch` , `https://api.1inch.exchange/v3.0/` ) ;
99}
10+ /**
11+ * Gets the best exchange rate for a given pair
12+ *@param srcToken - from token
13+ *@param toToken - to token
14+ *@param srcAmount - from token amount
15+ *@param side - trade direction i.e buy or sell
16+ *@returns best quote found
17+ */
1018getQuote = async ( params :{ srcToken :string , toToken :string , srcAmount :number | string , side ?:string } ) :Promise < Quote > => {
1119const { srcToken, toToken, srcAmount, side} = params
1220try {
@@ -26,18 +34,31 @@ export class OneInch extends Aggr {
2634
2735}
2836}
37+ /**
38+ * Builds a tx based on the given params
39+ *@param srcToken - from Token
40+ *@param toToken - to Token
41+ *@param srcAmount - from Token amount
42+ *@param slippage - slippage tolerance
43+ *@returns tx data that can be send to the network
44+ */
2945buildTx = async ( srcToken :string , toToken :string , srcAmount :number , slippage ?:number ) :Promise < string > => {
3046try {
47+ let defaultSlippage = 0.5
3148const { data} = await axios ( {
3249method :"GET" ,
33- url :`${ this . API_URL } ${ config . NETWORK . ID } /swap?fromTokenAddress=${ srcToken } &toTokenAddress=${ toToken } &amount=${ srcAmount } &fromAddress=${ config . WALLET . PUBLIC_KEY } &disableEstimate=true ${ slippage ?`&slippage= ${ slippage } ` :'' } `
50+ url :`${ this . API_URL } ${ config . NETWORK . ID } /swap?fromTokenAddress=${ srcToken } &toTokenAddress=${ toToken } &amount=${ srcAmount } &fromAddress=${ config . WALLET . PUBLIC_KEY } &disableEstimate=false&slippage= ${ slippage ?`${ slippage } ` :defaultSlippage } `
3451} )
3552return data
3653} catch ( error :any ) {
3754throw new Error ( JSON . stringify ( error ) ) ;
3855}
3956}
4057
58+ /**
59+ * Gets supported protocols by 1inch price aggregator
60+ *@returns Supported protocols by 1inch price aggregator
61+ */
4162getProtocols = async ( ) :Promise < string [ ] > => {
4263try {
4364const { data} :any = await axios ( {
@@ -51,4 +72,22 @@ export class OneInch extends Aggr {
5172}
5273}
5374
75+ /**
76+ * Approves spender to trade the given amount of a token
77+ *@param tokenAddress address of the token to approve
78+ *@param amount amount of the the quantity to approve: default is infinity
79+ *@returns approve data that can be send to the network
80+ */
81+ approve = async ( tokenAddress :string , amount ?:string ) => {
82+ try {
83+ const { data} = await axios ( {
84+ method :"GET" ,
85+ url :`${ this . API_URL } ${ config . NETWORK . ID } /approve/calldata?tokenAddress=${ tokenAddress } `
86+ } )
87+ return data
88+ } catch ( error :any ) {
89+ throw new Error ( JSON . stringify ( error ) ) ;
90+ }
91+ }
92+
5493}