Table of Contents generated with DocToc
Coinbase lacks the support for low-fee recurring purchases, which are supported by the base version of Coinbase. However, the base version of Coinbase lacks charges a flat fee for each transaction based on its value:
Total Transaction Amount | USD Fee |
---|---|
$10 or less | $0.99 |
More than $10 but less than or equal to $25 | $1.49 |
More than $25 but less than or equal to $50 | $1.99 |
More than $50 but less than or equal to $200 | $2.99 |
More than $200 | 1.49% of the total transaction amount |
This application allows users to configure recurring purchases leveraging Coinbase Advanced Trade API, which charges much lower fees.
coinbase-pro-order
is a fully-managed and serverless application that allows
you to configure recurring purchases through Coinbase Advanced Trade API.
This application is designed to be deployed to Google Cloud Platform.
To learn more about how to deploy this application, see the README in the terraform directory.
To learn more about how to run this application locally, see the README the Run Application Locally section.
In any way, the application requires the following services:
In order to authenticate against Coinbase you will need to create and remember these pieces of information:
- Key
- Secret
Anyone with access to these values will have access to your account, so handle these values wisely.
In order to create a Coinbase API key:
-
Click "New API Key"
-
Add all the accounts that you plan on interacting with. This includes the "Cash (USD)" account, if you're buying with USD cash.
-
Add permissions to the API key:
- For making orders
Cash (USD)
: this permission is required to query for account balanceswallet:accounts:read
: this permission is required to query for account balanceswallet:buys:create
: this permission is required to create new orderswallet:orders:read
: this permission is required to query for order statuswallet:transactions:read
: this permission is required to query for transaction historywallet:user:read
: this permission is required to query for user information
- For making deposits:
wallet:accounts:read
: this permission is required to query for account balanceswallet:deposits:create
: this permission is required to create new depositswallet:payment-methods:read
: this permission is required to query for payment methods
- For making orders
-
Click "Create"
-
Make a note of the API key, and API secret. Store these values safely.
More information on Coinbase Advanced Trace API can be found here:
- Authentication https://docs.cloud.coinbase.com/advanced-trade-api/docs/rest-api-auth
- Permissions: https://docs.cloud.coinbase.com/advanced-trade-api/docs/rest-api-overview#advanced-trade-endpoints
:info: This project is in the process of migrating to the Coinbase Advanced Trade API Python SDK, available at https://github.com/coinbase/coinbase-advanced-py. The new SDK is not fully supported here yet. It also only supports the Cloud API Trading Keys, so at the moment you need two different sets of keys.
To obtain the Cloud API Trading Keys:
- Navigate here: https://docs.cloud.coinbase.com/advanced-trade-api/docs/auth#cloud-api-trading-keys
- Follow the instructions to create a key, with "View", "Trade", and "Transfer" permissions. TODO: create two different keys, one for trading and one for depositing.
- Make a note of the API key name, and Private key secret. Store these values safely.
This system supports notifications via Telegram. In order to configure notifications, you will need to create a Telegram bot and retrieve your chat ID.
- Start a chat with
@BotFather
and type/newbot
- Reply to BotFather with a name for your bot
- Choose a username for the bot. It must be unique and end with
bot
- Save the HTTP API key associated with the bot
- Start a chat with
@myidbot
and retrieve your chat ID number by submitting the/start
and then the/getid
commands - Start a conversation with the bot created in step #3, and type
/start
Create a virtual environment with a supported Python version:
pyenv install 3.12.0
pyenv virtualenv 3.12.0 coinbase-pro-order-3.12.0
pyenv activate coinbase-pro-order-3.12.0
Install the development requirements:
pip install -r requirements-dev.txt
See .pre-commit-config.yaml for information on which hooks are configured.
pre-commit install
pre-commit install -t pre-push
Create a .env
file in the project root, and override the following variables.
Variable | Type | Description |
---|---|---|
COINBASE_API_KEY | Required | The Coinbase API key name |
COINBASE_API_SECRET | Required | The Coinbase API secret for this API key |
COINBASE_TRADING_API_KEY | Required | The Coinbase Advanced Trade API key name |
COINBASE_TRADING_PRIVATE_KEY | Required | The Coinbase Advanced Trade API private key |
TELEGRAM_BOT_TOKEN | Required | The Telegram bot token of the bot created earlier |
TELEGRAM_CHAT_ID | Required | The Telegram chat ID for the destination user |
COINBASE_API_BASE_URL | Optional | The Coinbase API base URL (defaults to https://api.coinbase.com) |
LOGGING_LEVEL | Optional | The logging level (defaults to INFO) |
The .env
file will be automatically loaded.
With the functions-framework
installed, you can run the application locally
by setting up a listener for the function you want to execute.
There are two ways of running the application locally:
- Run the application locally using the
functions-framework
CLI
make run_orders_function
make run_deposit_function
make run_mockoon
- Run the application locally using docker-compose:
make up
To execute a coinbase deposit locally, create a test_event.json
file with deposit request. Example:
{
"amount": 10.0,
"currency": "USD"
}
With the listener running, you can send a test event to the function and have it read the contents of that file:
curl -L 'http://localhost:8081' \
-H 'Content-Type: application/json' \
-H 'ce-id: 123451234512345' \
-H 'ce-specversion: 1.0' \
-H 'ce-time: 2020-01-02T12:34:56.789Z' \
-H 'ce-type: google.cloud.pubsub.topic.v1.messagePublished' \
-H 'ce-source: //pubsub.googleapis.com/projects/MY-PROJECT/topics/MY-TOPIC' \
-d '{
"message": {
"data": "'"$(jq -c . < test_event.json | base64)"'"
},
"subscription": "projects/MY-PROJECT/subscriptions/MY-SUB"
}'
To execute a coinbase order locally, create a test_event.json
file with the array of orders. Example:
[
{
"product_id": "BTC-USD",
"price": 10.0
}
]
With the listener running, you can send a test event to the function and have it read the contents of that file:
curl -L 'http://localhost:8082' \
-H 'Content-Type: application/json' \
-H 'ce-id: 123451234512345' \
-H 'ce-specversion: 1.0' \
-H 'ce-time: 2020-01-02T12:34:56.789Z' \
-H 'ce-type: google.cloud.pubsub.topic.v1.messagePublished' \
-H 'ce-source: //pubsub.googleapis.com/projects/MY-PROJECT/topics/MY-TOPIC' \
-d '{
"message": {
"data": "'"$(jq -c . < test_event.json | base64)"'"
},
"subscription": "projects/MY-PROJECT/subscriptions/MY-SUB"
}'
Run the unit tests and the coverage tests:
make test
make test-coverage
The infrastructure and deployment of this project is handled by Terraform. View the Terraform README for instructions on how to deploy this cloud function to the Google Cloud Platform.