Contracting - Smart Contracts with Python
With Contracting you can write smart contracts in a subset of Python. You can then deploy these contracts to the Lamden Blockchain Cilantro.
Contracting is simply a Python package. Because of this you can use existing Python tooling and the Contracting API to develop smart contracts with ease. That is unlike Solidity, which requires external services like Truffle or TestRPC.
Below is an example of a simple token smart contract in Python. With it you can transfer tokens and check token balances.
def token_contract():
balances = Hash()
owner = Variable()
@construct
def seed():
owner.set(ctx.caller)
@export
def balance_of(wallet_id):
return balances[wallet_id]
@export
def transfer(to, amount):
balances[ctx.caller] -= amount
balances[to] += amount
sender_balance = balances[ctx.caller]
assert sender_balance >= 0, "Sender balance must be non-negative!!!"
@export
def mint(to, amount):
assert ctx.caller == owner.get(), 'Only the original contract author can mint!'
balances[to] += amount
Installing
Ubuntu 18.04 LTS
Requirements
sudo apt-get update
sudo apt-get upgrade
apt install python3-pip
Install RocksDB & Dependencies
sudo apt-get install libgflags-dev libsnappy-dev zlib1g-dev libbz2-dev liblz4-dev libzstd-dev librocksdb-dev
Install Contracting
pip3 install contracting
Start Rocks Server
rocks serve &
OSX
Install RocksDB
brew install rocksdb
If Homebrew is not installed, install it first: https://brew.sh/
Install Contracting
pip3 install contracting
Start Rocks Server
rocks serve &
Using Contracting in a Development Environment
With Contracting now installed, you can develop smart contracts without an instance of the blockchain. This is to improve the speed of development. Here is how you would go about testing a token contract in a Jupyter notebook / IPython console:
In [1]: from contracting.client import ContractingClient
In [2]: def token_contract():
...:
...: balances = Hash()
...: owner = Variable()
...:
...: @construct
...: def seed():
...: owner.set(ctx.caller)
...:
...: @export
...: def balance_of(wallet_id):
...: return balances[wallet_id]
...:
...: @export
...: def transfer(to, amount):
...: balances[ctx.caller] -= amount
...: balances[to] += amount
...: sender_balance = balances[ctx.caller]
...:
...: assert sender_balance >= 0, "Sender balance must be non-negative!!!"
...:
...: @export
...: def mint(to, amount):
...: assert ctx.caller == owner.get(), 'Only the original contract author can mint!'
...: balances[to] += amount
...:
In [3]: client = ContractingClient(signer='stu')
In [4]: client.submit(token_contract, name='token')
In [5]: token = client.get_contract('token')
In [6]: token.mint(to='stu', amount=100000)
In [7]: token.balance_of(wallet_id='stu')
Out[7]: 100000
Get started with Contracting by Example
- A very simple Counter contract
- Ingredients of a Smart Contract
- Interacting with the Client
- Standard Library and Extending Contracting
- Imports and Advanced Data Storage
FAQs
pip install contracting
is not installing on my computer!
If you're using a Mac, you can run into the problem that the C libraries required for Contracting are not getting compiled and the package fails to install.
To fix this:
- Upgrade XCode
- Upgrade all software and restart your computer
- Run
xcode-select --install
. - Run
pip install contracting
again. - Run
open /Library/Developer/CommandLineTools/Packages/macOS_SDK_headers_for_macOS_10.14.pkg
if this does not work. - Install the package and run
pip install contracting
again. It should work now.