A subset of Python for developing smart contracts on the Xian Network
A subset of Python for developing smart contracts on the Xian Network
Xian Contracting is a Python-based smart contract development and execution framework. Unlike traditional blockchain platforms like Ethereum, Xian Contracting leverages Python's VM to create a more accessible and familiar environment for developers to write smart contracts.
Variable and Hash data structurespip install xian-contracting
Here's a complete token contract example with approval system:
…
Variable: Single-value storage
counter = Variable()
counter.set(0) # Set value
current = counter.get() # Get value
Hash: Key-value storage with support for complex and multi-level keys
balances = Hash()
# Single-level key
balances['alice'] = 100
alice_balance = balances['alice']
# Multi-level keys for complex relationships
balances['alice', 'bob'] = 50 # e.g., alice approves bob to spend 50 tokens
approved_amount = balances['alice', 'bob'] # Get the approved amount
# You can use up to 16 dimensions in key tuples
data['user', 'preferences', 'theme'] = 'dark'
@construct: Initializes contract state (can only be called once)
@construct
def seed():
owner.set(ctx.caller)
@export: Makes function callable from outside the contract
@export
def increment(amount: int):
counter.set(counter.get() + amount)
The ctx object provides important runtime information:
ctx.caller: Address of the account calling the contractctx.this: Current contract's addressctx.signer: Original transaction signerctx.owner: Contract owner's addressThe ContractingClient class is your main interface for deploying and interacting with contracts:
from contracting.client import ContractingClient
# Initialize the client
client = ContractingClient()
# Submit a contract
with open('token.py', 'r') as f:
contract = f.read()
client.submit(name='con_token', code=contract)
# Get contract instance
token = client.get_contract('con_token')
# Call contract methods
token.transfer(amount=100, to='bob')
The framework includes a powerful storage system:
from contracting.storage.driver import Driver
driver = Driver()
# Direct storage operations
driver.set('key', 'value')
driver.get('key')
# Contract storage
driver.set_contract(name='contract_name', code=contract_code)
driver.get_contract('contract_name')
Contracts can emit events which can be tracked by external systems:
def token_contract():
transfer_event = LogEvent(
'transfer',
{
'sender': {'type': str, 'idx': True},
'receiver': {'type': str, 'idx': True},
'amount': {'type': float}
}
)
@export
def transfer(amount: float, to: str):
# ... transfer logic ...
# Emit event
transfer_event({
'sender': ctx.caller,
'receiver': to,
'amount': amount
})
When developing contracts, you can use the linter to check for common issues:
from contracting.client import ContractingClient
client = ContractingClient()
violations = client.lint(contract_code)
This project is licensed under the Creative Commons Attribution‑NonCommercial 4.0 International - see the LICENSE file for details. Non‑commercial use only. See LICENSE for details.
No open issues yet, or sync has not completed.