Introduction
Xian is a pioneering blockchain platform that enables developers to write smart contracts using a subset of Python, making blockchain development more accessible and efficient. This guide will walk you through creating and testing a simple smart contract on the Xian network.
Prerequisites
Before you begin, ensure you have the following:
- Python 3.11: This version of Python installed on your system.
Step 1: Setting Up the Contracting Client
The Contracting Client is essential for submitting and testing contracts on the Xian network. Here's how to set it up:
- Install the Contracting Client:
git clone https://github.com/xian-network/xian-contracting
cd xian-contracting
pip3.11 install -e .
- Initialize the Client in Your Python Script:
from contracting.client import ContractingClient
client = ContractingClient()
This client allows you to submit, interact with, and test your smart contracts.
Step 2: Writing a Smart Contract
In Xian, smart contracts are written in a subset of Python. Below is an example of a simple contract that responds with different animal sounds based on the input value.
Filename: contract.py
# contract.py
@export
def call_this(self, a: int) -> str:
return self.complex_function(a)
def complex_function(self, a: int) -> str:
if a > 50:
return 'Quack!'
elif a < 10:
return 'Oink!'
elif a == 15:
return 'Woof!'
else:
return 'Meow!'
Explanation:
-
@export
: Decorator that makes the function accessible externally. -
call_this
: Calls the internalcomplex_function
with the provided argument. -
complex_function
: Returns different strings based on the value ofa
.
Step 3: Testing the Smart Contract
Testing ensures your contract behaves as expected. We'll use Python's built-in unittest
framework alongside the Contracting Client.
Filename: test_contract.py
# test_contract.py
import unittest
from contracting.client import ContractingClient
class TestContract(unittest.TestCase):
def setUp(self):
self.c = ContractingClient()
self.c.flush()
with open('contract.py') as f:
code = f.read()
self.c.submit(code, name='test_me')
self.contract = self.c.get_contract('test_me')
def test_a_over_50_returns_quack(self):
result = self.contract.call_this(a=51)
self.assertEqual(result, 'Quack!')
def test_a_under_10_returns_oink(self):
result = self.contract.call_this(a=5)
self.assertEqual(result, 'Oink!')
def test_a_equals_15_returns_woof(self):
result = self.contract.call_this(a=15)
self.assertEqual(result, 'Woof!')
def test_a_between_10_and_50_returns_meow(self):
result = self.contract.call_this(a=30)
self.assertEqual(result, 'Meow!')
def tearDown(self):
self.c.flush()
if __name__ == '__main__':
unittest.main()
Explanation:
-
setUp
: Initializes the Contracting Client, submits the contract, and retrieves it for testing. -
tearDown
: Flushes the client to reset the state after all tests. - Individual test methods: Each tests a specific condition of the
complex_function
.
Running the Tests:
Execute the tests using:
python test_contract.py
Conclusion
By leveraging Xian's Contracting engine, developers can write and test smart contracts using familiar Python syntax. This approach simplifies blockchain development and accelerates the deployment of decentralized applications.
For more detailed information, refer to the Xian Contracting Documentation.
Top comments (1)
Nice article