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
Ubuntu 18.04 LTS
Clone Contracting Repo
git clone https://github.com/Lamden/contracting.git cd contracting
apt install python3-pip
sudo apt-get update sudo apt-get upgrade sudo apt-get install redis-server sudo systemctl enable redis-server.service
Validate redis is running
sudo systemctl status redis-server.service
python3 ./setup.py develop
install webserver.py depenancies
pip3 install sanic sanic_cors Cython
START CONTRACTING API SERVER
pip3 install contracting brew install redis brew services start redis
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 : from contracting.client import ContractingClient In : 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 : client = ContractingClient(signer='stu') In : client.submit(token_contract, name='token') In : token = client.get_contract('token') In : token.mint(to='stu', amount=100000) In : token.balance_of(wallet_id='stu') Out: 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
Contracting uses Redis to store the state of the blockchain. This means you can use any Redis tooling to inspect the storage and retrieval of information to and from your smart contracts.
You can also use a GUI like Medis without any issue.
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
pip install contractingagain.
open /Library/Developer/CommandLineTools/Packages/macOS_SDK_headers_for_macOS_10.14.pkgif this does not work.
- Install the package and run
pip install contractingagain. It should work now.