Quickstart

Quickstart on how to use Mamba blockchain development framework.

The minimum required version of Python is 3.6. Create a virtual environment.

$ python3.7 -m venv .venv

Or you can use virtualenv.

$ virtualenv -p python3.7 .venv

Activate the virtual environment.

$ source .venv/bin/activate

Make sure you are able to install Vyper. https://vyper.readthedocs.io/en/latest/installing-vyper.html

Then install Mamba blockchain development framework using pip.

(.venv) $ pip install black-mamba

After installing Mamba blockchain development framework, create a project directory.

(.venv) $ mkdir hellomamba; cd hellomamba

Initialize this directory.

(.venv) $ mamba init

The command created some skeleton files. You can start writing your smart contract in contracts directory.

(.venv) $ edit contracts/HelloWorld.vy

Replace edit with your favorite text editor: vim, code, emacs, nano, gedit, mvim, atom.


greeting: bytes[20]

@public
def __init__():
    self.greeting = "Hello World"

@public
def setGreeting(x: bytes[20]):
    self.greeting = x

@public
def greet() -> bytes[20]:
    return self.greeting

Then you can compile this smart contract.

(.venv) $ mamba compile

The result of the compilation process is saved in build/contracts/HelloWorld.json. You can find abi and bytecode there. You can also create a test for this smart contract. Create a new file: test/test_greeter.py.


from black_mamba.testlib import contract, eth_tester


def test_initial_greeting():
    hello_world_contract = contract("HelloWorld")
    assert hello_world_contract.functions.greet().call() == b"Hello World"

def test_initial_accounts(eth_tester):
    accounts = eth_tester.get_accounts()
    assert len(accounts) == 10
    assert eth_tester.get_balance(accounts[0]) == 1000000000000000000000000

Run the test.

(.venv) $ py.test test/test_greeter.py

You can deploy the smart contact to the blockchain. Let's use Ganache for this purpose. The blockchain configuration can be found in settings.py file.


networks = {
    "development": {
        "mode": "HTTP",
        "host": "localhost",
        "port": 7545,
        "network_id": "*"
    }
    #"development": {
    #    "mode": "IPC",
    #    "url": "/home/sarahconnor/ethereum/data/geth.ipc"
    #}
}

Run Ganache. Your deployment script is in migrations/deploy_HelloWorld.py.


from black_mamba.deploy import DeployContract
from web3 import Web3


deploy_contract_instance = DeployContract()
parameters = []
tx_params = { "from": None }
private_key = None

deploy_contract_instance.deploy_contract("HelloWorld", parameters, tx_params, private_key)

Since you are using Ganache, you don't have to fill the from account and the private key. Mamba will use the first account in Ganache and Ganache does not need the private key. Run the migration script (or deployment script if you will).

(.venv) $ python migrations/deploy_HelloWorld.py

The migration result is saved in deployed/receipt_HelloWorld.json. You can find the smart contract's address in this file. Let's create a script to interact with this smart contract.

(.venv) $ edit decentralized_app/set_name.py

from black_mamba.deploy import DeployContract


deployed = DeployContract()
contract = deployed.deployed_contract("HelloWorld")
greet = contract.functions.greet().call()
print(greet)

tx_param = {
    "from": deployed.w3.geth.personal.listAccounts()[0]
}
tx_hash = contract.functions.setGreeting(b"Hello Satoshi").transact(tx_param)
tx_receipt = deployed.w3.eth.waitForTransactionReceipt(tx_hash)
print(tx_receipt)

Then you can run the script.

(.venv) $ python decentralized_app/set_name.py