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

Remove transaction module #442

Open
wants to merge 15 commits into
base: master
Choose a base branch
from
70 changes: 17 additions & 53 deletions src/solana/rpc/api.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,6 @@
from time import sleep, time
from typing import Dict, List, Optional, Sequence, Union

from solders.hash import Hash as Blockhash
from solders.keypair import Keypair
from solders.message import VersionedMessage
from solders.pubkey import Pubkey
from solders.rpc.responses import (
Expand Down Expand Up @@ -64,9 +62,9 @@
from solders.transaction import VersionedTransaction

from solana.rpc import types
from solana.transaction import Transaction
from solders.transaction import Transaction

from .commitment import Commitment, Finalized
from .commitment import Commitment
from .core import (
_COMMITMENT_TO_SOLDERS,
RPCException,
Expand Down Expand Up @@ -407,13 +405,13 @@ def get_fee_for_message(
Example:
>>> from solders.keypair import Keypair
>>> from solders.system_program import TransferParams, transfer
>>> from solana.transaction import Transaction
>>> from solders.message import Message
>>> leading_zeros = [0] * 31
>>> sender, receiver = Keypair.from_seed(leading_zeros + [1]), Keypair.from_seed(leading_zeros + [2])
>>> txn = Transaction().add(transfer(TransferParams(
... from_pubkey=sender.pubkey(), to_pubkey=receiver.pubkey(), lamports=1000)))
>>> msg = Message([transfer(TransferParams(
... from_pubkey=sender.pubkey(), to_pubkey=receiver.pubkey(), lamports=1000))])
>>> solana_client = Client("http://localhost:8899")
>>> solana_client.get_fee_for_message(txn.compile_message()).value # doctest: +SKIP
>>> solana_client.get_fee_for_message(msg).value # doctest: +SKIP
5000
"""
body = self._get_fee_for_message_body(message, commitment)
Expand Down Expand Up @@ -999,65 +997,30 @@ def send_raw_transaction(self, txn: bytes, opts: Optional[types.TxOpts] = None)
def send_transaction(
self,
txn: Union[VersionedTransaction, Transaction],
*signers: Keypair,
opts: Optional[types.TxOpts] = None,
recent_blockhash: Optional[Blockhash] = None,
) -> SendTransactionResp:
"""Send a transaction.

Args:
txn: transaction object.
signers: Signers to sign the transaction. Only supported for legacy Transaction.
opts: (optional) Transaction options.
recent_blockhash: (optional) Pass a valid recent blockhash here if you want to
skip fetching the recent blockhash or relying on the cache.
Only supported for legacy Transaction.

Example:
>>> from solders.keypair import Keypair
>>> from solders.pubkey import Pubkey
>>> from solana.rpc.api import Client
>>> from solders.system_program import TransferParams, transfer
>>> from solana.transaction import Transaction
>>> from solders.message import Message
>>> leading_zeros = [0] * 31
>>> sender, receiver = Keypair.from_seed(leading_zeros + [1]), Keypair.from_seed(leading_zeros + [2])
>>> txn = Transaction().add(transfer(TransferParams(
... from_pubkey=sender.pubkey(), to_pubkey=receiver.pubkey(), lamports=1000)))
>>> solana_client = Client("http://localhost:8899")
>>> solana_client.send_transaction(txn, sender).value # doctest: +SKIP
Signature(
1111111111111111111111111111111111111111111111111111111111111111,
)
"""
if isinstance(txn, VersionedTransaction):
if signers:
msg = "*signers args are not used when sending VersionedTransaction."
raise ValueError(msg)
if recent_blockhash is not None:
msg = "recent_blockhash arg is not used when sending VersionedTransaction."
raise ValueError(msg)
versioned_tx_opts = types.TxOpts(preflight_commitment=self._commitment) if opts is None else opts
return self.send_raw_transaction(bytes(txn), opts=versioned_tx_opts)
last_valid_block_height = None
if recent_blockhash is None:
blockhash_resp = self.get_latest_blockhash(Finalized)
recent_blockhash = self.parse_recent_blockhash(blockhash_resp)
last_valid_block_height = blockhash_resp.value.last_valid_block_height

txn.recent_blockhash = recent_blockhash

txn.sign(*signers)
opts_to_use = (
types.TxOpts(
preflight_commitment=self._commitment,
last_valid_block_height=last_valid_block_height,
)
if opts is None
else opts
)

txn_resp = self.send_raw_transaction(txn.serialize(), opts=opts_to_use)
return txn_resp
>>> ixns = [transfer(TransferParams(
... from_pubkey=sender.pubkey(), to_pubkey=receiver.pubkey(), lamports=1000))]
>>> msg = Message(ixns, sender.pubkey())
>>> client = Client("http://localhost:8899")
>>> client.send_transaction(Transaction([sender], msg, client.get_latest_blockhash()).value.blockhash) # doctest: +SKIP
""" # noqa: E501
tx_opts = types.TxOpts(preflight_commitment=self._commitment) if opts is None else opts
return self.send_raw_transaction(bytes(txn), opts=tx_opts)

def simulate_transaction(
self,
Expand All @@ -1074,6 +1037,7 @@ def simulate_transaction(
commitment: Bank state to query. It can be either "finalized", "confirmed" or "processed".

Example:
>>> from solders.transaction import Transaction
>>> solana_client = Client("http://localhost:8899")
>>> full_signed_tx_hex = (
... '01b3795ccfaac3eee838bb05c3b8284122c18acedcd645c914fe8e178c3b62640d8616d061cc818b26cab8ecf3855ecc'
Expand All @@ -1082,7 +1046,7 @@ def simulate_transaction(
... '000000000000000000000000000000000000000000839618f701ba7e9ba27ae59825dd6d6bb66d14f6d5d0eae215161d7'
... '1851a106901020200010c0200000040420f0000000000'
... )
>>> tx = Transaction.deserialize(bytes.fromhex(full_signed_tx_hex))
>>> tx = Transaction.from_bytes(bytes.fromhex(full_signed_tx_hex))
>>> solana_client.simulate_transaction(tx).value.logs # doctest: +SKIP
['BPF program 83astBRguLMdt2h5U1Tpdq5tjFoJ6noeGwaY3mDLVcri success']
"""
Expand Down
68 changes: 16 additions & 52 deletions src/solana/rpc/async_api.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,6 @@
from time import time
from typing import Dict, List, Optional, Sequence, Union

from solders.hash import Hash as Blockhash
from solders.keypair import Keypair
from solders.message import VersionedMessage
from solders.pubkey import Pubkey
from solders.rpc.responses import (
Expand Down Expand Up @@ -62,9 +60,9 @@
from solders.transaction import VersionedTransaction

from solana.rpc import types
from solana.transaction import Transaction
from solders.transaction import Transaction

from .commitment import Commitment, Finalized
from .commitment import Commitment
from .core import (
_COMMITMENT_TO_SOLDERS,
TransactionExpiredBlockheightExceededError,
Expand Down Expand Up @@ -419,13 +417,13 @@ async def get_fee_for_message(
Example:
>>> from solders.keypair import Keypair
>>> from solders.system_program import TransferParams, transfer
>>> from solana.transaction import Transaction
>>> from solders.message import Message
>>> leading_zeros = [0] * 31
>>> sender, receiver = Keypair.from_seed(leading_zeros + [1]), Keypair.from_seed(leading_zeros + [2])
>>> txn = Transaction().add(transfer(TransferParams(
... from_pubkey=sender.pubkey(), to_pubkey=receiver.pubkey(), lamports=1000)))
>>> msg = Message([transfer(TransferParams(
... from_pubkey=sender.pubkey(), to_pubkey=receiver.pubkey(), lamports=1000))])
>>> solana_client = AsyncClient("http://localhost:8899")
>>> (await solana_client.get_fee_for_message(txn.compile_message())).value # doctest: +SKIP
>>> (await solana_client.get_fee_for_message(msg)).value # doctest: +SKIP
5000
"""
body = self._get_fee_for_message_body(message, commitment)
Expand Down Expand Up @@ -1012,62 +1010,28 @@ async def send_raw_transaction(self, txn: bytes, opts: Optional[types.TxOpts] =
async def send_transaction(
self,
txn: Union[VersionedTransaction, Transaction],
*signers: Keypair,
opts: Optional[types.TxOpts] = None,
recent_blockhash: Optional[Blockhash] = None,
) -> SendTransactionResp:
"""Send a transaction.

Args:
txn: transaction object.
signers: Signers to sign the transaction. Only supported for legacy Transaction.
opts: (optional) Transaction options.
recent_blockhash: (optional) Pass a valid recent blockhash here if you want to
skip fetching the recent blockhash or relying on the cache.
Only supported for legacy Transaction.

Example:
>>> from solders.keypair import Keypair
>>> from solders.system_program import TransferParams, transfer
>>> from solana.transaction import Transaction
>>> from solders.message import Message
>>> from solders.transaction import Transaction
>>> leading_zeros = [0] * 31
>>> sender, receiver = Keypair.from_seed(leading_zeros + [1]), Keypair.from_seed(leading_zeros + [2])
>>> txn = Transaction().add(transfer(TransferParams(
... from_pubkey=sender.pubkey(), to_pubkey=receiver.pubkey(), lamports=1000)))
>>> solana_client = AsyncClient("http://localhost:8899")
>>> (await solana_client.send_transaction(txn, sender)).value # doctest: +SKIP
Signature(
1111111111111111111111111111111111111111111111111111111111111111,
)
"""
if isinstance(txn, VersionedTransaction):
if signers:
msg = "*signers args are not used when sending VersionedTransaction."
raise ValueError(msg)
if recent_blockhash is not None:
msg = "recent_blockhash arg is not used when sending VersionedTransaction."
raise ValueError(msg)
versioned_tx_opts = types.TxOpts(preflight_commitment=self._commitment) if opts is None else opts
return await self.send_raw_transaction(bytes(txn), opts=versioned_tx_opts)
last_valid_block_height = None
if recent_blockhash is None:
blockhash_resp = await self.get_latest_blockhash(Finalized)
recent_blockhash = self.parse_recent_blockhash(blockhash_resp)
last_valid_block_height = blockhash_resp.value.last_valid_block_height

txn.recent_blockhash = recent_blockhash

txn.sign(*signers)
opts_to_use = (
types.TxOpts(
preflight_commitment=self._commitment,
last_valid_block_height=last_valid_block_height,
)
if opts is None
else opts
)
txn_resp = await self.send_raw_transaction(txn.serialize(), opts=opts_to_use)
return txn_resp
>>> ixns = [transfer(TransferParams(
... from_pubkey=sender.pubkey(), to_pubkey=receiver.pubkey(), lamports=1000))]
>>> msg = Message(ixns, sender.pubkey())
>>> client = AsyncClient("http://localhost:8899")
>>> (await client.send_transaction(Transaction([sender], msg, (await client.get_latest_blockhash()).value.blockhash))) # doctest: +SKIP
""" # noqa: E501
return await self.send_raw_transaction(bytes(txn), opts=opts)

async def simulate_transaction(
self,
Expand All @@ -1092,7 +1056,7 @@ async def simulate_transaction(
... '000000000000000000000000000000000000000000839618f701ba7e9ba27ae59825dd6d6bb66d14f6d5d0eae215161d7'
... '1851a106901020200010c0200000040420f0000000000'
... )
>>> tx = Transaction.deserialize(bytes.fromhex(full_signed_tx_hex))
>>> tx = Transaction.from_bytes(bytes.fromhex(full_signed_tx_hex))
>>> (await solana_client.simulate_transaction(tx)).value.logs # doctest: +SKIP
['BPF program 83astBRguLMdt2h5U1Tpdq5tjFoJ6noeGwaY3mDLVcri success']
"""
Expand Down
6 changes: 2 additions & 4 deletions src/solana/rpc/core.py
Original file line number Diff line number Diff line change
Expand Up @@ -81,7 +81,7 @@
from solders.transaction_status import UiTransactionEncoding

from solana.rpc import types
from solana.transaction import Transaction
from solders.transaction import Transaction

from .commitment import Commitment, Confirmed, Finalized, Processed

Expand Down Expand Up @@ -499,9 +499,7 @@ def _simulate_transaction_body(
commitment_to_use = _COMMITMENT_TO_SOLDERS[commitment or self._commitment]
config = RpcSimulateTransactionConfig(sig_verify=sig_verify, commitment=commitment_to_use)
if isinstance(txn, Transaction):
if txn.recent_blockhash is None:
raise ValueError("transaction must have a valid blockhash")
return SimulateLegacyTransaction(txn.to_solders(), config)
return SimulateLegacyTransaction(txn, config)
return SimulateVersionedTransaction(txn, config)

@staticmethod
Expand Down
Loading
Loading