Last active
November 9, 2018 01:35
-
-
Save apalepu23/75cfdb4eb0b624e7a52dfe7bcf90201c to your computer and use it in GitHub Desktop.
callable
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// Specify signatures of callable/callback functions (with no spaces!) | |
const CALLABLE = "countVotes(uint,uint[],uint[])"; | |
const CALLBACK = "updatePollStatus(uint,uint,uint)"; | |
async enigmaTask(pollID) { | |
let poll = await this.props.voting.polls.call(pollID, { | |
from: this.props.enigmaSetup.accounts[this.props.curAccount], | |
gas: GAS | |
}); | |
let pollCreator = poll[0]; | |
// Get list of voters from contract given pollID | |
let voters = await this.props.voting.getVotersForPoll.call(pollID, { | |
from: pollCreator, | |
gas: GAS | |
}); | |
// Initialize empty encryptedVotes and weights arrays | |
let encryptedVotes = []; | |
let weights = []; | |
if (voters.length === 0) { | |
encryptedVotes.push(getEncryptedValue(0)); | |
weights.push(0); | |
} | |
// Loop through all the voters | |
for (let i = 0; i < voters.length; i++) { | |
// Obtain poll info (encrypted vote and weight) for that voter | |
let pollInfoForVoter = await this.props.voting.getPollInfoForVoter.call( | |
pollID, | |
voters[i], | |
{ | |
from: pollCreator, | |
gas: GAS | |
} | |
); | |
// Add encrypted vote to encryptedVotes array | |
encryptedVotes.push(pollInfoForVoter[0]); | |
let weight = this.props.enigmaSetup.web3.utils.toBN( | |
this.props.enigmaSetup.web3.utils.fromWei( | |
String(pollInfoForVoter[1].toNumber()), | |
"Ether" | |
) | |
); | |
// Add weight to weights array | |
weights.push(weight); | |
} | |
let blockNumber = await this.props.enigmaSetup.web3.eth.getBlockNumber(); | |
/* | |
Take special note of the arguments passed in here (blockNumber, dappContractAddress, | |
callable, callableArgs, callback, fee, preprocessors). This is the critical step for how | |
you run the secure computation from your front-end!!! | |
*/ | |
let task = await this.props.enigmaSetup.enigma.createTask( | |
blockNumber, | |
this.props.voting.address, | |
CALLABLE, | |
[pollID, encryptedVotes, weights], | |
CALLBACK, | |
ENG_FEE, | |
[] | |
); | |
let resultFee = await task.approveFee({ | |
from: this.props.enigmaSetup.accounts[0], | |
gas: GAS | |
}); | |
let result = await task.compute({ | |
from: this.props.enigmaSetup.accounts[0], | |
gas: GAS | |
}); | |
console.log("got tx:", result.tx, "for task:", task.taskId, ""); | |
console.log("mined on block:", result.receipt.blockNumber); | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment