h-agent-swap 1.9.8 → 2.1.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/package.json +1 -1
- package/src/abi/PancakePair.json +718 -0
- package/src/abi/TokenMarket.json +20 -98
- package/src/agent-swap.js +194 -19
- package/src/index.js +4 -2
- package/src/swap-pair.js +26 -0
package/src/agent-swap.js
CHANGED
|
@@ -4,10 +4,10 @@ let { abi, bytecode } = require("./abi/TokenMarket.json")
|
|
|
4
4
|
|
|
5
5
|
let { plus, reduce, multiplication, division, saveNum, charCompare,longHandle,toSmall } = require("h-token-staking/src/utils")
|
|
6
6
|
const ERC20 = require("h-token-staking/src/ERC20")
|
|
7
|
-
|
|
7
|
+
let SwapLP = require("./swap-pair")
|
|
8
8
|
|
|
9
9
|
class AgentSwap extends HI {
|
|
10
|
-
constructor({ provider, account, rpcUrl, privateKey, contract }) {
|
|
10
|
+
constructor({ provider, account, rpcUrl, privateKey, contract,lp = "" }) {
|
|
11
11
|
super({ provider, account, rpcUrl, privateKey, contract, abi })
|
|
12
12
|
this.mainToken = "0x0000000000000000000000000000000000000000"
|
|
13
13
|
this.maxVal = "999000000000000000000000000000000000000000000000000"
|
|
@@ -16,6 +16,159 @@ class AgentSwap extends HI {
|
|
|
16
16
|
this.pointMax = "100000000"
|
|
17
17
|
|
|
18
18
|
this.point = 0.01
|
|
19
|
+
this.lp = lp
|
|
20
|
+
}
|
|
21
|
+
|
|
22
|
+
async estimate({ side = 0,amount = 0, point = 0 , wallet = "" ,usdt = "" }) {
|
|
23
|
+
let usdt_precision = 6
|
|
24
|
+
let tau_precision = 18
|
|
25
|
+
let user = this.web3.eth.defaultAccount
|
|
26
|
+
let data = {
|
|
27
|
+
wallet:wallet ? wallet : user,
|
|
28
|
+
usdt_pool:0,
|
|
29
|
+
tau_pool:0,
|
|
30
|
+
usdt:usdt ? usdt : "0xc2132D05D31c914a87C6611C10748AEb04B58e8F",
|
|
31
|
+
tau:"",
|
|
32
|
+
inAmount:0,
|
|
33
|
+
outAmount:0,
|
|
34
|
+
tau_balance:0,
|
|
35
|
+
feeAmount:0,
|
|
36
|
+
realOutAmount:0,
|
|
37
|
+
win:false,
|
|
38
|
+
change:0,
|
|
39
|
+
buyAmount:0,
|
|
40
|
+
taxAmount:0,
|
|
41
|
+
backAmount:0,
|
|
42
|
+
buyType:1
|
|
43
|
+
}
|
|
44
|
+
let lp = new SwapLP({ ...this.network, contract:this.lp })
|
|
45
|
+
let result = await lp.getReserves()
|
|
46
|
+
|
|
47
|
+
if(result.token0.toLocaleLowerCase() == data.usdt.toLocaleLowerCase()) {
|
|
48
|
+
data.usdt_pool = saveNum(toSmall(result._reserve0,usdt_precision),usdt_precision)
|
|
49
|
+
data.tau_pool = saveNum(toSmall(result._reserve1,tau_precision),tau_precision)
|
|
50
|
+
data.tau = result.token1
|
|
51
|
+
}else {
|
|
52
|
+
data.usdt_pool = saveNum(toSmall(result._reserve1,usdt_precision),usdt_precision)
|
|
53
|
+
data.tau_pool = saveNum(toSmall(result._reserve0,tau_precision),tau_precision)
|
|
54
|
+
data.tau = result.token0
|
|
55
|
+
}
|
|
56
|
+
let tau = new ERC20({ ...this.network,contract:data.tau })
|
|
57
|
+
let balance = await tau.balanceOf(data.wallet)
|
|
58
|
+
data.tau_balance = toSmall(balance,18)
|
|
59
|
+
|
|
60
|
+
console.log(`
|
|
61
|
+
<=======swap before amount=======>
|
|
62
|
+
${JSON.stringify(data,null,2)}
|
|
63
|
+
`)
|
|
64
|
+
|
|
65
|
+
// 第一次兑换
|
|
66
|
+
let path = [data.tau,data.usdt]
|
|
67
|
+
if(side == 0) {
|
|
68
|
+
data.inAmount = amount
|
|
69
|
+
let amountInWithFee = multiplication(data.inAmount,9975)
|
|
70
|
+
let numerator = multiplication(amountInWithFee,data.usdt_pool)
|
|
71
|
+
let denominator = plus(multiplication(data.tau_pool,10000),amountInWithFee)
|
|
72
|
+
data.outAmount = saveNum(division(numerator,denominator),6)
|
|
73
|
+
|
|
74
|
+
data.tau_balance = reduce(data.tau_balance,data.inAmount)
|
|
75
|
+
data.tau_pool = plus(data.tau_pool,data.inAmount)
|
|
76
|
+
data.usdt_pool = reduce(data.usdt_pool,data.outAmount)
|
|
77
|
+
|
|
78
|
+
}else {
|
|
79
|
+
data.outAmount = amount
|
|
80
|
+
let numerator = multiplication(multiplication(data.tau_pool,data.outAmount),10000)
|
|
81
|
+
let denominator = multiplication(reduce(data.usdt_pool,data.outAmount),9975)
|
|
82
|
+
let inAmount = saveNum(division(numerator,denominator),18)
|
|
83
|
+
data.inAmount = this.pointOffset({
|
|
84
|
+
amount:inAmount,
|
|
85
|
+
point
|
|
86
|
+
})
|
|
87
|
+
data.tau_balance = reduce(data.tau_balance,data.inAmount)
|
|
88
|
+
data.tau_pool = plus(data.tau_pool,inAmount)
|
|
89
|
+
data.usdt_pool = reduce(data.usdt_pool,data.outAmount)
|
|
90
|
+
}
|
|
91
|
+
console.log(`
|
|
92
|
+
<=======swap after amount =======>
|
|
93
|
+
${JSON.stringify(data,null,2)}
|
|
94
|
+
`)
|
|
95
|
+
|
|
96
|
+
// 历史累加输入输出
|
|
97
|
+
let amounts = await this.SC.methods.tokenAmounts(data.wallet,data.usdt).call()
|
|
98
|
+
amounts.outAmount = toSmall(amounts.outAmount,usdt_precision)
|
|
99
|
+
amounts.inAmount = toSmall(amounts.inAmount,usdt_precision)
|
|
100
|
+
// 获取手续费比率
|
|
101
|
+
let config = await this.SC.methods.rateAmount(data.tau,data.usdt).call()
|
|
102
|
+
config.feeRate = division(config.feeRate,this.pointMax)
|
|
103
|
+
|
|
104
|
+
data.feeAmount = multiplication(data.outAmount,config.feeRate)
|
|
105
|
+
// 获取扣除手续费后预计得到的usdt
|
|
106
|
+
let outAmount = reduce(data.outAmount,data.feeAmount)
|
|
107
|
+
// 计算当前兑换后的价格
|
|
108
|
+
let amountInWithFee = multiplication(1,9975)
|
|
109
|
+
let numerator = multiplication(amountInWithFee,data.usdt_pool)
|
|
110
|
+
let denominator = plus(multiplication(data.tau_pool,10000),amountInWithFee)
|
|
111
|
+
let price = saveNum(division(numerator,denominator),6)
|
|
112
|
+
|
|
113
|
+
// 判断盈亏
|
|
114
|
+
let totalIn = amounts.inAmount
|
|
115
|
+
let totalOut = plus(plus(amounts.outAmount,outAmount),multiplication(data.tau_balance,price))
|
|
116
|
+
if(parseFloat(totalIn) >= parseFloat(totalOut)) {
|
|
117
|
+
data.win = false
|
|
118
|
+
data.change = reduce(totalIn,totalOut)
|
|
119
|
+
}else {
|
|
120
|
+
data.win = true
|
|
121
|
+
data.change = reduce(totalOut,totalIn)
|
|
122
|
+
}
|
|
123
|
+
|
|
124
|
+
let limit = await this.limitBuy(data.usdt)
|
|
125
|
+
let tax = await this.SC.methods.taxConfig(data.tau,data.usdt).call()
|
|
126
|
+
tax.tax = division(tax.tax,this.pointMax)
|
|
127
|
+
tax.backBuy = division(tax.backBuy,this.pointMax)
|
|
128
|
+
console.log(tax)
|
|
129
|
+
console.log(limit)
|
|
130
|
+
if(data.win) {
|
|
131
|
+
if(parseFloat(limit.inMin) <= parseFloat(data.change) && parseFloat(data.change) > 0) {
|
|
132
|
+
if(parseFloat(data.change) >= parseFloat(outAmount)){
|
|
133
|
+
data.buyAmount = multiplication(outAmount,tax.backBuy)
|
|
134
|
+
data.taxAmount = multiplication(outAmount,tax.tax)
|
|
135
|
+
}else {
|
|
136
|
+
data.buyAmount = multiplication(data.change,tax.backBuy)
|
|
137
|
+
data.taxAmount = multiplication(data.change,tax.tax)
|
|
138
|
+
}
|
|
139
|
+
data.buyType = 0
|
|
140
|
+
data.realOutAmount = reduce(reduce(data.outAmount,data.buyAmount),data.taxAmount)
|
|
141
|
+
}else {
|
|
142
|
+
// data.realOutAmount = outAmount
|
|
143
|
+
data.buyAmount = multiplication(outAmount,tax.backBuy)
|
|
144
|
+
data.realOutAmount = reduce(data.outAmount,data.buyAmount)
|
|
145
|
+
}
|
|
146
|
+
}else {
|
|
147
|
+
data.buyAmount = multiplication(outAmount,tax.backBuy)
|
|
148
|
+
data.realOutAmount = reduce(data.outAmount,data.buyAmount)
|
|
149
|
+
if(parseFloat(limit.outMin) <= parseFloat(data.change) && parseFloat(data.change) > 0) {
|
|
150
|
+
data.backAmount = data.change
|
|
151
|
+
}
|
|
152
|
+
}
|
|
153
|
+
|
|
154
|
+
console.log(`totalIn:${totalIn}`)
|
|
155
|
+
console.log(`totalOut:${totalOut}`)
|
|
156
|
+
console.log(`
|
|
157
|
+
<=======swap complete amount =======>
|
|
158
|
+
${JSON.stringify(data,null,2)}`)
|
|
159
|
+
return data
|
|
160
|
+
}
|
|
161
|
+
|
|
162
|
+
|
|
163
|
+
pointOffset({ point,amount,isplus = true }) {
|
|
164
|
+
let result
|
|
165
|
+
if(isplus) {
|
|
166
|
+
result = division(multiplication(amount,plus(this.pointMax,point)),this.pointMax)
|
|
167
|
+
}else {
|
|
168
|
+
result = division(multiplication(amount,reduce(this.pointMax,point)),this.pointMax)
|
|
169
|
+
}
|
|
170
|
+
|
|
171
|
+
return result
|
|
19
172
|
}
|
|
20
173
|
|
|
21
174
|
pointHandle(amount, isplus = true) {
|
|
@@ -351,7 +504,7 @@ class AgentSwap extends HI {
|
|
|
351
504
|
|
|
352
505
|
|
|
353
506
|
|
|
354
|
-
async tradeConfig({ amount,path,side = 0,point = 1000 }) {
|
|
507
|
+
async tradeConfig({ amount,path,side = 0,point = 1000, target = "" }) {
|
|
355
508
|
let inToken = path[0]
|
|
356
509
|
let outToken = path[path.length - 1]
|
|
357
510
|
let IToken = new HERC20({ ...this.network, contract: inToken })
|
|
@@ -366,25 +519,47 @@ class AgentSwap extends HI {
|
|
|
366
519
|
} else {
|
|
367
520
|
inAmount = await this.computeAmount({ amount,path,side,point,format:true })
|
|
368
521
|
outAmount = await OToken.toAmount(amount)
|
|
522
|
+
inputAmount = await IToken.toAmount(amount)
|
|
369
523
|
}
|
|
370
|
-
let user = this.web3.eth.defaultAccount
|
|
371
|
-
|
|
524
|
+
let user = target ? target : this.web3.eth.defaultAccount
|
|
525
|
+
console.log([inToken,outToken,path,inputAmount,outAmount,user])
|
|
526
|
+
|
|
527
|
+
let result;
|
|
528
|
+
let decimals = await OToken.decimals()
|
|
529
|
+
try{
|
|
530
|
+
|
|
531
|
+
result = await this.SC.methods.fundRead(inToken,outToken,path,inputAmount,outAmount,user).call()
|
|
532
|
+
console.log(result)
|
|
533
|
+
|
|
534
|
+
result.feeAmount = toSmall(result.group.feeAmount,decimals)
|
|
535
|
+
result.outReal = toSmall(result.group.outAmount,decimals)
|
|
536
|
+
result.taxAmount = toSmall(result.group.taxAmount,decimals)
|
|
537
|
+
result.backAmount = toSmall(result.group.backAmount,decimals)
|
|
538
|
+
result.buyAmount = toSmall(result.group.buyAmount,decimals)
|
|
539
|
+
result.change = toSmall(result.group.change,decimals)
|
|
540
|
+
result.win = result.group.win
|
|
541
|
+
|
|
542
|
+
result.feeRate = division(result.config.feeRate,this.pointMax)
|
|
543
|
+
result.reward = division(result.config.reward,this.pointMax)
|
|
544
|
+
result.rewardAmount = multiplication(result.reward,toSmall(outAmount,decimals))
|
|
545
|
+
result.outAmount = toSmall(outAmount,decimals)
|
|
546
|
+
result.inAmount = inAmount
|
|
547
|
+
}catch(err){
|
|
548
|
+
console.log(err)
|
|
549
|
+
result = {}
|
|
550
|
+
result.outAmount = toSmall(outAmount,decimals)
|
|
551
|
+
result.inAmount = inAmount
|
|
552
|
+
result.feeAmount = 0
|
|
553
|
+
result.outReal = 0
|
|
554
|
+
result.taxAmount = 0
|
|
555
|
+
result.buyAmount = 0
|
|
556
|
+
result.change = 0
|
|
557
|
+
result.win = false
|
|
558
|
+
}
|
|
559
|
+
|
|
372
560
|
console.log(result)
|
|
373
561
|
|
|
374
|
-
|
|
375
|
-
result.feeAmount = toSmall(result.group.feeAmount,decimals)
|
|
376
|
-
result.outReal = toSmall(result.group.outAmount,decimals)
|
|
377
|
-
result.taxAmount = toSmall(result.group.taxAmount,decimals)
|
|
378
|
-
result.backAmount = toSmall(result.group.backAmount,decimals)
|
|
379
|
-
result.buyAmount = toSmall(result.group.buyAmount,decimals)
|
|
380
|
-
result.change = toSmall(result.group.change,decimals)
|
|
381
|
-
result.win = parseInt(result.group.win) == 1
|
|
382
|
-
|
|
383
|
-
result.feeRate = division(result.config.feeRate,this.pointMax)
|
|
384
|
-
result.reward = division(result.config.reward,this.pointMax)
|
|
385
|
-
result.rewardAmount = multiplication(result.reward,toSmall(outAmount,decimals))
|
|
386
|
-
result.outAmount = toSmall(outAmount,decimals)
|
|
387
|
-
result.inAmount = inAmount
|
|
562
|
+
|
|
388
563
|
return result
|
|
389
564
|
}
|
|
390
565
|
|
package/src/index.js
CHANGED
|
@@ -2,15 +2,17 @@
|
|
|
2
2
|
let AgentSwap = require("./agent-swap")
|
|
3
3
|
let SwapFactory = require("./swap-factory")
|
|
4
4
|
let SwapRouter = require("./swap-router")
|
|
5
|
-
|
|
5
|
+
let SwapLP = require("./swap-pair")
|
|
6
6
|
if (window) {
|
|
7
7
|
window.AgentSwap = AgentSwap
|
|
8
8
|
window.SwapFactory = SwapFactory
|
|
9
9
|
window.SwapRouter = SwapRouter
|
|
10
|
+
window.SwapLP = SwapLP
|
|
10
11
|
}
|
|
11
12
|
|
|
12
13
|
module.exports = {
|
|
13
14
|
AgentSwap,
|
|
14
15
|
SwapFactory,
|
|
15
|
-
SwapRouter
|
|
16
|
+
SwapRouter,
|
|
17
|
+
SwapLP
|
|
16
18
|
}
|
package/src/swap-pair.js
ADDED
|
@@ -0,0 +1,26 @@
|
|
|
1
|
+
let { HI, HERC20, HERC721 } = require("h-token-staking")
|
|
2
|
+
let { abi } = require("./abi/PancakePair.json")
|
|
3
|
+
|
|
4
|
+
let { plus, reduce, multiplication, division, saveNum, charCompare,longHandle,toSmall } = require("h-token-staking/src/utils")
|
|
5
|
+
|
|
6
|
+
|
|
7
|
+
class SwapLP extends HI {
|
|
8
|
+
constructor({ provider, account, rpcUrl, privateKey, contract }) {
|
|
9
|
+
super({ provider, account, rpcUrl, privateKey, contract, abi })
|
|
10
|
+
this.mainToken = "0x0000000000000000000000000000000000000000"
|
|
11
|
+
this.maxVal = "999000000000000000000000000000000000000000000000000"
|
|
12
|
+
}
|
|
13
|
+
|
|
14
|
+
|
|
15
|
+
async getReserves() {
|
|
16
|
+
let reserves = await this.SC.methods.getReserves().call()
|
|
17
|
+
let token0 = await this.SC.methods.token0().call()
|
|
18
|
+
let token1 = await this.SC.methods.token1().call()
|
|
19
|
+
return {
|
|
20
|
+
...reserves,
|
|
21
|
+
token0,token1
|
|
22
|
+
}
|
|
23
|
+
}
|
|
24
|
+
}
|
|
25
|
+
|
|
26
|
+
module.exports = SwapLP
|