py-near 1.1.4__py3-none-any.whl → 1.1.7__py3-none-any.whl

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
py_near/account.py CHANGED
@@ -1,6 +1,9 @@
1
1
  import asyncio
2
+ import base64
2
3
  import collections
4
+ import hashlib
3
5
  import json
6
+ import logging
4
7
  from typing import List, Union, Dict, Optional
5
8
 
6
9
  import base58
@@ -13,7 +16,7 @@ from py_near import utils
13
16
  from py_near.dapps.ft.async_client import FT
14
17
  from py_near.dapps.phone.async_client import Phone
15
18
  from py_near.dapps.staking.async_client import Staking
16
- from py_near.exceptions.exceptions import parse_error
19
+ from py_near.exceptions.provider import RpcTimeoutError
17
20
  from py_near.models import (
18
21
  TransactionResult,
19
22
  ViewFunctionResult,
@@ -100,7 +103,7 @@ class Account(object):
100
103
  ]
101
104
  self._latest_block_hash_ts = utils.timestamp()
102
105
 
103
- async def _sign_and_submit_tx(
106
+ async def sign_and_submit_tx(
104
107
  self, receiver_id, actions: List[Action], nowait=False
105
108
  ) -> Union[TransactionResult, str]:
106
109
  """
@@ -120,6 +123,14 @@ class Account(object):
120
123
  await self._update_last_block_hash()
121
124
 
122
125
  block_hash = base58.b58decode(self._latest_block_hash.encode("utf8"))
126
+ trx_hash = transactions.calc_trx_hash(
127
+ self.account_id,
128
+ pk,
129
+ receiver_id,
130
+ access_key.nonce + 1,
131
+ actions,
132
+ block_hash,
133
+ )
123
134
  serialized_tx = transactions.sign_and_serialize_transaction(
124
135
  self.account_id,
125
136
  pk,
@@ -128,20 +139,20 @@ class Account(object):
128
139
  actions,
129
140
  block_hash,
130
141
  )
142
+
131
143
  if nowait:
132
144
  return await self._provider.send_tx(serialized_tx)
133
- result = await self._provider.send_tx_and_wait(serialized_tx)
134
-
135
- if "Failure" in result["status"]:
136
- if isinstance(result["status"]["Failure"]["ActionError"]["kind"], str):
137
- error_type = result["status"]["Failure"]["ActionError"]["kind"]
138
- raise parse_error(error_type, {})
139
-
140
- error_type, args = list(
141
- result["status"]["Failure"]["ActionError"]["kind"].items()
142
- )[0]
143
- raise parse_error(error_type, args)
144
- return TransactionResult(**result)
145
+ try:
146
+ result = await self._provider.send_tx_and_wait(serialized_tx)
147
+ return TransactionResult(**result)
148
+ except RpcTimeoutError:
149
+ for _ in range(constants.TIMEOUT_WAIT_RPC // 3):
150
+ await asyncio.sleep(3)
151
+ result = await self._provider.get_tx(trx_hash, receiver_id)
152
+ if result:
153
+ return result
154
+ raise RpcTimeoutError("Transaction not found")
155
+
145
156
  except Exception as e:
146
157
  raise e
147
158
  finally:
@@ -204,7 +215,7 @@ class Account(object):
204
215
  :param nowait: if nowait is True, return transaction hash, else wait execution
205
216
  :return: transaction hash or TransactionResult
206
217
  """
207
- return await self._sign_and_submit_tx(
218
+ return await self.sign_and_submit_tx(
208
219
  account_id, [transactions.create_transfer_action(amount)], nowait
209
220
  )
210
221
 
@@ -227,10 +238,14 @@ class Account(object):
227
238
  :param nowait: if nowait is True, return transaction hash, else wait execution
228
239
  :return: transaction hash or TransactionResult
229
240
  """
230
- args = json.dumps(args).encode("utf8")
231
- return await self._sign_and_submit_tx(
241
+ ser_args = json.dumps(args).encode("utf8")
242
+ return await self.sign_and_submit_tx(
232
243
  contract_id,
233
- [transactions.create_function_call_action(method_name, args, gas, amount)],
244
+ [
245
+ transactions.create_function_call_action(
246
+ method_name, ser_args, gas, amount
247
+ )
248
+ ],
234
249
  nowait,
235
250
  )
236
251
 
@@ -255,7 +270,7 @@ class Account(object):
255
270
  transactions.create_full_access_key_action(public_key),
256
271
  transactions.create_transfer_action(initial_balance),
257
272
  ]
258
- return await self._sign_and_submit_tx(account_id, actions, nowait)
273
+ return await self.sign_and_submit_tx(account_id, actions, nowait)
259
274
 
260
275
  async def add_public_key(
261
276
  self,
@@ -281,7 +296,7 @@ class Account(object):
281
296
  public_key, allowance, receiver_id, method_names
282
297
  ),
283
298
  ]
284
- return await self._sign_and_submit_tx(self.account_id, actions, nowait)
299
+ return await self.sign_and_submit_tx(self.account_id, actions, nowait)
285
300
 
286
301
  async def add_full_access_public_key(
287
302
  self, public_key: Union[str, bytes], nowait=False
@@ -295,7 +310,7 @@ class Account(object):
295
310
  actions = [
296
311
  transactions.create_full_access_key_action(public_key),
297
312
  ]
298
- return await self._sign_and_submit_tx(self.account_id, actions, nowait)
313
+ return await self.sign_and_submit_tx(self.account_id, actions, nowait)
299
314
 
300
315
  async def delete_public_key(self, public_key: Union[str, bytes], nowait=False):
301
316
  """
@@ -307,7 +322,7 @@ class Account(object):
307
322
  actions = [
308
323
  transactions.create_delete_access_key_action(public_key),
309
324
  ]
310
- return await self._sign_and_submit_tx(self.account_id, actions, nowait)
325
+ return await self.sign_and_submit_tx(self.account_id, actions, nowait)
311
326
 
312
327
  async def call_delegate_transaction(
313
328
  self,
@@ -323,9 +338,7 @@ class Account(object):
323
338
  actions = [
324
339
  transactions.create_signed_delegate(delegate_action, signature),
325
340
  ]
326
- return await self._sign_and_submit_tx(
327
- delegate_action.sender_id, actions, nowait
328
- )
341
+ return await self.sign_and_submit_tx(delegate_action.sender_id, actions, nowait)
329
342
 
330
343
  async def deploy_contract(self, contract_code: bytes, nowait=False):
331
344
  """
@@ -334,7 +347,7 @@ class Account(object):
334
347
  :param nowait: if nowait is True, return transaction hash, else wait execution
335
348
  :return: transaction hash or TransactionResult
336
349
  """
337
- return await self._sign_and_submit_tx(
350
+ return await self.sign_and_submit_tx(
338
351
  self.account_id,
339
352
  [transactions.create_deploy_contract_action(contract_code)],
340
353
  nowait,
@@ -348,7 +361,7 @@ class Account(object):
348
361
  :param nowait: if nowait is True, return transaction hash, else wait execution
349
362
  :return: transaction hash or TransactionResult
350
363
  """
351
- return await self._sign_and_submit_tx(
364
+ return await self.sign_and_submit_tx(
352
365
  self.account_id,
353
366
  [transactions.create_staking_action(public_key, amount)],
354
367
  nowait,
@@ -389,14 +402,14 @@ class Account(object):
389
402
  await self._update_last_block_hash()
390
403
 
391
404
  private_key = ed25519.SigningKey(pk)
392
- public_key = private_key.get_verifying_key()
405
+ verifying_key = private_key.get_verifying_key()
393
406
  return DelegateActionModel(
394
407
  sender_id=self.account_id,
395
408
  receiver_id=receiver_id,
396
409
  actions=actions,
397
410
  nonce=access_key.nonce + 1,
398
411
  max_block_height=self._latest_block_height + 1000,
399
- public_key=base58.b58encode(public_key.to_bytes()).decode("utf-8"),
412
+ public_key=base58.b58encode(verifying_key.to_bytes()).decode("utf-8"),
400
413
  )
401
414
 
402
415
  def sign_delegate_transaction(
py_near/dapps/fts.py CHANGED
@@ -11,6 +11,7 @@ class FTS:
11
11
  USDTe = FtModel("dac17f958d2ee523a2206206994597c13d831ec7.factory.bridge.near", 6)
12
12
  DAIe = FtModel("6b175474e89094c44da98b954eedeac495271d0f.factory.bridge.near", 18)
13
13
  USDCe = FtModel("a0b86991c6218b36c1d19d4a2e9eb0ce3606eb48.factory.bridge.near", 6)
14
+ USDC = FtModel("17208628f84f5d6ad33f0da3bbbeb27ffcb398eac501a31bd6ad2011e36133a1", 6)
14
15
  AURORA = FtModel("aaaaaa20d9e0e2461697782ef11675f668207961.factory.bridge.near", 18)
15
16
  WBTCe = FtModel("2260fac5e5542a773aa44fbcfedf7c193bc2c599.factory.bridge.near", 8)
16
17
  ETH = FtModel("aurora", 18)
@@ -1,13 +1,17 @@
1
1
  import json
2
2
  from dataclasses import dataclass
3
3
 
4
+ from py_near.exceptions.provider import LackBalanceForState
5
+
4
6
 
5
7
  class RpcNotAvailableError(Exception):
6
8
  pass
7
9
 
8
10
 
9
11
  class ActionErrorKind(Exception):
10
- pass
12
+ def __init__(self, **kargs):
13
+ for arg, value in kargs.items():
14
+ setattr(self, arg, value)
11
15
 
12
16
 
13
17
  @dataclass
@@ -170,6 +174,7 @@ class NewReceiptValidationError(ActionErrorKind):
170
174
 
171
175
  pass
172
176
 
177
+
173
178
  class DelegateActionExpired(ActionErrorKind):
174
179
  """
175
180
  Error occurs when a new `DelegateActionExpired` created by the `FunctionCall` action fails
@@ -193,7 +198,8 @@ _ERROR_TYPE_TO_EXCEPTION = {
193
198
  "FunctionCallError": FunctionCallError,
194
199
  "ExecutionError": ExecutionError,
195
200
  "NewReceiptValidationError": NewReceiptValidationError,
196
- "'DelegateActionExpired'": DelegateActionExpired,
201
+ "DelegateActionExpired": DelegateActionExpired,
202
+ "LackBalanceForState": LackBalanceForState,
197
203
  }
198
204
 
199
205
 
py_near/providers.py CHANGED
@@ -2,7 +2,7 @@ import base64
2
2
  import json
3
3
 
4
4
  import aiohttp
5
- from aiohttp import ClientResponseError, ClientConnectorError
5
+ from aiohttp import ClientResponseError, ClientConnectorError, ServerDisconnectedError
6
6
  from loguru import logger
7
7
 
8
8
  from py_near.constants import TIMEOUT_WAIT_RPC
@@ -72,6 +72,7 @@ class JsonProvider(object):
72
72
  RpcTimeoutError,
73
73
  ClientResponseError,
74
74
  ClientConnectorError,
75
+ ServerDisconnectedError,
75
76
  ConnectionError,
76
77
  ) as e:
77
78
  logger.error(f"Rpc error: {e}")
py_near/transactions.py CHANGED
@@ -49,6 +49,33 @@ def sign_and_serialize_transaction(
49
49
  return base64.b64encode(signed_trx).decode("utf-8")
50
50
 
51
51
 
52
+ def calc_trx_hash(
53
+ account_id,
54
+ private_key,
55
+ receiver_id,
56
+ nonce,
57
+ actions: List[Action],
58
+ block_hash: bytes,
59
+ ) -> str:
60
+ if isinstance(private_key, str):
61
+ pk = base58.b58decode(private_key.replace("ed25519:", ""))
62
+ else:
63
+ pk = private_key
64
+ private_key = ed25519.SigningKey(pk)
65
+
66
+ transaction = Transaction(
67
+ account_id,
68
+ private_key.get_verifying_key().to_bytes(),
69
+ nonce,
70
+ receiver_id,
71
+ block_hash,
72
+ actions,
73
+ )
74
+
75
+ signed_trx = bytes(bytearray(transaction.get_hash()))
76
+ return base58.b58encode(signed_trx).decode("utf-8")
77
+
78
+
52
79
  def create_create_account_action():
53
80
  return CreateAccountAction()
54
81
 
@@ -1,6 +1,6 @@
1
1
  Metadata-Version: 2.1
2
2
  Name: py-near
3
- Version: 1.1.4
3
+ Version: 1.1.7
4
4
  Summary: Pretty simple and fully asynchronous framework for working with NEAR blockchain
5
5
  Author: pvolnov
6
6
  Author-email: petr@herewallet.app
@@ -13,7 +13,7 @@ Classifier: Programming Language :: Python :: 3.10
13
13
  Classifier: Programming Language :: Python :: 3.11
14
14
  Requires-Dist: aiohttp (>=3.7.4,<4.0.0)
15
15
  Requires-Dist: ed25519 (>=1.5,<2.0)
16
- Requires-Dist: py-near-primitives (>=0.2.2,<0.3.0)
16
+ Requires-Dist: py-near-primitives (>=0.2.1,<0.3.0)
17
17
  Description-Content-Type: text/markdown
18
18
 
19
19
  # py-near
@@ -1,5 +1,5 @@
1
1
  py_near/__init__.py,sha256=t5fAxjaU8dN8xpQR2vz0ZGhfTkdVy2RCbkhJhZFglk4,50
2
- py_near/account.py,sha256=MBpGKu6O7fKmx7faj-Y0opAEEzHecrW4Fy99K9p3Ncc,16398
2
+ py_near/account.py,sha256=FYnGZK1rLhxE-hSzYThK0yfpE_pYIvdmryxU650Xmvg,16648
3
3
  py_near/constants.py,sha256=inaWIuwmF1EB5JSB0ynnZY5rKY_QsxhF9KuCOhPsM6k,164
4
4
  py_near/dapps/__init__.py,sha256=47DEQpj8HBSa-_TImW-5JCeuQeRkm5NMpJWZG3hSuFU,0
5
5
  py_near/dapps/core.py,sha256=LtN9aW2gw2mvEdhzQcQJIidtjv-XL1xjb0LK8DzqtqE,231
@@ -7,7 +7,7 @@ py_near/dapps/ft/__init__.py,sha256=hx8qh4yEs37Ul_JhDWn8LHd-9lzsaATivxKOJPaUqTE,
7
7
  py_near/dapps/ft/async_client.py,sha256=Oir5EQOGueq0lsNn0ybe_1uvMnmV5K1m_mHFuFWIUu4,6717
8
8
  py_near/dapps/ft/exceptions.py,sha256=UjXLFsDQX0vDGS9CGO7HE9XpLD0vovFNUzCb11WKAtc,92
9
9
  py_near/dapps/ft/models.py,sha256=_URS8nXZvfuxjU7Ov2E-nteBak5qKtPKN3bLOR7sC9k,186
10
- py_near/dapps/fts.py,sha256=hyhTY6-l6dhTwBhxa4RP_TRlM3qXBAvdP2wVvNyT4As,856
10
+ py_near/dapps/fts.py,sha256=ViMoQx0h-sgVTozr-JZk3hUGdGxsVP3oP3sAY8i8g5Q,946
11
11
  py_near/dapps/keypom/__init__.py,sha256=6fWGxsVr_lb_wTf_MHrbwBl1fgf78b5ezmsVa3JeFjI,32
12
12
  py_near/dapps/keypom/async_client.py,sha256=DS594S00xhcyvVtD1W0PjmtmT3YhvXTZZ3hskbn1-1w,1422
13
13
  py_near/dapps/keypom/exceptions.py,sha256=pGz0w1Ubh2A1aHEWvUX4dVaCcO9r2Y-h2JwhBY5KJbU,45
@@ -21,13 +21,13 @@ py_near/dapps/staking/async_client.py,sha256=TYudiPwF4tQ03rZuWNL-PUQ2kKpx06BxWWm
21
21
  py_near/dapps/staking/exceptions.py,sha256=UjXLFsDQX0vDGS9CGO7HE9XpLD0vovFNUzCb11WKAtc,92
22
22
  py_near/dapps/staking/models.py,sha256=zC5M_pc1oMqHq4GaYif1uwFbW6acD2BsiA9rbyiaUTs,124
23
23
  py_near/exceptions/__init__.py,sha256=47DEQpj8HBSa-_TImW-5JCeuQeRkm5NMpJWZG3hSuFU,0
24
- py_near/exceptions/exceptions.py,sha256=8gFkZpro3c-QrCUJmUSrgg3mCAMUl3VAf0UQPUkV2Tg,4839
24
+ py_near/exceptions/exceptions.py,sha256=5iOeRpoVy63hPF6PC-wQkj7k4PHsn2NtxTTl9Jkp88M,5050
25
25
  py_near/exceptions/provider.py,sha256=NU_sGBVmxJbDOANl35ADxZe9fpHLvwFBk_BQNiLMhgw,7642
26
26
  py_near/models.py,sha256=QKQxL1wkTKZ4MWb5dprJZYalzrvd7bHSXC3WeBujuWE,9426
27
- py_near/providers.py,sha256=8ZpZU3UC7UEcaiC1unkKDHbfOSd8scEGTmoeoZhAAXE,8328
28
- py_near/transactions.py,sha256=B1_KB48te-WP2SECvFwCMHym9epJfiNBUX6tVSZXTmQ,2566
27
+ py_near/providers.py,sha256=gI_xYFr_3xMleyDkI_3LpkcW4-yaDy5QSRzZKe3FkNM,8394
28
+ py_near/transactions.py,sha256=QAXegv2JpKISk92NaChtIH6-QPHrcWbrwdKH_lH4TsU,3186
29
29
  py_near/utils.py,sha256=FirRH93ydH1cwjn0-sNrZeIn3BRD6QHedrP2VkAdJ6g,126
30
- py_near-1.1.4.dist-info/LICENSE,sha256=I_GOA9xJ35FiL-KnYXZJdATkbO2KcV2dK2enRGVxzKM,1023
31
- py_near-1.1.4.dist-info/METADATA,sha256=5DgNLRG7PgA5iEx_ItXSUUnWfkfyKxCtrsr5i7UT4UU,4661
32
- py_near-1.1.4.dist-info/WHEEL,sha256=Zb28QaM1gQi8f4VCBhsUklF61CTlNYfs9YAZn-TOGFk,88
33
- py_near-1.1.4.dist-info/RECORD,,
30
+ py_near-1.1.7.dist-info/LICENSE,sha256=I_GOA9xJ35FiL-KnYXZJdATkbO2KcV2dK2enRGVxzKM,1023
31
+ py_near-1.1.7.dist-info/METADATA,sha256=4DKDgC8HX6ayN73Ni8l7RqAyKye1lHdUrmPLKUpbWBU,4661
32
+ py_near-1.1.7.dist-info/WHEEL,sha256=Zb28QaM1gQi8f4VCBhsUklF61CTlNYfs9YAZn-TOGFk,88
33
+ py_near-1.1.7.dist-info/RECORD,,