-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.js
159 lines (143 loc) · 5.47 KB
/
index.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
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
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
const TokenRepository = require("./src/repository/TokenRepository");
const PoolRepository = require("./src/repository/PoolRepository");
const BalanceRepository = require("./src/repository/BalanceRepository");
const SwapService = require("./src/service/SwapService");
const PoolService = require("./src/service/PoolService");
const IsTokenUnLockedDTO = require("./src/dto/IsTokenUnLockedDTO");
const GetPoolsOfAccountDTO = require("./src/dto/GetPoolsOfAccountDTO");
const GetTokenOfAccountInSwapDTO = require("./src/dto/GetTokenOfAccountInSwapDTO");
const SwapPriceService = require("./src/service/SwapPriceService");
module.exports = class SwapDSK {
constructor({nodeAPI, swapContract, carbContract, graphContract}) {
this._tokenRepo = new TokenRepository({nodeAPI});
this._poolRepo = new PoolRepository({nodeAPI});
this._balanceRepo = new BalanceRepository({nodeAPI});
this._swapPriceService = new SwapPriceService({
swapAddress: swapContract,
carbAddress: carbContract,
tokenRepository: this._tokenRepo,
poolRepository: this._poolRepo,
});
this._swapService = new SwapService({
nodeAPI,
host: "",
contractAddress: swapContract,
graphAddress: graphContract,
carbAddress: carbContract,
tokenRepository: this._tokenRepo,
balanceRepository: this._balanceRepo,
swapPriceService: this._swapPriceService,
});
this._poolService = new PoolService({
contractAddress: swapContract,
host: "",
nodeAPI,
balanceRepository: this._balanceRepo,
poolRepository: this._poolRepo,
tokenRepository: this._tokenRepo,
carbAddress: carbContract,
});
}
async isTokenUnlocked(req = new IsTokenUnLockedDTO({})) {
return this._swapService.isTokenUnLocked(req);
}
async getPoolsOfAccount(req = new GetPoolsOfAccountDTO({})) { // PoolAccountValue
return this._poolService.getPoolsOfAccount(req.account);
}
async getAllPools() { // PoolValue
return this._poolService.getSwapPools();
}
async getTokensOfAccountInSwap(req = new GetTokenOfAccountInSwapDTO({})) { // TokenAccount
return this._swapService.getTokens(req.account);
}
async calculateSwapParams({
isFrom,
fromToken,
toToken,
fromAmount,
toAmount,
avatar = "empty",
isTransfer,
recipientAddress,
slippage,
blocks
}) {
return this._swapService.getSwapTokenToTokenCall({
isFrom,
fromToken,
toToken,
fromAmount,
toAmount,
avatar,
isTransfer,
recipientAddress,
slippage,
blocks,
});
}
async calculateAddLiquidityParams({carbAmount, tokenAddress, blocks, maxTokenAmount, minCarbAmount}) {
return this._swapService.getAddLiquidityCall({carbAmount, tokenAddress, blocks, maxTokenAmount, minCarbAmount})
}
async calculateRemoveLiquidityParams({
minCarbAmount,
blocks,
tokenAddress,
carbAmount,
contributionAmount,
minTokenAmount,
}) {
return this._swapService.getRemoveLiquidityCall({
minCarbAmount,
blocks,
tokenAddress,
carbAmount,
contributionAmount,
minTokenAmount,
});
}
calculateSwapTokens({
isFrom,
toToken,
fromToken,
fromAmount,
toAmount,
fromPool,
toPool,
carbToken,
graphToken,
graphPool
}) {
return this._swapPriceService.calculateTokenToTokenSwap({
isFrom,
toToken,
fromToken,
fromAmount,
toAmount,
fromPool,
toPool,
carbToken,
graphToken,
graphPool
});
}
async getFeesAndRewardsBalances(account) {
const carbBalance = await this._swapService.getCarbBalance(account);
const graphBalance = await this._swapService.getGraphBalance(account);
return {
carbBalance,
graphBalance
};
}
async getTokenOfAccount({account, tokenAddress}) {
return this._poolService.getTokenOfAccount({account, tokenAddress});
}
async getToken(tokenAddress) {
return this._poolService.getToken(tokenAddress);
}
priceOfTokenInCarbWithPool({token, pool}) {
return this._swapPriceService.priceOfTokenInCarbWithPool({
token,
pool: {carbAmount: pool.x, tokenAmount: pool.y}
});
}
};