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 have development tools (Python development header files, compiler) installed and you need to have wheel installed as well.


(.venv) $ pip install wheel

Then install Mamba blockchain development framework using pip.

(.venv) $ pip install black-mamba

In this quick start guide, we use Vyper programming language. If you want to work on Solidity files, you must install solc compiler first and put it in the PATH environment variable so Mamba can find the Solidity compiler.

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: String[20]

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

@external
def setGreeting(x: String[20]):
    self.greeting = x

@external
def greet() -> String[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, w3, TestContract

class TestHello(TestContract):
    def test_initial_greeting(self, w3):
        hello_world_contract = contract("HelloWorld")
        assert hello_world_contract.functions.greet().call() == "Hello World"

    def test_initial_accounts(self, w3):
        accounts = w3.eth.accounts
        assert len(accounts) == 10
        assert w3.eth.getBalance(accounts[2]) == 1000000000000000000000000

Run the test.

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

You are using EthereumTesterProvider with this way of testing. However, you can use Ganache CLI as tester provider. Refer to the relevant documentation.

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("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