Skip to main content
Pre-release

This release is a pre-release and may not be stable for production use.

PyPI Version PyPI Python Version Downloads

Build and Deploy Documentation to GitHub Pages Tests

DefichainPython

This library is a community funded project. The objective is to interact with the defichain blockchain as effortlessly as possible, 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! If you have suggestions for improvement or other ideas open an issue, write me on Twitter or via email (introc@volz.link)!

License & Disclaimer

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

Metadata

Release files for defichainTest 0.0.1b1

For a detailed explanation of source distributions (sdists) and built distributions (wheels), please see the package formats documentation.

Source distribution (sdist)

Source distribution for defichainTest 0.0.1b1
File Size Uploaded
defichainTest-0.0.1b1.tar.gz 204.8 kB Details

Built distribution (wheel)

Table of built distributions (wheels) for defichainTest 0.0.1b1
File Interpreter ABI Platform
defichainTest-0.0.1b1-py3-none-any.whl Python 3 none any Details

Total release size: 455.6 kB

Release files / defichainTest-0.0.1b1.tar.gz

Download URL defichainTest-0.0.1b1.tar.gz
Size 204.8 kB
Tags Source
SHA-256 checksum
How to use checksums
cb6337a7116c4374c82216c46e53d0c25f34479310409567ed7c6d9a1172db5e
BLAKE2b-256 checksum
How to use checksums
d65973ff9a9be0f414a8b8e1b4170705ddca01c4d7e9f3e3ff81cbfc820e682e
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via twine/4.0.2 CPython/3.11.4

Release files / defichainTest-0.0.1b1-py3-none-any.whl

Download URL defichainTest-0.0.1b1-py3-none-any.whl
Size 250.8 kB
Tags Python 3
SHA-256 checksum
How to use checksums
e3f251bb88e88fb0c35c4e1960d5c665da32b4882196e1631542ddbbf4523889
BLAKE2b-256 checksum
How to use checksums
709bf9b9f766c4af085d1aec11700265cb378a45500d2778d44b79cfe7ff3b4a
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via twine/4.0.2 CPython/3.11.4

Release history Release notifications | RSS feed

This release

0.0.1b1 This release

2 release files

Anthropic, PBC Visionary sponsor Bloomberg Visionary sponsor Hudson River Trading Visionary sponsor Meta Visionary sponsor NVIDIA Visionary sponsor Microsoft Sustainability sponsor Depot Continuous Integration AWS Cloud computing and Security Sponsor Datadog Monitoring Fastly CDN Google Download Analytics Sentry Error logging StatusPage Status page