Skip to main content

xdc.py

xdc.py is a Python library for interacting with XDC (XinFin Digital Contract) tokens (XRC20 for now) using web3.py.

Installation

You can install xdc.py using pip:

pip install xdc

Usage

Initializing the XRC20 Object

import xdc

# Initialize XRC20 object with RPC URL
rpc_url = 'https://rpc.xdcrpc.com'  # Replace with your RPC URL
xrc20 = xdc.XRC20(rpc_url)

Getting Token Information

Get Token Name

token_address = '0x...'  # Token's contract address
name = xrc20.name(token_address)
print(f"Token Name: {name}")

Get Total Supply

token_address = '0x...'  # Token's contract address
total_supply = xrc20.total_supply(token_address)
print(f"Total Supply: {total_supply}")

Get Decimals

token_address = '0x...'  # Token's contract address
decimals = xrc20.decimals(token_address)
print(f"Decimals: {decimals}")

Get Symbol

token_address = '0x...'  # Token's contract address
symbol = xrc20.symbol(token_address)
print(f"Symbol: {symbol}")

Get Balance of an Address

token_address = '0x...'  # Token's contract address
owner_address = '0x...'  # Address to which you want to check the balance
balance = xrc20.balance_of(token_address, owner_address)
print(f"Balance of {owner_address}: {balance}")

Transferring XDC and Tokens

Transfer XDC

owner_address = '0x...'  # Sender's address
owner_private_key = '0x...'  # Sender's private key
receiver_address = '0x...'  # Receiver's address
amount = 1  # Amount to transfer in XDC

tx_hash = xrc20.transfer_xdc(owner_address, owner_private_key, receiver_address, amount)
print(f"Transfer XDC Transaction Hash: {tx_hash}")

Transfer Tokens

token_address = '0x...'  # Token's contract address
owner_address = '0x...'  # Sender's address
owner_private_key = '0x...'  # Sender's private key
receiver_address = '0x...'  # Receiver's address
amount = 1  # Amount of tokens to transfer

tx_hash = xrc20.transfer_token(token_address, owner_address, owner_private_key, receiver_address, amount)
print(f"Transfer Token Transaction Hash: {tx_hash}")

Approve Token Transfer

token_address = '0x...'  # Token's contract address
owner_address = '0x...'  # Sender's address (address that allows to spend tokens)
owner_private_key = '0x...'  # Owner's private key
spender_address = '0x...'  # Spender's address (address allowed to spend tokens)
amount = 1  # Amount of tokens to approve

tx_hash = xrc20.approve(token_address, owner_address, owner_private_key, spender_address, amount)
print(f"Approve Token Transfer Transaction Hash: {tx_hash}")

Increase/Decrease Allowance

token_address = '0x...'  # Token's contract address
owner_address = '0x...'  # Owner's address (address that allows to spend tokens)
owner_private_key = '0x...'  # Owner's private key
spender_address = '0x...'  # Spender's address (address allowed to spend tokens)
amount = 1  # Amount of tokens to adjust allowance

# Increase Allowance
tx_hash_increase = xrc20.increase_allowance(token_address, owner_address, owner_private_key, spender_address, amount)
print(f"Increase Allowance Transaction Hash: {tx_hash_increase}")

# Decrease Allowance
tx_hash_decrease = xrc20.decrease_allowance(token_address, owner_address, owner_private_key, spender_address, amount)
print(f"Decrease Allowance Transaction Hash: {tx_hash_decrease}")

Transfer Tokens From

token_address = '0x...'  # Token's contract address
owner_address = '0x...'  # Owner's address (address that allows to spend tokens)
spender_address = '0x...'  # Spender's address (address allowed to spend tokens)
spender_private_key = '0x...'  # Spender's private key
receiver_address = '0x...'  # Receiver's address
amount = 1  # Amount of tokens to transfer

tx_hash = xrc20.transfer_from(token_address, owner_address, spender_address, spender_private_key, receiver_address, amount)
print(f"Transfer From Transaction Hash: {tx_hash}")

License

This library is licensed under the MIT License.

Acknowledgments

Release files for xdc 1.0.4

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

Source distribution (sdist)

Source distribution for xdc 1.0.4
File Size Uploaded
xdc-1.0.4.tar.gz 4.8 kB Details

Release files / xdc-1.0.4.tar.gz

Download URL xdc-1.0.4.tar.gz
Size 4.8 kB
Tags Source
SHA-256 checksum
How to use checksums
817e6d9f549800aa92871198b82a45255008a079489fb504e86e6f5d06987aaa
BLAKE2b-256 checksum
How to use checksums
863c32d8ff18467d1ee8b9115cd1088a50785e77d883ea1270aa813790fa8cb0
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via twine/5.1.0 CPython/3.12.3

Release history Release notifications | RSS feed

This release

1.0.4 This release

1 release file

1.0.3

1 release file

1.0.1

1 release file

1.0

1 release file

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