Skip to content
This repository has been archived by the owner on Apr 17, 2023. It is now read-only.

Commit

Permalink
Add support for 1inch price aggregator
Browse files Browse the repository at this point in the history
  • Loading branch information
dennohpeter committed Oct 16, 2021
1 parent 610b958 commit 9bb2b4c
Show file tree
Hide file tree
Showing 2 changed files with 42 additions and 3 deletions.
4 changes: 2 additions & 2 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -4,10 +4,10 @@

- Project Setup :heavy_check_mark:
- Initial commit :heavy_check_mark:
- Add support for 1inch price aggregator
- Add support for 1inch price aggregator :heavy_check_mark:
- Add ability to monitor multiple tokens concurrently
- Add ability to buy and sell tokens across various supported exchanges on different blockchains
- Add support for ethereum blockchain
- Add support for ethereum blockchain :heavy_check_mark:

`v1.0.0`

Expand Down
41 changes: 40 additions & 1 deletion src/lib/1inch.io.ts → src/lib/1inch.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,14 @@ export class OneInch extends Aggr {
constructor() {
super(`1Inch`, `https://api.1inch.exchange/v3.0/`);
}
/**
* Gets the best exchange rate for a given pair
* @param srcToken - from token
* @param toToken - to token
* @param srcAmount - from token amount
* @param side - trade direction i.e buy or sell
* @returns best quote found
*/
getQuote = async (params: { srcToken: string, toToken: string, srcAmount: number | string, side?: string }): Promise<Quote> => {
const { srcToken, toToken, srcAmount, side } = params
try {
Expand All @@ -26,18 +34,31 @@ export class OneInch extends Aggr {

}
}
/**
* Builds a tx based on the given params
* @param srcToken - from Token
* @param toToken - to Token
* @param srcAmount - from Token amount
* @param slippage - slippage tolerance
* @returns tx data that can be send to the network
*/
buildTx = async (srcToken: string, toToken: string, srcAmount: number, slippage?: number): Promise<string> => {
try {
let defaultSlippage = 0.5
const { data } = await axios({
method: "GET",
url: `${this.API_URL}${config.NETWORK.ID}/swap?fromTokenAddress=${srcToken}&toTokenAddress=${toToken}&amount=${srcAmount}&fromAddress=${config.WALLET.PUBLIC_KEY}&disableEstimate=true${slippage ? `&slippage=${slippage}` : ''}`
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}`
})
return data
} catch (error: any) {
throw new Error(JSON.stringify(error));
}
}

/**
* Gets supported protocols by 1inch price aggregator
* @returns Supported protocols by 1inch price aggregator
*/
getProtocols = async (): Promise<string[]> => {
try {
const { data }: any = await axios({
Expand All @@ -51,4 +72,22 @@ export class OneInch extends Aggr {
}
}

/**
* Approves spender to trade the given amount of a token
* @param tokenAddress address of the token to approve
* @param amount amount of the the quantity to approve: default is infinity
* @returns approve data that can be send to the network
*/
approve = async (tokenAddress: string, amount?: string) => {
try {
const { data } = await axios({
method: "GET",
url: `${this.API_URL}${config.NETWORK.ID}/approve/calldata?tokenAddress=${tokenAddress}`
})
return data
} catch (error: any) {
throw new Error(JSON.stringify(error));
}
}

}

0 comments on commit 9bb2b4c

Please sign in to comment.