-
Notifications
You must be signed in to change notification settings - Fork 0
/
solana_client.py
55 lines (46 loc) · 1.79 KB
/
solana_client.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
from typing import Sequence, Tuple
from solders.pubkey import Pubkey
from solders.keypair import Keypair
from solders.system_program import transfer, TransferParams
from solana.transaction import Transaction
from solana.rpc.api import Client
from solana.exceptions import SolanaExceptionBase
from solana.rpc.core import RPCException
class SolanaClient:
def __init__(self, rpc_client: Client, signer: Keypair) -> None:
self.rpc_client = rpc_client
self.signer = signer
def call_program(self, instruction):
txn = Transaction().add(instruction)
try:
self.rpc_client.send_transaction(txn, self.signer)
return True
except SolanaExceptionBase as exc:
print(exc.error_msg)
except RPCException as exc:
print(exc)
return False
def transfer_lamports(self, sender: Pubkey, receiver: Pubkey, amount: int):
instruction = transfer(
TransferParams(from_pubkey=sender,
to_pubkey=receiver, lamports=amount)
)
txn = Transaction().add(instruction)
try:
self.rpc_client.send_transaction(txn, self.signer)
except SolanaExceptionBase as exc:
print(exc.error_msg)
def transfer_many_lamports(self, sender: Pubkey, receivers: Sequence[Tuple[Pubkey, int]]):
txn = Transaction()
for rec in receivers:
instruction = transfer(
TransferParams(from_pubkey=sender,
to_pubkey=rec[0], lamports=rec[1])
)
txn.add(instruction)
try:
self.rpc_client.send_transaction(txn, self.signer)
except SolanaExceptionBase as exc:
print(exc.error_msg)
except RPCException as exc:
print(exc)