-
Notifications
You must be signed in to change notification settings - Fork 0
/
tibetswap.ps1
428 lines (359 loc) · 16.5 KB
/
tibetswap.ps1
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
Function Build-UrlWithParameters {
<#
.SYNOPSIS
Build a url from a hashtable of parameters.
.DESCRIPTION
URL builder to help with REST api endpoints that use URL Query parameters.
.PARAMETER BaseUrl
The base url of the REST endpoint. This will be the first part of the website and should begin with https:// or http://.
.PARAMETER Parameters
This is a hashtable of key value pairs that represent the url querey parameters for the REST endpoint.
.EXAMPLE
Build-UrlWithParameters -BaseUrl "https://api.dexie.space/v1/offers" -Parameters @{"status"=4}
https://api.dexie.space/v1/offers?status=4
.EXAMPLE
Build-UrlWithParameters -BaseUrl "https://api.dexie.space/v1/offers" -Parameters @{"status"=4;"offered"="XCH"}
https://api.dexie.space/v1/offers?status=4&offered=XCH
.LINK
https://api.v2.tibetswap.io/
.OUTPUTS
[string]
.FUNCTIONALITY
This is a helper function for building uri
#>
param (
[string]$BaseUrl,
[hashtable]$Parameters
)
# Validate inputs
if (-not $BaseUrl) {
throw "BaseUrl is required."
}
if (-not $Parameters) {
return $BaseUrl
}
# Convert hashtable to query string
$queryString = ($Parameters.GetEnumerator() | ForEach-Object {
"$($_.Key)=$([System.Web.HttpUtility]::UrlEncode($_.Value))"
}) -join "&"
# Construct the full URL
if ($BaseUrl -match '\?') {
# If the base URL already ends with a '?', append parameters directly
$FullUrl = -join($BaseUrl,"&",$queryString)
} else {
# Otherwise, add '?' before the parameters
$FullUrl = -join($BaseUrl,"?",$queryString)
}
return $FullUrl
}
Function Get-TibetTokens {
<#
.SYNOPSIS
Retrieve a list of all traded tokens on TibetSwap
.DESCRIPTION
Retrieve a list of tokens with asset_id, (pair_id/launcher_id), name, short_name from TibetSwap API.
.OUTPUTS
[PSCustomObject]@{
asset_id: --CAT2 tail id for asset.
pair_id: --The TibetSwap id for traded pair. Also known as the launcher_id.
name: --Name of coin.
short_name: --Dexie CODE for coin
image_url: --Image for coin representation
verified: --Is Verified True/False
}
.EXAMPLE
Get-TibetTokens
Output:
[
{
"asset_id": "a628c1c2c6fcb74d53746157e438e108eab5c0bb3e5c80ff9b1910b3e4832913",
"pair_id": "1a6d4f404766f984d014a3a7cab15021e258025ff50481c73ea7c48927bd28af",
"name": "Spacebucks",
"short_name": "SBX",
"image_url": "https://nftstorage.link/ipfs/bafybeicyyqrk4llkvosnstdehby5pajumgzh7imvkt3dlywape65putcne/a628c1c2c6fcb74d53746157e438e108eab5c0bb3e5c80ff9b1910b3e4832913.png",
"verified": true
},
{
"asset_id": "db1a9020d48d9d4ad22631b66ab4b9ebd3637ef7758ad38881348c5d24c38f20",
"pair_id": "c0952d9c3761302a5545f4c04615a855396b1570be5c61bfd073392466d2a973",
"name": "dexie bucks",
"short_name": "DBX",
"image_url": "https://nftstorage.link/ipfs/bafybeidiupubzjznsxonxxjvrw3jeemwsb6lgtir23mgfqoeybwcnqljsi/db1a9020d48d9d4ad22631b66ab4b9ebd3637ef7758ad38881348c5d24c38f20.png",
"verified": true
}, ...
]
.LINK
https://api.v2.tibetswap.io/docs#/default/get_tokens_tokens_get
#>
$uri = 'https://api.v2.tibetswap.io/tokens'
Return Invoke-RestMethod -Method Get -Uri $uri
}
Function Get-TibetPairs {
<#
.SYNOPSIS
Get a list of all traded pairs on TibetSwap
.DESCRIPTION
This function allows you to page through the entire list of traded pairs on TibetSwap.
Each pair is a CAT2_XCH traded pair.
.EXAMPLE
Get-TibetPairs
launcher_id : 30a37ab26511c142e48c22a6de3141140e66b45135b85ff0b5b3c3ec7a62c271
asset_id : ccda69ff6c44d687994efdbee30689be51d2347f739287ab4bb7b52344f8bf1d
liquidity_asset_id : ba9750db89ef16feb3eabadbfb199509e3e1290851188e5625d0956ca1354b85
xch_reserve : 1218567060353797
token_reserve : 473838226946
liquidity : 488453915457
last_coin_id_on_chain : fde48c263183fa101a1d8c5e2fc47dd3145568411994555f4a5fdd984de644fd
launcher_id : ad79860e5020dcdac84336a5805537cbc05f954c44caf105336226351d2902c0
asset_id : e816ee18ce2337c4128449bc539fbbe2ecfdd2098c4e7cab4667e223c3bdc23d
liquidity_asset_id : 7ccf260808c1c3b8d87e02ea2f8df9cf71d793b62c6cb42bd654aa289410f9b5
xch_reserve : 343281258474997
token_reserve : 1817886086
liquidity : 2918214769
last_coin_id_on_chain : e05b49f2cceb9884a8b4ce73872538607d7d98938518b1070c628090d655452c
launcher_id : b62577aed2b09182f3db8b21049d687b597f1526fb88268c1b4f4dec4add28ec
asset_id : ec9d874e152e888231024c72e391fc484e8b6a1cf744430a322a0544e207bf46
...
.OUTPUTS
launcher_id: --The TibetSwap id for traded pair. Also known as the pair_id.
asset_id: --CAT2 tail id for asset.
liquidity_asset_id: --CAT2 tail for the liquidity token.
xch_reserve: --Amount of XCH held as Mojos in the smart coin.
token_reserve: --Amount of CAT2 held in Mojos in the smart coin.
liquidity: --Amout of liquidity tokens in circulation.
last_coin_id_on_chain: --The last coin id on chain.
.PARAMETER skip
Skips foward in query.
.PARAMETER limit
Limit number of results to display.
.EXAMPLE
Get-TibetPairs -limit 10 -skip 2
Output:
launcher_id : 30a37ab26511c142e48c22a6de3141140e66b45135b85ff0b5b3c3ec7a62c271
asset_id : ccda69ff6c44d687994efdbee30689be51d2347f739287ab4bb7b52344f8bf1d
liquidity_asset_id : ba9750db89ef16feb3eabadbfb199509e3e1290851188e5625d0956ca1354b85
xch_reserve : 1230784588317177
token_reserve : 469098040564
liquidity : 488453915457
last_coin_id_on_chain : f9a27d87d9ee642653b807445fdc0d7e2217b115aef420192c974b965fd7b1d9
launcher_id : ad79860e5020dcdac84336a5805537cbc05f954c44caf105336226351d2902c0
asset_id : e816ee18ce2337c4128449bc539fbbe2ecfdd2098c4e7cab4667e223c3bdc23d
liquidity_asset_id : 7ccf260808c1c3b8d87e02ea2f8df9cf71d793b62c6cb42bd654aa289410f9b5
xch_reserve : 342661258981470
token_reserve : 1821152282
liquidity : 2918214769
last_coin_id_on_chain : ceb5b60caed76623d8e1e31809e9adfd0c98995effe51bd2a4fa570c5eaa5ab9
.LINK
https://api.v2.tibetswap.io/docs#/default/read_pairs_pairs_get
#>
param(
[int]$skip,
[int]$limit
)
$Parameters = @{}
if($skip){
$Parameters.Add('skip',$skip)
}
if($limit){
$Parameters.Add('limit',$limit)
}
$uri = Build-UrlWithParameters -BaseUrl 'https://api.v2.tibetswap.io/pairs'
return Invoke-RestMethod -Method Get -Uri $uri
}
Function Get-TibetToken {
<#
.SYNOPSIS
Retrieve an individual traded token by asset_id
.DESCRIPTION
Retrieve a tokens with asset_id, (pair_id/launcher_id), name, short_name from TibetSwap API.
.EXAMPLE
Get-TibetToken -asset_id a628c1c2c6fcb74d53746157e438e108eab5c0bb3e5c80ff9b1910b3e4832913
Output:
asset_id : a628c1c2c6fcb74d53746157e438e108eab5c0bb3e5c80ff9b1910b3e4832913
pair_id : 1a6d4f404766f984d014a3a7cab15021e258025ff50481c73ea7c48927bd28af
name : Spacebucks
short_name : SBX
image_url : https://nftstorage.link/ipfs/bafybeicyyqrk4llkvosnstdehby5pajumgzh7imvkt3dlywape65putcne/a628c1c2c6fcb74d53746157e438e10
8eab5c0bb3e5c80ff9b1910b3e4832913.png
verified : True
.PARAMETER asset_id
The CAT2 tail id for the token.
.LINK
https://api.v2.tibetswap.io/docs#/default/get_token_token__asset_id__get
#>
param(
[string] $asset_id
)
$uri = -join('https://api.v2.tibetswap.io/token/',$asset_id)
Return Invoke-RestMethod -Method Get -Uri $uri
}
Function Get-TibetPair {
<#
.SYNOPSIS
Retrive trading pair data from a specific trading pair.
.DESCRIPTION
Retrives data on total locked value for a trading pair. The liquidiy values are displayed
in mojos not XCH notation by defult.
.PARAMETER launcher_id
The luancher_id is also know as the pair_id in the Get-TibetTokens endpoint.
.PARAMETER display_as_xch_notation
Convert the xch_reserve and token_reserve to XCH notation.
xch_reserve / 1000000000000
token_reserve / 1000
.EXAMPLE
Get-TibetPair -launcher_id 1a6d4f404766f984d014a3a7cab15021e258025ff50481c73ea7c48927bd28af
launcher_id : 1a6d4f404766f984d014a3a7cab15021e258025ff50481c73ea7c48927bd28af
asset_id : a628c1c2c6fcb74d53746157e438e108eab5c0bb3e5c80ff9b1910b3e4832913
liquidity_asset_id : fbe2694f6fbc63a4d9c8a598dc70f8b6fdf468e8e595ca4c6efee5a2f1ec71a4
xch_reserve : 138871205361620
token_reserve : 4694378021
liquidity : 6216902999
last_coin_id_on_chain : aa73c5ff4bfb6382b2457498e7eefb06a6d8730822f77d6930ce65614dd26ebc
.EXAMPLE
Get-TibetPair -launcher_id 1a6d4f404766f984d014a3a7cab15021e258025ff50481c73ea7c48927bd28af -display_as_xch_notation
launcher_id : 1a6d4f404766f984d014a3a7cab15021e258025ff50481c73ea7c48927bd28af
asset_id : a628c1c2c6fcb74d53746157e438e108eab5c0bb3e5c80ff9b1910b3e4832913
liquidity_asset_id : fbe2694f6fbc63a4d9c8a598dc70f8b6fdf468e8e595ca4c6efee5a2f1ec71a4
xch_reserve : 138.87120536162
token_reserve : 4694378.021
liquidity : 6216902999
last_coin_id_on_chain : aa73c5ff4bfb6382b2457498e7eefb06a6d8730822f77d6930ce65614dd26ebc
#>
param(
[string]$launcher_id,
[switch]$display_as_xch_notation
)
$uri = -join('https://api.v2.tibetswap.io/pair/',$launcher_id)
$response = Invoke-RestMethod -Method Get -Uri $uri
if($display_as_xch_notation.IsPresent){
$response.xch_reserve = [decimal]$response.xch_reserve / 1000000000000
$response.token_reserve = [decimal]$response.token_reserve / 1000
}
return $response
}
Function Get-TibetRouter {
Return Invoke-RestMethod -Method Get -Uri 'https://api.v2.tibetswap.io/router'
}
Function Get-TibetQuote {
<#
.SYNOPSIS
Retrive a trade quote from TibetSwap for a given pair_id (launcher_id).
.DESCRIPTION
Retrive an offer quote from TibetSwap. This will show what the AMM will accept as a trade.
.EXAMPLE
Retrieve a quote for 0.001 SpaceBucks
Get-TibetQuote -pair_id 1a6d4f404766f984d014a3a7cab15021e258025ff50481c73ea7c48927bd28af -amount_in 1
Output:
amount_in : 1
amount_out : 29375
price_warning : False
price_impact : 4.2305381420249E-10
fee :
asset_id : a628c1c2c6fcb74d53746157e438e108eab5c0bb3e5c80ff9b1910b3e4832913
input_reserve : 4694378021
output_reserve : 138871205361620
.EXAMPLE
Retrieve a quote for 0.000000000001 XCH worth of SpaceBucks
Get-TibetQuote -pair_id 1a6d4f404766f984d014a3a7cab15021e258025ff50481c73ea7c48927bd28af -amount_in 1 -xch_is_input
amount_in : 1
amount_out : 0
price_warning : False
price_impact : 0
fee :
asset_id : a628c1c2c6fcb74d53746157e438e108eab5c0bb3e5c80ff9b1910b3e4832913
input_reserve : 138871205361620
output_reserve : 4694378021
.EXAMPLE
Retrieve quote for 1 XCH worth of SpaceBucks
Get-TibetQuote -pair_id 1a6d4f404766f984d014a3a7cab15021e258025ff50481c73ea7c48927bd28af -amount_in 1 -xch_is_input -display_as_xch_notation
amount_in : 1
amount_out : 33328.88
price_warning : False
price_impact : 0.0141490807094357
fee :
asset_id : a628c1c2c6fcb74d53746157e438e108eab5c0bb3e5c80ff9b1910b3e4832913
input_reserve : 138871205361620
output_reserve : 4694378021
xch_notation : True
#>
param(
[string]$pair_id,
[Decimal]$amount_in,
[Decimal]$amount_out,
[switch]$xch_is_input,
[switch]$estimate_fee,
[switch]$display_as_xch_notation
)
$uri = -join('https://api.v2.tibetswap.io/quote/',$pair_id)
$Parameters = @{}
if($display_as_xch_notation.IsPresent){
if($xch_is_input.IsPresent){
$amount_in = $amount_in * 1000000000000
$amount_out = $amount_out * 1000
} else {
$amount_out = $amount_out * 1000000000000
$amount_in = $amount_in * 1000
}
}
if($amount_in){
$Parameters.Add('amount_in',$amount_in)
}
if($amount_out){
$Parameters.Add('amount_out',$amount_out)
}
if($xch_is_input){
$Parameters.Add('xch_is_input',1)
} else {
$Parameters.Add('xch_is_input',0)
}
if($estimate_fee.IsPresent){
$Parameters.Add('estimate_fee',1)
} else {
$Parameters.Add('estimate_fee',0)
}
$uri = Build-UrlWithParameters -BaseUrl $uri -Parameters $Parameters
$result = Invoke-RestMethod -Method Get -Uri $uri
if($display_as_xch_notation.IsPresent){
if($xch_is_input.IsPresent){
$result.amount_in = [decimal]($result.amount_in / 1000000000000)
$result.amount_out = [decimal]($result.amount_out / 1000)
} else {
$result.amount_out = [decimal]($result.amount_out / 1000000000000)
$result.amount_in = [decimal]($result.amount_in / 1000)
}
$result | Add-Member -Name xch_notation -Type NoteProperty -Value $true
}
return $result
}
Function Send-TibetOffer {
<#
.SYNOPSIS
Send TibetSwap an offer.
.DESCRIPTION
Send an offer to TibetSwap to get accepted. The offers can be to swap, add/remove liquidity.
.LINK
https://api.v2.tibetswap.io/docs#/default/create_offer_endpoint_offer__pair_id__post
.EXAMPLE
Send an offer to swap XCH for SpaceBucks
Send-TibetOffer -pair_id 1a6d4f404766f984d014a3a7cab15021e258025ff50481c73ea7c48927bd28af -offer offer1d24..... -action SWAP
success message offer_id
------- ------- --------
True {"status": "SUCCESS", "success": true} 3RR9miy92Vfxr1kgb72Z28sDMhU44BivD1nwkxRLPMHb
#>
param(
[Parameter(mandatory=$true)]
[string]$pair_id,
[Parameter(mandatory=$true)]
[string]$offer,
[Parameter(mandatory=$true)]
[Validateset("SWAP","ADD_LIQUIDITY","REMOVE_LIQUIDITY")]
[string]$action
)
$uri = -join('https://api.v2.tibetswap.io/offer/',$pair_id)
$body = @{
offer = $offer
action = $action
} | ConvertTo-Json
$contentType = 'application/json'
Invoke-RestMethod -Method Post -Uri $uri -Body $body -ContentType $contentType
}