-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathindex.ts
50 lines (43 loc) · 2.07 KB
/
index.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
import Web3 from "web3"
import { findRoute } from "./routing/main"
import { fetchPoolsData } from "./swap/graph_communication"
import { Pool, Quote} from "./utils/types/types";
import { generateCalldata, prepareSwapParams } from "./utils/utils";
export interface RateXConfig {
rpcUrl: string;
chainId: number;
dexes?: Array<Dexes>;
graphApiKey: string;
}
export enum Dexes {
UNISWAP_V2 = "UniswapV2",
UNISWAP_V3 = "UniswapV3",
SUSHISWAP_V2 = "Sushiswap",
BALANCER = "Balancer",
CAMELOT = "Camelot",
}
export class RateX {
rpcProvider: Web3
chainId: number
graphApiKey: string
dexes: Array<Dexes>
constructor(config: RateXConfig) {
this.rpcProvider = new Web3(new Web3.providers.HttpProvider(config.rpcUrl))
this.chainId = config.chainId
this.graphApiKey = config.graphApiKey
this.dexes = config.dexes || [Dexes.SUSHISWAP_V2, Dexes.UNISWAP_V2, Dexes.UNISWAP_V3, Dexes.BALANCER, Dexes.CAMELOT]
};
async getQuote(tokenIn: string, tokenOut: string, amountIn: bigint): Promise<Quote> {
const pools: Pool[] = await fetchPoolsData(tokenIn, tokenOut, 5, 5, this.chainId, this.rpcProvider, this.graphApiKey, this.dexes)
const route = await findRoute(tokenIn, tokenOut, amountIn, pools, this.chainId)
return route
}
async getSwapCalldata(tokenIn: string, tokenOut: string, amountIn: bigint, slippagePercentage: number, recipient: string, deadlineInMinutes: number): Promise<string> {
let quote = await this.getQuote(tokenIn, tokenOut, amountIn);
return generateCalldata(quote, slippagePercentage, deadlineInMinutes, tokenIn, tokenOut, amountIn, recipient);
}
async getSwapParameters(tokenIn: string, tokenOut: string, amountIn: bigint, slippagePercentage: number, recipient: string, deadlineInMinutes: number): Promise<ReturnType<typeof prepareSwapParams>> {
let quote = await this.getQuote(tokenIn, tokenOut, amountIn);
return prepareSwapParams(quote, slippagePercentage, deadlineInMinutes, tokenIn, tokenOut, amountIn, recipient);
}
}