Coder Social home page Coder Social logo

pvolnov / py-near Goto Github PK

View Code? Open in Web Editor NEW
144.0 4.0 10.0 109 KB

Python high-level framework for NEAR Protocol

Home Page: https://py-near.readthedocs.io/

License: MIT License

Python 100.00%
asynchronous near near-protocol nearprotocol python web3py

py-near's Introduction

py-near

Financial Contributors on Open Collective PyPi Package Version Supported python versions Documentation Status Github issues MIT License Twitter

py-near is a pretty simple and fully asynchronous framework for working with NEAR blockchain.

Examples

πŸ“š Click to see some basic examples

Few steps before getting started...

  • Install the latest stable version of py-near, simply running pip install py-near
  • Create NEAR account and get your private key wallet

Simple money transfer

from py_near.account import Account
import asyncio
from py_near.dapps.core import NEAR

ACCOUNT_ID = "bob.near"
PRIVATE_KEY = "ed25519:..."


async def main():
   acc = Account(ACCOUNT_ID, PRIVATE_KEY)

   await acc.startup()
   print(await acc.get_balance() / NEAR)
   print(await acc.get_balance("bob.near") / NEAR)

   tr = await acc.send_money("bob.near", NEAR * 2)
   print(tr.transaction.hash)
   print(tr.logs)


asyncio.run(main())

Transfer money by phone number

from py_near.account import Account
import asyncio
from py_near.dapps.core import NEAR

ACCOUNT_ID = "bob.near"
PRIVATE_KEY = "ed25519:..."


async def main():
   acc = Account(ACCOUNT_ID, PRIVATE_KEY)

   await acc.startup()
   tr = await acc.phone.send_near_to_phone("+15626200911", NEAR // 10)
   print(tr.transaction.hash)


asyncio.run(main())

Parallel requests

Only one parallel request can be made from one private key. All transaction calls execute sequentially. To make several parallel calls you need to use several private keys

acc = Account("bob.near", private_key1)

for i in range(2):
  signer = InMemorySigner.from_random(AccountId("bob.near"), KeyType.ED25519)
  await acc.add_full_access_public_key(str(signer.public_key))
  print(signer.secret_key)

Now we can call transactions in parallel

acc = Account("bob.near", [private_key1, private_key2, private_key3])
# request time = count transactions / count public keys
tasks = [
  asyncio.create_task(acc.send_money("alisa.near", 1)),
  asyncio.create_task(acc.send_money("alisa.near", 1)),
  asyncio.create_task(acc.send_money("alisa.near", 1)),
]
for t in task:
  await t

Official py-near resources:

Contributors

Code Contributors

This project exists thanks to all the people who contribute. [Code of conduct].

py-near's People

Contributors

bxdoan avatar pvolnov avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar  avatar  avatar

py-near's Issues

JSONDecodeError while parsing error messages

An example of an actual error field of an RPC response:

{"name": "HANDLER_ERROR", "cause": {"info": {}, "name": "INVALID_TRANSACTION"}, "code": -32000, "message": "Server error", "data": {"TxExecutionError": {"InvalidTxError": "Expired"}}}

When fed into JsonProvider.get_error_from_response it raises an exception:

json.decoder.JSONDecodeError: Expecting value: line 1 column 1 (char 0)

The correct behavior would be to return an instance of py_near.exceptions.provider.Expired.

add NFT transfer support

  • Transfer NFT to any user
  • Sell/buy NFT on marketplaces
  • Send NFT by phone number
  • add NFT metadata parsers
  • parsing NFT collection holders
  • get royalty data

function_call the call function generate two transactions?

async def call_report_prices():
    try:
        accountId = ""
        privateKey = ""
        rpc = "https://rpc.mainnet.near.org"

        account = Account(accountId, privateKey, rpc)

        sender = "inscription.near"
        method_name = "inscribe"
        method_args = {
            "p": "nrc-20",
            "op": "mint",
            "tick": "neat",
            "amt": "100000000"
        }

        result = await account.function_call(sender, method_name, method_args)
        
        print("Transaction Hash:", result.transaction.hash)
        print("Transaction Hash url",result.transaction.url)
    except Exception as e:
        print(f"Error in call_report_prices: {e}")

async def main():
    for _ in range(2):
        await call_report_prices()
        await asyncio.sleep(1)

if __name__ == "__main__":
    asyncio.run(main())

Hey, I would like to ask if I can use the function_ When using the call function to execute the contract method, two identical hash transactions will appear. Firstly, the first transaction is sent to me by the system, and then I send it to the method I call, Inscribe. It will continue to throw rpc timeout errors. I want to know if the transaction sent to me by the system was generated by SDK?
https://nearblocks.io/zh-cn/address/0e82f1241097f1ffefb99a1a167cc91470700b0ac4b6a9870200514ef2dd43a7

Error while entering command pip install py-near

I am trying to install py-near for my project.Getting error
Collecting py-near
Using cached py_near-1.1.24-py3-none-any.whl.metadata (4.7 kB)
Requirement already satisfied: aiohttp<4.0.0,>=3.7.4 in c:\users\administrator.examnr-04\appdata\local\programs\python\python311\lib\site-packages (from py-near) (3.8.5)
Collecting ed25519<2.0,>=1.5 (from py-near)
Using cached ed25519-1.5.tar.gz (868 kB)
Preparing metadata (setup.py) ... done
Collecting py-near-primitives<0.3.0,>=0.2.3 (from py-near)
Using cached py_near_primitives-0.2.3-cp37-abi3-win_amd64.whl.metadata (1.4 kB)
Requirement already satisfied: attrs>=17.3.0 in c:\users\administrator.examnr-04\appdata\local\programs\python\python311\lib\site-packages (from aiohttp<4.0.0,>=3.7.4->py-near) (23.1.0)
Requirement already satisfied: charset-normalizer<4.0,>=2.0 in c:\users\administrator.examnr-04\appdata\local\programs\python\python311\lib\site-packages (from aiohttp<4.0.0,>=3.7.4->py-near) (3.1.0)
Requirement already satisfied: multidict<7.0,>=4.5 in c:\users\administrator.examnr-04\appdata\local\programs\python\python311\lib\site-packages (from aiohttp<4.0.0,>=3.7.4->py-near) (6.0.4)
Requirement already satisfied: async-timeout<5.0,>=4.0.0a3 in c:\users\administrator.examnr-04\appdata\local\programs\python\python311\lib\site-packages (from aiohttp<4.0.0,>=3.7.4->py-near) (4.0.3)
Requirement already satisfied: yarl<2.0,>=1.0 in c:\users\administrator.examnr-04\appdata\local\programs\python\python311\lib\site-packages (from aiohttp<4.0.0,>=3.7.4->py-near) (1.9.2)
Requirement already satisfied: frozenlist>=1.1.1 in c:\users\administrator.examnr-04\appdata\local\programs\python\python311\lib\site-packages (from aiohttp<4.0.0,>=3.7.4->py-near) (1.4.0)
Requirement already satisfied: aiosignal>=1.1.2 in c:\users\administrator.examnr-04\appdata\local\programs\python\python311\lib\site-packages (from aiohttp<4.0.0,>=3.7.4->py-near) (1.3.1)
Requirement already satisfied: idna>=2.0 in c:\users\administrator.examnr-04\appdata\local\programs\python\python311\lib\site-packages (from yarl<2.0,>=1.0->aiohttp<4.0.0,>=3.7.4->py-near) (3.4)
Using cached py_near-1.1.24-py3-none-any.whl (25 kB)
Using cached py_near_primitives-0.2.3-cp37-abi3-win_amd64.whl (1.5 MB)
Building wheels for collected packages: ed25519
Building wheel for ed25519 (setup.py) ... error
error: subprocess-exited-with-error

Γ— python setup.py bdist_wheel did not run successfully.
β”‚ exit code: 1
╰─> [3 lines of output]
set build\lib.win-amd64-cpython-311\ed25519/_version.py to '1.5'
error: Microsoft Visual C++ 14.0 or greater is required. Get it with "Microsoft C++ Build Tools": https://visualstudio.microsoft.com/visual-cpp-build-tools/
[end of output]

note: This error originates from a subprocess, and is likely not a problem with pip.
ERROR: Failed building wheel for ed25519
Running setup.py clean for ed25519
Failed to build ed25519
ERROR: Could not build wheels for ed25519, which is required to install pyproject.toml-based projects
Anyone help me with it

Error when type "pip install py-near", my Python version is 3.12.1

C:\Users\Admin>pip install py-near
Collecting py-near
Using cached py_near-1.1.30-py3-none-any.whl.metadata (4.7 kB)
Requirement already satisfied: aiohttp<4.0.0,>=3.7.4 in c:\users\admin\appdata\local\programs\python\python312\lib\site-packages (from py-near) (3.9.3)
Collecting ed25519<2.0,>=1.5 (from py-near)
Using cached ed25519-1.5.tar.gz (868 kB)
Installing build dependencies ... done
Getting requirements to build wheel ... error
error: subprocess-exited-with-error

Γ— Getting requirements to build wheel did not run successfully.
β”‚ exit code: 1
╰─> [31 lines of output]
C:\Users\Admin\AppData\Local\Temp\pip-install-x35th7se\ed25519_c378159047794ef0bc923945ed0239fe\versioneer.py:467: SyntaxWarning: invalid escape sequence '\s'
LONG_VERSION_PY['git'] = '''
Traceback (most recent call last):
File "C:\Users\Admin\AppData\Local\Programs\Python\Python312\Lib\site-packages\pip_vendor\pyproject_hooks_in_process_in_process.py", line 353, in
main()
File "C:\Users\Admin\AppData\Local\Programs\Python\Python312\Lib\site-packages\pip_vendor\pyproject_hooks_in_process_in_process.py", line 335, in main
json_out['return_val'] = hook(**hook_input['kwargs'])
^^^^^^^^^^^^^^^^^^^^^^^^^^^^
File "C:\Users\Admin\AppData\Local\Programs\Python\Python312\Lib\site-packages\pip_vendor\pyproject_hooks_in_process_in_process.py", line 118, in get_requires_for_build_wheel
return hook(config_settings)
^^^^^^^^^^^^^^^^^^^^^
File "C:\Users\Admin\AppData\Local\Temp\pip-build-env-4jer5g7j\overlay\Lib\site-packages\setuptools\build_meta.py", line 325, in get_requires_for_build_wheel
return self._get_build_requires(config_settings, requirements=['wheel'])
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
File "C:\Users\Admin\AppData\Local\Temp\pip-build-env-4jer5g7j\overlay\Lib\site-packages\setuptools\build_meta.py", line 295, in _get_build_requires
self.run_setup()
File "C:\Users\Admin\AppData\Local\Temp\pip-build-env-4jer5g7j\overlay\Lib\site-packages\setuptools\build_meta.py", line 480, in run_setup
super().run_setup(setup_script=setup_script)
File "C:\Users\Admin\AppData\Local\Temp\pip-build-env-4jer5g7j\overlay\Lib\site-packages\setuptools\build_meta.py", line 311, in run_setup
exec(code, locals())
File "", line 115, in
File "C:\Users\Admin\AppData\Local\Temp\pip-install-x35th7se\ed25519_c378159047794ef0bc923945ed0239fe\versioneer.py", line 1405, in get_version
return get_versions()["version"]
^^^^^^^^^^^^^^
File "C:\Users\Admin\AppData\Local\Temp\pip-install-x35th7se\ed25519_c378159047794ef0bc923945ed0239fe\versioneer.py", line 1339, in get_versions
cfg = get_config_from_root(root)
^^^^^^^^^^^^^^^^^^^^^^^^^^
File "C:\Users\Admin\AppData\Local\Temp\pip-install-x35th7se\ed25519_c378159047794ef0bc923945ed0239fe\versioneer.py", line 399, in get_config_from_root
parser = configparser.SafeConfigParser()
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
AttributeError: module 'configparser' has no attribute 'SafeConfigParser'. Did you mean: 'RawConfigParser'?
[end of output]

note: This error originates from a subprocess, and is likely not a problem with pip.
error: subprocess-exited-with-error

Γ— Getting requirements to build wheel did not run successfully.
β”‚ exit code: 1
╰─> See above for output.

note: This error originates from a subprocess, and is likely not a problem with pip.

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    πŸ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. πŸ“ŠπŸ“ˆπŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❀️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.