Skip to content

Commit

Permalink
✨ Add getTokenAccountsByOwner method
Browse files Browse the repository at this point in the history
  • Loading branch information
GitBolt committed Jan 21, 2022
1 parent c35aa2e commit f187b06
Show file tree
Hide file tree
Showing 8 changed files with 37 additions and 10 deletions.
3 changes: 2 additions & 1 deletion solathon/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,4 +3,5 @@
from .client import Client
from .publickey import PublicKey
from .keypair import Keypair
from .transaction import Transaction
from .transaction import Transaction

27 changes: 22 additions & 5 deletions solathon/client.py
Original file line number Diff line number Diff line change
Expand Up @@ -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()
Expand All @@ -76,3 +92,4 @@ def send_transaction(
)
res = self.http.send(data)
return res

3 changes: 2 additions & 1 deletion solathon/core/instructions.py
Original file line number Diff line number Diff line change
Expand Up @@ -130,4 +130,5 @@ def transfer(sender, receiver, lamports) -> TransactionInstruction:
],
program_id=SYS_PROGRAM_ID,
data=data,
)
)

1 change: 1 addition & 0 deletions solathon/core/types.py
Original file line number Diff line number Diff line change
Expand Up @@ -11,3 +11,4 @@ class RPCResponse(TypedDict):
id: int
result: Any
error: RPCError

1 change: 1 addition & 0 deletions solathon/keypair.py
Original file line number Diff line number Diff line change
Expand Up @@ -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))

1 change: 1 addition & 0 deletions solathon/publickey.py
Original file line number Diff line number Diff line change
Expand Up @@ -35,3 +35,4 @@ def __str__(self) -> str:

def to_base58(self) -> bytes:
return base58.b58encode(bytes(self))

8 changes: 6 additions & 2 deletions solathon/transaction.py
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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
Expand Down Expand Up @@ -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)

3 changes: 2 additions & 1 deletion solathon/utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -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)
return float(lamports * 1000000000)

0 comments on commit f187b06

Please sign in to comment.