async def _for_account(self, account: str):  # pragma: no cover
        """
        Stream transactions for the server Stellar address.
        """
        async with Server(settings.HORIZON_URI,
                          client=AiohttpClient()) as server:
            try:
                # Ensure the distribution account actually exists
                await server.load_account(account)
            except NotFoundError:
                # This exception will crash the process, but the anchor needs
                # to provide valid accounts to watch.
                raise RuntimeError(
                    "Stellar distribution account does not exist in horizon")
            last_completed_transaction = (Transaction.objects.filter(
                Q(kind=Transaction.KIND.withdrawal)
                | Q(kind=Transaction.KIND.send),
                receiving_anchor_account=account,
                status=Transaction.STATUS.completed,
            ).order_by("-completed_at").first())

            cursor = "now"
            if last_completed_transaction:
                cursor = last_completed_transaction.paging_token

            endpoint = server.transactions().for_account(account).cursor(
                cursor)
            async for response in endpoint.stream():
                self.process_response(response, account)
Example #2
0
    async def test_get_data_async(self):
        url = "https://httpbin.org/get"
        client = AiohttpClient()
        resp = (await
                BaseCallBuilder(url,
                                client).cursor(89777).order(desc=False
                                                            ).limit(25).call())

        assert resp["args"] == {
            "cursor": "89777",
            "limit": "25",
            "order": "asc"
        }
        assert resp["headers"] == {
            "Accept": "*/*",
            "Accept-Encoding": "gzip, deflate",
            "Content-Type": "application/x-www-form-urlencoded",
            "Host": "httpbin.org",
            "User-Agent":
            "py-stellar-sdk/{}/AiohttpClient".format(__version__),
            "X-Client-Name": "py-stellar-sdk",
            "X-Client-Version": __version__,
        }
        assert resp[
            "url"] == "https://httpbin.org/get?cursor=89777&order=asc&limit=25"
 async def test_resolve_by_stellar_address_federation_not_found_async(self):
     with pytest.raises(
             FederationServerNotFoundError,
             match=
             "Unable to find federation server at sdk-test.overcat.me.",
     ):
         await resolve_stellar_address("hello*sdk-test.overcat.me",
                                       client=AiohttpClient())
Example #4
0
 async def test_submit_transaction_with_te(self):
     xdr = "AAAAAHI7fpgo+b7tgpiFyYWimjV7L7IOYLwmQS7k7F8SronXAAAAZAE+QT4AAAAJAAAAAQAAAAAAAAAAAAAAAF1MG8cAAAAAAAAAAQAAAAAAAAAAAAAAAOvi1O/HEn+QgZJw+EMZBtwvTVNmpgvE9p8IRfwp0GY4AAAAAAExLQAAAAAAAAAAARKuidcAAABAJVc1ASGp35hUquGNbzzSqWPoTG0zgc89zc4p+19QkgbPqsdyEfHs7+ng9VJA49YneEXRa6Fv7pfKpEigb3VTCg=="
     te = TransactionEnvelope.from_xdr(xdr, Network.PUBLIC_NETWORK_PASSPHRASE)
     horizon_url = "https://horizon.stellar.org"
     client = AiohttpClient()
     async with Server(horizon_url, client) as server:
         resp = await server.submit_transaction(te, True)
         assert resp["envelope_xdr"] == xdr
 async def test_resolve_by_stellar_address_with_federation_url_async(self):
     record = await resolve_stellar_address(
         "hello*example.com",
         federation_url=self.FEDERATION_SERVER,
         client=AiohttpClient(),
     )
     assert (record.account_id ==
             "GAWCQ74PIJO2NH6F3KZ4AMX27UAKBXWC7KG3FLYJOFIMRQF3REXAMPLE")
Example #6
0
 async def test_load_acount_async(self):
     account_id = "GDV6FVHPY4JH7EEBSJYPQQYZA3OC6TKTM2TAXRHWT4EEL7BJ2BTDQT5D"
     horizon_url = "https://horizon.stellar.org"
     client = AiohttpClient()
     async with Server(horizon_url, client) as server:
         account = await server.load_account(account_id)
         assert account.account_id == account_id
         assert isinstance(account.sequence, int)
Example #7
0
 async def test_with(self):
     async with AiohttpClient() as client:
         url = "http://httpbin.org/get"
         params = {"hello": "world", "stellar": "sdk"}
         resp = await client.get(url, params=params)
         assert resp.status_code == 200
         json = resp.json()
         assert json["args"] == params
         assert json["headers"]["User-Agent"] == USER_AGENT
Example #8
0
 async def test_stream(self):
     async with AiohttpClient() as client:
         resp = []
         async for msg in client.stream(
                 "https://horizon.stellar.org/ledgers", {"cursor": "now"}):
             assert isinstance(msg, dict)
             resp.append(msg)
             if len(resp) == 2:
                 break
 async def test_resolve_by_account_id_federation_not_found_async(self):
     with pytest.raises(
             FederationServerNotFoundError,
             match=
             "Unable to find federation server at sdk-test.overcat.me.",
     ):
         await resolve_account_id(self.ACCOUNT_ID,
                                  domain="sdk-test.overcat.me",
                                  client=AiohttpClient())
Example #10
0
 async def test_get_stream_data_async(self):
     url = "https://horizon.stellar.org/ledgers"
     client = AiohttpClient()
     resp = BaseCallBuilder(url, client).cursor("now").stream()
     messages = []
     async for msg in resp:
         assert isinstance(msg, dict)
         messages.append(msg)
         if len(messages) == 2:
             break
Example #11
0
 async def test_load_acount_muxed_account_str_async(self):
     account_id = (
         "MAAAAAAAAAAAJUXL4LKO7RYSP6IIDETQ7BBRSBW4F5GVGZVGBPCPNHYIIX6CTUDGHDUWO"
     )
     horizon_url = "https://horizon.stellar.org"
     client = AiohttpClient()
     async with Server(horizon_url, client) as server:
         account = await server.load_account(account_id)
         assert account.account_id == MuxedAccount.from_account(account_id)
         assert isinstance(account.sequence, int)
         assert account.thresholds == Thresholds(1, 2, 3)
Example #12
0
 async def test_get(self):
     user_agent = "Hello/Stellar/overcat"
     client = AiohttpClient(pool_size=10, user_agent=user_agent)
     url = "http://httpbin.org/get"
     params = {"hello": "world", "stellar": "sdk"}
     resp = await client.get(url, params=params)
     assert resp.status_code == 200
     json = resp.json()
     assert json["args"] == params
     assert json["headers"]["User-Agent"] == user_agent
     await client.close()
Example #13
0
    async def test_status_400_raise_async(self):
        url = "https://horizon.stellar.org/accounts/BADACCOUNTID"
        client = AiohttpClient()
        with pytest.raises(BadRequestError) as err:
            await BaseCallBuilder(url, client).call()

        exception = err.value
        assert exception.status == 400
        assert exception.type == "https://stellar.org/horizon-errors/bad_request"
        assert exception.title == "Bad Request"
        assert exception.detail == "The request you sent was invalid in some way."
        assert exception.extras == {
            "invalid_field": "account_id",
            "reason": "invalid address",
        }
Example #14
0
    async def test_status_404_raise_async(self):
        url = "https://horizon.stellar.org/not_found"
        client = AiohttpClient()
        with pytest.raises(NotFoundError) as err:
            await BaseCallBuilder(url, client).call()

        exception = err.value
        assert exception.status == 404
        assert exception.type == "https://stellar.org/horizon-errors/not_found"
        assert exception.title == "Resource Missing"
        assert (
            exception.detail ==
            "The resource at the url requested was not found.  This "
            "usually occurs for one of two reasons:  The url requested is not valid, "
            "or no data in our database could be found with the parameters provided."
        )
        assert exception.extras is None
Example #15
0
 async def test_post(self):
     client = AiohttpClient()
     url = "http://httpbin.org/post"
     data = {
         "tx":
         "AAAAABa3N0+hJk17vP/AnYK5xV4o/PhOnEfgi36HlYo4g+3nAAAAZQFDfjoAAaTSAAAAAA"
         "AAAAEAAAAJX3VwZGF0ZWRfAAAAAAAAAQAAAAEAAAAAFrc3T6EmTXu8/8CdgrnFXij8+E6cR+"
         "CLfoeVijiD7ecAAAADAAAAAAAAAAFFVFgAAAAAAIhWSba8wLvB8YFRdzLJPkoyQSFmvRMQeaD"
         "Kym9JD6yTAAAAAAfjgC8NOvYPAA7nFwAAAAAGU5P1AAAAAAAAAAE4g+3nAAAAQOlPDNg4a76N/4"
         "VQh5oKc+RaUZVlK3Pr1HJphQn/yMthQh9gVGUbg/MHKl1RnKPuvmpzyqpBgb1zBVgyAYfIaQI="
     }
     resp = await client.post(url, data=data)
     assert resp.status_code == 200
     json = resp.json()
     assert json["headers"][
         "Content-Type"] == "application/x-www-form-urlencoded"
     assert json["form"] == data
     await client.close()
Example #16
0
    async def _for_account(self, account: str):  # pragma: no cover
        """
        Stream transactions for the server Stellar address.
        """
        async with Server(settings.HORIZON_URI,
                          client=AiohttpClient()) as server:
            try:
                # Ensure the distribution account actually exists
                await server.load_account(account)
            except NotFoundError:
                # This exception will crash the process, but the anchor needs
                # to provide valid accounts to watch.
                raise RuntimeError(
                    "Stellar distribution account does not exist in horizon")

            endpoint = server.transactions().for_account(account).cursor("now")
            async for response in endpoint.stream():
                self.process_response(response)
Example #17
0
    async def test_get_data_async(self):
        url = "https://httpbin.org/get"
        client = AiohttpClient()
        resp = (await
                BaseCallBuilder(url,
                                client).cursor(89777).order(desc=False
                                                            ).limit(25).call())

        assert resp["args"] == {
            "cursor": "89777",
            "limit": "25",
            "order": "asc"
        }
        assert resp["headers"][
            "User-Agent"] == "py-stellar-sdk/{}/AiohttpClient".format(
                __version__)
        assert resp["headers"]["X-Client-Name"] == "py-stellar-sdk"
        assert resp["headers"]["X-Client-Version"] == __version__
        assert resp[
            "url"] == "https://httpbin.org/get?cursor=89777&order=asc&limit=25"
Example #18
0
 async def test_resolve_by_stellar_address_async(self):
     record = await resolve_stellar_address(self.STELLAR_ADDRESS,
                                            client=AiohttpClient())
     assert record == self.FEDERATION_RECORD
Example #19
0
 async def test_get_success_async(self):
     client = AiohttpClient()
     toml = await fetch_stellar_toml("overcat.me", client)
     assert toml.get(
         "FEDERATION_SERVER") == "https://federation.overcat.workers.dev"
Example #20
0
 async def test_fetch_base_fee_async(self):
     horizon_url = "https://horizon.stellar.org"
     client = AiohttpClient()
     async with Server(horizon_url, client) as server:
         base_fee = await server.fetch_base_fee()
         assert isinstance(base_fee, int)
Example #21
0
 async def test_resolve_by_account_id_with_domain_async(self):
     record = await resolve_account_id(self.ACCOUNT_ID,
                                       domain=self.DOMAIN,
                                       client=AiohttpClient())
     assert record == self.FEDERATION_RECORD