Skip to content

Commit

Permalink
feat: USDC
Browse files Browse the repository at this point in the history
feat: wait transactions with RPC Timeout
  • Loading branch information
pvolnov committed Oct 3, 2023
1 parent 7106eb9 commit e3c734d
Show file tree
Hide file tree
Showing 3 changed files with 61 additions and 21 deletions.
2 changes: 1 addition & 1 deletion pyproject.toml
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ readme = "README.md"
python = "^3.7"
ed25519 = "^1.5"
aiohttp = "^3.7.4"
py-near-primitives = "^0.2.2"
py-near-primitives = "^0.2.1"

[tool.poetry.dev-dependencies]
black = "^21.12b0"
Expand Down
53 changes: 33 additions & 20 deletions src/py_near/account.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,9 @@
import asyncio
import base64
import collections
import hashlib
import json
import logging
from typing import List, Union, Dict, Optional

import base58
Expand All @@ -13,7 +16,7 @@
from py_near.dapps.ft.async_client import FT
from py_near.dapps.phone.async_client import Phone
from py_near.dapps.staking.async_client import Staking
from py_near.exceptions.exceptions import parse_error
from py_near.exceptions.provider import RpcTimeoutError
from py_near.models import (
TransactionResult,
ViewFunctionResult,
Expand Down Expand Up @@ -120,6 +123,14 @@ async def sign_and_submit_tx(
await self._update_last_block_hash()

block_hash = base58.b58decode(self._latest_block_hash.encode("utf8"))
trx_hash = transactions.calc_trx_hash(
self.account_id,
pk,
receiver_id,
access_key.nonce + 1,
actions,
block_hash,
)
serialized_tx = transactions.sign_and_serialize_transaction(
self.account_id,
pk,
Expand All @@ -128,20 +139,20 @@ async def sign_and_submit_tx(
actions,
block_hash,
)

if nowait:
return await self._provider.send_tx(serialized_tx)
result = await self._provider.send_tx_and_wait(serialized_tx)

if "Failure" in result["status"]:
if isinstance(result["status"]["Failure"]["ActionError"]["kind"], str):
error_type = result["status"]["Failure"]["ActionError"]["kind"]
raise parse_error(error_type, {})

error_type, args = list(
result["status"]["Failure"]["ActionError"]["kind"].items()
)[0]
raise parse_error(error_type, args)
return TransactionResult(**result)
try:
result = await self._provider.send_tx_and_wait(serialized_tx)
return TransactionResult(**result)
except RpcTimeoutError:
for _ in range(constants.TIMEOUT_WAIT_RPC // 3):
await asyncio.sleep(3)
result = await self._provider.get_tx(trx_hash, receiver_id)
if result:
return result
raise RpcTimeoutError("Transaction not found")

except Exception as e:
raise e
finally:
Expand Down Expand Up @@ -227,10 +238,14 @@ async def function_call(
:param nowait: if nowait is True, return transaction hash, else wait execution
:return: transaction hash or TransactionResult
"""
args = json.dumps(args).encode("utf8")
ser_args = json.dumps(args).encode("utf8")
return await self.sign_and_submit_tx(
contract_id,
[transactions.create_function_call_action(method_name, args, gas, amount)],
[
transactions.create_function_call_action(
method_name, ser_args, gas, amount
)
],
nowait,
)

Expand Down Expand Up @@ -323,9 +338,7 @@ async def call_delegate_transaction(
actions = [
transactions.create_signed_delegate(delegate_action, signature),
]
return await self.sign_and_submit_tx(
delegate_action.sender_id, actions, nowait
)
return await self.sign_and_submit_tx(delegate_action.sender_id, actions, nowait)

async def deploy_contract(self, contract_code: bytes, nowait=False):
"""
Expand Down Expand Up @@ -389,14 +402,14 @@ async def create_delegate_action(
await self._update_last_block_hash()

private_key = ed25519.SigningKey(pk)
public_key = private_key.get_verifying_key()
verifying_key = private_key.get_verifying_key()
return DelegateActionModel(
sender_id=self.account_id,
receiver_id=receiver_id,
actions=actions,
nonce=access_key.nonce + 1,
max_block_height=self._latest_block_height + 1000,
public_key=base58.b58encode(public_key.to_bytes()).decode("utf-8"),
public_key=base58.b58encode(verifying_key.to_bytes()).decode("utf-8"),
)

def sign_delegate_transaction(
Expand Down
27 changes: 27 additions & 0 deletions src/py_near/transactions.py
Original file line number Diff line number Diff line change
Expand Up @@ -49,6 +49,33 @@ def sign_and_serialize_transaction(
return base64.b64encode(signed_trx).decode("utf-8")


def calc_trx_hash(
account_id,
private_key,
receiver_id,
nonce,
actions: List[Action],
block_hash: bytes,
) -> str:
if isinstance(private_key, str):
pk = base58.b58decode(private_key.replace("ed25519:", ""))
else:
pk = private_key
private_key = ed25519.SigningKey(pk)

transaction = Transaction(
account_id,
private_key.get_verifying_key().to_bytes(),
nonce,
receiver_id,
block_hash,
actions,
)

signed_trx = bytes(bytearray(transaction.get_hash()))
return base58.b58encode(signed_trx).decode("utf-8")


def create_create_account_action():
return CreateAccountAction()

Expand Down

0 comments on commit e3c734d

Please sign in to comment.