-
Notifications
You must be signed in to change notification settings - Fork 209
Expand file tree
/
Copy pathretrieveRewards.js
More file actions
73 lines (67 loc) · 2.9 KB
/
retrieveRewards.js
File metadata and controls
73 lines (67 loc) · 2.9 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
const getDefaultAccount = require("../wallet/getDefaultAccount");
const style = require("../textStyle");
const { batchRetrieveRewards } = require("../../../../common/VotingUtils");
const getAvailableRewards = require("./getRewardsByRoundId");
const inquirer = require("inquirer");
const argv = require("minimist")(process.argv.slice());
/**
* This prompts the user to select from a list of round ID's that have one or more rewards that can be retrieved.
* For the selected round ID, we batch retrieve all of the rewards for the round.
*
* @param {* Object} web3 Web3 provider
* @param {* Object} voting deployed Voting.sol contract instance
*/
const retrieveRewards = async (web3, voting, designatedVoting) => {
// TODO(#901): MetaMask provider sometimes has trouble reading past events
if (argv.network === "metamask") {
console.log(
`Sorry, we currently do not support retrieving rewards for Metamask users! Try again with another web3 provider.`
);
return;
}
style.spinnerReadingContracts.start();
// If the user is using the two key contract, then the account is the designated voting contract's address
const account = designatedVoting ? designatedVoting.address : await getDefaultAccount(web3);
const { rewardsByRoundId, roundIds } = await getAvailableRewards(web3, voting, account);
style.spinnerReadingContracts.stop();
if (roundIds.length > 0) {
console.group(
`${style.instruction(
`\nPlease select which round ID of resolved price requests you would like to retrieve rewards for:`
)}`
);
roundIds.push({ name: "back" });
const list = await inquirer.prompt({
type: "list",
name: "roundIdList",
message: `You will retrieve rewards for all price requests for the round ID you select. You can only retrieve rewards for price requests that you have voted (committed AND revealed) and that have resolved.`,
choices: roundIds
});
if (list["roundIdList"] !== "back") {
const roundId = list["roundIdList"];
const resolvedVotes = rewardsByRoundId[roundId];
// Batch retrieve rewards
style.spinnerWritingContracts.start();
const { successes, batches } = await batchRetrieveRewards(resolvedVotes, roundId, voting, account);
style.spinnerWritingContracts.stop();
// Print results
console.log(
style.success(
`You have successfully retrieved ${successes.length} reward${
successes.length === 1 ? "" : "s"
} in ${batches} batch${batches === 1 ? "" : "es"}.`
)
);
console.group(style.success(`Receipts:`));
for (let i = 0; i < successes.length; i++) {
console.log(`- transaction: ${style.link(`https://etherscan.io/tx/${successes[i].txnHash}`)}`);
}
console.groupEnd();
}
console.log(`\n`);
console.groupEnd();
} else {
console.log(`You have no rewards to retrieve.`);
}
};
module.exports = retrieveRewards;