Package: xian-py
How to installpipinstallxian-pyWalletCreate new walletfromxian_py.walletimportWallet# Create wallet from scratchwallet=Wallet()Create wallet from existing private keyfromxian_py.walletimportWallet# Create wallet from existing private keyprivkey='ed30796abc4ab47a97bfb37359f50a9c362c7b304a4b4ad1b3f5369ecb6f7fd8'wallet=Wallet(privkey)Get private key and public keyfromxian_py.walletimportWalletwallet=Wallet()# Public keyaddress=wallet.public_keyprint(f'address:{address}')# Private keyprivkey=wallet.private_keyprint(f'private key:{privkey}')Sign message with private keyfromxian_py.walletimportWalletwallet=Wallet()# Sign message with private keymessage='I will sign this message'signed=wallet.sign_msg(message)print(f'Signed message:{signed}')XianReplacesome-chain-idwith the chain ID of the network that you want to connect to. A chain ID is an ID that ensures that transactions can only be used in the network for which they were originally generated.Official chain IDs for Xian can be foundhereSubmit contractfromxian_py.walletimportWalletfromxian_py.xianimportXianwallet=Wallet('ed30796abc4ab47a97bfb37359f50a9c362c7b304a4b4ad1b3f5369ecb6f7fd8')xian=Xian('http://<node IP>:26657','some-chain-id',wallet)# Contract codecode='''token_name = Variable() # Optional@constructdef seed():# Create a token with the information from fixtures/tokenInfotoken_name.set("Test Token")@exportdef set_token_name(new_name: str):# Set the token nametoken_name.set(new_name)'''# Deploy contract on networksubmit=xian.submit_contract('con_new_token',code)print(f'success:{submit["success"]}')print(f'tx_hash:{submit["tx_hash"]}')Approve contract and retrieve approved amountfromxian_py.walletimportWalletfromxian_py.xianimportXianwallet=Wallet('ed30796abc4ab47a97bfb37359f50a9c362c7b304a4b4ad1b3f5369ecb6f7fd8')xian=Xian('http://<node IP>:26657','some-chain-id',wallet)# Get approved amountapproved=xian.get_approved_amount('con_new_token')print(f'approved:{approved}')# Approve the default amountapprove=xian.approve('con_new_token')print(f'success:{approve["success"]}')print(f'tx_hash:{approve["tx_hash"]}')# Get approved amount againapproved=xian.get_approved_amount('con_new_token')print(f'approved:{approved}')Get token balance for an addressfromxian_py.walletimportWalletfromxian_py.xianimportXianwallet=Wallet('ed30796abc4ab47a97bfb37359f50a9c362c7b304a4b4ad1b3f5369ecb6f7fd8')xian=Xian('http://<node IP>:26657','some-chain-id',wallet)balance=xian.get_balance('con_new_token')print(f'balance:{balance}')TransactionsSend a transaction - High level usagefromxian_py.walletimportWalletfromxian_py.xianimportXianwallet=Wallet('ed30796abc4ab47a97bfb37359f50a9c362c7b304a4b4ad1b3f5369ecb6f7fd8')xian=Xian('http://<node IP>:26657','some-chain-id',wallet)send=xian.send_tx(contract='currency',function='transfer',kwargs={'to':'burned','amount':1000,})print(f'success:{send["success"]}')print(f'tx_hash:{send["tx_hash"]}')Send a transaction - Low level usagefromxian_py.walletimportWalletfromxian_py.transactionsimportget_nonce,create_tx,broadcast_txnode_url="http://<node IP>:26657"wallet=Wallet('ed30796abc4ab47a97bfb37359f50a9c362c7b304a4b4ad1b3f5369ecb6f7fd8')next_nonce=get_nonce(node_url,wallet.public_key)print(f'next nonce:{next_nonce}')tx=create_tx(contract="currency",function="transfer",kwargs={"to":"burned","amount":100,},nonce=next_nonce,stamps=100,chain_id='some-chain-id',private_key=wallet.private_key)print(f'tx:{tx}')data=broadcast_tx(node_url,tx)print(f'success:{data["success"]}')print(f'tx_hash:{data["tx_hash"]}')Retrieve transaction by hashfromxian_py.walletimportWalletfromxian_py.xianimportXianwallet=Wallet('ed30796abc4ab47a97bfb37359f50a9c362c7b304a4b4ad1b3f5369ecb6f7fd8')xian=Xian('http://<node IP>:26657','some-chain-id',wallet)tx=xian.get_tx('some tx hash')print(f'transaction:{tx}')