-
Notifications
You must be signed in to change notification settings - Fork 3
/
operator.js
100 lines (80 loc) · 2.53 KB
/
operator.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
/* global module, Buffer */
const abi = require('./lib/abi.js');
const addresses = require('./lib/address.js');
const base58 = require('base-58');
module.exports = function(web3, contractOpts = {}) {
//
// Config
//
this.networkId = contractOpts['networkId'] || web3.version.network;
this.contractName = 'Operator';
this.config = {
address: addresses[this.contractName][this.networkId],
abi: abi[this.contractName][this.networkId]
};
['address', 'abi'].forEach(m => {
if (contractOpts && contractOpts[m]) {
this.config[m] = contractOpts[m];
}
});
if (!this.config.abi) { throw 'ABI not found for contract: ' + this.contractName; }
if (!this.config.address) { throw 'address not found for contract: ' + this.contractName;}
this.contract = web3.eth.contract(this.config.abi).at(this.config.address);
//
// Utils
//
this.getValueFromContract = function(contractMethod, ...args) {
return new Promise((resolve, reject) => {
this.contract[contractMethod](...args, (err, data) => {
if (err) { reject(err); return; }
resolve(data);
});
});
};
//
// Contributors
//
this.contributorsCount = function() {
return this.getValueFromContract('contributorsCount');
};
this.addContributor = function(address, profileHashBase58, isCore) {
const profileHashHex = Buffer.from(base58.decode(profileHashBase58))
.toString('hex');
return new Promise((resolve, reject) => {
this.contract.addContributor(address, '0x'+profileHashHex, isCore, (err, res) => {
if (err) { reject(err); } else { resolve(res); }
});
});
};
this.getContributor = function(id) {
return new Promise((resolve, reject) => {
this.contract.getContributor(id, (err, res) => {
if (err) { reject(err); return; }
const profileHashHex = res[1].replace('0x','');
const profileHashBase58 = base58.encode(new Buffer(profileHashHex, 'hex'));
const contributor = {
id: id,
address: res[0],
profileHash: profileHashBase58,
isCore: res[2]
};
resolve(contributor);
});
});
};
//
// Proposals
//
this.proposalsCount = function() {
return this.getValueFromContract('proposalsCount');
};
this.proposals = function(i) {
return this.getValueFromContract('proposals', i);
};
this.addProposal = this.contract.addProposal;
this.vote = this.contract.vote;
//
// Events
//
this.allEvents = this.contract.allEvents;
};