-
Notifications
You must be signed in to change notification settings - Fork 15
/
swapr-tx-client.ts
487 lines (440 loc) · 16.2 KB
/
swapr-tx-client.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
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
const BigNum = require('bn.js')
import { readFileSync } from 'fs'
import {
makeSmartContractDeploy,
makeContractCall,
TransactionVersion,
FungibleConditionCode,
serializeCV,
deserializeCV,
standardPrincipalCV,
uintCV,
BooleanCV,
ListCV,
// @ts-ignore
PrincipalCV,
UIntCV,
ChainID,
makeStandardSTXPostCondition,
makeContractSTXPostCondition,
StacksTestnet,
broadcastTransaction,
PostConditionMode,
} from '@blockstack/stacks-transactions'
import {
wait,
waitForTX,
} from '../tx-utils'
import { replaceString } from '../utils'
export class SwaprTXClient {
keys: any
network: any
contract_name: string
token1: string
token2: string
constructor(token1, token2, keys, network) {
this.keys = keys
this.network = network
this.token1 = token1
this.token2 = token2
this.contract_name = `swapr-${token1}-${token2}`
}
async deployContract(keys_owner: any) {
const fee = new BigNum(14640)
const contract_swapr_body = replaceString(
replaceString(
replaceString(
replaceString(
replaceString(
replaceString(
replaceString(readFileSync('./contracts/swapr.clar').toString(),
'SP2NC4YKZWM2YMCJV851VF278H9J50ZSNM33P3JM1.my-token',
`${this.keys.stacksAddress}.${this.token1}`),
'SP1QR3RAGH3GEME9WV7XB0TZCX6D5MNDQP97D35EH.my-token',
`${this.keys.stacksAddress}.${this.token2}`),
'SP1XY88EQMX4CKK4VD7FGS235N6PASR0ACF68GK01',
`${this.keys.stacksAddress}`),
'SZ2J6ZY48GV1EZ5V2V5RB9MP66SW86PYKKQ9H6DPR',
`${keys_owner.stacksAddress}`),
'{{token1}}',
this.token1),
'{{token2}}',
this.token2),
'SP138CBPVKYBQQ480EZXJQK89HCHY32XBQ0T4BCCD.swapr',
`${this.keys.stacksAddress}.${this.contract_name}`
)
// console.log("contract_swapr_body", this.token1, this.token2, contract_swapr_body)
console.log(`deploying ${this.contract_name} contract with ${this.token1} ${this.token2}`)
const transaction_deploy_trait = await makeSmartContractDeploy({
contractName: this.contract_name,
codeBody: contract_swapr_body,
senderKey: this.keys.secretKey,
network: this.network,
fee,
// nonce: new BigNum(0),
})
const tx_id = await broadcastTransaction(transaction_deploy_trait, this.network)
const tx = await waitForTX(this.network.coreApiUrl, tx_id, 10000)
const result = deserializeCV(Buffer.from(tx.tx_result.hex.substr(2), "hex"))
return result
}
async addToPosition(x: number, y: number, params: { keys_sender: any }) {
console.log("addToPosition", params.keys_sender.stacksAddress, x, y)
const fee = new BigNum(256)
const transaction = await makeContractCall({
contractAddress: this.keys.stacksAddress,
contractName: this.contract_name,
functionName: "add-to-position",
functionArgs: [uintCV(x), uintCV(y)],
senderKey: params.keys_sender.secretKey,
network: this.network,
postConditionMode: PostConditionMode.Allow,
postConditions: [
// makeStandardSTXPostCondition(
// keys_sender.stacksAddress,
// FungibleConditionCode.Equal,
// new BigNum(amount)
// ),
// makeStandardFungiblePostCondition(
// ),
],
fee,
// nonce: new BigNum(0),
})
const tx_id = await broadcastTransaction(transaction, this.network)
const tx = await waitForTX(this.network.coreApiUrl, tx_id, 10000)
const result = deserializeCV(Buffer.from(tx.tx_result.hex.substr(2), "hex")) as ListCV
return result
}
async reducePosition(percent: number, params: { keys_sender: any }) {
console.log("reducePosition", params.keys_sender.stacksAddress, percent)
const fee = new BigNum(256)
const transaction = await makeContractCall({
contractAddress: this.keys.stacksAddress,
contractName: this.contract_name,
functionName: "reduce-position",
functionArgs: [uintCV(percent)],
senderKey: params.keys_sender.secretKey,
network: this.network,
postConditionMode: PostConditionMode.Allow,
postConditions: [
// makeStandardSTXPostCondition(
// keys_sender.stacksAddress,
// FungibleConditionCode.Equal,
// new BigNum(amount)
// ),
// makeStandardFungiblePostCondition(
// ),
],
fee,
// nonce: new BigNum(0),
})
const tx_id = await broadcastTransaction(transaction, this.network)
const tx = await waitForTX(this.network.coreApiUrl, tx_id, 10000)
const result = deserializeCV(Buffer.from(tx.tx_result.hex.substr(2), "hex")) as ListCV
return result
}
async swapXforY(dx: number, params: { keys_sender: any }) {
console.log("swapXforY", params.keys_sender.stacksAddress, dx)
const fee = new BigNum(256)
const transaction = await makeContractCall({
contractAddress: this.keys.stacksAddress,
contractName: this.contract_name,
functionName: "swap-x-for-y",
functionArgs: [uintCV(dx)],
senderKey: params.keys_sender.secretKey,
network: this.network,
postConditionMode: PostConditionMode.Allow,
postConditions: [
// makeStandardSTXPostCondition(
// keys_sender.stacksAddress,
// FungibleConditionCode.Equal,
// new BigNum(amount)
// ),
// makeStandardFungiblePostCondition(
// ),
],
fee,
// nonce: new BigNum(0),
})
const tx_id = await broadcastTransaction(transaction, this.network)
const tx = await waitForTX(this.network.coreApiUrl, tx_id, 10000)
const result = deserializeCV(Buffer.from(tx.tx_result.hex.substr(2), "hex")) as ListCV
return result
}
async swapYforX(dy: number, params: { keys_sender: any }) {
console.log("swapYforX", params.keys_sender.stacksAddress, dy)
const fee = new BigNum(256)
const transaction = await makeContractCall({
contractAddress: this.keys.stacksAddress,
contractName: this.contract_name,
functionName: "swap-y-for-x",
functionArgs: [uintCV(dy)],
senderKey: params.keys_sender.secretKey,
network: this.network,
postConditionMode: PostConditionMode.Allow,
postConditions: [
// makeStandardSTXPostCondition(
// keys_sender.stacksAddress,
// FungibleConditionCode.Equal,
// new BigNum(amount)
// ),
// makeStandardFungiblePostCondition(
// ),
],
fee,
// nonce: new BigNum(0),
})
const tx_id = await broadcastTransaction(transaction, this.network)
const tx = await waitForTX(this.network.coreApiUrl, tx_id, 10000)
const result = deserializeCV(Buffer.from(tx.tx_result.hex.substr(2), "hex")) as ListCV
return result
}
// read only
async positionOf(keys_owner: any, params: { keys_sender: any }) {
console.log("balanceOf with sender", keys_owner.stacksAddress, params.keys_sender.stacksAddress)
const function_name = "get-position-of"
const owner = serializeCV(standardPrincipalCV(keys_owner.stacksAddress))
const options = {
method: "POST",
headers: {
"Content-Type": "application/json",
},
body: `{"sender":"${params.keys_sender.stacksAddress}","arguments":["0x${owner.toString("hex")}"]}`,
}
const response = await fetch(`${this.network.coreApiUrl}/v2/contracts/call-read/${this.keys.stacksAddress}/${this.contract_name}/${function_name}`, options)
if (response.ok) {
const result = await response.json()
if (result.okay) {
const result_value = deserializeCV(Buffer.from(result.result.substr(2), "hex"))
const result_data = result_value as UIntCV
console.log(function_name, result_data)
// @ts-ignore
return result_data.value.value
} else {
console.log(result)
}
} else {
console.log("not 200 response", response)
}
}
// read only
async balances(params: { keys_sender: any }) {
console.log("balances with sender", params.keys_sender.stacksAddress)
const function_name = "get-balances"
const options = {
method: "POST",
headers: {
"Content-Type": "application/json",
},
body: `{"sender":"${params.keys_sender.stacksAddress}","arguments":[]}`,
}
const response = await fetch(`${this.network.coreApiUrl}/v2/contracts/call-read/${this.keys.stacksAddress}/${this.contract_name}/${function_name}`, options)
if (response.ok) {
const result = await response.json()
if (result.okay) {
const result_value = deserializeCV(Buffer.from(result.result.substr(2), "hex"))
const result_data = result_value as ListCV
// console.log(function_name, result_data)
// @ts-ignore
return [result_data.value.list[0].value, result_data.value.list[1].value]
} else {
console.log(result)
}
} else {
console.log("not 200 response", response)
}
}
// read only
async positions(params: { keys_sender: any }) {
console.log("balanceOf with sender", params.keys_sender.stacksAddress)
const function_name = "get-positions"
const options = {
method: "POST",
headers: {
"Content-Type": "application/json",
},
body: `{"sender":"${params.keys_sender.stacksAddress}","arguments":[]}`,
}
const response = await fetch(`${this.network.coreApiUrl}/v2/contracts/call-read/${this.keys.stacksAddress}/${this.contract_name}/${function_name}`, options)
if (response.ok) {
const result = await response.json()
if (result.okay) {
const result_value = deserializeCV(Buffer.from(result.result.substr(2), "hex"))
const result_data = result_value as UIntCV
console.log(function_name, result_data)
// @ts-ignore
return result_data.value.value
} else {
console.log(result)
}
} else {
console.log("not 200 response", response)
}
}
// read only
async balancesOf(keys_owner: any, params: { keys_sender: any }) {
console.log("balanceOf with sender", keys_owner.stacksAddress, params.keys_sender.stacksAddress)
const function_name = "get-balances-of"
const owner = serializeCV(standardPrincipalCV(keys_owner.stacksAddress))
const options = {
method: "POST",
headers: {
"Content-Type": "application/json",
},
body: `{"sender":"${params.keys_sender.stacksAddress}","arguments":["0x${owner.toString("hex")}"]}`,
}
const response = await fetch(`${this.network.coreApiUrl}/v2/contracts/call-read/${this.keys.stacksAddress}/${this.contract_name}/${function_name}`, options)
if (response.ok) {
const result = await response.json()
if (result.okay) {
const result_value = deserializeCV(Buffer.from(result.result.substr(2), "hex"))
const result_data = result_value as ListCV
// console.log(function_name, result_data)
// @ts-ignore
return [result_data.value.list[0].value, result_data.value.list[1].value]
} else {
console.log(result)
}
} else {
console.log("not 200 response", response)
}
}
// read only
async fees(params: { keys_sender: any }) {
console.log("balanceOf with sender", params.keys_sender.stacksAddress)
const function_name = "get-fees"
const options = {
method: "POST",
headers: {
"Content-Type": "application/json",
},
body: `{"sender":"${params.keys_sender.stacksAddress}","arguments":[]}`,
}
const response = await fetch(`${this.network.coreApiUrl}/v2/contracts/call-read/${this.keys.stacksAddress}/${this.contract_name}/${function_name}`, options)
if (response.ok) {
const result = await response.json()
if (result.okay) {
const result_value = deserializeCV(Buffer.from(result.result.substr(2), "hex"))
const result_data = result_value as ListCV
// console.log(function_name, result_data)
// @ts-ignore
return [result_data.value.list[0].value, result_data.value.list[1].value]
} else {
console.log(result)
}
} else {
console.log("not 200 response", response)
}
}
async setFeeTo(keys_collector: any, params: { keys_sender: any }) {
console.log("setFeeTo", keys_collector.stacksAddress, params.keys_sender.stacksAddress)
const fee = new BigNum(256)
const transaction = await makeContractCall({
contractAddress: this.keys.stacksAddress,
contractName: this.contract_name,
functionName: "set-fee-to-address",
functionArgs: [standardPrincipalCV(keys_collector.stacksAddress)],
senderKey: params.keys_sender.secretKey,
network: this.network,
postConditionMode: PostConditionMode.Allow,
postConditions: [
// makeStandardSTXPostCondition(
// keys_sender.stacksAddress,
// FungibleConditionCode.Equal,
// new BigNum(amount)
// ),
// makeStandardFungiblePostCondition(
// ),
],
fee,
// nonce: new BigNum(0),
})
const tx_id = await broadcastTransaction(transaction, this.network)
const tx = await waitForTX(this.network.coreApiUrl, tx_id, 10000)
const result = deserializeCV(Buffer.from(tx.tx_result.hex.substr(2), "hex")) as BooleanCV
return result
}
async resetFeeTo(params: { keys_sender: any }) {
console.log("resetFeeTo", params.keys_sender.stacksAddress)
const fee = new BigNum(256)
const transaction = await makeContractCall({
contractAddress: this.keys.stacksAddress,
contractName: this.contract_name,
functionName: "reset-fee-to-address",
functionArgs: [],
senderKey: params.keys_sender.secretKey,
network: this.network,
postConditionMode: PostConditionMode.Allow,
postConditions: [
// makeStandardSTXPostCondition(
// keys_sender.stacksAddress,
// FungibleConditionCode.Equal,
// new BigNum(amount)
// ),
// makeStandardFungiblePostCondition(
// ),
],
fee,
// nonce: new BigNum(0),
})
const tx_id = await broadcastTransaction(transaction, this.network)
const tx = await waitForTX(this.network.coreApiUrl, tx_id, 10000)
const result = deserializeCV(Buffer.from(tx.tx_result.hex.substr(2), "hex")) as BooleanCV
return result
}
async collectFees(params: { keys_sender: any }) {
console.log("collectFees", params.keys_sender.stacksAddress)
const fee = new BigNum(256)
const transaction = await makeContractCall({
contractAddress: this.keys.stacksAddress,
contractName: this.contract_name,
functionName: "collect-fees",
functionArgs: [],
senderKey: params.keys_sender.secretKey,
network: this.network,
postConditionMode: PostConditionMode.Allow,
postConditions: [
// makeStandardSTXPostCondition(
// keys_sender.stacksAddress,
// FungibleConditionCode.Equal,
// new BigNum(amount)
// ),
// makeStandardFungiblePostCondition(
// ),
],
fee,
// nonce: new BigNum(0),
})
const tx_id = await broadcastTransaction(transaction, this.network)
const tx = await waitForTX(this.network.coreApiUrl, tx_id, 10000)
const result = deserializeCV(Buffer.from(tx.tx_result.hex.substr(2), "hex")) as ListCV
return result
}
// read only
async getFeeTo(params: { keys_sender: any }) {
console.log("balanceOf with sender", params.keys_sender.stacksAddress)
const function_name = "get-fee-to-address"
const options = {
method: "POST",
headers: {
"Content-Type": "application/json",
},
body: `{"sender":"${params.keys_sender.stacksAddress}","arguments":[]}`,
}
const response = await fetch(`${this.network.coreApiUrl}/v2/contracts/call-read/${this.keys.stacksAddress}/${this.contract_name}/${function_name}`, options)
if (response.ok) {
const result = await response.json()
if (result.okay) {
const result_value = deserializeCV(Buffer.from(result.result.substr(2), "hex"))
const result_data = result_value as PrincipalCV
console.log(function_name, result_data)
return result_data.value.value
} else {
console.log(result)
}
} else {
console.log("not 200 response", response)
}
}
}