-
Notifications
You must be signed in to change notification settings - Fork 0
/
grid.ps1
418 lines (297 loc) · 12.4 KB
/
grid.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
<#
First Draft of an AMM.
DON'T USE This unless you know read through and understand what it does. you may lose money!
Trading pair launcher IDs on TibetSwap.
The full list can be pulled from the API: https://api.v2.tibetswap.io/pairs?skip=0&limit=10
Docs: https://api.v2.tibetswap.io/docs#/default/read_pairs_pairs_get
NOTES:
You need to make sure you have enough coins to trade with. You can use chia wallet coins list -i {wallet_id} to list your coins.
It's best if you split your coins into equal amounts based on how you're going to trade.
If you are trading in 0.2 xch steps, split your CAT to be 1-2% more than what 0.2 XCH will buy.
RUN::
Start-TradeForCoin -Coin HOA -xch_max 1 -xch_step 0.20 -max_time 60
. this will create 5 buy and 5 sell transactions for HOA coin for 0.2 XCH each and base the pricing off TibetSwap's current pricing.
. Assuming you have a Coin in your Wallet called HOA
. Supported Coins are HOA / SBX / DBX
. Add your own coins by using
. ** MAKE SURE THE $PAIRS NAME OF COIN MATCHES WHAT YOUR WALLET SHOWS **
. ** MAKE SURE THE $PAIRS NAME OF COIN MATCHES WHAT YOUR WALLET SHOWS **
. ** MAKE SURE THE $PAIRS NAME OF COIN MATCHES WHAT YOUR WALLET SHOWS **
TODO:
1. CREATE AUTOMATED WAY TO MAKE SURE YOU HAVE ENOUGH COINS FOR TRADING.
2. SET MAX LIMITS OF EXPOSURE FOR A COIN SO IT DOESN'T GO ON A BUYING/SELLING SPREE.
#>
<#
This file contains a helper function that will create offers.
#>
. ./offerhelper.ps1
# fee_charged is used to determine the spread between buy and sell offers. To keep competative against tibetswap, you need to offer less than 0.007 (0.07%)
$fee_charged = 0.006
# Default number of minutes in an offer
$offer_length = 60
# CAT Asset IDs
# You can get the coin asset_id from https://dexie.space/assets
$cats = @{
'DBX'='db1a9020d48d9d4ad22631b66ab4b9ebd3637ef7758ad38881348c5d24c38f20'
'SBX'='a628c1c2c6fcb74d53746157e438e108eab5c0bb3e5c80ff9b1910b3e4832913'
'HOA'='e816ee18ce2337c4128449bc539fbbe2ecfdd2098c4e7cab4667e223c3bdc23d'
}
# Trading pairs launcher_id for TibetSwap
# You can pull the launcher_id from https://api.v2.tibetswap.io/token/{asset_id}
# . ** MAKE SURE THE $PAIRS NAME OF COIN MATCHES WHAT YOUR WALLET SHOWS **
$pairs = @{
'DBX'='c0952d9c3761302a5545f4c04615a855396b1570be5c61bfd073392466d2a973'
'SBX'='1a6d4f404766f984d014a3a7cab15021e258025ff50481c73ea7c48927bd28af'
'HOA'='ad79860e5020dcdac84336a5805537cbc05f954c44caf105336226351d2902c0'
}
## Easy Rounding function instead of useing [Math]::round
Function round($number){
return [Math]::round($number)
}
# Pull token reserve data from tibetswap to establish the current state of the pool assets.
Function Get-PoolAssetsForId{
param(
$launcher_id
)
$uri = -join(' https://api.v2.tibetswap.io/pair/',$launcher_id)
$uri
$data = Invoke-RestMethod -uri $uri
return $data
}
# Get a quote for how many tokens are given out if you add XCH to the Pool.
# XCH is in full XCH notation and not mojos.
Function Add-XchToPool{
param(
$pool_data,
[decimal]$amount,
$fee_percentage
)
# Find the constant product of the AMM (A*B=K)
$k = $pool_data.token_reserve * $pool_data.xch_reserve
# Convert XCH to Mojos
$amount = $amount * 1000000000000
# Add the XCH to the current Pool xch_reserve
$tmpXch = $pool_data.xch_reserve + $amount
# Calculate how many tokens should be in the pool, then subtract it from what it has to find the amount of tokens to offer
$offerTokens = ($k / $tmpXch) - $pool_data.token_reserve
# Calculate fee from the offered tokens
$tmpfee = $offerTokens * (-1 * $fee_percentage)
# Calculate what will be offered after fee.
$offerTokens = round($offerTokens + $tmpfee)
#return an array of this information that can be used to create offers.
return [ordered]@{"tokens"=$offerTokens/1000;"fee"=($tmpfee/1000);"XCH"=($amount/1000000000000)}
}
# Get a quote for how many tokens to receive if you remove XCH from the pool.
Function Remove-XCHFromPool{
param(
#Expects input from Get-PoolAssetsForId
$pool_data,
[decimal]$amount,
$fee_percentage
)
# Find the constant product of the AMM (A*B=K)
$k = $pool_data.token_reserve * $pool_data.xch_reserve
# Convert XCH to Mojos
$amount = $amount * 1000000000000
# Remove the XCH to the current Pool xch_reserve
$tmpXch = $pool_data.xch_reserve - $amount
# Calculate how many tokens should be in the pool, then subtract it from what it has to find the amount of tokens to offer
$offerTokens = ($k / $tmpXch) - $pool_data.token_reserve
# Calculate fee from the offered tokens
$tmpfee = $offerTokens * (-1 * $fee_percentage)
# Calculate what will be offered after fee.
$offerTokens = round($offerTokens - $tmpfee)
#return an array of this information that can be used to create offers.
return [ordered]@{"tokens"=$offerTokens/1000;"fee"=($tmpfee/1000);"XCH"=($amount/1000000000000)}
}
# Calculate the new pool totals if you add CATs to the pool
Function Add-TokenToPool{
param(
$pool_data,
[int64]$amount,
$fee_percentage
)
# Find the constant product of the AMM (A*B=K)
$k = $pool_data.token_reserve * $pool_data.xch_reserve
# Convert CAT to Mojos
$amount = $amount*1000
# Add the CAT to the current Pool xch_reserve
$tmpToken = $pool_data.token_reserve + $amount
# Calculate how much XCH should be in the pool, then subtract it from what it has to find the amount of XCH to offer
$offerXch = ($k / $tmpToken) - $pool_data.xch_reserve
# Calculate fee from the offered tokens
$tmpfee = $offerXch * (-1 * $fee_percentage)
# Calculate what will be offered after fee.
$offerXch = round($offerXch + $tmpfee)
#return an array of this information that can be used to create offers.
return [ordered]@{'XCH'=($offerXch/1000000000000);'fee'=($tmpfee/1000000000000);"token"=$amount}
}
# This will build out the array of offer data needed to create all the offers.
# Coin is the coin you wish to trade.
# xch_max is how much XCH in total you want to trade with on each side of the offer.
# xch_steps is the size of offers you wish to create.
# Example: Create-TradingArrayForLauncherID -coin HOA -xch_max 4 -xch_stpes 1
# -- This will create 4 offers to buy and 4 offers to sell 1 XCH worth of HOA using the constant product model
Function Create-TradingArrayForLauncherId{
param(
$coin,
$xch_max,
$xch_steps
)
# Find the launcher ID from the Friendly Coin Name
$launcher_id = $pairs.$coin
# Make a placeholder table to hold offer/request amounts for the purchase and sell of token.
$table=@{}
$table.coin = $coin
$table.buy = @()
$table.sell = @()
# Get the current assets in TibetSwap for the pair
$pool_data = Get-PoolAssetsForId($launcher_id)
# error checking to make sure the step size is not larger than the maximum xch.
if($xch_max -ge $xch_steps){
# Setting the minimum number of offers
$min = 1
# Set the max number of offers. Making sure no wierd amount of offers are created.
$max = [Math]::Floor($xch_max / $xch_steps)
# Create array that increments up to the maximum number of steps
$range = $min .. $max
foreach($step in $range){
# Calculate how much XCH will be added or removed from the pool. Used to calculate how many CAT tokens to request/offer.
$xch = $step*$xch_steps
# Calculate what the pool looks like after xch removal
$buy = (Remove-XCHFromPool -pool_data $pool_data -amount $xch -fee_percentage $fee_charged )
# Counting the steps
if($step -gt 1){
# Get the amount of xch in mojos needed then convert to XCH notation.
$buy.XCH = [math]::round(($xch_steps + ([decimal](($step-1)/1000000000000))),12)
# Count Down the steps
$last_step = $step - 1
# Add result to table.
$buy.tokens = $buy.tokens - (($table.buy[0..$last_step].tokens | Measure-Object -Sum).Sum)
}
# Add to buy table
$table.buy += $buy
# Calculate what the pool looks like after xch addition
$sell = (Add-XchToPool -pool_data $pool_data -amount $xch -fee_percentage $fee_charged )
# Counting the steps
if($step -gt 1){
# Get the amount of xch in mojos needed then convert to XCH notation.
$sell.XCH = [math]::round(($xch_steps + ([decimal](($step-1)/1000000000000))),12)
# Count Down the steps
$last_step = $step - 1
# Add result to table.
$sell.tokens = ($sell.tokens - ($table.sell[0..$last_step].tokens | Measure-Object -Sum).Sum)
}
# Add to sell table
$table.sell += $sell
}
}
# Return the results of all calculations.
return $table
}
Function Clear-ExpiredOffers{
Write-Host "Clearing Old Offers"
$DateTime = (Get-Date).ToUniversalTime()
$timestamp = [System.Math]::Truncate((Get-Date -Date $DateTime -UFormat %s))
$offers = Get-AllOffers
$offers = $offers | Where-Object {$_.valid_times.max_time -le $timestamp}
foreach($offer in $offers){
Cancel-Offer -trade_id $offer.trade_id
}
}
function Cancel-Offer {
param (
$trade_id,
[switch]
$secure
)
$onchain=$false
if($secure.IsPresent){
$onchain=$true;
}
$json = @{
'trade_id'=$trade_id;
'secure'=$onchain
} | ConvertTo-Json
return (chia rpc wallet cancel_offer $json) | ConvertFrom-Json
}
Function Create-OffersForTradingArray{
param(
# Input from Create-TradingArrayForLauncherId
$trading_array,
# Input Time in Minutes
$max_time
)
# Set default lenth of time
if(-NOT $max_time.IsPresent){
$max_time = $offer_length
}
$bundle = @()
foreach($buy in $trading_array.buy){
# Build output for screen
$text = -join("Buy - ",$buy.tokens," ", $trading_array.coin," for ",$buy.XCH, " XCH")
# Show output on screen
Write-Output $text
# Create an offer instance
$offer = [ChiaOffer]::new()
# Offer XCH
$offer.offerxch($buy.XCH)
# Request CAT
$offer.requested($trading_array.coin,$buy.tokens)
# Add max Time
$offer.addTimeInMinutes($max_time)
# Create Offer
$offer.createoffer()
# Add info to Bundle variable
$bundle += ($offer.offertext | ConvertFrom-Json)
# Post to Dexie
$offer.postToDexie()
}
foreach($sell in $trading_array.sell){
# Build output for screen
$text = -join("Sell - ",$sell.tokens," ", $trading_array.coin," for ",$sell.XCH, " XCH")
# Show output on screen
Write-Output $text
# Create an offer instance
$offer = [ChiaOffer]::new()
# Make the tokens negative for RPC
$sell.tokens = $sell.tokens * -1
# Offer CAT
$offer.offered($trading_array.coin,$sell.tokens)
# Request XCH
$offer.requestxch($sell.XCH)
# Add max time
$offer.addTimeInMinutes($max_time)
# Create Offer
$offer.createoffer()
# Add to bundle variable
$bundle += ($offer.offertext | ConvertFrom-Json)
# Post to dexie
$offer.postToDexie()
}
# Output the bundle to screen
return $bundle
}
# Easy function for clearning old offers and creating new ones.
<#
If you wish to run this every 60 minutes you can do something like this
while($true){
Start-TradeForCoin -coin HOA -xch_max 4 -xch_steps 1 -max_time 60
# Start-Sleep takes times in seconds. I'm adding on an extra 30 seconds to give the process time to make sure everything is set.
Start-Sleep ((60*60)+30)
}
#>
Function Start-TradeForCoin{
param(
$coin,
$xch_max,
$xch_steps,
$max_time
)
# Clear Out Old Offers
Clear-ExpiredOffers
# Create the trading data array
$data = Create-TradingArrayForLauncherId -coin $coin -xch_max $xch_max -xch_steps $xch_steps
# Create the offers for trading data.
Create-OffersForTradingArray -trading_array $data -max_time $max_time
}