From f187b06c6bec27ac452cf1ff343c47f09f76c02e Mon Sep 17 00:00:00 2001 From: GitBolt Date: Sat, 22 Jan 2022 00:44:22 +0530 Subject: [PATCH] =?UTF-8?q?=E2=9C=A8=20Add=20getTokenAccountsByOwner=20met?= =?UTF-8?q?hod?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- solathon/__init__.py | 3 ++- solathon/client.py | 27 ++++++++++++++++++++++----- solathon/core/instructions.py | 3 ++- solathon/core/types.py | 1 + solathon/keypair.py | 1 + solathon/publickey.py | 1 + solathon/transaction.py | 8 ++++++-- solathon/utils.py | 3 ++- 8 files changed, 37 insertions(+), 10 deletions(-) diff --git a/solathon/__init__.py b/solathon/__init__.py index d69821a..df31b98 100644 --- a/solathon/__init__.py +++ b/solathon/__init__.py @@ -3,4 +3,5 @@ from .client import Client from .publickey import PublicKey from .keypair import Keypair -from .transaction import Transaction \ No newline at end of file +from .transaction import Transaction + diff --git a/solathon/client.py b/solathon/client.py index 79f6334..d1bad86 100644 --- a/solathon/client.py +++ b/solathon/client.py @@ -57,11 +57,27 @@ def get_recent_blockhash(self) -> RPCResponse: res = self.http.send(data) return res - def send_transaction( - self, - transaction: Transaction, - recent_blockhash: Optional[str] = None, - ) -> RPCResponse: + def get_token_accounts_by_owner(self, public_key: str | PublicKey, + **kwargs) -> RPCResponse: + if "mint_id" not in kwargs and "program_id" not in kwargs: + raise ValueError("You must pass either mint_id or program_id keyword argument") + + mint_id = kwargs.get("mint_id") + program_id = kwargs.get("program_id") + encoding = kwargs.get("encoding", "jsonParsed") # Who doesn't like JSON? + + data = self.http.build_data( + method="getTokenAccountsByOwner", + params=[ + str(public_key), + {"mint": mint_id} if mint_id else {"programId": program_id}, + {"encoding": encoding} + ] + ) + res = self.http.send(data) + return res + + def send_transaction(self, transaction: Transaction, recent_blockhash: Optional[str] = None,) -> RPCResponse: if recent_blockhash is None: blockhash_resp = self.get_recent_blockhash() @@ -76,3 +92,4 @@ def send_transaction( ) res = self.http.send(data) return res + diff --git a/solathon/core/instructions.py b/solathon/core/instructions.py index f804c13..940011a 100644 --- a/solathon/core/instructions.py +++ b/solathon/core/instructions.py @@ -130,4 +130,5 @@ def transfer(sender, receiver, lamports) -> TransactionInstruction: ], program_id=SYS_PROGRAM_ID, data=data, - ) \ No newline at end of file + ) + diff --git a/solathon/core/types.py b/solathon/core/types.py index a9f8083..65d7d5c 100644 --- a/solathon/core/types.py +++ b/solathon/core/types.py @@ -11,3 +11,4 @@ class RPCResponse(TypedDict): id: int result: Any error: RPCError + diff --git a/solathon/keypair.py b/solathon/keypair.py index 02a13bf..d9fa5de 100644 --- a/solathon/keypair.py +++ b/solathon/keypair.py @@ -48,3 +48,4 @@ def from_private_key(cls, private_key: bytes) -> Keypair: private_key = base58.b58decode(private_key) seed = private_key[:32] return cls(NaclPrivateKey(seed)) + diff --git a/solathon/publickey.py b/solathon/publickey.py index 42eb2b4..0b52184 100644 --- a/solathon/publickey.py +++ b/solathon/publickey.py @@ -35,3 +35,4 @@ def __str__(self) -> str: def to_base58(self) -> bytes: return base58.b58encode(bytes(self)) + diff --git a/solathon/transaction.py b/solathon/transaction.py index cb042ef..35273a5 100644 --- a/solathon/transaction.py +++ b/solathon/transaction.py @@ -9,7 +9,11 @@ from nacl.signing import VerifyKey from .keypair import Keypair from .publickey import PublicKey -from .core.instructions import transfer, TransactionInstruction, AccountMeta +from .core.instructions import ( + transfer, + TransactionInstruction, + AccountMeta + ) def encode_length(value: int) -> bytes: elems, rem_len = [], value @@ -54,7 +58,6 @@ class MessageArgs(NamedTuple): class Message: def __init__(self, args: MessageArgs) -> None: - """Init message object.""" self.header = args.header self.account_keys = [PublicKey(key) for key in args.account_keys] self.recent_blockhash = args.recent_blockhash @@ -348,3 +351,4 @@ def __serialize(self, signed_data: bytes) -> bytes: f"transaction too large: {len(wire_transaction)} > {PACKET_DATA_SIZE}") return bytes(wire_transaction) + diff --git a/solathon/utils.py b/solathon/utils.py index 02cf034..6ed2c51 100644 --- a/solathon/utils.py +++ b/solathon/utils.py @@ -2,4 +2,5 @@ def lamport_to_sol(lamports: float) -> float: return float(lamports / 1000000000) def sol_to_lamport(lamports: float) -> float: - return float(lamports * 1000000000) \ No newline at end of file + return float(lamports * 1000000000) +