Skip to content

Instantly share code, notes, and snippets.

@rhettre
Last active August 1, 2024 14:52
Show Gist options
  • Save rhettre/c230d72656032a7969b5822922c45bf0 to your computer and use it in GitHub Desktop.
Save rhettre/c230d72656032a7969b5822922c45bf0 to your computer and use it in GitHub Desktop.
Automate Cryptocurrency Withdrawals From Kraken
import time
import requests
import urllib.parse
import hashlib
import hmac
import base64
import json
# Read Kraken API key and secret stored in environment variables
api_url = "https://api.kraken.com"
api_key = ''
api_sec = ''
def get_kraken_signature(urlpath, data, secret):
postdata = urllib.parse.urlencode(data)
encoded = (str(data['nonce']) + postdata).encode()
message = urlpath.encode() + hashlib.sha256(encoded).digest()
mac = hmac.new(base64.b64decode(secret), message, hashlib.sha512)
sigdigest = base64.b64encode(mac.digest())
return sigdigest.decode()
# Attaches auth headers and returns results of a POST request
def kraken_request(uri_path, data, api_key, api_sec):
headers = {}
headers['API-Key'] = api_key
# get_kraken_signature() as defined in the 'Authentication' section
headers['API-Sign'] = get_kraken_signature(uri_path, data, api_sec)
req = requests.post((api_url + uri_path), headers=headers, data=data)
return req
#Get balances for all accounts
def getAccountBalances():
# Construct the request and print the result
resp = kraken_request('/0/private/Balance', {
"nonce": str(int(time.time()*1000))
}, api_key, api_sec)
return resp.json()['result']
#Get account coin balance passing in the coin
def getCoinBalance(coin):
accounts = getAccountBalances()
print(accounts)
try:
return accounts[coin]
except KeyError:
print(f"You don't have any {coin} in your Kraken account, or you are using an invalid coin in coin_to_withdraw.")
return 0
def getWithdrawalInfo(coin, my_wallet_address, my_balance):
# Construct the request and print the result
resp = kraken_request('/0/private/WithdrawInfo', {
"nonce": str(int(1000*time.time())),
"asset": coin,
"key": my_wallet_address,
"amount": my_balance
}, api_key, api_sec)
print(f"getWithdrawalInfo: {resp.json()}")
if(resp.json()['error'] != []):
return resp.json()['error']
return resp.json()['result']
def crypto_Withdrawal(coin, my_wallet_address, amount):
# Construct the request and print the result
resp = kraken_request('/0/private/Withdraw', {
"nonce": str(int(1000*time.time())),
"asset": coin,
"key": my_wallet_address,
"amount": amount
}, api_key, api_sec)
return resp.json()
def lambda_handler(event, context):
#Which coin do you want to withdraw? XXBT = Bitcoin, ETH = Ethereum etc
coin_to_withdraw = "XXBT"
#Which wallet do you want to withdraw to? Type in the name of your wallet as it appears in Kraken
wallet_name = "COLDCARD Mk4"
#Get the current balance of the coin you want to withdraw
balance = getCoinBalance(coin_to_withdraw)
if float(balance) == 0:
print(f"Your balance for {coin_to_withdraw} is zero.")
else:
#Get the maximum allowable withdraw amount
max_withdrawal = getWithdrawalInfo(coin_to_withdraw, wallet_name, balance)['limit']
#Set the amount you want to withdraw. You can make this a static number (ie. 1 for 1 BTC) or you can set it to max_withdrawal or some factor of max_withdrawal
amount_to_withdraw = max_withdrawal
#Process the withdrawal
print(crypto_Withdrawal(coin_to_withdraw, wallet_name, amount_to_withdraw))
return {
'statusCode': 200,
'body': json.dumps('End of script')
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment