This library is a community funded project. The objective is to interact with the defichain blockchain as effortlessly as possible. This can be archived by utilizing a variety of methods:
Download the PyPi package:
pip install defichain -UClone the GitHub Repository:
git clone https://github.com/eric-volz/DefichainPython.git1. Node / RPC:
The node implementation empowers you to easily connect to your defichain node without having to deal with everything else. You can execute any function witch is supported by your defichain node.
Make a poolswap via your node:
# Import node objectfromdefichainimportNode# Specify connectionnode=Node("user", "password", url="127.0.0.1", port=8554)
addressFrom="df1qn6utdvuaq0yshc4sah6puzf0dnlfkc2c8ryme8"tokenFrom="BTC"amount=0.1addressTo="df1qn6utdvuaq0yshc4sah6puzf0dnlfkc2c8ryme8"tokenTo="DFI"maxprice=3000# Execute poolswaptxid=node.poolpair.poolswap(addressFrom, tokenFrom, amount, addressTo, tokenTo, maxprice)
# Print txidprint(txid)2. Ocean:
Ocean is an API infrastructure, which is operated by Birthday Research. You can use it to retrieve a lot of information about the blockchain and to interact with it.
Ask for the utxo balance of an address:
# Import ocean objectfromdefichainimportOcean# Specify connectionocean=Ocean(network="mainnet")
# Execute getBalace methodutxo_balance=ocean.address.getBalance("df1qn6utdvuaq0yshc4sah6puzf0dnlfkc2c8ryme8")
# Print the utxo balance of the requested addressprint(utxo_balance)3. HDWallet:
HDWallet (Hierarchical Deterministic Wallet) is a special wallet implementation for defichain. You can use it to derive your private / public keys from your mnemonic seed. This implementation is used in combination with the following feature: Transactions
Create a wallet for mainnet from mnemonic seed:
# Import wallet and networkfromdefichainimportWalletfromdefichain.networksimportDefichainMainnet# Mnemonic seedmnemonic="avocado key fan step egg engage winter upper attitude carry regret mixed utility body party trip valid oppose gas ensure deputy suspect blur trade"# Create wallet for specified network and insert mnemonic seedwallet=Wallet(DefichainMainnet)
wallet.from_mnemonic(mnemonic)
# Derive first account from the walletaccount=wallet.get_account(0)
# Print every address typeprint(account.get_p2pkh()) # 8KvWa4oCfAhdyUNK8pXJS8XnddsxY6ZY7Jprint(account.get_p2sh()) # dUiMDov5Jxg3qKcy9yi6petuUtrvBRezUSprint(account.get_p2wpkh()) # df1qx52ql637w4t7uk2vjdatj3a24cnvuu4fkxryrr# Print every private key typeprint(account.get_privateKey()) # c72f08c17b475d641a711ef1e16bcdb0cc0c1210e6da846060b2e04d5c2299b3print(account.get_wif()) # L3tu3Bx5n8aWgcDd14btMPgxQ8H5VYbbNoodrNESaonom64YPnr94. Transactions:
This transaction implementation enables you to create, sign and broadcast your own transaction just within python. It supports native utxo (send, sendall, …), as well as defi transactions (poolswap, takeloan, …) for mainnet and testnet.
# Import ocean, wallet, network and txbuilderfromdefichainimportOceanfromdefichainimportWalletfromdefichain.networksimportDefichainMainnetfromdefichainimportTxBuilder# Specify ocean connectionocean=Ocean(network="mainnet")
# Create wallet and accountmnemonic="avocado key fan step egg engage winter upper attitude carry regret mixed utility body party trip valid oppose gas ensure deputy suspect blur trade"wallet=Wallet(DefichainMainnet)
wallet.from_mnemonic(mnemonic)
account=wallet.get_account(0)
# Create TxBuilderbuilder=TxBuilder(account.get_p2wpkh(), account, ocean)
addressFrom=account.get_p2wpkh()
tokenFrom="BTC"amount=0.1addressTo=account.get_p2wpkh()
tokenTo="DFI"maxprice=3000# Build poolswap transactiontx=builder.pool.poolswap(addressFrom, tokenFrom, amount, addressTo, tokenTo, maxprice)
# Send transaction into the blockchaintxid=builder.send_tx(tx)
# Print txidprint(txid)This project is funded by the Defichain Community:
Thank you for your trust!
Because of that the community has the right to know about the progress and status of the project. You can view the status of each module yourself on the status page!
All official updates and CFPs are listed on this page!
If you have suggestions for improvement or other ideas, open an issue, write me on Twitter / X, leave a comment in the telegram group or write me oldschool via email (defichainpython@volz.link)!
By using (this repo), you (the user) agree to be bound by the terms of this license (MIT License).
