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

Send tx cleanup #11

Merged
merged 2 commits into from
Nov 17, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion pyproject.toml
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ authors = ["kevinheavey <kevinheavey123@gmail.com>"]
[tool.poetry.dependencies]
python = "^3.9"
construct-typing = "^0.5.1"
solana = "^0.18.1"
solana = "^0.18.2"
sumtypes = "^0.1a5"
apischema = "^0.15.6"
inflection = "^0.5.1"
Expand Down
62 changes: 6 additions & 56 deletions src/anchorpy/provider.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,17 +4,14 @@
from pathlib import Path
from os import getenv, environ
import json
import asyncio
import time

from abc import abstractmethod, ABC
from typing import Optional, Union, NamedTuple, cast
from typing import Optional, Union, NamedTuple

from solana.keypair import Keypair
from solana.rpc import types
from solana.rpc.async_api import AsyncClient
from solana.rpc.commitment import Finalized, Processed, Confirmed, Commitment
from solana.rpc.core import RPCException
from solana.rpc.commitment import Finalized, Processed, Confirmed
from solana.transaction import Transaction, TransactionSignature
from solana.publickey import PublicKey

Expand Down Expand Up @@ -128,55 +125,8 @@ async def send(
if opts is None:
opts = self.opts
all_signers = [self.wallet.payer] + signers
raw_resp = await self.client.send_transaction(
tx, *all_signers, opts=opts._replace(skip_confirmation=True)
)
resp = cast(
TransactionSignature,
raw_resp["result"],
)
if opts.skip_preflight:
return resp # TODO this looks wrong
await self._confirm_transaction(resp, commitment=opts.preflight_commitment)
return resp

async def _confirm_transaction(
self,
tx_sig: str,
commitment: Commitment = Finalized,
) -> types.RPCResponse:
timeout = time.time() + 30
while time.time() < timeout:
resp = await self.client.get_signature_statuses([tx_sig])
resp_value = resp["result"]["value"][0]
if resp_value is not None:
confirmation_status = resp_value["confirmationStatus"]
confirmation_rank = COMMITMENT_RANKS[confirmation_status]
commitment_rank = COMMITMENT_RANKS[commitment]
if confirmation_rank >= commitment_rank:
break
await asyncio.sleep(0.5)
else:
maybe_rpc_error = resp.get("error")
if maybe_rpc_error is not None:
raise RPCException(maybe_rpc_error)
raise UnconfirmedTxError(f"Unable to confirm transaction {tx_sig}")
return resp

async def _send_raw_transaction(
self, txn: Union[bytes, str], opts: types.TxOpts = DEFAULT_OPTIONS
) -> TransactionSignature:
resp = await self.client.send_raw_transaction(
txn, opts._replace(skip_confirmation=True)
)
signature = cast(
TransactionSignature,
resp["result"],
)
if opts.skip_confirmation:
return signature
self._confirm_transaction(signature, opts.preflight_commitment)
return signature
resp = await self.client.send_transaction(tx, *all_signers, opts=opts)
return resp["result"]

async def send_all(
self,
Expand Down Expand Up @@ -205,8 +155,8 @@ async def send_all(
signed_txs = self.wallet.sign_all_transactions(txs)
sigs = []
for signed in signed_txs:
res = await self._send_raw_transaction(signed.serialize(), opts=opts)
sigs.append(res)
resp = await self.client.send_raw_transaction(signed.serialize(), opts=opts)
sigs.append(resp["result"])
return sigs

async def __aenter__(self) -> Provider:
Expand Down