def send_transaction(self): print("---Sending Transaction---") try: to_account = self.get_to_account() print("Trying to unlock Wallet!") self.unlock_wallet() except ConnectionError: print("Transaction Failed due to connection error... is the Daemon running?") TRANSACTION_ERRORS.inc() return None except Exception as e: print("Error unlocking wallet...") print(e) return None tx_amount = Currency.random(self.min_tx_amount, self.max_tx_amount) fee_amount = Currency.random(self.min_fee_amount, self.max_fee_amount) try: response = self.coda.send_payment(to_account, self.public_key, tx_amount, fee_amount, memo="BeepBoop") except Exception as e: print("Error sending transaction...", e) TRANSACTION_ERRORS.inc() return None if not response.get("errors", None): print("Sent a Transaction {}".format(response)) TRANSACTIONS_SENT.inc() else: print("Error sending transaction: Request: {} Response: {}".format(self.public_key, response)) TRANSACTION_ERRORS.inc() return response
def test_sub_underflow(): try: Currency(5) - Currency(7) raise Exception('no underflow') except CurrencyUnderflow: pass except: raise
def test_random(): assert (Currency.random(Currency(5), Currency(5)).nanocodas() == 5 * precision) for _ in range(25): rand = Currency.random(Currency(3, format=CurrencyFormat.NANO), Currency(5, format=CurrencyFormat.NANO)) assert (3 <= rand.nanocodas() and rand.nanocodas() <= 5)
def test_constructor_whole_int(): n = 500 assert Currency(n).nanocodas() == n * precision
def test_mul_currency(): assert ( Currency(5) * Currency(2, format=CurrencyFormat.NANO)).nanocodas() == 10 * precision
def test_mul_int(): assert (Currency(5) * 2).nanocodas() == 10 * precision
def test_sub(): assert (Currency(5) - Currency(2)).nanocodas() == 3 * precision
def test_add(): assert (Currency(5) + Currency(2)).nanocodas() == 7 * precision
def test_constructor_nano_int(): n = 500 assert Currency(n, format=CurrencyFormat.NANO)
def test_constructor_whole_string(): n = "5.5" assert Currency(n).nanocodas() == float(n) * precision
def test_constructor_whole_float(): n = 5.5 assert Currency(n).nanocodas() == n * precision
def getenv_currency(env_var: str, lower_bound: Currency, upper_bound: Currency) -> Currency: return getenv_default_map(env_var, Currency, Currency.random(lower_bound, upper_bound))
return default else: return f(value) def getenv_str(env_var: str, default: str) -> str: return os.getenv(env_var, default).strip() def getenv_int(env_var: str, default: int) -> int: return getenv_default_map(env_var, int, default) def getenv_currency(env_var: str, lower_bound: Currency, upper_bound: Currency) -> Currency: return getenv_default_map(env_var, Currency, Currency.random(lower_bound, upper_bound)) CODA_PUBLIC_KEY = getenv_str("CODA_PUBLIC_KEY", "4vsRCVyVkSRs89neWnKPrnz4FRPmXXrWtbsAQ31hUTSi41EkbptYaLkzmxezQEGCgZnjqY2pQ6mdeCytu7LrYMGx9NiUNNJh8XfJYbzprhhJmm1ZjVbW9ZLRvhWBXRqes6znuF7fWbECrCpQ") CODA_PRIVKEY_PASS = getenv_str("CODA_PRIVKEY_PASS", "naughty blue worm") AGENT_MIN_FEE = getenv_currency("AGENT_MIN_FEE", Currency("0.06"), Currency("0.1")) AGENT_MAX_FEE = getenv_currency("AGENT_MAX_FEE", AGENT_MIN_FEE, AGENT_MIN_FEE + Currency("0.2")) AGENT_MIN_TX = getenv_currency("AGENT_MIN_TX", Currency("0.0015"), Currency("0.005")) AGENT_MAX_TX = getenv_currency("AGENT_MAX_TX", AGENT_MIN_TX, AGENT_MIN_TX + Currency("0.01")) AGENT_TX_BATCH_SIZE = getenv_int("AGENT_TX_BATCH_SIZE", 1) AGENT_SEND_EVERY_MINS = getenv_int("AGENT_SEND_EVERY_MINS", random.randint(1, 5)) AGENT_METRICS_PORT = getenv_int("AGENT_METRICS_PORT", 8000) CODA_CLIENT_ARGS = { "graphql_host": getenv_str("CODA_HOST", "localhost"), "graphql_port": getenv_str("CODA_PORT", "3085") } ## Prometheus Metrics
from CodaClient import Client, Currency, CurrencyFormat import os import schedule import time import urllib3 import random from requests.exceptions import ConnectionError from prometheus_client import Counter, start_http_server CODA_PUBLIC_KEY = os.getenv( "CODA_PUBLIC_KEY", "4vsRCVyVkSRs89neWnKPrnz4FRPmXXrWtbsAQ31hUTSi41EkbptYaLkzmxezQEGCgZnjqY2pQ6mdeCytu7LrYMGx9NiUNNJh8XfJYbzprhhJmm1ZjVbW9ZLRvhWBXRqes6znuF7fWbECrCpQ" ).strip() CODA_PRIVKEY_PASS = os.getenv("CODA_PRIVKEY_PASS", "naughty blue worm") AGENT_MIN_FEE = os.getenv("AGENT_MIN_FEE") or Currency.random( Currency("0.06"), Currency("0.1")) AGENT_MAX_FEE = os.getenv("AGENT_MAX_FEE") or Currency.random( AGENT_MIN_FEE, AGENT_MIN_FEE + Currency("0.2")) AGENT_MIN_TX = os.getenv("AGENT_MIN_TX") or Currency.random( Currency("0.0015"), Currency("0.005")) AGENT_MAX_TX = os.getenv("AGENT_MAX_TX") or Currency.random( AGENT_MIN_TX, AGENT_MIN_TX + Currency("0.01")) AGENT_SEND_EVERY_MINS = os.getenv("AGENT_SEND_EVERY_MINS", random.randint(1, 5)) AGENT_METRICS_PORT = os.getenv("AGENT_METRICS_PORT", 8000) CODA_CLIENT_ARGS = { "graphql_host": os.getenv("CODA_HOST", "localhost"), "graphql_port": os.getenv("CODA_PORT", "3085") }