-1

So I am currently trying to deploy my smart contract using a brownie script as shown in the code below:

from brownie import accounts, network
import sys

sys.path.append("../")
import globalVars

def main():

    import_str = "from {0} import {1}".format("brownie", globalVars.contractName)
    exec(import_str)

    network.connect('development')
    accounts.add()
    account = accounts[0]
   
    transactDetails = {'from' : account}
    prompt = f"contract_instance = {globalVars.contractName}.deploy( 
                         *globalVars.constructorInputs , {transactDetails} )"
    exec(prompt)

    return contract_instance

Meanwhile, when python interprets exec(prompt), it raises this error:

File "<string>", line 1
    contract_instance = Voting.deploy( * globalVars.constructorInputs , {'from': <Account '0x66aB6D9362d4F35596279692F0251Db635165871'>} )
                                                                                 ^
SyntaxError: invalid syntax

I was expecting the smart contract to be deployed like in standard manner like in this code:

from brownie import accounts, network, Voting
import sys

sys.path.append("../")
import globalVars

def main():

    network.connect("development")
    accounts.add()
    account = accounts[0]
    print(globalVars.constructorInputs)
    contract_instance = Voting.deploy( * globalVars.constructorInputs ,  {'from' : account} )
    
    
    return contract_instance

I tried troubleshooting but could not find a solution, could someone please help!

buran
  • 13,682
  • 10
  • 36
  • 61
mnsdali
  • 3
  • 2
  • `exec()` should be avoided. See chepner's answer for how to do so. On the other hand, you can use `contract_instance = eval(f'{...}').deploy(*..., transactDetails)`. – InSync Mar 27 '23 at 13:32
  • @InSync thank you so much bro, your alternative could be so helpful to me in further processes, xoxo – mnsdali Mar 29 '23 at 10:43

1 Answers1

0

Don't use exec at all. Use something like

import brownie
import globalVars

def main():

    contract = getattr(brownie, globalVars.contractName)

    network.connect('development')
    accounts.add()
    account = accounts[0]
   
    transactDetails = {'from' : account}
    contract_instance = contract.deploy( 
                         *globalVars.constructorInputs , **transactDetails)

    return contract_instance
chepner
  • 497,756
  • 71
  • 530
  • 681
  • Thank you, your reply has helped, just one tiny remark: I store my global variables in a python file called globalVars which is located between my flask directory and my brownie project, that is why I use: import sys sys.path.append("../") import globalVars inside my code, so that the target file could recognize which variables I am using. Anyway thanks for the fast feedback! Solved! – mnsdali Mar 27 '23 at 13:57