Skip to content

Instantly share code, notes, and snippets.

@rhettre
Last active April 1, 2023 22:19
Show Gist options
  • Save rhettre/3b7d47b661cbfbacde50114f3b92b0d3 to your computer and use it in GitHub Desktop.
Save rhettre/3b7d47b661cbfbacde50114f3b92b0d3 to your computer and use it in GitHub Desktop.
This Python script contains an AWS Lambda function designed to automate deposits into Coinbase accounts. It was created in response to the retirement of the Coinbase Pro API, which previously facilitated deposit automation. The script leverages the Coinbase Wallet API to list payment methods and initiate deposits in USD. Users can customize the …
import json
import hmac
import hashlib
import time
import requests
from requests.auth import AuthBase
import os
with open(os.path.join(os.path.dirname(__file__), 'config.json')) as config_file:
config = json.load(config_file)
API_KEY = config['API_KEY']
API_SECRET = config['API_SECRET']
#Get your DEPOSIT_ID and PAYMENT_METHOD from calling list_payment_methods
DEPOSIT_ID = config['DEPOSIT_ID']
PAYMENT_METHOD = config['PAYMENT_METHOD']
#Change your deposit amount and fiat currency
DEPOSIT_AMOUNT = config['DEPOSIT_AMOUNT']
DEPOSIT_CURRENCY = config['DEPOSIT_CURRENCY']
#Coinbase API Endpoints
DEPOSIT_ENDPOINT = f'https://api.coinbase.com/v2/accounts/{DEPOSIT_ID}/deposits'
PAYMENT_METHOD_ENDPOINT = 'https://api.coinbase.com/v2/payment-methods'
#Authenticate with Coinbase API
class CoinbaseWalletAuth(AuthBase):
def __init__(self, api_key, secret_key):
self.api_key = api_key
self.secret_key = secret_key
def __call__(self, request):
timestamp = str(int(time.time()))
message = timestamp + request.method + request.path_url + (request.body or '')
signature = hmac.new(self.secret_key.encode(), message.encode(), hashlib.sha256).hexdigest()
request.headers.update({
'CB-ACCESS-SIGN': signature,
'CB-ACCESS-TIMESTAMP': timestamp,
'CB-ACCESS-KEY': self.api_key,
})
return request
#Function to List Payment Methods - need this for getting DEPOSIT_ID and Payment_Method
def list_payment_methods():
auth = CoinbaseWalletAuth(API_KEY, API_SECRET)
response = requests.get(PAYMENT_METHOD_ENDPOINT, auth=auth)
data = response.json()
payment_methods = [{
'id': item['fiat_account']['id'] if item['type'] == 'fiat_account' else item['id'],
'name': item['name'],
'currency': item['currency'],
'type': item['type'],
'description': (item['limits']['buy'][0]['description']
if 'limits' in item and 'buy' in item['limits'] and item['limits']['buy']
else None)
} for item in data['data']]
for method in payment_methods:
print(method)
# Function to Deposit USD
def deposit_fiat(amount):
auth = CoinbaseWalletAuth(API_KEY, API_SECRET)
data = {
'type': 'deposit',
'amount': amount,
'currency': DEPOSIT_CURRENCY,
'payment_method': PAYMENT_METHOD
}
response = requests.post(DEPOSIT_ENDPOINT, data=data, auth=auth)
if response.status_code == 201:
print(f'You deposited {amount} {DEPOSIT_CURRENCY} into your Coinbase account!')
print(response.json())
return response.json()
else:
raise Exception(f'Error: {response.text}')
def lambda_handler(event, context):
list_payment_methods()
deposit_fiat(DEPOSIT_AMOUNT)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment