Running Geth Private Network for Development

Mamba framework can run Geth private development server with some preloaded accounts.

You can run Geth private development server with Mamba framework. But you need to install Geth software separately and put geth application on PATH. You can download Geth from Ethereum website. Assuming you have Mamba installed already and you are inside Mamba project directory, you can run this command:

(.venv) $ mamba server

Mamba uses IPC (Inter Process Communication) mode and disables RPC (Remote Procedure Call) and WS (Web Socket) modes. If you want to use RPC and WS modes, you can add --httpserver flag.

(.venv) $ mamba server --httpserver

The Geth private development server's directory is in datadir directory inside Mamba project directory. There is genesis.json file inside datadir which you can read. There are 10 preloaded accounts (same as Ganache) with 100 ETH. The private key of these accounts can be seen in datadir/private_key.json.

Let's write a smart contract and compile it.

(.venv) $ edit contracts/HelloWorld.vy

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
(.venv) $ mamba compile

You must edit the settings file so our Mamba project directory can be linked to Geth private development server.

(.venv) $ edit settings.py

networks = {
    "development": {
        "mode": "IPC",
        "url": "/Users/sarahconnor/darkfate/mambaproject/datadir/geth.ipc"
    }
}

Then you must edit the migration script: migrations/deploy_HelloWorld.py. Then execute it.


from black_mamba.deploy import DeployContract


deploy_contract_instance = DeployContract()
parameters = []
tx_params = { "from": "0x0269AaeB4128435E763c3E3ACEE554a68CE95fA9" }
# Don't hardcode the private key. This is just for demonstration purpose.
private_key = "2f702d87dabd62ce7c14543f01d24a5ed05a49c1ee80640866db7dbb50d91cf2"

deploy_contract_instance.deploy_contract("HelloWorld", parameters, tx_params, private_key)
(.venv) $ python migrations/deploy_HelloWorld.py

Of course, if you want to, you can set nonce, gas, gasPrice in tx_params dictionary. But in this example, we just use default values. Mamba can figure them out.

Let's create a simple decentralized application.

(.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)

account = "0x0269AaeB4128435E763c3E3ACEE554a68CE95fA9"
nonce = deployed.w3.eth.getTransactionCount(account)
tx_param = {
    "from": account,
    "nonce": nonce
}
txn = contract.functions.setGreeting(b"Hello Satoshi").buildTransaction(tx_param)
# Don't hardcode the private key. This is just for demonstration purpose.
private_key = "2f702d87dabd62ce7c14543f01d24a5ed05a49c1ee80640866db7dbb50d91cf2"
signed_txn = deployed.w3.eth.account.signTransaction(txn, private_key=private_key)
tx_hash = deployed.w3.eth.sendRawTransaction(signed_txn.rawTransaction)
tx_receipt = deployed.w3.eth.waitForTransactionReceipt(tx_hash)
print(tx_receipt)

greet = contract.functions.greet().call()
print(greet)

Then you can happily execute the script.

(.venv) $ python decentralized_app/set_name.py