How to calculate average of some numbers in the secret contract of Enigma?

1down votefavorite

I want to calculate the average of the Millionaires based on this article.

And this is my code.

// CALLABLE FUNCTION to calculate the average assets
function computeAverage(address[] _addresses, uint[] _netWorths)
    returns (uint)
    uint average;
    uint sum = 0;
    for (uint i = 0; i < _netWorths.length; i++) {
        sum += _netWorths[i];
    average = sum / _netWorths.length;
    return average;

// CALLBACK FUNCTION to change contract state
function averageAssets(uint _uint) public onlyEnigma() {
    averageAssetsResult = _uint;
    emit CallbackFinished();

This average becomes some big number like 52576064 (when the actual number is 125), 52573696 (when the actual number is 100).

I think this is natural because in secret contract we need to hide the actual number. Is there some built-in average function?

I came here from this question. Thanks Adi!!

1 Like

Hey Hidehiro!

Super glad you posted this initially, actually helped point out a little bug that has now been resolved, so thank you :slight_smile:

There were two issues at play here:

1.) string vs. uint: In the developer guide pushed out, there was a bug where we were actually passing in an encrypted value of a string type instead of a number type for the net worth. To fix this, I made two modifications, which you can see in the guide.

The first is in the AddMillionaireDialog.js section (line 126) - I specify that the input field is of type number.

The second is in the MillionairesProblemWrapper.js section (line 58) - I pass in a BigNumber instance of the net worth.

2.) Even if you had properly passed in an int, it wasn’t being properly handled. This has now been resolved in the internal services and you can ensure you are using the latest correct code by running docker-compose build --no-cache within your network tab (the one that holds the enigma-docker-network) and relaunching with ./launch.bash -s -t

Hopefully with these two you should be good to go, but please reach out if something isn’t working still :slight_smile: