Skip to content

Instantly share code, notes, and snippets.

@brendan-mccaffrey
Last active March 11, 2023 03:19
Show Gist options
  • Save brendan-mccaffrey/a9ba7eae8b5476b85248c7cf08eadf7e to your computer and use it in GitHub Desktop.
Save brendan-mccaffrey/a9ba7eae8b5476b85248c7cf08eadf7e to your computer and use it in GitHub Desktop.
import os
import dotenv
from coinbase.wallet.client import Client
dotenv.load_dotenv()
# make api key, will yield API_KEY and SECRET_KEY
# passphrase is ur password to login app
API_KEY = os.environ.get("API_KEY")
SECRET_KEY = os.environ.get("SECRET_KEY")
PASSPHRASE = os.environ.get("PASSPHRASE")
USDC_ACCT_ID = ""
ETH_ACCT_ID = ""
WALLET_ADDR = ""
USD_PAYMENT_METHOD = ""
client = Client(API_KEY, SECRET_KEY)
def log_action(action):
with open("transactions.json", "a") as file:
json.dump(action, file, indent=4)
file.write("\n")
print("Logged")
def list_accounts():
print(client.get_accounts()["data"][:50])
def list_payment_methods():
print(client.get_payment_methods())
def usdc_balance():
usdc_acct = client.get_account(USDC_ACCT_ID)
balance = usdc_acct.balance.amount
return float(balance)
def eth_balance():
eth_acct = client.get_account(ETH_ACCT_ID)
balance = eth_acct.balance.amount
return float(balance)
def convertUSDCtoUSD(amt):
sale = client.sell(
USDC_ACCT_ID,
amount=amt,
currency="USDC",
)
log_action(sale)
def buyETHwithUSD(amt):
buy = client.buy(
ETH_ACCT_ID,
amount=amt,
currency="USD",
payment_method=USD_PAYMENT_METHOD,
)
log_action(buy)
def transfer_eth(amt):
withdraw = client.send_money(
ETH_ACCT_ID,
to=WALLET_ADDR,
amount=amt,
currency="ETH",
payment_method=USD_PAYMENT_METHOD,
)
log_action(withdraw)
def loop_arb():
while True:
print()
time.sleep(5)
balance = usdc_balance()
if balance > 50:
print("-----------------------")
print("| We have USDC balance |")
print("-----------------------")
convertUSDCtoUSD(balance)
print("Converted USDC to USD")
buyETHwithUSD(balance - 20)
print("Bought ETH with USD")
else:
print("We don't have USDC balance")
loop_arb()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment