Skip to content
A subset of Python for developing smart contracts.
Branch: dev
Clone or download
Fetching latest commit…
Cannot retrieve the latest commit at this time.
Permalink
Type Name Latest commit message Commit time
Failed to load latest commit information.
.circleci first refactor to name change Apr 30, 2019
.github/ISSUE_TEMPLATE Bump for merge into both master and dev Jan 23, 2019
contracting fixed metapath (i think) Jun 13, 2019
docker
examples
lightops Added lightops for AWS deployments of contracting container May 22, 2019
scripts
tests unittests pass (optimize this!!) Jun 13, 2019
.gitignore Added lightops for AWS deployments of contracting container May 22, 2019
LICENSE Making package. May 30, 2018
MANIFEST.in vrsion Nov 20, 2018
Makefile Fix Apr 10, 2019
README.md Update README.md Jun 12, 2019
dev-requirements.txt try running this instead Nov 14, 2018
directory_structure_proposal.txt
key_mgmt_structure.txt Create key_mgmt_structure.txt Apr 10, 2019
medis.png
requirements.txt updated /contracts, /, and /contracts/name routes May 14, 2019
security_notes.md Add easy init_local_noauth_dev method to executer for Cilantro dev/test. May 31, 2018
setup.py updating redis req 2.10.6 -> 3.2.0 May 17, 2019

README.md

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

Clone Contracting Repo

git clone https://github.com/Lamden/contracting.git
cd contracting

Install pip3

apt install python3-pip

install redis

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

install contracting

python3 ./setup.py develop

install webserver.py depenancies

pip3 install sanic sanic_cors Cython

START CONTRACTING API SERVER

python3 contracting/webserver.py

OSX

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 [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

  1. A very simple Counter contract
  2. Ingredients of a Smart Contract
  3. Interacting with the Client
  4. Standard Library and Extending Contracting
  5. Imports and Advanced Data Storage

Storage Model

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.

Medis

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:

  1. Upgrade XCode
  2. Upgrade all software and restart your computer
  3. Run xcode-select --install.
  4. Run pip install contracting again.
  5. Run open /Library/Developer/CommandLineTools/Packages/macOS_SDK_headers_for_macOS_10.14.pkg if this does not work.
  6. Install the package and run pip install contracting again. It should work now.
You can’t perform that action at this time.