h-agent-swap 3.1.2 → 3.1.5

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 CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "h-agent-swap",
3
- "version": "3.1.2",
3
+ "version": "3.1.5",
4
4
  "description": "agent-swap",
5
5
  "main": "src/index.js",
6
6
  "files": [
package/src/BERC20.js CHANGED
@@ -1,5 +1,6 @@
1
1
  let { HI } = require("h-token-staking")
2
2
  let { abi, bytecode } = require("./abi/ERC20Standard.json")
3
+ let OCMJSON = require("./abi/OCM.json")
3
4
  let { plus, reduce, multiplication, division, saveNum, charCompare,longHandle } = require("h-token-staking/src/utils")
4
5
 
5
6
  class BERC20 extends HI {
@@ -24,6 +25,7 @@ class BERC20 extends HI {
24
25
  }
25
26
  }
26
27
 
28
+
27
29
  }
28
30
 
29
31
 
@@ -0,0 +1,254 @@
1
+ let { HI, HERC20, HERC721 } = require("h-token-staking")
2
+ let Quoter = require("./Quoter")
3
+ let PancakeFactory = require("./PancakeFactory")
4
+ let { abi, bytecode } = require("./abi/Interaction.json")
5
+ let { plus, reduce, multiplication, division, saveNum, charCompare, longHandle } = require("h-token-staking/src/utils")
6
+
7
+ let Success = (data) => {
8
+ return {
9
+ code: 0,
10
+ data
11
+ }
12
+ }
13
+
14
+ let ERR = {
15
+ InsufficientBalance: {
16
+ code: 100,
17
+ msg: "Insufficient balance"
18
+ },
19
+ ApproveFailed: {
20
+ code: 101,
21
+ msg: "Approve failed"
22
+ },
23
+ InsufficientReserves: {
24
+ code: 102,
25
+ msg: "Insufficient reserves"
26
+ },
27
+ invalidNFT: {
28
+ code: 103,
29
+ msg: "invalid nft"
30
+ },
31
+ invalidTokenID: {
32
+ code: 104,
33
+ msg: "invalid tokenId"
34
+ },
35
+ InvalidLP: {
36
+ code: 105,
37
+ msg: "invalid liquidity"
38
+ },
39
+ }
40
+
41
+ class Interaction extends HI {
42
+ constructor({ provider, account, rpcUrl, privateKey, contract }) {
43
+ super({ provider, account, rpcUrl, privateKey, contract, abi })
44
+ this.mainToken = "0x0000000000000000000000000000000000000000"
45
+ this.maxVal = "999000000000000000000000000000000000000000000000000"
46
+
47
+ this.weth = ""
48
+ this.router = ""
49
+ this.usdt = ""
50
+ this.ocm = ""
51
+ this.usdtTreasury = ""
52
+ this.betPause = false
53
+
54
+ this.pointMax = "100000000"
55
+
56
+
57
+ this.point = 0.01
58
+
59
+ this.initContracts()
60
+
61
+
62
+ }
63
+
64
+ async initContracts() {
65
+ let route = ""
66
+ let chainId = await this.web3.eth.getChainId()
67
+ if (chainId == 56) {
68
+ route = "0x10ED43C718714eb63d5aA57B78B54704E256024E"
69
+ } else if (chainId == 97) {
70
+ route = "0xD99D1c33F9fC3444f8101754aBC46c52416550D1"
71
+ }
72
+ this.router = route
73
+ if(!!this.contract) {
74
+ await this.getRouter()
75
+ await this.getWETH()
76
+ await this.getUSDT()
77
+ await this.getOCM()
78
+ await this.getUsdtTreasury()
79
+ await this.getBetPause()
80
+ }
81
+ }
82
+
83
+ pointHandle(amount, isplus = true) {
84
+ let rate = 1
85
+ if (isplus) {
86
+ rate = plus(1, this.point)
87
+ } else {
88
+ rate = reduce(1, this.point)
89
+ }
90
+ amount = multiplication(amount, rate)
91
+ amount = saveNum(amount, 0)
92
+ return amount
93
+ }
94
+
95
+
96
+
97
+
98
+ async deploy({ usdt, ocm, usdtTreasury }) {
99
+ try {
100
+ let from = this.web3.eth.defaultAccount
101
+ // let chainId = await this.web3.eth.getChainId()
102
+ // let route = ""
103
+ // let quotev2 = ""
104
+
105
+ await this.initContracts()
106
+
107
+
108
+ let gasPrice = await this.GasPrice()
109
+ let result = await this.SC.deploy({
110
+ data: bytecode,
111
+ arguments: [this.router, usdt, ocm, usdtTreasury]
112
+ }).send({ from, gasPrice });
113
+ this.SC = result
114
+ return result
115
+ } catch (err) {
116
+ console.log(err)
117
+ }
118
+ }
119
+
120
+ async isOwner() {
121
+ let owner = await this.SC.methods.owner().call()
122
+
123
+ let isOwner = charCompare(owner, this.web3.eth.defaultAccount)
124
+ return isOwner
125
+ }
126
+
127
+ async manage(method, param = []) {
128
+ let isOwner = await this.isOwner()
129
+ if (isOwner) {
130
+ let parmas = await this.buildTx(method, param)
131
+ let result = parmas && await this.broadTx(parmas)
132
+ return result
133
+ } else {
134
+ console.log("非合约所有者调用")
135
+ }
136
+ return false
137
+ }
138
+
139
+
140
+ async setting({ route, usdt, ocm, usdtTreasury, betPause }) {
141
+ let result = await this.manage("setting", [route, usdt, ocm, usdtTreasury, betPause])
142
+ return result
143
+ }
144
+
145
+ async updateWhite({ address, onoff }) {
146
+
147
+ let params = [
148
+ address,
149
+ onoff
150
+ ]
151
+ let result = await this.manage("updateWhite", params)
152
+ return result
153
+ }
154
+
155
+ async extract({ token, amount, recipient }) {
156
+ let inToken = new HERC20({ ...this.network, contract: token })
157
+ amount = await inToken.toAmount(amount)
158
+
159
+ let result = await this.manage("extract", [inToken.contract,recipient, amount])
160
+ return result
161
+ }
162
+
163
+ async balanceOf(token) {
164
+ let result = await this.SC.methods.balanceOf(token).call()
165
+ let targetToken = new HERC20({ ...this.network, contract: token })
166
+ let amount = await targetToken.fromAmount(result)
167
+ return amount
168
+ }
169
+
170
+ async getRouter() {
171
+ if (!this.router) {
172
+ let result = await this.SC.methods.route().call()
173
+ this.router = result
174
+ }
175
+ return this.router
176
+ }
177
+
178
+ async getWETH() {
179
+ if (!this.weth) {
180
+ let result = await this.SC.methods.weth().call()
181
+ this.weth = result
182
+ }
183
+ return this.weth
184
+ }
185
+
186
+ async getUSDT() {
187
+ if (!this.usdt) {
188
+ let result = await this.SC.methods.usdt().call()
189
+ this.usdt = result
190
+ }
191
+ return this.usdt
192
+ }
193
+
194
+ async getOCM() {
195
+ if (!this.ocm) {
196
+ let result = await this.SC.methods.ocm().call()
197
+ this.ocm = result
198
+ }
199
+ return this.ocm
200
+ }
201
+ async getUsdtTreasury() {
202
+ if (!this.usdtTreasury) {
203
+ let result = await this.SC.methods.usdtTreasury().call()
204
+ this.usdtTreasury = result
205
+ }
206
+ return this.usdtTreasury
207
+ }
208
+
209
+ async getBetPause() {
210
+ let result = await this.SC.methods.betPause().call()
211
+ this.betPause = result
212
+ return this.betPause
213
+ }
214
+
215
+
216
+
217
+ async bet({ amount,expect }) {
218
+ let Token0 = new HERC20({ ...this.network, contract: this.usdt })
219
+ await Token0.tradeApprove(this.contract,amount)
220
+ amount = await Token0.toAmount(amount)
221
+ let ethAmount = 0
222
+ // if(token == "") {
223
+ // token = this.mainToken
224
+ // ethAmount = amount
225
+ // }
226
+
227
+ let parmas = await this.buildTx("bet", [
228
+ amount,
229
+ expect
230
+ ],ethAmount)
231
+ let result = parmas && await this.broadTx(parmas)
232
+ return result
233
+ }
234
+
235
+
236
+ async allocationSet(allocations = []) {
237
+ allocations = allocations.map(item => {
238
+ return [
239
+ item.path,
240
+ item.point,
241
+ item.rate,
242
+ item.to
243
+ ]
244
+ })
245
+ console.log(allocations)
246
+
247
+ let result = await this.manage("allocationSet", [allocations])
248
+ return result
249
+ }
250
+
251
+ }
252
+
253
+
254
+ module.exports = Interaction
package/src/OCM.js ADDED
@@ -0,0 +1,48 @@
1
+ let { HI } = require("h-token-staking")
2
+ let { abi, bytecode } = require("./abi/OCM.json")
3
+ let { plus, reduce, multiplication, division, saveNum, charCompare,longHandle } = require("h-token-staking/src/utils")
4
+
5
+ class OCM extends HI {
6
+ constructor({ provider, account, rpcUrl, privateKey, contract }) {
7
+ super({ provider, account, rpcUrl, privateKey, contract, abi })
8
+ }
9
+
10
+ async deploy({ name,symbol,decimal,amount }) {
11
+ try {
12
+ let from = this.web3.eth.defaultAccount
13
+ let gasPrice = await this.GasPrice()
14
+ amount = longHandle(amount,decimal)
15
+ amount = saveNum(amount,0)
16
+ let result = await this.SC.deploy({
17
+ data: bytecode,
18
+ arguments: [ name,symbol,amount,decimal ]
19
+ }).send({ from, gasPrice });
20
+ this.SC = result
21
+ return result
22
+ } catch (err) {
23
+ console.log(err)
24
+ }
25
+ }
26
+
27
+ async addPoolAddress({ address, onoff }) {
28
+ let params = [
29
+ address,
30
+ onoff
31
+ ]
32
+ let result = await this.manage("addPoolAddress", params)
33
+ return result
34
+ }
35
+
36
+ async addToWhitelist({ address, onoff }) {
37
+ let params = [
38
+ address,
39
+ onoff
40
+ ]
41
+ let result = await this.manage("addToWhitelist", params)
42
+ return result
43
+ }
44
+
45
+ }
46
+
47
+
48
+ module.exports = OCM
@@ -0,0 +1,22 @@
1
+ let { HI } = require("h-token-staking")
2
+ let { abi } = require("./abi/PancakeFactory.json")
3
+ let { plus,reduce,multiplication,division,saveNum } = require("h-token-staking/src/utils")
4
+
5
+ class PancakeFactory extends HI {
6
+ constructor({ provider, account, rpcUrl, privateKey, contract }) {
7
+ super({ provider, account, rpcUrl, privateKey, contract, abi })
8
+ }
9
+
10
+
11
+
12
+ async getPool({ token0,token1,fee }) {
13
+ fee = division(fee,100)
14
+ fee = multiplication(fee,1000000)
15
+ let result = await this.SC.methods.getPool(token0,token1,fee).call()
16
+ return result
17
+ }
18
+
19
+ }
20
+
21
+
22
+ module.exports = PancakeFactory
package/src/Quoter.js ADDED
@@ -0,0 +1,105 @@
1
+ let { HI, HERC20 } = require("h-token-staking")
2
+ let { abi } = require("./abi/QuoterV2.json")
3
+ let { plus,reduce,multiplication,division,saveNum } = require("h-token-staking/src/utils")
4
+
5
+ class Quoter extends HI {
6
+ constructor({ provider, account, rpcUrl, privateKey, contract }) {
7
+ // 0xb4a466911556e39210a6bb2faecbb59e4eb7e43d
8
+ super({ provider, account, rpcUrl, privateKey, contract, abi })
9
+ }
10
+
11
+ async quoteExactInput({ path,amount }) {
12
+ try{
13
+ console.log(path,amount)
14
+ let { amountOut } = await this.SC.methods.quoteExactInput(path,amount).call()
15
+ return amountOut
16
+ }catch(err){
17
+ console.log(err)
18
+ console.error("流动性不足")
19
+ return 0
20
+ }
21
+ }
22
+
23
+ async quoteExactInputMulti(list = [],amountIn) {
24
+ let amountOut = amountIn
25
+ if(list.length) {
26
+ for(let i = 0; i < list.length; i++) {
27
+ let result = await this.quoteExactInputSingle(list[i],amountOut)
28
+ let outToken = new HERC20({ ...this.network, contract: list[i].tokenOut })
29
+ amountOut = await outToken.fromAmount(result.amountOut)
30
+ }
31
+ }else {
32
+ throw "list.length > 0"
33
+ }
34
+ return amountOut
35
+ }
36
+
37
+ async quoteExactInputSingle({ tokenIn,tokenOut,fee },amountIn) {
38
+ try{
39
+ let inToken = new HERC20({ ...this.network, contract: tokenIn })
40
+ amountIn = await inToken.toAmount(amountIn)
41
+ fee = division(fee, 100)
42
+ fee = multiplication(fee, 1000000)
43
+ fee = saveNum(fee,0)
44
+ console.log([
45
+ tokenIn,tokenOut,amountIn,fee,0
46
+ ])
47
+ let result = await this.SC.methods.quoteExactInputSingle([
48
+ tokenIn,tokenOut,amountIn,fee,0
49
+ ]).call()
50
+ return result
51
+ }catch(err){
52
+ console.log(err)
53
+ return false
54
+ }
55
+ }
56
+
57
+
58
+ async quoteExactOutputMulti(list = [],amountOut) {
59
+ let amountIn = amountOut
60
+ if(list.length) {
61
+ for(let i = 0; i < list.length; i++) {
62
+ let result = await this.quoteExactOutputSingle(list[i],amountIn)
63
+ console.log(result)
64
+ let inToken = new HERC20({ ...this.network, contract: list[i].tokenIn })
65
+ amountIn = await inToken.fromAmount(result.amountIn)
66
+ }
67
+ }else {
68
+ throw "list.length > 0"
69
+ }
70
+ return amountIn
71
+ }
72
+
73
+ async quoteExactOutputSingle({ tokenIn,tokenOut,fee },amountOut) {
74
+ try{
75
+ let outToken = new HERC20({ ...this.network, contract: tokenOut })
76
+ amountOut = await outToken.toAmount(amountOut)
77
+ fee = division(fee, 100)
78
+ fee = multiplication(fee, 1000000)
79
+ fee = saveNum(fee,0)
80
+ let result = await this.SC.methods.quoteExactOutputSingle([
81
+ tokenIn,tokenOut,amountOut,fee,0
82
+ ]).call()
83
+ return result
84
+ }catch(err){
85
+ console.log(err)
86
+ return false
87
+ }
88
+ }
89
+
90
+ async quoteExactOutput({ path,amount }) {
91
+ try{
92
+ let { amountIn } = await this.SC.methods.quoteExactOutput(path,amount).call()
93
+ return amountIn
94
+ }catch(err){
95
+ console.log(err)
96
+ console.error("流动性不足")
97
+ return 0
98
+ }
99
+ }
100
+
101
+
102
+ }
103
+
104
+
105
+ module.exports = Quoter