Skip to main content

Defichain Python Library

Project description

DefichainPython Logo

PyPI Version PyPI Python Version Downloads

Docs Build Tests

Telegram Group Twitter / X Status Page

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:

  1. Node / RPC

  2. Ocean

  3. HDWallet

  4. Transactions

Installation

Download the PyPi package:

pip install defichain -U

Clone the GitHub Repository:

git clone https://github.com/eric-volz/DefichainPython.git

Features

1. 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 object
from defichain import Node

# Specify connection
node =  Node("user", "password", url="127.0.0.1", port=8554)

addressFrom = "df1qn6utdvuaq0yshc4sah6puzf0dnlfkc2c8ryme8"
tokenFrom = "BTC"
amount = 0.1
addressTo = "df1qn6utdvuaq0yshc4sah6puzf0dnlfkc2c8ryme8"
tokenTo = "DFI"
maxprice = 3000

# Execute poolswap
txid = node.poolpair.poolswap(addressFrom, tokenFrom, amount, addressTo, tokenTo, maxprice)

# Print txid
print(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 object
from defichain import Ocean

# Specify connection
ocean =  Ocean(network="mainnet")

# Execute getBalace method
utxo_balance = ocean.address.getBalance("df1qn6utdvuaq0yshc4sah6puzf0dnlfkc2c8ryme8")

# Print the utxo balance of the requested address
print(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 network
from defichain import Wallet
from defichain.networks import DefichainMainnet

#  Mnemonic seed
mnemonic = "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 seed
wallet = Wallet(DefichainMainnet)
wallet.from_mnemonic(mnemonic)

# Derive first account from the wallet
account = wallet.get_account(0)

# Print every address type
print(account.get_p2pkh())  # 8KvWa4oCfAhdyUNK8pXJS8XnddsxY6ZY7J
print(account.get_p2sh())  # dUiMDov5Jxg3qKcy9yi6petuUtrvBRezUS
print(account.get_p2wpkh())  # df1qx52ql637w4t7uk2vjdatj3a24cnvuu4fkxryrr

# Print every private key type
print(account.get_privateKey())  # c72f08c17b475d641a711ef1e16bcdb0cc0c1210e6da846060b2e04d5c2299b3
print(account.get_wif())  # L3tu3Bx5n8aWgcDd14btMPgxQ8H5VYbbNoodrNESaonom64YPnr9

4. 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 txbuilder
from defichain import Ocean
from defichain import Wallet
from defichain.networks import DefichainMainnet
from defichain import TxBuilder

# Specify ocean connection
ocean =  Ocean(network="mainnet")

# Create wallet and account
mnemonic = "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 TxBuilder
builder = TxBuilder(account.get_p2wpkh(), account, ocean)

addressFrom = account.get_p2wpkh()
tokenFrom = "BTC"
amount = 0.1
addressTo = account.get_p2wpkh()
tokenTo = "DFI"
maxprice = 3000

# Build poolswap transaction
tx = builder.pool.poolswap(addressFrom, tokenFrom, amount, addressTo, tokenTo, maxprice)

# Send transaction into the blockchain
txid = builder.send_tx(tx)

# Print txid
print(txid)

Community

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)!

License & Disclaimer

By using (this repo), you (the user) agree to be bound by the terms of this license (MIT License).

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

defichain-3.0.0.tar.gz (219.7 kB view details)

Uploaded Source

Built Distribution

If you're not sure about the file name format, learn more about wheel file names.

defichain-3.0.0-py3-none-any.whl (273.6 kB view details)

Uploaded Python 3

File details

Details for the file defichain-3.0.0.tar.gz.

File metadata

  • Download URL: defichain-3.0.0.tar.gz
  • Upload date:
  • Size: 219.7 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/4.0.2 CPython/3.11.5

File hashes

Hashes for defichain-3.0.0.tar.gz
Algorithm Hash digest
SHA256 b862f206d510fefe55567d98b1a982aadd5064372662929e9a888e31d5558961
MD5 ce81f664b8a4bd17670b24c1e7bcc84f
BLAKE2b-256 83f08f4d12c8dcd59f22e22d1fb0851569b64e60539a0c03229794eaaa282ec7

See more details on using hashes here.

File details

Details for the file defichain-3.0.0-py3-none-any.whl.

File metadata

  • Download URL: defichain-3.0.0-py3-none-any.whl
  • Upload date:
  • Size: 273.6 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/4.0.2 CPython/3.11.5

File hashes

Hashes for defichain-3.0.0-py3-none-any.whl
Algorithm Hash digest
SHA256 4417556eccccd089463a81d30dfffdad826297e00d370b70a49cd6c354df8fbd
MD5 1729aca87ce22d56911fa9eaa5ec69e7
BLAKE2b-256 7f3345f2484a5133c57a73f6b28acb9d78fc3327e289dd2b95b92f1faa6ef1d8

See more details on using hashes here.

Supported by

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