Using Parameters in the Constructor of Smart Contract

The constructor in the smart contract can accept parameters. Mamba framework supports the usage of the parameters in the constructor.

Initialize Mamba project directory. See Quickstart to learn how to install Mamba framework.

(.venv) $ mamba init

Create a smart contract's code in Vyper.

(.venv) $ edit contracts/HelloParameters.vy

greeting: bytes[20]
index: uint256

@public
def __init__(_greeting: bytes[20], _number: uint256):
    self.greeting = _greeting
    self.index = _number

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

@public
def get_index() -> uint256:
    return self.index

Then you can compile this smart contract.

(.venv) $ mamba compile

The result of the compilation process is saved in build/contracts/HelloParameters.json. Let's create a test for this smart contract. Create a new test file: test/test_parameters.py.


from black_mamba.testlib import contract


def test_parameters():
    hello_parameters_contract = contract("HelloParameters", [b"Hello Parameters", 34])
    assert hello_parameters_contract.functions.greet().call() == b"Hello Parameters"
    assert hello_parameters_contract.functions.get_index().call() == 34

Basically you put the parameters as an array in the second parameter of contract function. Let's deploy your smart contract. Run Ganache first. Your deployment script is in migrations/deploy_HelloParameters.py.


from black_mamba.deploy import DeployContract


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

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

You can add the parameters for the smart contract to parameters variable. Your migration script should be like this one.


from black_mamba.deploy import DeployContract


deploy_contract_instance = DeployContract()
parameters = [b"Hello Parameters", 34]
tx_params = { "from": "" }
private_key = None

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

Then you can run your migration script.

(.venv) $ python migrations/deploy_HelloParameters.py