Skip to main content

Contracting

Python Powered Smart Contracts

[object Object]

Native Python

Develop smart contracts in your favorite Python IDE and testing suite.

[object Object]

Get Empowerd

Translate your ideas to the blockchain without worrying about abstract coding paradigms.

[object Object]

Save Time

Spend time focusing on your product and not complex smart contract languages.

Quickstart

1. Install

pip3 install contracting

2. Create Your First Smart Contract


#create some state 
balances = Hash(default_value=0) 

#seed initial balances 
@construct 
def seed(): 
  balances['me'] = 1000000 

#transfer the caller's funds to another account
@export
def transfer(amount: float, to: str):
  assert amount > 0, 'Cannot send negative balances!'
  assert balances[ctx.caller] >= amount, 'Insufficient Funds to send!'

  # remove amount from senders account
  balances[ctx.caller] -= amount
  # add amount to receivers account
  balances[to] += amount

# give permission to the spender to spend your funds
@export
def approve(amount: float, to: str):
  assert amount > 0, 'Cannot send negative balances!'

  # Add the amount to the callers
  balances[ctx.caller, to] += amount

# transfer someone's funds to another account
@export
def transfer_from(amount: float, to: str, main_account: str):
  assert amount > 0, 'Cannot send negative balances!'

  assert balances[main_account, ctx.caller] >= amount, 'Not enough coins approved to send! You have {} and are trying to spend {}'\
      .format(balances[main_account, ctx.caller], amount)
  assert balances[main_account] >= amount, 'Not enough coins to send!'

  # reduce the approval amount by the amount being spent
  balances[main_account, ctx.caller] -= amount

  # remove amount spent from the main account
  balances[main_account] -= amount
  # add the amount spent to the receivers account
  balances[to] += amount
                  

3. Upload Your First Contact To The Blockchain

Open your lamden wallet and select the 'Smart Contract' section

Click on the button 'Submit to Network'

Simply press button 'Confirm Transaction'

4. Interact With The Block Service To Access Your Contract Data

Open shell and then run the following command:

curl http://165.22.47.195:3535/contracts/con_my_first_contract

Then you will get the source code of your contract and other relevant information

{
   "con_my_first_contract": {
      "__code__": "__balances = Hash(default_value=0, contract='con_my_first_contract', name=\n    'balances')......",
      "__compiled__": {
         "__bytes__": "e3000000000000000000000......"
      },
      "__developer__": "b17eabddbd474bdaebfca71f654ab7dc1195510062e3cdbbdc442a028b050e41",
      "__owner__": null,
      "__submitted__": {
         "__time__": [
            2022,
            6,
            9,
            16,
            26,
            34,
            0
         ]
      },
      "balances": {
         "me": 1000000
      }
   }
}

Tutorials

"My Token" Tutorial Series

Create a token smart contract from scratch using Python's Contracting package. The simple smart contract will mint an initial supply to a user of our choice and then define a tansfer method for our users to use. To finish it off we create unit test to validate our new smart contact does exactly what we want it to do.

Juypter Notebook Quickstart

Jupyter is a great tool for Python programmers to develop and explore in as they combine the high feedback of a REPL with the presentation and saving of a program. If you are a Python programmer, chances are you already have Jupyter installed. If not, follow this guide to get started.

Lamden Standards

There are three standards in Lamden, LST001, LST002 and LST003. LST001 is a basic Token Interface. For LST002, it's used for adding/changing metadata on smart contracts. LST003 is a basic NFT Interface. They are both great example of smart contacts on Lamden Blockchain.