Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
180 commits
Select commit Hold shift + click to select a range
2d9d2a2
yarn.lock file update
Beastlorion Jul 8, 2023
6b0e050
fix prepare.js copyFileSync
Beastlorion Jul 8, 2023
2440cb1
fix maxtrade_amt typo
Beastlorion Jul 8, 2023
cdbf1b4
Changed portfolioRebalanceAtStart from Y/N to boolean
Beastlorion Jul 9, 2023
1a71277
Change portfolioRebalanceAtStart type from string to bool
Beastlorion Jul 9, 2023
cc8a145
removed loadbot, many changes in Abstract bot, created avax_usdc whic…
Beastlorion Jul 10, 2023
e639d62
initial addlimitorderlists working with getQty and price levels
Beastlorion Jul 10, 2023
435b99c
Move initial orders logic. Replaces all bids or asks when one of the …
Beastlorion Jul 11, 2023
e044a2e
initial orders, update orders, getQty, cancelReplace all working for …
Beastlorion Jul 13, 2023
03af3a0
bids and asks working. Can place and replace 10 orders at a time
Beastlorion Jul 13, 2023
d287d1c
rename bot to Market Maker Bot. Removed unnecessary timers. adjusted …
Beastlorion Jul 13, 2023
7173cb3
removed hardcoded % adjustments for replacement order testing
Beastlorion Jul 13, 2023
4e15464
fixed order level logging/tracking, getqty, getspread, and added configs
Beastlorion Jul 13, 2023
debb8a0
Order placing, updating, and tracking correctly. Set configuration in…
Beastlorion Jul 14, 2023
7ab37ec
added timestamps to orders. Checks if orders haven't been updated in …
Beastlorion Jul 15, 2023
acd45be
adjust startingbid and starting ask to avoid overlapping orders and p…
Beastlorion Jul 15, 2023
3c76b19
improve overlapping order avoidance in the order updater and added ti…
Beastlorion Jul 15, 2023
32cca8f
updated dependencies, fixed issues with duplicating orders and overla…
Beastlorion Jul 16, 2023
cb3dce5
change deploy script for production
Beastlorion Jul 16, 2023
8c7d620
changed interval to 15 seconds from accidental 150 seconds
Beastlorion Jul 16, 2023
5f5856c
fixed timestamps. Should be updating orders in memory correctly now
Beastlorion Jul 16, 2023
31baad8
check for level!=-1 and add logic for custom rpc if using production
Beastlorion Jul 16, 2023
4d09090
get best orders from live orderbook
Beastlorion Jul 17, 2023
c209604
add default urls for production
Beastlorion Jul 17, 2023
62958f2
increase gas price to +15% from +5%
Beastlorion Jul 17, 2023
4952ccf
add leniancy in order qty
Beastlorion Jul 17, 2023
addf83e
add delay for avax/usdt pair to avoid nonce conflicts with avax/usdc
Beastlorion Jul 17, 2023
89d3566
replace portfoliotot with portfoliototavail. :(
Beastlorion Jul 17, 2023
7b028ab
fixed getqty calculations
Beastlorion Jul 17, 2023
140a173
add nonce tracker
Beastlorion Jul 17, 2023
3df02c7
fixed pathing to absolute pathing for nonce tracker
Beastlorion Jul 17, 2023
43b2584
removed nonce tracker, wasn't working. fixed best bid and best ask.
Beastlorion Jul 17, 2023
9583b3d
fixed currentbestorders
Beastlorion Jul 17, 2023
d26d40a
retry if failed cancel/replace or cancelOrderList
Beastlorion Jul 18, 2023
54135c3
removed timestamp, as it wasn't really helping. removed some unecessa…
Beastlorion Jul 18, 2023
1ff6cf9
commented out some logger.errors that were shutting the bot down.
Beastlorion Jul 18, 2023
af760d4
If not enough funds to replace, cancel order so that it doesn't sit t…
Beastlorion Jul 18, 2023
80ea3c5
comment out logger.error
Beastlorion Jul 18, 2023
aafbd38
added promise error handling to getbestorders()
Beastlorion Jul 18, 2023
24d715d
remove unhandledRejection logic to end process
Beastlorion Jul 18, 2023
4154115
add timeout to retry cancelorderlist so it doesn't spam
Beastlorion Jul 18, 2023
2f6589b
move order sorting logic to order sorter. remove type 7
Beastlorion Jul 18, 2023
5bf59e2
add extra tries to cancel order and cancelReplace
Beastlorion Jul 18, 2023
0242cfe
add tries to addorder if issue with clientorderid
Beastlorion Jul 18, 2023
a4e34c4
fixed orderqty comparisons
Beastlorion Jul 18, 2023
934bac6
don't pass empty orderids to checkOrderOnChain
Beastlorion Jul 18, 2023
0cdad12
fix getqty available funds input
Beastlorion Jul 18, 2023
15e3ee4
cancel retry includes list
Beastlorion Jul 18, 2023
807d419
add delays and get new nonces for retries
Beastlorion Jul 18, 2023
c8e7d04
moved refresh logic to beginning of order updater instead of periodic…
Beastlorion Jul 19, 2023
c9e84fa
add try/catch for gasest functions
Beastlorion Jul 20, 2023
4d9ed2a
add try/catch to get gas price function
Beastlorion Jul 21, 2023
11b5c80
slight adjustments to getQty
Beastlorion Jul 21, 2023
ce971ed
fixed gasprice typo and adjust getqty to avoid out of funds errors
Beastlorion Jul 21, 2023
6ef0e73
fix getQty prices and processOpenOrders before closing orders when st…
Beastlorion Jul 21, 2023
2658c09
fix bug in placeinitalorders in bidsEnRoute
Beastlorion Jul 21, 2023
9d0f9b2
add .01% quantity buffer when recycling cancelreplace quanities
Beastlorion Jul 21, 2023
b6533ec
lower buffer
Beastlorion Jul 21, 2023
6d3f811
rehaul the available funds variable to efficiently place funds in orders
Beastlorion Jul 21, 2023
e9637ff
lower getqty partial buffer to .99 from .975
Beastlorion Jul 21, 2023
d1a9d99
remove break from replace bids
Beastlorion Jul 21, 2023
779f101
fixed issue with available fudns for addorder during replace orders
Beastlorion Jul 21, 2023
1e9ffd2
fix issue with adding order inside replace orders
Beastlorion Jul 21, 2023
f3f3f81
use startingbid price to calculate quantity instead of actual bid price
Beastlorion Jul 22, 2023
12e2bff
fix some issues with available funds variables in replacebids
Beastlorion Jul 22, 2023
7d14ebb
add slight buffer to bidamount
Beastlorion Jul 22, 2023
8d30ba0
pass in available funds instead of portfoliotot for bidqty calc
Beastlorion Jul 22, 2023
4d66491
fix bug in getqty
Beastlorion Jul 22, 2023
895f384
fix placeAsks availableFunds and portfolioavail. Removed portfoliotot
Beastlorion Jul 22, 2023
5b4cc06
give funds priority to further back orders instead of small close to …
Beastlorion Jul 27, 2023
c0700c8
fix bug in replace loops
Beastlorion Jul 27, 2023
a0df3db
give fund priority back to closer orders
Beastlorion Jul 27, 2023
9ad522a
add support for sAVAX/AVAX. Calls benqi savax contract for sAVAX rede…
Beastlorion Jul 28, 2023
20ff4e4
add TakerBot, fix code in BotFactory
Beastlorion Aug 3, 2023
c1dca21
fix bug with decimal rounding on asks
Beastlorion Aug 3, 2023
2de2419
add exception for savax/avax in getBestOrders() because for some reas…
Beastlorion Aug 3, 2023
a06859b
fix bestorder price adjustments
Beastlorion Aug 3, 2023
80f6d3f
remove comment
Beastlorion Aug 3, 2023
0165d30
add timestamp lastUpdated for marketMaker to replace orders
Beastlorion Aug 5, 2023
41fd81f
round to decimal before deciding whether to adjust bid/ask price due …
Beastlorion Aug 5, 2023
b638717
adjust savax price buffer
Beastlorion Aug 5, 2023
eea6e52
adjust savax price buffer to .9979
Beastlorion Aug 5, 2023
e6b59dc
use rounded prices for comparing for replacing orders
Beastlorion Aug 5, 2023
58dbee8
decrease savax discount to .15%
Beastlorion Aug 16, 2023
5c5031a
switching savax to custom price feed
Beastlorion Aug 16, 2023
fa98e63
add bot type using order lists instead of cancelreplace
Beastlorion Aug 25, 2023
d66129f
remove filepath file and comment out reference in abstract bot
Beastlorion Aug 25, 2023
8686056
remove lastnonce
Beastlorion Aug 25, 2023
396055c
hardcode gas allowance for cancelreplace
Beastlorion Aug 25, 2023
96aa9ad
add taker option
Beastlorion Sep 4, 2023
7c177f9
use max trade amount for taker trades if over max trade limit
Beastlorion Sep 4, 2023
c7d9317
use mintradeamount for taker trades
Beastlorion Sep 4, 2023
502ef71
lower interval back to 15 seconds
Beastlorion Sep 5, 2023
301b877
fix lastupdate variable in marketmakerbot
Beastlorion Sep 5, 2023
5ff4f84
fix taker bid amount
Beastlorion Sep 6, 2023
68a8f0b
parsefloat for portfoliotot
Beastlorion Sep 6, 2023
0de363d
fix maxtradeamount calculation for taker trades
Beastlorion Sep 8, 2023
afbc416
add try catch to get revert reason
Beastlorion Sep 11, 2023
982a7d2
fix typo in replacebids inputs
Beastlorion Sep 18, 2023
6c80270
rounding away from market price to avoid post only errors
Beastlorion Sep 18, 2023
0f040c7
add rounding for starting prices
Beastlorion Sep 18, 2023
06960c6
adjust comparisons to avoid post only errors
Beastlorion Sep 18, 2023
bd3ba49
fix rounding order prices
Beastlorion Sep 21, 2023
3de037a
adjust ask price by minimum increment if same as bid price
Beastlorion Sep 21, 2023
0526708
fix replacebids price
Beastlorion Sep 21, 2023
c5b07a2
Update README.md for customized_beastlorion
Beastlorion Oct 6, 2023
3b70576
Update README.md
Beastlorion Oct 6, 2023
cb957dd
add comments, remove some deprecated variables, removed takerbot
Beastlorion Oct 7, 2023
5ecffca
Merge branch 'main' of https://github.com/Beastlorion/dexalot_sampleb…
Beastlorion Oct 7, 2023
7073527
Update README.md
Beastlorion Oct 7, 2023
58655b5
Update README.md
Beastlorion Oct 7, 2023
64c00bd
Merge branch 'customized_beastlorion' of https://github.com/Beastlori…
Beastlorion Oct 7, 2023
8296f19
merge readme
Beastlorion Oct 7, 2023
c1ebe64
remove unnecessary code from marketMakerBotOrderLists, update readme,…
Beastlorion Oct 7, 2023
17d4934
Update README.md
Beastlorion Oct 9, 2023
362b9d9
Update README.md
Beastlorion Oct 9, 2023
dacdad3
fix taker bid amount
Beastlorion Oct 27, 2023
43e4447
Merge branch 'main' of https://github.com/Beastlorion/dexalot_samplebot
Beastlorion Oct 27, 2023
caf686a
add defensiveSkew to config + getBidSpread() + getAskSpread()
Beastlorion Oct 29, 2023
57159bc
add slip for volatile markets and add defensiveSkew config to readme
Beastlorion Oct 29, 2023
d368b92
Merge branch 'customized_beastlorion'
Beastlorion Oct 29, 2023
0ec3f9d
lower leeway in orderamounts by 10x
Beastlorion Oct 29, 2023
eb5b42a
refactoring bid ask spreads, fixing related bugs
Beastlorion Oct 29, 2023
72a4957
give back some leeway to amount to replace
Beastlorion Oct 29, 2023
df7ee75
add fix for taker buy amounts
Beastlorion Oct 30, 2023
d339bcf
use addorderList function. also add warning if addorder fails without…
Beastlorion Oct 30, 2023
0ef33bd
add retrigger in case orders were not placed at ideal prices
Beastlorion Oct 31, 2023
0ca87d2
add retrigger conditions to skip if best prices haven't changed
Beastlorion Oct 31, 2023
4e31002
fix bug in skip retrigger
Beastlorion Oct 31, 2023
1415f33
remove extra retrigger conditions
Beastlorion Oct 31, 2023
895259f
only retrigger when has enough funds to bother, implies within price …
Beastlorion Oct 31, 2023
b026a0a
move retrigger to abstract class, set to true if failed to cancelreplace
Beastlorion Oct 31, 2023
3ab4e59
add slip config
Beastlorion Nov 1, 2023
5597964
add useretrigger config
Beastlorion Nov 1, 2023
c9c0960
fix bug with retrigger continuing to fire when set to false
Beastlorion Nov 10, 2023
85e616b
slip = lastchange/2
Beastlorion Nov 14, 2023
0b25689
implement independentOrders and hardcode addOrderLimitList gas
Beastlorion Nov 15, 2023
8799a37
C:/Program Files/Git/100 for independent spreads
Beastlorion Nov 15, 2023
a7ed711
check for useIndependentLevels
Beastlorion Nov 16, 2023
9e19085
add slip and defensiveSkew for marketMakerLists
Beastlorion Nov 16, 2023
de064c8
remove cleanUpAndExit call when failing to place order list
Beastlorion Nov 16, 2023
57f4292
add variable orderRefreshTime
Beastlorion Nov 18, 2023
0fa402c
update readme environment variables
Beastlorion Nov 18, 2023
7ac9f73
add analytics bot
Beastlorion Nov 29, 2023
8478442
adding additional analytics fields and calculations
Beastlorion Nov 29, 2023
e5d8b9e
more analytics
Beastlorion Nov 29, 2023
b94af75
add custom dates
Beastlorion Nov 29, 2023
37a73fe
concat order rows and use pages
Beastlorion Nov 29, 2023
8535a50
fix concat
Beastlorion Nov 29, 2023
3b7ba44
remove page limit but add wait
Beastlorion Nov 29, 2023
f6f95c3
change logging, add new fields, shorten wait time for records, add pa…
Beastlorion Nov 29, 2023
83d5d93
remove wait time
Beastlorion Nov 29, 2023
0143f2e
change number of rows logic
Beastlorion Nov 29, 2023
d5720f9
change counter logic
Beastlorion Nov 29, 2023
b3b9599
use promises instead of waiting for each set of records to come back
Beastlorion Nov 29, 2023
e2cbb92
fix typo
Beastlorion Nov 29, 2023
b334df8
correct maker and taker fills
Beastlorion Nov 29, 2023
4475570
try to work around max records returned
Beastlorion Nov 29, 2023
ee9afaa
add shift
Beastlorion Nov 29, 2023
2f289a0
remove limit
Beastlorion Nov 29, 2023
78d5bdd
remove process exit
Beastlorion Nov 29, 2023
1039234
fix bug triggered when using negative spreads
Beastlorion Dec 2, 2023
20b0cb7
Merge branch 'customized_beastlorion'
Beastlorion Dec 2, 2023
eaf29b7
Merge branch 'analytics'
Beastlorion Dec 2, 2023
f1d96ab
use while loop and async instead of calling recursively in finally
Beastlorion Jan 23, 2024
1787f35
remove unused variable from abstract bot
Beastlorion Feb 13, 2024
2680b57
fix readme typo. Change defensiveSkew logic
Beastlorion Feb 13, 2024
afce066
fix bug in new defensiveSkew
Beastlorion Feb 13, 2024
ed15f22
add tires to cancel all to avoid spamming
Beastlorion Mar 6, 2024
eaaa4c3
don't set lastChange if lastMarketPrice was 0
Beastlorion Mar 16, 2024
fda7832
add support for volatility spreads
Beastlorion Mar 16, 2024
9ab60ca
fix cancelOrderList inputs
Mar 21, 2024
7fca979
undo previous commit and fix cancelAll inputs
Beastlorion Mar 21, 2024
f00bc0e
add support for memecoins
Beastlorion Mar 22, 2024
5dd0bea
only use avalanche token contracts
Beastlorion Apr 5, 2024
3783f47
use avalanche environment
Beastlorion Apr 5, 2024
bdc3370
fix bug with environments selection
Beastlorion Apr 5, 2024
bf00cea
fix cancelOrderList inputs in lists version
Beastlorion Apr 13, 2024
1db22e9
use subnet symbols for getTokenDetails
Beastlorion Apr 15, 2024
0e92f29
use subnet symbols for balances
Beastlorion Apr 15, 2024
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -6,4 +6,4 @@
Procfile
.env.*
package-lock.json
/logs-*
/logs-*
139 changes: 112 additions & 27 deletions README.md
Original file line number Diff line number Diff line change
@@ -1,58 +1,143 @@
## Sample Bot
# Beastlorion's Dexalot MarketMaker

## Setup
```
nvm install 16.15.1
yarn install
```

make sure to have the .env.fuji in the following. You can supply the private key encrypted or plain text
You can get AVAX from fuji faucet and sell your avax against USDC at Dexalot Fuji at https://app.dexalot-test.com
if you need more USDC please contact the Dexalot Team.

Config parameters are declared in .env.production for production or .env.fuji for testnet.
For production, I recommend using a secrets manager for your wallet's private key for enhanced security. Also if you're using a remote server, which you probably should be, you should whitelist the IP that you will be connecting to it from.

Paste this into your .env.fuji with your private key and address. PLEASE USE A TEST WALLET.

{
"bot_id" : "100",
"pair" : "AVAX/USDC",
"bot_type" : "SampleBot",
"private_key" : "51dda7beacb289d7eaaed575c3aeexxxxxxxxxxxxxx",
"account_no" : "0x051A4F2EBFb9d57D3655581xxxxxxxxxxxxx"
```
{ "bot_id" : "100",
"bot_type" : "MarketMakerBot",
"private_key" : "xxx",
"account_no" : "xxx",
"rpc_url" : "",
"ENCYRPT_SECRET" : "secret",
"ENCRYPT_SALT" : "salt",
"AVAX/USDC" :
{
"bidSpread" : "0.3",
"askSpread" : "0.3",
"takerSpread" : "1.0",
"takerEnabled" : false,
"flatAmount" : "1",
"orderLevels" : "2",
"orderLevelSpread" : "0.30",
"orderLevelQty" : "0.5",
"refreshOrderTolerance" : "0.05",
"refreshOrderTime" : "0",
"defensiveSkew" : "0",
"slip" : false,
"useIndependentLevels" : false,
"independentLevels":
{
"2":{
"customQty" : "2",
"customSpread": "0.4",
"tolerance": "0.3"
},
"3":{
"customQty" : "4",
"customSpread": "1",
"tolerance": "0.75"
}
}
}
}
```

To trade other markets(some are not active on the testnet), you will need to add objects for the tokens below the "AVAX/USDC" object. They are case sensitive.

You can get AVAX from fuji faucet and sell your avax against USDC at Dexalot Fuji at https://app.dexalot-test.com
Examples of other markets:

if you need more USDC please contact the Dexalot Team.
```
"AVAX/USDt"
"EUROC/USDC"
"USDt/USDC"
"WETH.e/USDC"
"BTC.b/USDC"
"sAVAX/AVAX"
```

## To run
- You will need to start a price_feeds instance which serves price data to a local port: https://github.com/Beastlorion/dexalotBot_price_feeds
- You can edit the price calculations there if you'd like.
- Once that is running on the same machine, run one of the following with your pair of choice:

MarketMakerLists (RECOMMENDED for beginners)
Fuji Testnet:
```
yarn sampleBot-fuji
yarn marketMakerLists-fuji --pair="AVAX/USDC"
```

## Flow
Production:
```
yarn marketMakerLists-prod --pair="AVAX/USDC"
```

This sample Bot extends the AbstractBot implementation. AbstractBot performs various functions:
Alternatively, if you have a custom rpc node, use this version to keep your orders active at all times:
Fuji Testnet:
```
yarn marketMaker-fuji --pair="AVAX/USDC"
```

Production:
```
yarn marketMaker-prod --pair="AVAX/USDC"
```


## Abstract Class Flow
AbstractBot performs various functions:
- Get the mainnet/subnet environments, pairs listed, token details from the RESTAPI
- Create references to the necessary contracts
- Deposit initial amounts to the contract from the mainnet for trading (Note it takes about 30 seconds for the deposit to reach the subnet)
- (unused currently) Deposit initial amounts to the contract from the mainnet for trading (Note it takes about 30 seconds for the deposit to reach the subnet)
- Requests open orders from the RESTAPI in case of crash recovery
- Keeps a list of its outstanding orders, and a local orderbook in memory
- Gets the best 2 bid/asks orderbook from the chain
- It listens to OrderStatusChanged events from the blockchain in case one of its outstading orders gets hit/lifted.
- it also captures OrderStatusChanged event as a part of tx results when sending an order and updates the order status in memory.
The OrderStatusChanged event raised from the blockchain to all the listeners a few seconds later. So the same event is processed twice.
Once when the order is sent out and again when it is received from the blockchain by the independent listener thread.
Hence it is normal to see the message "Duplicate Order event: ......"
(I've commented these messages out ->)Hence it is normal to see the message "Duplicate Order event: ......"
- Double Checks the order status from the chain every 10 min, in case an OrderStatusChanged event is missed.


NOTE:
Sample Bot has hardcoded ALOT price in its getNewMarketPrice() function and will send orders using these hardcoded prices.

Feel free to change the sampleBot and/or add a new bot type that extends AbstractBot. if it is a new bot , add it to the BotFactory.ts and the bot_type in the above .env.fuji file

## TO DO
Listen to tradePairs' Executed event (trades) from the blockchain OR
Web Socket Connection to Dexalot APIs to receive the orderbook, and trades events instead of getting them from the blockchain.

- When placing, replacing, or canceling orders, if there is an error, it may attempt to try again once or twice.

## MarketMakerBot Flow
MarketMakerBot extends the AbstractBot implementation. It holds the main logic for the bot:
- Fetches prices for the chosen market from the price_feed bot in getNewMarketPrice()
- Places initial PostOnly Orders using addLimitOrderList() and starts the order updater loop. If the orderLevels config is > 1, it will attempt to add an additional bid and ask with increased quantity based on the orderLevelsQty config
- orderUpdater() will run if certain conditions are met. During initialization it will run.
- The interval variable determines how long to wait after executing orders before calling orderUpdater() again. If orderUpdater did not run, it will retrigger in a few seconds, after it fetches fresh data.
- If the conditions are met, it will refresh balances, get the best bid and ask prices, and refresh the nonce of the wallet.
- Next it checks for the order levels of the orders and sorts them. If there are duplicate records somehow, it will cancel the offending orders and they will be added again on the next loop.
- If the "marketTakerEnabled" config == true AND the best bid or ask is passed the price threshold "takerSpread" config, it will trigger an "immediate or cancel" trade at marketPrice * (1 +/- takerSpread)
- If the conditions for a taker trade are not met, it will begin refreshing the prices and quantities of the orders for the pair taking care to avoid PostOnly trade conflicts.
- When it is finished it will set a timer and then begin calling orderUpdater() again.

## MarketMakerBotOrderLists (Recommended if not using your own rpc node)
This bot is an alternative to MarketMakerBot. The key difference is instead of replacing individual orders, it will cancel all of the active orders in one call, wait for confirmation, and then place all fresh orders in one call.

Pros:
- Uses far fewer calls, which is good for avoiding rate limitting. (If you're serious about this you'll want to start your own rpc node for unlimitted rpc calls. You can add a custom rpc url to the config file)
- The logic is much simpler and easier to understand.
- Allows for many orders for each pair with lower likelihood of errors. You could have 10 or more bids and sells active for each market.

Cons:
- Your orders will be off the books for about 6-8 seconds each time they need to be cancelled and replaced.
- Currently does not have taker orders enabled.

## DISCLAIMER

This bot is provided for facilitating the integration with Dexalot. It should not be used in PRODUCTION environments unless it is throughly tested.
I am sharing this bot for free to encourage people to try providing liquidity for Dexalot Exchange. It is still a work in progress. I assume no responsibility for you or others using this.
USE AT YOUR OWN RISK

Good luck! :D
Loading