Skip to main content

Python Solana Package

Project description

PySolana

PySolana is a Python library for dealing with Solana blockchain.

Installation

Use the package manager pip to install pysolana.

pip install pysolana

Usage

There are 2 modules in pysolana:

  • api.py includes all RPC API Solana methods.
  • sol.py includes class Sol that used to simply manage Solana accounts with Python3

api.py

You can see all RPC API Solana methods here

Example:

print(getTransactionCount()) # 555309062

sol.py

Keypairs

To generate new keypair you need to create Sol object in code.

keypair = Sol(chain='devnet')
print(keypair.seedphrase) # 12-words seedphrase that used to import keypair
print(keypair.pubkey) # Solana account address

To import keypair put seedphrase in __init__ method.

kp1 = Sol(chain='devnet')
kp2 = Sol(seedphrase=kp1.seedphrase, chain='testnet')
print(kp1.pubkey == kp2.pubkey) # True

One keypair can be used in different chains.

There are some methods in Sol class:

set_chain

set_chain(chain) method used to change keypair chain.

kp = Sol(chain='mainnet') # Created keypair

kp.balance() # Check balance in mainnet
kp.set_chain('testnet') # Set chain to testnet
kp.balance() # Check balance in testnet

balance

balance() method used to get balance of account.

kp = Sol(seedphrase=my_seedphrase, chain='mainnet') # Created keypair

print(kp.balance()) # 15.0

airdrop

aidrop(amount, wait=True) method used to request airdrop of SOL tokens in devnet.

kp = Sol(chain='devnet') # Created keypair

print(kp.balance()) # 0.0
kp.airdrop(100) # Request airdrop to account in devnet.
print(kp.balance()) # 100.0

kp.airdrop(100, wait=False)
# Here I can do anything without waiting transaction to confirm
print(kp.balance()) # 100.0
# Transaction is not confirmed in chain and balance() showing only confirmed balance.

transfer

transfer(to, amount, wait=True) method used to send SOL tokens to another account.

kp1 = Sol(chain='devnet') # Created first keypair
kp2 = Sol(chain='devnet') # Created second keypair

kp1.airdrop(100)
kp2.airdrop(100)

print(kp1.balance()) # 100.0
print(kp2.balance()) # 100.0

kp1.transfer(kp2, 10) # 'to' can be Sol object or str pubkey

print(kp1.balance()) # 89.999995
print(kp2.balance()) # 110.0

kp2.transfer(kp1.pubkey, 10)

print(kp1.balance()) # 99.999995
print(kp2.balance()) # 99.999995

kp1.transfer(kp2, 50, wait=False)
# Here I can do anything without waiting transaction to confirm
print(kp1.balance()) # 99.999995
print(kp2.balance()) # 99.999995
# Transaction is not confirmed in chain and balance() showing only confirmed balance.

License

MIT

Project details


Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Source Distribution

pysolana-0.1.6.tar.gz (4.3 kB view hashes)

Uploaded Source

Supported by

AWS AWS Cloud computing and Security Sponsor Datadog Datadog Monitoring Fastly Fastly CDN Google Google Download Analytics Microsoft Microsoft PSF Sponsor Pingdom Pingdom Monitoring Sentry Sentry Error logging StatusPage StatusPage Status page