def test_send_payment(self, mock_post, snapshot):
        mock_post.return_value = self._mock_response(json_data={"data": "foo"})

        client = Client()
        client.send_payment("to_pk", "from_pk", "amount", "fee", "memo")
        snapshot.assert_match(mock_post.call_args_list)
Example #2
0
class Agent(object):
    """Represents a generic agent that operates on the coda blockchain"""
    def __init__(self,
                 client_args,
                 public_key,
                 privkey_pass,
                 max_tx_amount=AGENT_MAX_TX,
                 max_fee_amount=AGENT_MAX_FEE):
        self.coda = Client(**client_args)
        self.public_key = public_key
        self.privkey_pass = privkey_pass
        self.max_fee_amount = max_fee_amount
        self.max_tx_amount = max_tx_amount
        self.to_account = None

    def get_to_account(self):
        if not self.to_account:
            print("Getting new wallet to send to...")
            response = self.coda.create_wallet(self.privkey_pass)
            self.to_account = response["createAccount"]["publicKey"]
            print("Public Key: {}".format(self.to_account))
        return self.to_account

    def unlock_wallet(self):
        response = self.coda.unlock_wallet(self.public_key, self.privkey_pass)
        print("Unlocked Wallet!")
        return response

    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 = random.randint(1, self.max_tx_amount)
        fee_amount = random.randint(1, 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)
            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