How to Run an AtomicSwap Market-Making Bot

3 144
Avatar for SmartBCH
1 year ago

Running a Market-Making Bot can help maintaining the 1:1 peg between BCH and SBCH and get some earnings. We have tried our best to make it robust and secure. This article introduces how to run it in a robust and secure way.

Parameters

First of all, you need to decide some parameters which your bot and its counterparties must abide by. They are:

  1. bch-lock-time: how many bch blocks must go by before a locked fund can be refunded. Recommended value: 144, which means 24 hours.

  2. penalty-bps: if the counterparty failed to unlock the fund, it will be slashed with a penalty (in base points). Recommended value: 100-500 (1% - 5%).

  3. fee-bps: the counterparty will be charged with a fee for a successful swap (in base points). Recommended value: 10 - 30 (0.1% - 0.3%)

  4. min-swap-amt: the minimum amount for a swap. Recommended value: 0.01~0.1BCH

  5. max-swap-amt: the maximum amount for a swap. Recommended value: 1~10BCH

  6. bch-confirmations: the minimum confirmations that a BCH mainchain locking transaction must reach before it can be served by your bot. Recommended value: 6~10.

Prepare a cloud machine and start full node clients

It's suggested to run a bitcoincashnode full-node client and a smartbchd full-node client on your own cloud machine, which provide trustworthy information to your bot. An azure instance DC2sv3 with 16GB ram will be a best fit because it also supports SGX. The bot can work in an SGX enclave for better protection of your private keys. The clients need about 600GB SSD space.

If you do not want to use an SGX instance because it's more expensive, any machine with 16GB ram and 600GB SSD will do.

Then you can start bitcoincashnode and smartbchd.

Prepare accounts on the mainchain and the sidechain

The bot uses a mainchain account and a sidechain account to send transactions. So you must prepare these accounts' private keys and send some BCH to them.

It's suggested to use ElectronCash to generate the WIF private key for the mainchain account and use MetaMask to generate the HEX private key for the sidechain account. For both of these two wallets, you can use the same mnemonics.

After you get the WIF private key from ElectronCash, please use following command to get the public key of the mainchain account:

git clone https://github.com/smartbch/atomic-swap-covenants.git
cd atomic-swap-covenants
npm i
MAINNET=true ts-node scripts/htlc.ts user-info --wif=<WIF-from-ElectronCash>

The pkh line of the output is what you will need.

The mainchain account's address must be imported into the bitcoincashnode client through its RPC. Please run the following command on your cloud machine:

curl --user user:pass --data-binary '{"jsonrpc": "1.0", "id":"curltest", "method": "importaddress", "params": ["bitcoincash:xxx", "asbot", true] }' -H 'content-type: text/plain;' http://localhost:8332/

Register your bot to the HTLC smart contract

The HTLC smart contract was deployed at 0x4dec1a09269DaDbAfBDBAF34A4cc224eCC1Bdc42. Its source code was opensourced here.

When it was deployed, the constructor set the following parameters:

  1. minStakedValue = 1BCH. You must stake one BCH into the contract when registering a bot and can take it back after retiring.

  2. minRetireDelay = 48 hours. You must wait for 48 hours after retirement before you can take back your staked BCH.

Staking BCH prevents evil guys from spaming the HTLC smart contracts by registering unworkable bots.

Please make sure your bot's side chain account has at lease 1.01 BCH and then use the following commands to register your bot to the HTLC smart contract:

git clone https://github.com/smartbch/atomic-swap-contracts.git
cd atomic-swap-contracts
npm i

KEY=<EVM-private-key-from-MetaMask> \
HARDHAT_NETWORK=sbch_mainnet node ./scripts/htlc.js register-bot \
	--signer=0 \
	--htlc-addr=0x4dec1a09269DaDbAfBDBAF34A4cc224eCC1Bdc42 \
	--intro=MyBotIsTheBest_or_you_can_write_something_else \
	--pkh=<the-public-key-hash-that-you-got-in-the-last-step> \
	--bch-lock-time=144 \
	--penalty-bps=200 \
	--fee-bps=10 \
	--min-swap-amt=0.01 \
	--max-swap-amt=1.0 \
	--staked-val=1.0 \
	--status-checker=<An-EVM-account-that-can-mark-your-bot-as-unavailable>

You can use an account as your bot's status checker that watches whether your bot is online. When it finds that your bot is unavailable, it will send a transaction to the HTLC contract to mark your bot's status as "unavailable" such that users will not trade with it. If you do not need this feature, just set status checker to the same address as your bot.

Start Your Bot

Now you can start your bot on the cloud instance:

git clone https://github.com/smartbch/atomic-swap-bot.git
cd atomic-swap-bot

ego-go build github.com/smartbch/atomic-swap-bot/cmd/asbot
ego sign asbot
mkdir data
ego run asbot \
  --db-file=/data/bot.db \
  --bch-rpc-url=http://localhost:8332 \
  --sbch-rpc-url=http://localhost:8545 \
  --sbch-htlc-addr=0x4dec1a09269DaDbAfBDBAF34A4cc224eCC1Bdc42 \
  --sbch-gas-price=1.05 \
  --bch-confirmations=10 \
  --bch-lock-fee-rate=2 \
  --bch-unlock-fee-rate=2 \
  --bch-refund-fee-rate=2 \
  --sbch-lock-gas=500000 \
  --sbch-unlock-gas=500000 \
  --sbch-refund-gas=500000 \
  --debug=false

Here we set the miner fee rate to be 2sat/byte. You can lower it to 1sat/byte as well.

The above ego run asbot command will output something like this to the console:

EGo v1.3.0 (360a6a40836461465fdbd0742dfb0f980b68c638)
[erthost] loading enclave ...
[erthost] entering enclave ...
[ego] starting application ...
The ecies pubkey: 03052743d278846f90cedb64282a3ea3db20a8414b627e4aff3dc5408110073eed
Enter the encrypted BCH WIF: 

It needs you to encrypt the BCH WIF private key and the EVM hex private key on you local computer and input the encrypted keys on the cloud machine's console.

On your local computer, run the following command:

go run github.com/smartbch/atomic-swap-bot/cmd/encrypt

It asks you to input the ecies pubkey generated by the above ego run asbot command, your bot's WIF private key and its EVM private key. After your input these data to it, it will output the encrypted WIF key and EVM private key as hex strings. Copy and enter them to the ego run asbot command's prompt, and then your bot will decrypt the keys and use them to sign transactions.

Your bot will keep the private keys only in its memory. If it works in an SGX enclave, the private keys are impossible to be stolen from the protected memory.

Start Slaves for Your Bot (optional)

If your bot's cloud machine panics, it may miss the time window for unlocking funds and lose coins. To further secure your funds, you can use one or more slaves to help your bot. Every slave watches the counterparties' secret-revealing transactions independently. If your bot failed to unlock funds ten minutes after secret-revealing, a slave will send a transaction to help your bot unlock the fund.

It's good for a slave to have its own bitcoincashnode client and smartbch client. To start a slave, please use the following command:

git clone https://github.com/smartbch/atomic-swap-bot.git
cd atomic-swap-bot/cmd/asbot
go build .

./asbot \
--debug=false \
--db-file=bot.db \
--bch-rpc-url=http://localhost:8332 \
--sbch-rpc-url=http://localhost:8545 \
--bch-master-addr=bitcoincash:qz0ypj3acm80ve9qpzdy4rm7s9k0u7zrpu6nzfzgyh \
--sbch-master-addr=0xd7d00B26Eac76f1c8B646442ECBfb33201842149 \
--sbch-htlc-addr=0x4dec1a09269DaDbAfBDBAF34A4cc224eCC1Bdc42 \
--sbch-gas-price=1.05 \
--bch-confirmations=0 \
--bch-lock-fee-rate=2 \
--bch-unlock-fee-rate=2 \
--bch-refund-fee-rate=2 \
--sbch-lock-gas=500000 \
--sbch-unlock-gas=500000 \
--sbch-refund-gas=500000 \
--slave=true 

Where the bch-master-addr and sbch-master-addr are the addresses of your bot's mainchain account and side chain account.

4
$ 0.25
$ 0.21 from @TheRandomRewarder
$ 0.03 from @Jane
$ 0.01 from @halu
Avatar for SmartBCH
1 year ago

Comments

parameters 2 & 3 are fixed. The strikethrough is typo. Thank you.

$ 0.00
1 year ago

You appear to have edit errors in parameters 2 & 3 (regarding bps). The strikethrough in both cases is marking out one bps but also the non-matching percent, making it unclear which was the original recommendation and which is current.

$ 0.00
1 year ago

I had enough of command line when running LN so I will pass on this because I don't have the time to learn nor the energy to do so. I hope those who operate the bots give us better rates when going from BCH to sBCH.

$ 0.00
1 year ago