kevinheavey / anchorpy

The Python Anchor client.
https://kevinheavey.github.io/anchorpy/
MIT License
222 stars 59 forks source link

Borsh extension ImportError #94

Closed rcolomina closed 1 year ago

rcolomina commented 2 years ago

After creating a test script for my first anchorpy client, generated from the target program (i.e. idl.json),

#!/bin/bash

# The program is is the eshxro staking one
# This requires the json generated by anchor on eshxro staking
# give whatever name to the client

anchorpy client-gen --program-id 2jmux3fWV5zHirkEZCoSMEgTgdYZqkE9Qx2oQnxoHRgA eshxro_staking_idl.json eshxrostaking_client

however, my test script wasn't able to import the target struct I am interested on, giving me an import error.

import anchorpy

from solana.publickey import PublicKey
from eshxrostaking_client.accounts import StakePool

fetch an account

POOL_ID = "9zdpqAgENj4734TQvqjczMg2ekvvuGsxwJC6f7F1QWp4"
addr = PublicKey(POOL_ID)

from solana.rpc.api import Client

network = "https://api.devnet.solana.com"
client = Client(network)

acc = StakePool.fetch(client, addr)
if acc is None:
    # the fetch method returns null when the account is uninitialized
    raise ValueError("account not found")

convert to a JSON object

obj = acc.to_json()
print(obj)

load from JSON

acc_from_json = StakePool.from_json(obj)

However, I have an ImportError on anchorpy

ImportError: cannot import name 'EnumForCodegen' from 'anchorpy.borsh_extension'

Find below the complete backtrace

Traceback (most recent call last):
  File "/home/ruben/hxro-dot-com/backend/test.py", line 2, in <module>
    from eshxrostaking_client.accounts import StakePool
  File "/home/ruben/hxro-dot-com/backend/eshxrostaking_client/accounts/__init__.py", line 1, in <module>
    from .stake_pool import StakePool, StakePoolJSON
  File "/home/ruben/hxro-dot-com/backend/eshxrostaking_client/accounts/stake_pool.py", line 13, in <module>
    from .. import types
  File "/home/ruben/hxro-dot-com/backend/eshxrostaking_client/types/__init__.py", line 10, in <module>
    from . import pool_type
  File "/home/ruben/hxro-dot-com/backend/eshxrostaking_client/types/pool_type.py", line 4, in <module>
    from anchorpy.borsh_extension import EnumForCodegen
ImportError: cannot import name 'EnumForCodegen' from 'anchorpy.borsh_extension' (/home/ruben/hxro-dot-com/backend/env/lib/python3.9/site-packages/anchorpy/borsh_extension.py)

My python environment

Package               Version
--------------------- --------------
aiodns                3.0.0
aiohttp               3.8.3
aiosignal             1.2.0
anchorpy              0.8.3
anyio                 3.6.2
apischema             0.16.6
appnope               0.1.3
APScheduler           3.9.1
argon2-cffi           21.3.0
argon2-cffi-bindings  21.2.0
asttokens             2.0.8
async-timeout         4.0.2
attrs                 21.4.0
backcall              0.2.0
backoff               1.11.1
base58                2.1.1
based58               0.1.1
bidict                0.21.2
bleach                4.1.0
borsh-construct       0.1.1
cachetools            4.2.4
certifi               2021.10.8
cffi                  1.15.1
chardet               4.0.0
charset-normalizer    2.0.12
click                 7.1.2
construct             2.10.67
construct-typing      0.5.2
contourpy             1.0.5
cryptography          36.0.2
cycler                0.11.0
debugpy               1.6.3
decorator             5.1.1
defusedxml            0.7.1
dexteritysdk          0.1.1
dnspython             2.2.1
ed25519               1.5
entrypoints           0.4
executing             1.1.1
fastjsonschema        2.16.2
flake8                4.0.1
Flask                 2.0.0
Flask-Cors            3.0.10
Flask-Sockets         0.2.1
fonttools             4.38.0
frozenlist            1.3.1
gevent                21.1.2
gevent-websocket      0.10.1
greenlet              1.1.0
gunicorn              20.1.0
h11                   0.12.0
httpcore              0.13.7
httpx                 0.18.2
idna                  3.4
iniconfig             1.1.1
ipykernel             6.16.2
ipython               8.5.0
ipython-genutils      0.2.0
ipywidgets            7.7.2
iso8601               0.1.16
itsdangerous          2.1.2
jedi                  0.18.1
Jinja2                3.1.2
jsonrpcclient         4.0.2
jsonrpcserver         5.0.9
jsonschema            3.2.0
jupyter               1.0.0
jupyter-client        7.3.1
jupyter-console       6.4.4
jupyter_core          4.11.2
jupyter-server        1.15.6
jupyterlab-pygments   0.1.2
jupyterlab-widgets    1.1.1
kiwisolver            1.4.4
loguru                0.6.0
MarkupSafe            2.0.1
matplotlib            3.6.1
matplotlib-inline     0.1.6
mccabe                0.6.1
migra                 3.0.1621480950
mistune               0.8.4
more-itertools        8.14.0
multidict             6.0.2
nbclassic             0.4.5
nbclient              0.5.13
nbconvert             6.2.0
nbformat              5.7.0
nest-asyncio          1.5.6
notebook              6.5.1
notebook_shim         0.2.0
numpy                 1.23.4
OSlash                0.6.3
packaging             21.3
pandas                1.5.1
pandocfilters         1.5.0
parimutuelsdk         0.0.50
parso                 0.8.3
pexpect               4.8.0
pickleshare           0.7.5
Pillow                9.2.0
pip                   22.2.2
pluggy                0.13.1
podite                0.1.2
prometheus-client     0.13.1
prompt-toolkit        3.0.31
psutil                5.9.3
psycopg2              2.9.1
ptyprocess            0.7.0
pure-eval             0.2.2
py                    1.10.0
pycares               4.2.2
pycodestyle           2.8.0
pycparser             2.21
pyflakes              2.4.0
Pygments              2.13.0
pyheck                0.1.5
PyNaCl                1.5.0
pyparsing             3.0.7
pyrsistent            0.18.1
pytest                6.2.5
pytest-asyncio        0.17.2
pytest-xprocess       0.18.1
pythclient            0.1.2
python-dateutil       2.8.2
python-engineio       4.2.0
python-socketio       5.3.0
pytz                  2022.1
pytz-deprecation-shim 0.1.0.post0
pyzmq                 22.3.0
qtconsole             5.3.2
QtPy                  2.2.1
requests              2.27.1
rfc3986               1.5.0
schemainspect         3.0.1616029793
Send2Trash            1.8.0
setuptools            65.3.0
simple-websocket      0.2.0
six                   1.16.0
sniffio               1.3.0
solana                0.23.3
SQLAlchemy            1.4.22
sqlbag                0.1.1617247075
stack-data            0.5.1
sumtypes              0.1a6
terminado             0.13.3
testpath              0.5.0
toml                  0.10.2
toolz                 0.11.2
tornado               6.2
traitlets             5.5.0
types-cachetools      4.2.10
typing-extensions     3.10.0.2
tzdata                2022.5
tzlocal               4.2
urllib3               1.26.8
wcwidth               0.2.5
webencodings          0.5.1
websocket-client      1.2.3
websockets            10.4
Werkzeug              2.1.2
wheel                 0.37.1
widgetsnbextension    3.6.1
wsproto               1.0.0
yarl                  1.8.1
zope.event            4.5.0
zope.interface        5.4.0
zstandard             0.17.0
rcolomina commented 2 years ago

My issue was solved forcing anchorpy version to 0.11.0 and fighting a bit with the dependencies.

kevinheavey commented 2 years ago

So it works fine on the latest AnchorPy version?

kevinheavey commented 1 year ago

@rcolomina can you confirm?

kevinheavey commented 1 year ago

No response, closing

rcolomina commented 1 year ago

Yes, i does work. Many thanks!!