-
Notifications
You must be signed in to change notification settings - Fork 4
/
sdk.ts
140 lines (115 loc) · 3.32 KB
/
sdk.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
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
import * as UniSdk from '@uniswap/sdk'
import * as HoneySdk from 'honeyswap-sdk'
import * as PancakeSdk from '@pancakeswap-libs/sdk'
interface token {
chainId: number
address: string
symbol: string
name: string
decimals: number
}
function isMainNet (chainId) {
return chainId === 1
}
function isXDai (chainId) {
return chainId === 100
}
function isRopsten (chainId) {
return chainId === 3
}
type SwapToken = {
decimals: number
symbol: string
name: string
chainId: number
address: string
}
export default class Sdk {
sdk: { Token; Route; Pair; Fetcher; Trade; WETH; TokenAmount; TradeType }
constructor (public chainId: number) {
this.chainId = chainId
this.getSwapSdk(chainId)
}
getSwapSdk (chainId) {
if (isMainNet(chainId)) {
this.sdk = UniSdk
} else if (chainId === 3) {
this.sdk = UniSdk
} else if (isXDai(chainId)) {
this.sdk = HoneySdk
} else if (chainId === 56) {
console.log(`GOINFOR 56`)
this.sdk = PancakeSdk
} else {
throw new Error(`${chainId} is unsupported`)
}
}
createToken (chainId, address, symbol, name, decimals) {
console.log(
`{chainId, address, decimals, symbol, name} : ${JSON.stringify(
{ chainId, address, decimals, symbol, name },
null,
2
)}`
)
return new this.sdk.Token(chainId, address, decimals, symbol, name)
}
getSwapToken (token: token) {
if (!token) throw new Error('Cannot swap a nothing')
const { chainId, address, decimals, symbol, name } = token
if (!this.sdk) throw new Error('Sdk not initialised in constructor')
const { Token } = this.sdk
return new Token(chainId, address, decimals, symbol, name)
}
getPrice (pair, token, chainId) {
const { Route } = this.sdk
const route = new Route([pair], token)
const price = route.midPrice.toSignificant(6)
// console.log('JIS inv', route.midPrice.invert().toSignificant(6)) // 0.00496756
// console.log('price', price) // 201.306
return Number(price)
}
getExecutionPrice (pair, token: SwapToken, amount) {
console.log('getExecutionPrice')
const { Route, Trade, WETH, TokenAmount, TradeType } = this.sdk
const route = new Route([pair], token)
// const price = route.midPrice.toSignificant(6)
// console.log(`route : ${JSON.stringify(route, null, 2)}`)
// console.log(`this.chainId ---> : ${this.chainId}`)
// console.log(`amount ---> : ${amount}`)
// console.log(`typeof amount ---> : ${typeof amount}`)
// console.log(
// `AMOUNT : ${JSON.stringify(
// new TokenAmount(token, amount.toString()),
// null,
// 2
// )}`
// )
const trade = new Trade(
route,
new TokenAmount(token, amount.toString()),
TradeType.EXACT_INPUT
)
console.log(`trade : ${JSON.stringify(trade, null, 2)}`)
console.log(
`trade : ${JSON.stringify(
trade.executionPrice.toSignificant(6),
null,
2
)}`
)
console.log(
`trade.nextMidPrice.toSignificant(6) : ${JSON.stringify(
trade.nextMidPrice.toSignificant(6),
null,
2
)}`
)
return trade
}
async getPair (token0, token1, provider, chainId) {
const { Fetcher } = this.sdk
const pair = await Fetcher.fetchPairData(token0, token1, provider)
return pair
}
}