[Layer.zip (updated for deposits) generated from Docker allows you to use coinbase pro Python library when uploaded as a Layer in AWS - instructions for how to generate your own here: https://dev.to/mmascioni/using-external-python-packages-with-aws-lambda-layers-526o](https://s3-us-west-2.amazonaws.com/secure.notion-static.com/0ea7c87e-197a-469a-a39f-64c05422d338/layer.zip)
Layer.zip (updated for deposits) generated from Docker allows you to use coinbase pro Python library when uploaded as a Layer in AWS - instructions for how to generate your own here: https://dev.to/mmascioni/using-external-python-packages-with-aws-lambda-layers-526o
import cbpro
import json
#Insert your API key, secret, and passphrase from Coinbase Pro
my_key = ""
my_secret = ""
my_passphrase = ""
#Replace symbol with whatever currencypair you want to trade - list of currency pairs available on Notion
symbol = "BTC-USD"
#Replace buy size with amount you want to buy/sell
buy_size = 20
#Order = "sell" for a sell order and "buy" for a buy order
order = "buy"
def _makeOrder(buy_size, cb_key, cb_secret, cb_passphrase):
auth_client = cbpro.AuthenticatedClient(cb_key, cb_secret, cb_passphrase)
market_order = auth_client.place_market_order(symbol,order,funds=buy_size)
print(market_order)
return buy_size
def lambda_handler(event, context):
amount = _makeOrder(buy_size,my_key,my_secret,my_passphrase)
return {
'statusCode': 200,
'body': json.dumps(f"You bought {amount} {symbol.split('-')[1]} of {symbol.split('-')[0]}!")
}
import cbpro
import json
#Insert your API key, secret, and passphrase from Coinbase Pro
my_key = ""
my_secret = ""
my_passphrase = ""
auth_client = cbpro.AuthenticatedClient(my_key, my_secret, my_passphrase)
#Replace symbol with whatever currencypair you want to trade - list of currency pairs available on Notion
symbol = "BTC-USD"
#Replace buy size with amount you want to buy/sell and execution price with the price you want to set the limit order for
execution_price = "30000.00"
buy_size = 0.0001
#If you want to set a buy for a USD buy size of $5 for 90% (factor) of the spot price. You'll need to change the rounding factor for certain coins.
#Uncomment these lines for this functionality
#factor = 0.9
#rounding_factor = 2
#execution_price = str(round(float(auth_client.get_product_ticker(symbol)['price'])*factor,rounding_factor))
#print(f"Execution price: {execution_price}")
#usd_buy_size = 5
#buy_size = round(usd_buy_size/float(execution_price),8)
#print(f"Buy size: {buy_size}")
#Order = "sell" for a sell order and "buy for a buy order
order = "buy"
def _makeOrder(auth_client, buy_size):
exececute = auth_client.place_limit_order(symbol, order, execution_price, buy_size)
print(exececute)
return buy_size
def lambda_handler(event, context):
amount = _makeOrder(auth_client, buy_size)
return {
'statusCode': 200,
'body': json.dumps(f"{order.capitalize()} order executed: {amount} {symbol.split('-')[1]} of {symbol.split('-')[0]}!")
}
import json
import cbpro
my_key = ''
my_secret = ''
my_passphrase = ''
bank_id = ''
def _depositBTC(deposit_amount, cb_key, cb_secret, cb_password, bank_id):
auth_client = cbpro.AuthenticatedClient(cb_key, cb_secret, cb_password)
deposit = auth_client.deposit(deposit_amount, 'USD', bank_id)
print(deposit)
return deposit_amount
def lambda_handler(event, context):
deposit_amount = 20
message = _depositBTC(deposit_amount, my_key,my_secret,my_passphrase,bank_id)
return {
'statusCode': 200,
'body': json.dumps(f'You deposited ${message} to Coinbase!')
}
import json
import cbpro
my_key = ""
my_secret = ""
my_passphrase = ""
def _getBankID(my_key, my_secret, my_passphrase):
auth_client = cbpro.AuthenticatedClient(my_key, my_secret, my_passphrase)
payment_methods = auth_client.get_payment_methods()
print(payment_methods)
for method in payment_methods:
print(f"Account: {method['name']} | ID: {method['id']}")
def lambda_handler(event, context):
_getBankID(my_key, my_secret, my_passphrase)
return {
'statusCode': 200,
'body': json.dumps(f'These are you bank IDs!')
}
import cbpro
import json
from math import floor
my_key = ""
my_secret = ""
my_passphrase = ""
my_currency = "BTC"
withdraw_address = ""
withdraw_precision = 8
def _withdrawCrypto(cb_key, cb_secret, cb_passphrase):
auth_client = cbpro.AuthenticatedClient(cb_key, cb_secret, cb_passphrase)
for account in auth_client.get_accounts():
if account['currency'] == my_currency:
withdraw_size = (floor(float(account['available'])*(10**(withdraw_precision))))/(10**(withdraw_precision))
print(account)
print(f"Withdraw size = {withdraw_size}")
withdrawal = auth_client.crypto_withdraw(withdraw_size, my_currency, withdraw_address)
print(withdrawal)
return withdraw_size
def lambda_handler(event, context):
withdrawal = _withdrawCrypto(my_key,my_secret,my_passphrase)
return {
'statusCode': 200,
'body': json.dumps(f"You withdrew {my_currency}!")
}
import json
import gspread
from oauth2client.service_account import ServiceAccountCredentials
import cbpro
my_key = ''
my_secret = ''
my_password = ''
def _addTransaction(transaction):
transaction_date = str(transaction['created_at'][0:10])
transaction_id = transaction['trade_id']
provider = "Coinbase Pro"
quantity = float(transaction['size'])
btc_price = float(transaction['price'])
fee = float(transaction['fee'])
usd_amount = float(transaction['usd_volume']) + float(fee)
return [transaction_date, transaction_id, provider, quantity, btc_price, usd_amount, fee]
def _authenticateSpreadsheet():
#Set up access to the spreadsheet
scope = ["<https://spreadsheets.google.com/feeds>",
'<https://www.googleapis.com/auth/spreadsheets>',
"<https://www.googleapis.com/auth/drive.file>",
"<https://www.googleapis.com/auth/drive>"]
creds = ServiceAccountCredentials.from_json_keyfile_name("sheets_creds.json", scope)
client = gspread.authorize(creds)
return client.open("The Definitive Bitcoin Sheet").worksheet("BTC Buy Audit File")
def populateBTC(cb_key, cb_secret, cb_password):
auth_client = cbpro.AuthenticatedClient(cb_key, cb_secret, cb_password)
#Open the correct spreadsheet + worksheet
audit_file = _authenticateSpreadsheet()
btc_fills = list(auth_client.get_fills(product_id="BTC-USD"))
#Get last transaction ID added to worksheet and get a range of days to pull in
last_transaction = audit_file.get_all_records()[-1]['Transaction ID']
for transaction in btc_fills[::-1]:
if transaction['trade_id'] > last_transaction:
cbpro_row = _addTransaction(transaction)
audit_file.append_row(cbpro_row, value_input_option="USER_ENTERED")
def lambda_handler(event, context):
populateBTC(my_key,my_secret,my_password)
return {
'statusCode': 200,
'body': json.dumps('Transaction Recorded!')
}