Package: pysolana
PySolanaPySolana is a Python library for dealing with Solana blockchain.InstallationUse the package managerpipto install pysolana.pip3installpysolanaUsageThere are 2 modules inpysolana:apiincludes all RPC API Solana methods.solincludes classSolthat used to simply manage Solana accounts withPython3apiYou can see all RPC API Solana methodshere. Every method from RPC API writen to function inapimodule.Example:frompysolana.apiimport*print(getTransactionCount())# 555309062solKeypairsTo generate new keypair you need to createSolobject in code.frompysolana.solimport*keypair=Sol(chain='devnet')print(keypair.seedphrase)# 12-words seedphrase that used to import keypairprint(keypair.pubkey)# Solana account addressTo import keypair put seedphrase in__init__method.frompysolana.solimport*kp1=Sol(chain='devnet')kp2=Sol(seedphrase=kp1.seedphrase,chain='testnet')print(kp1.pubkey==kp2.pubkey)# TrueOne keypair can be used in different chains.There are some methods inSolclass:set_chainbalanceairdroptransfercreate_stake_accountdelegate_stakedeactivate_stakewithdraw_stakeset_chainset_chain(chain)method used to change keypair chain.frompysolana.solimport*kp=Sol(chain='mainnet')# Created keypairkp.balance()# Check balance in mainnetkp.set_chain('testnet')# Set chain to testnetkp.balance()# Check balance in testnetbalancebalance()method used to get balance of account.frompysolana.solimport*kp=Sol(seedphrase=my_seedphrase,chain='mainnet')# Created keypairprint(kp.balance())# 15.0airdropaidrop(amount, wait=True)method used to request airdrop of SOL tokens in devnet.frompysolana.solimport*kp=Sol(chain='devnet')# Created keypairprint(kp.balance())# 0.0kp.airdrop(100)# Request airdrop to account in devnet.print(kp.balance())# 100.0kp.airdrop(100,wait=False)# Here I can do anything without waiting transaction to confirmprint(kp.balance())# 100.0# Transaction is not confirmed in chain and balance() showing only confirmed balance.transfertransfer(to, amount, wait=True)method used to send SOL tokens to another account.frompysolana.solimport*kp1=Sol(chain='devnet')# Created first keypairkp2=Sol(chain='devnet')# Created second keypairkp1.airdrop(100)kp2.airdrop(100)print(kp1.balance())# 100.0print(kp2.balance())# 100.0kp1.transfer(kp2,10)# 'to' can be Sol object or str pubkeyprint(kp1.balance())# 89.999995print(kp2.balance())# 110.0kp2.transfer(kp1.pubkey,10)print(kp1.balance())# 99.999995print(kp2.balance())# 99.999995kp1.transfer(kp2,50,wait=False)# Here I can do anything without waiting transaction to confirmprint(kp1.balance())# 99.999995print(kp2.balance())# 99.999995# Transaction is not confirmed in chain and balance() showing only confirmed balance.create_stake_accountcreate_stake_account(amount)method used to create new stake account.frompysolana.solimport*kp=Sol(chain='devnet')# Create new keypairkp.airdrop(200)stake_account=kp.create_stake_account(100)# Creating new stake accountprint(stake_account)# Dhyi75k1aA4Rfn99gh4XCT64yxvGzCpu81ZE4iDe3RZzprint(kp.balance())# 99.99999delegate_stakedelegate_stake(stake_account, vote_account)method used to delegate stake at accountstake_accountfor validatorvote_account.frompysolana.solimport*kp=Sol(chain='devnet')# Create new keypairkp.airdrop(1000)stake_account=kp.create_stake_account(100)# Creating new stake accountkp.delegate_stake(stake_account,'5MMCR4NbTZqjthjLGywmeT66iwE9J9f7kjtxzJjwfUx2')# Delegating ALL stake to validatordeactivate_stakedeactivate_stake(stake_account)method used to deactivate stake fromstake_accountfrompysolana.solimport*kp=Sol(chain='devnet')# Create new keypairkp.airdrop(1000)stake_account=kp.create_stake_account(100)# Creating new stake account with 100 SOL stakedkp.delegate_stake(stake_account,'5MMCR4NbTZqjthjLGywmeT66iwE9J9f7kjtxzJjwfUx2')# Delegating ALL stake to validatorprint(kp.balance())# 899.999985kp.deactivate_stake(stake_account)# Deactivating ALL stake from stake_account# Stake transactions take some time to proceed in blockchain, so you need to wait until your SOL tokens back to account# Some time later:print(kp.balance())# 999.99998withdraw_stakewithdraw_stake(stake_account, amount)method used to withdraw stake dividends fromstake_accountfrompysolana.solimport*kp=Sol(chain='devnet')# Create new keypairkp.airdrop(1000)stake_account=kp.create_stake_account(100)# Creating new stake account with 100 SOL stakedkp.delegate_stake(stake_account,'5MMCR4NbTZqjthjLGywmeT66iwE9J9f7kjtxzJjwfUx2')# Delegating ALL stake to validatorprint(kp.balance())# 899.999985# Some time later:kp.withdraw_stake(stake_account,3)print(kp.balance())# 902.99998LicenseMIT