Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Swapping on testnet #8

Closed
fdreyer opened this issue Nov 11, 2021 · 3 comments
Closed

Swapping on testnet #8

fdreyer opened this issue Nov 11, 2021 · 3 comments

Comments

@fdreyer
Copy link
Contributor

fdreyer commented Nov 11, 2021

I am encountering the following issue when attempting to exchange two coins on tinyman using testnet.

Following the steps inhttps://github.com/tinymanorg/tinyman-py-sdk/blob/main/examples/swapping1.py, which work for Mainnet without problem, the code crashes with an AlgodHTTPError.
Here is a minimal example:

from tinyman.v1.client import TinymanTestnetClient
client=TinymanTestnetClient(user_address=address)
assert(client.is_opted_in())
algo=client.fetch_asset(0)
usdc=client.fetch_asset(10458941)
pool = client.fetch_pool(usdc, algo)
quote = pool.fetch_fixed_input_swap_quote(algo(1_000_000), slippage=0.01)
transaction_group = pool.prepare_swap_transactions_from_quote(quote)
transaction_group.sign_with_private_key(address, private_key)
result = client.submit(transaction_group, wait=True)

which fails with the following error

---------------------------------------------------------------------------
JSONDecodeError                           Traceback (most recent call last)
<ipython-input-10-82e8ae16ad39> in <module>
      5 transaction_group = pool.prepare_swap_transactions_from_quote(quote)
      6 transaction_group.sign_with_private_key(address, private_key)
----> 7 result = client.submit(transaction_group, wait=True)

~/.local/lib/python3.9/site-packages/tinyman/v1/client.py in submit(self, transaction_group, wait)
     32             txid = self.algod.send_transactions(transaction_group.signed_transactions)
     33         except AlgodHTTPError as e:
---> 34             raise Exception(json.loads(e.args[0])['message']) from None
     35         if wait:
     36             return wait_for_confirmation(self.algod, txid)

/usr/lib/python3.9/json/__init__.py in loads(s, cls, object_hook, parse_float, parse_int, parse_constant, object_pairs_hook, **kw)
    344             parse_int is None and parse_float is None and
    345             parse_constant is None and object_pairs_hook is None and not kw):
--> 346         return _default_decoder.decode(s)
    347     if cls is None:
    348         cls = JSONDecoder

/usr/lib/python3.9/json/decoder.py in decode(self, s, _w)
    335 
    336         """
--> 337         obj, end = self.raw_decode(s, idx=_w(s, 0).end())
    338         end = _w(s, end).end()
    339         if end != len(s):

/usr/lib/python3.9/json/decoder.py in raw_decode(self, s, idx)
    353             obj, end = self.scan_once(s, idx)
    354         except StopIteration as err:
--> 355             raise JSONDecodeError("Expecting value", s, err.value) from None
    356         return obj, end

JSONDecodeError: Expecting value: line 1 column 1 (char 0)

Is there something that needs to be set up differently for a swap on testnet, or perhaps something that changed on the algosdk library which is causing this issue?

@epequeno
Copy link

epequeno commented Nov 11, 2021

I've noticed similar issues when attempting to use testnet: #7

It seems that the tinyman SDK expects json responses in a particular form which are not matching with what testnet algod is returning. In your case json.loads(e.args[0]) is failing since e.args[0] doesn't appear to be returning a dict.

I don't think it's a problem with algosdk, your traceback jumped directly from tinyman/v1/client.py to /usr/lib/python3.9/json/__init__.py

I would try to run this section of code and play around with the exception to inspect what the e value actually contains, I'm assuming it doesn't have an e.args property at all or it's returning None

try:
txid = self.algod.send_transactions(transaction_group.signed_transactions)
except AlgodHTTPError as e:
raise Exception(json.loads(e.args[0])['message']) from None

@fdreyer
Copy link
Contributor Author

fdreyer commented Nov 11, 2021

After further delving into it, the issue arises when a wallet is not opted in for one of the assets for which a swap is requested.
I have added a simple function in TinymanClient to allow a user to opt in for an asset directly without having to do it manually through algosdk, see here:
#9

@fergalwalsh
Copy link
Contributor

Incorrect JSON parsing of error messages issue fixed in a3025d7

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

4 participants
@epequeno @fergalwalsh @fdreyer and others