Thorchain is a platform for trading cryptocurrencies in a totally decentralized, anonymous and trustless way. It currently supports trading over 30 cryptocurrencies, including Bitcoin, Ethereum, Binance Coin, Tether and Litecoin. To ensure that there is always liquidity available for users to make their trades, the network incentivizes people to become “liquidity providers” whereby they stake crypto they own and in exchange they earn additional crypto. The income paid to liquidity providers comes from fees paid by users of Thorchain and rewards built into the Thorchain protocol.
Thorchain is still in its early days and there have been bugs and growing pains experienced as the network has expanded. Knowing this was likely to be the case, the Thorchain developers imposed a “maximum liquidity cap” to prevent liquidity providers from over-exposing themselves to risks. The developers regularly increase the liquidity cap which allows new or existing liquidity providers to stake more assets. However due to the enormous interest in Thorchain, the new liquidity cap would usually be reached very shortly after being raised. This meant it was crucial to get in quick.
For this reason we developed and open sourced a Thorchain Liquidity Cap Alert (click here for the code) application to notify us immediately whenever the liquidity cap is raised. In the remainder of this post we provide an overview of how we built this application and explain why we chose the technologies we did.
What needed to be done
The current liquidity cap value is easy to determine: it is the value under the
mimir//MAXIMUMLIQUIDITYRUNE field in the JSON returned by https://thornode.thorchain.info/thorchain/mimir. Our task was therefore to write an application that:
- Fetches the liquidity cap value
- Compares it with the previous value
- If the value has changed:
- Send alerts via email, SMS and phone calls
- Record the new value
To achieve this we went with the following technologies:
- AWS Lambda running a Python application to perform all the logic
- AWS S3 to persist the liquidity cap value between lambda invocations
- AWS SNS to send alerts via email and SMS
- Twilio to perform the phone call alert
- AWS CDK to provision the AWS services and deploy the code to Lambda
AWS Lambda and Python
Lambda is a serverless platform provided by AWS which allows you to just provide your code and then AWS takes care of provisioning the compute infrastructure to run your code. You are only billed for the number of times your function is invoked and how long it runs. Lambda has a very generous free tier whereby the first million requests to your function are free. This means if you only need your function to run every few seconds and it takes less than a second or 2 to complete then you will be able to run it for free. In our case we were happy to check for liquidity cap changes once every minute and our checks completed in under a second so we were well within free tier.
The biggest advantage with Lambda, however, is you don’t need to worry at all about provisioning or managing servers. This means you don’t need to worry about updating servers, monitoring uptime, backups etc and you get a high availability application without needing to configure failover servers or autoscaling policies.
We went with Python for our Lambda function because it is easy to code, is well supported by AWS and has libraries that make it easy to interact with the Thorchain API, other AWS services and external services like Twilio.
AWS S3 for persistence
There are many ways to persist data in AWS. Typically you would want to use a database like RDS or DynamoDB however in our case we just needed to store a single value. A simple way to achieve this is to write the value to a file in AWS’s object storage service, S3. Reading and writing to an S3 object can each be done with a single line of Python using the boto3 library.
Sending the alerts with AWS SNS and Twilio
A really simple way of sending emails and SMS messages is with AWS SNS. You simply create an SNS topic and create subscriptions to this topic for each email and phone number. Then every message written to the topic will be sent out to each of those emails and phone numbers. The first 1,000 emails sent each month will be free but each SMS will cost a few cents.
Unfortunately AWS doesn’t currently have a simple way to send phone call alerts so we opted for Twilio to send these. Fortunately Twilio has a great Python library which allows you to create the a voice call with only 2 lines of code.
Deploying with AWS CDK
There are many ways to provision resources and deploy to AWS: CloudFormation, Terraform and AWS CDK being the most popular. We prefer AWS CDK because it allows developers to write the infrastructure code in the same language that the application is written in which means they don’t need to learn a new language and there is less context switching.
How things went
The application performed without a hitch and successfully notified us every time the liquidity cap was raised. We came across one unexpected edge case when the liquidity cap was lowered, our app gave the same alerts saying it was raised, however this was easy to fix.
This allowed us to invest in Thorchain and start realizing profits from this enormously promising decentralized technology. We have also done a feasibility study on running a thornode.