Algorithmic Execution Bot for Perpetual Protocol v1
This is a basic framework for an Algorithmic Execution Bot for Perpetual Protocol v1 and can be easily extended to work on multiple AMM venues depending on the developer's knowledge of blockchain and smart contracts development.
$ git clone https://github.com/d5shen/interstellar-algos.git
$ cd interstellar-algos
$ npm install
$ cp .env.production.sample .env.production
$ npm run build
If running in Windows Powershell, you need to install git and grep (through Gow or other ports of grep to windows)
You will need access to your own xDai node - both WebSocket and http JSON RPC. Check out QuickNode, GetBlock, or Ankr.
Provide your private keys in .env.production
:
WEB3_ENDPOINT=wss://your-xdai-websocket-endpoint
WEB3_ENDPOINT_RO=https://your-xdai-json-rpc-endpoint
# The private key must start with "0x" - add it if necessary (e.g. from private key exported from Metamask)
ARBITRAGEUR_PK=YOUR_WALLET_PRIVATE_KEY
Note the node endpoint defined in .env.production
must point to an xDai node. By default, xDai's official endpoint is used. You can also choose to use Quicknode, or spin up your own node. Ethereum nodes such as Infura or Alchemy will not work.
Edit the basic trading parameters in configs.json
:
{
"ammConfigMap": {
"SUSHI-USDC": {
"PERPFI_LEVERAGE": 10,
"MAX_SLIPPAGE_RATIO": 0.0050
},
"FTT-USDC": {
"PERPFI_LEVERAGE": 10,
"MAX_SLIPPAGE_RATIO": 0.0050
},
"...": {
"..."
}
}
}
- Deposit xUSDC for trading on Perpetual Protocol Exchange - xUSDC can be deposited into your wallet either via perp.exchange, or using the xDai Omni Bridge. xUSDC is the USDC token that has been transfered to xDai.
- Deposit xDAI in your wallet to pay for gas on xDai (note xDai gas fees are typically 1 Gwei and paid in xDAI, DAI tokens that have been transfered to xDai.)
You can run interstellar-algos
in *nix bash or Windows Powershell.
You will need two consoles - one for the server and one for the Command Line Interface (CLI).
$ npm run server
or
$ env $(cat .env.production | grep -v '#') npx ts-node --files src/index.ts
$ npm run cli
$ ./run.ps1
$ env $(cat .env.production | grep -v '#') npx ts-node --files src/ui/main.ts
The Time-Weighted Average Price (TWAP) Algo executes user's order based on a fixed time period sliced into multiple intervals.
To input a TWAP order in the CLI, the command pattern is:
INPUT> TWAP [Pair] [BUY|SELL] [USDC Notional] [Total Time (mins)] [Interval (mins)]
For example:
INPUT> TWAP SUSHI-USDC BUY 5000 120 10
This will buy 5000 USDC worth of SUSHI-USDC perp over 120 minutes, executing a small trade every 10 minutes
The Percentage-of-Volume (POV) Algo executes user's order based on a percentage of the pair's total volume traded a fixed time period on Perpetual Protocol
To input a POV order in the CLI, the command pattern is:
INPUT> POV [Pair] [BUY|SELL] [USDC Notional] [POV (decimal)] [Interval (mins)] [Max Clip Size (optional)]
For example:
INPUT> POV SUSHI-USDC BUY 10000 0.05 10 2000
This will buy 10000 USDC worth of SUSHI-USDC perp at 5% of the total SUSHI-USDC traded volume on Perpetual Protocol, with at least 10 minutes between each trade, and no more than a clip size of 2000 USDC. This means that if, within a 10 minute interval, 90,000 USDC volume was traded, the user should target 5% of that (4500 USDC) in the next clip but because a max clip size of 2000 USDC was specified, it will only trade 2000 USDC worth of SUSHI-USDC perp.