Forums

Having Trouble Returning a value from a smart contract, using flask.

I am using flask and web3.eth and connecting to a contract, but am having issues returning a value for one of the smart contract functions. No errors are being logged. I have placed several print statements to see where the app is stopping and found that it stops when I call a smart contract function.

Also I should mention that I am able to run this exact code locally without issues.

This is the function that is most likely failing:

def getDataFromTokenID(tokenid, contract):
    print('getting uri')
    uri = contract.functions.tokenURI(tokenid).call() # This is where it stops printing
    print('PRINT:',uri)
    html = requests.get(uri)
    name, img_url = html.json()['name'], html.json()['image']
    code = name[-5:]
    return name, img_url, code

The function above is called in the following blueprint:

@TokenInfo.route('/rarity/<int:tokenid>', methods=['GET'])
def sendTokenInfo(tokenid):

    contract_address = ' ' # left empty for posting purposes
    w3 = Web3(Web3.WebsocketProvider(' ')) # left empty purposefully as well
    contract = w3.eth.contract(address=contract_address, abi=contract_abi.abi)

    model = Shape_classifier()
    model.load_state_dict(load(os.getcwd()+'/mysite/app/state_dict.pth'))

    uri = current_app.config['MONGO_URI']
    mongo.init_app(current_app, uri)
    gs = mongo.db.gantomstone_info

    try:
        id_exists = [{"$match": {'_id': tokenid}}, {"$count": "count"}]
        list(gs.aggregate(id_exists))[0]

    except:
        print('getting data from token id')
        name, img_url, serial = getDataFromTokenID(tokenid, contract) ## Stops printing here
        print('opening image')
        img = Image.open(requests.get(img_url, stream=True).raw)
        shape = getImageShape(img, model)
        colors = getColors(getCounts(img))
        rgb_count = getCounts(img)
        serialTF = getCodeInfo(serial)
        to_db = {'_id': tokenid, 'name': name, 'img_url': img_url, 'serial': serial,
                 'shape': shape, 'colors': colors, 'serialTF': serialTF, 'rgb_count': rgb_count}
        gs.insert_one(to_db)

    rarity = getRarity(gs, tokenid)
    gs.update_one({'_id': tokenid}, {
                  '$set': {'rarity_values': rarity}}, upsert=True)

    to_json = list(gs.find({'_id': tokenid}))[0]

    return jsonify(to_json)

I have tried moving contract address around (out of TokenInfo view function and into the functions file) to no avail. I have also tried changing the function inputs to receive the get request args which made no difference either.

If anyone has any idea how to get this working that would be amazing and I would be very grateful.

Thank you.

.

I see some HARAKIRIs on workers in your server logs -- we have a 5 min timeout on all web processes. Btw. does it use websockets? If so, it won't work on PA.

Oh that's unfortunate, yeah it uses websockets. Thats the problem. Thank you.

No problem.