-
Notifications
You must be signed in to change notification settings - Fork 82
/
paraswap.js
75 lines (69 loc) · 1.97 KB
/
paraswap.js
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
const axios = require('axios');
const BigNumber = require('bignumber.js');
const ParswapURL = 'https://apiv4.paraswap.io/v2';
// https://developers.paraswap.network/
class Paraswap {
constructor(apiURL = ParswapURL) {
this.apiURL = apiURL;
this.referrer = 'arb-bot';
}
async getPrice(from, to, srcAmount, network) {
// TODO: Add error handling
try {
const requestURL =
`${this.apiURL}/prices/?from=${from.address}&to=${to.address}` +
`&amount=${srcAmount}&fromDecimals=${from.decimals}&toDecimals` +
`=${to.decimals}&side=SELL&network=${network}`;
const { data } = await axios.get(requestURL, {
headers: {
'X-Partner': this.referrer,
},
});
return {
price: data.priceRoute.destAmount,
payload: data.priceRoute,
};
} catch (e) {
throw new Error(
`Paraswap unable to fetch price ${from.address} ${to.address} ${network} ${e.message}`,
);
}
}
async buildTransaction(
pricePayload,
from,
to,
srcAmount,
minDestAmount,
network,
userAddress,
) {
try {
const requestURL = `${this.apiURL}/transactions/${network}`;
const requestData = {
priceRoute: pricePayload,
srcToken: from.address,
destToken: to.address,
srcAmount: srcAmount,
destAmount: minDestAmount,
userAddress: userAddress,
referrer: this.referrer,
srcDecimals: from.decimals,
destDecimals: to.decimals,
};
const { data } = await axios.post(requestURL, requestData);
return {
from: data.from,
to: data.to,
data: data.data,
gasLimit: '0x' + new BigNumber(data.gas).toString(16),
value: '0x' + new BigNumber(data.value).toString(16)
};
} catch (e) {
throw new Error(
`Paraswap unable to buildTransaction ${from.address} ${to.address} ${network} ${e.message}`,
);
}
}
}
module.exports = Paraswap;