0

I'm looking for something like an environment variable or a #define where the constant value gets substituted into the smart contract prior to compilation.

I have carefully read through the Brownie docs and found nothing but this seems like something that should exist.

  • What is it you're trying to achieve? Why does it need to be before compilation and not just passed in as a constructor argument? – The Ref May 20 '22 at 12:02

1 Answers1

0

I ended up writing a Python function that reads the contract and extracts constant definitions from it into a dictionary that can be used by other Python scripts:

import re


def get_integer_constant_definitions_from_contract(contract_path):
    """Searches through a smart contract for constant declarations.

    Args:
        contract_path: The file path of the contract to search.

    Returns: A dictionary whose keys are the constant names and whose values
        are the corresponding constant values.

    """

    contract = open(contract_path, "r")
    contract_text = contract.read()

    # Matches constants with names in all caps and base 10 integer values.
    p = re.compile("(^[A-Z][A-Z_]+).*=[ ]?([0-9]+)[ ]*$", re.MULTILINE)
    base_10_matches = p.findall(contract_text)

    constants = {}
    for match in base_10_matches:
        constants[match[0]] = int(match[1])

    # Matches constants with names in all caps and hex integer values.
    p = re.compile("(^[A-Z][A-Z_]+).*=[ ]?(0[Xx][0-9A-Fa-f]+)[ ]*$", re.MULTILINE)
    hex_matches = p.findall(contract_text)

    for match in hex_matches:
        constants[match[0]] = int(match[1], 16)

    return constants