A twitter bot that calculates the progression towards Bitcoin's next halving event.
The halving is a recurrent event that happens every 210,000 blocks that are mined. Each time a block is mined, the miner is rewarded with bitcoin via a special coinbase transaction. The coinbase transaction is a combination of The block subsidy and transaction fees. Transacton fees are accumulated over all transactions in the mined block and are added to the coinbase. The number of Fresh bitcoins to be minted is determined by the block subsidy and are added to the coinbase transaction. The initial block subsidy started at 50BTC and will continue to be cut in half every 210,000 blocks until the block subsidy reaches just 1 Satoshi at which point the subsidy can no longer be divided because it has reached the smallest denomination of a Bitcoin. This concludes the block subsidy halving lifecyle and end the supply of new Bitcoin.
The block subsidy is a somewhat controversial, but important feature of the Bitcoin protocol because it determines the total supply of Bitcoin and the flow of new bitcoin.
The bitcoinHalvingLambda.py module contains all of the logic for calculating Bitcoin's subsidy amount, progress towards next halving event, and reward era given a number of important initilization parameters such as the current block height, initial mining subsidy, halving interval, and number of coins that make up a single bitcoin. It also contains logic for posting to twitter, getting the current block height, and reading and writing to AWS SSM parameter storage.
I schedule the lambda run every 10 minutes, which is the aproximate time it takes for a new block to be found by miners.
- get the block height every 10 minutes
- recalculate progress
- Send a new tweet everytime a percentage of progress has been achieved
The AWS Lambda functions are best used stateless. However, this function depends on knowning the last computed percentage. We can't store that in the lambda function itself, so one way of getting that percentage is to store the value using the AWS Systems Manager Parameter Store. more information about amazon SSM can be found here.
An alternative solution that would make this module less dependant on AWS might be to use a regex to find the percentage from the latest tweet.
Check out the subsidy amount for each subsidy era. A csv file has been generated with the bitcoinHalvingSchedule.py script named bitcoin_subsidy_schedule.csv. There is also a markdown supply.md file.
subsidy era | subsidy (sats) | total supply (sats) |
---|---|---|
1 | 5000000000 | 1050000000000000 |
2 | 2500000000 | 1575000000000000 |
3 | 1250000000 | 1837500000000000 |
... | ... | ... |
32 | 2 | 2099999997480000 |
33 | 1 | 2099999997690000 |
34 | 0 | 2099999997690000 |
start the python virtual environment
source ./env/activate
set environemnt variables for twitter api access
env\Scripts\activate.bat
using unittest you can run a test on the subsidy calculator
python -m unittest test_bitcoinHalvingLambda
to test a single unit test:
python -m unittest test_bitcoinHalvingLambda.TestTweet.test_tweet_message
You will need a twitter account and api access.
zip dependencies
cd package
zip -r9 ../function.zip .
cd ../
add function to zip
zip -g function.zip bitcoinHalvingLambda.py
update lamba function via aws-cli
aws lambda update-function-code --function-name updateBitcoinProgressTwitterBot --zip-file fileb://function.zip
or run the update script:
./update.sh