Telegram bot for seamless WBTC <> BTC conversion. Uses the Telegraf Telegram bot framework, bitcoinjs-lib, web3, and BitGo v2 API.
Bots are special Telegram accounts designed to handle messages automatically. Users can interact with bots by sending them command messages in private or group chats.
$ npm install
- Create an .env file in the root directory, containing your node API key and Telegram bot token. Note: Omit the bot prefix in the Telegram token.
e.g.
BTC_RPC_KEY=xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx
ETH_RPC_KEY=xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx
TELEGRAM_TOKEN=xxxxxxxxx:xxxxxxxxxxxxxxxxxxxx-xxxxxxxxxxxxxx
- Edit the RPC URLs, HTLC addresses, etc. in
./src/config/default.json
.
To run in development mode (restarts at every file change)
npm run dev
To run in production mode
npm run start
To start interacting with the bot, issue the following command:
/start
An atomic swap enables the exchange of one cryptoasset for another cryptoasset without the need of centralized intermediaries. Atomic swaps can take place between two different blockchains as well. The swap is atomic, as it completes successfully or not at all. Both parties must acknowledge the receipt of funds within a given timeframe using a cryptographic hash function.
Hash Time Locked Contracts (HTLC) is the mechanism that enables atomic swaps.
- Alice and Bob agree to swap 1 A-coin from blockchain-A with 1 B-coin from blockchain B respectively.
- Alice generates a large random number, called the preimage m.
- Alice creates an HTLC on blockchain-A, locked with the hashed preimage h, with the following conditions:
Pay 1 A-coin to Bob's public key if m for hash(m) is known and signed by Bob.
Pay 1 A-coin to her own public key, redeemable only after 24-hours, signed by herself .
- Alice broadcasts the contract to blockchain-A and shares the transaction to Bob.
- After Bob verifies the transaction, he creates an HTLC on blockchain-B, locked with h as referenced from the transaction Alice broadcasted (Bob does not know m), wih the following conditions:
Pay 1 B-coin to Alice's public key if m for hash(m) is known and signed by Bob.
Pay 1 B-coin to his own public key, redeemable only after 12-hours, signed by himself.
- Bob broadcasts the contract to blockchain-B and shares the transaction to Alice.
- Alice submits a transaction in blockchain-B to redeem the 1 B-coin from the contract in blockchain-B using m.
- The transaction reveals m, and Bob uses m to submit a transaction in blockchain-A to redeem the 1 A-coin.
- Should Alice fail to redeem the 1 B-coin within 12-hours, Bob can claim back his 1 B-coin from the contract in blockchain-B.
- Should Bob fail to redeem the 1 A-coin within 24 hours, Alice can claim back her 1-A coin from the contract in blockchain-A.
- The bot acts as the maker and counterparty. It holds liquidity for both BTC and WBTC.
- A user queries the available BTC/WBTC liquidity available. He can initiate an atomic swap with the amount capped at the available liquidity for either BTC or WBTC.
- The atomic swap starts with the bot generating the preimage and creating the HTLC on either Bitcoin or Ethereum, depending on the destination cryptoasset of the swap for the user.
- The bot locks the required amount minus fees on the HTLC.
- The bot shares the transaction and hashed preimage for the user to verify.
- Once verified, the user creates the HTLC in the corresponding blockchain, locked with the hashed preimage shared in the previous step.
- The user shares the transaction hash for the bot to verify.
- Once verified, the bot redeems the cryptoasset from the user's HTLC using the preimage. Once redeemed, the bot notifies the user as well of the preimage.
- The bot expects the user to redeem the cryptoasset himself. So in the bot's perspective, the atomic swap flow has completed.
HTLCs are essentially escrow contracts that enable conditional payments. HTLC is a time-bound smart contract between parties that involves the generation of a cryptographic hash function, which can be verified between them. HTLCs form the foundation for atomic swaps - a user and the counterparty create these HTLC escrows on their respective chains, and the funds that are deposited into the two escrows of the swap are properly locked and released. This dual escrow mechanism reduces the need to trust the counterparty for proper execution of the trade. The bot leverages the HTLC standards BIP-199 and ERC-1630.
Atomic Swaps are currently executed on-chain, thus the process will move at the speed of the slowest chain.
There is a 0.5% fee imposed to the user.
12 hours for the bot. 24 hours for the user.