-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathindex.js
142 lines (128 loc) · 4.01 KB
/
index.js
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
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
import Vue from "vue";
import Vuex from "vuex";
import web3 from "../providers/web3";
import DaoTokenJson from "../providers/abi/DaoToken.json";
import CommunityVotingJson from "../providers/abi/CommunityVoting.json";
Vue.use(Vuex);
const store = new Vuex.Store({
state: {
web3: null,
DaoTokenContract: null,
CommunityVotingContract: null,
accountAddress: null,
member: false,
chairmanConnected: false,
blockNumber: 0,
mintingEnabled: false,
tokenHoldersList: [],
propositionsList: [],
alert: {
visible: false,
message: null,
},
},
getters: {
numberOfTokenHolders: (state) => {
return state.tokenHoldersList.length;
},
propositions: (state) => {
return state.propositionsList.slice().reverse();
},
numberOfPropositions: (state) => {
return state.propositionsList.length;
},
alertVisible: (state) => {
return state.alert.visible;
},
alertMessage: (state) => {
return state.alert.message;
},
},
mutations: {
loadAccountData: async (state) => {
const accounts = await state.web3.eth.getAccounts();
state.accountAddress = accounts[0];
const contract = state.DaoTokenContract;
// check if account is a member
const balance = await contract.methods
.balanceOf(state.accountAddress)
.call();
state.member = balance > 0;
// check if account is a chairman
const chairmanAddress = await contract.methods.chairman().call();
state.chairmanConnected = state.accountAddress == chairmanAddress;
},
loadBlockNumber: async (state) => {
const block = await state.web3.eth.getBlockNumber();
state.blockNumber = block;
},
loadMembers: async (state) => {
const contract = state.DaoTokenContract;
const members = await contract.methods.tokenHolders().call();
// shuffle for fun
var arrayCopy = members.slice(0);
const sorter = arrayCopy.sort(function() {
return Math.random() - 0.5;
});
state.tokenHoldersList = sorter;
},
loadMintingStatus: async (state) => {
const contract = state.DaoTokenContract;
try {
const minting = await contract.methods.isMintable().call();
state.mintingEnabled = minting;
} catch (err) {
console.error(err);
}
},
loadPropositionsList: async (state) => {
const contract = state.CommunityVotingContract;
const propositionsNumber = await contract.methods
.getNumberOfPropositions()
.call();
// load only newer propositions
const currentLen = state.propositionsList.length;
for (let i = currentLen; i < propositionsNumber; i++) {
const prop = await contract.methods.getPropositionInfo(i).call();
state.propositionsList.push(prop);
}
},
prepareWeb3: (state, web3) => {
// store new web3
state.web3 = web3;
// load dao token contract
const tokenAddress = "0xebc303f547DB6c9f18355E247871f3563E1E86d4";
const DaoTokenContract = new state.web3.eth.Contract(
DaoTokenJson.abi,
tokenAddress
);
// load community voting contract
const communityAddress = "0x153BcF2593E3e2645A443BC2739a0b504932c9b4";
const CommunityVotingContract = new state.web3.eth.Contract(
CommunityVotingJson.abi,
communityAddress
);
// store contract references
state.DaoTokenContract = DaoTokenContract;
state.CommunityVotingContract = CommunityVotingContract;
},
clearAlert: (state) => {
state.alert.visible = false;
state.alert.message = null;
},
createAlert: (state, message) => {
state.alert.visible = true;
state.alert.message = message;
},
},
actions: {
updateApplicationData(context) {
context.commit("loadBlockNumber");
context.commit("loadMembers");
context.commit("loadMintingStatus");
context.commit("loadPropositionsList");
},
},
});
store.commit("prepareWeb3", web3);
export default store;