-
Notifications
You must be signed in to change notification settings - Fork 2
/
llm-tools.js
277 lines (258 loc) · 15.4 KB
/
llm-tools.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
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
/*
llm utility functions
use with Mist/MetaMask injected web3
*/
/* // https://ethereum.stackexchange.com/questions/11444/web3-js-with-promisified-api
window.promisify = (inner) =>
new Promise((resolve, reject) =>
inner((err, res) => {
if (err) { console.log(err); reject(err); }
resolve(res);
})
);
await window.promisify(() => console.log('hello')); // did not work! why?
*/
(function () {
window.llm = {
/* config */
// https://ropsten.etherscan.io/address/0x1e927cddb41e9347a47b23781a6a36e208575b73
FactoryContractAddress: '0x1e927cddb41e9347a47b23781a6a36e208575b73',
help() {
console.log(`
[ General utilities ]
llm.initWeb3(); // Init window.web3, llm.factoryContractInstance, llm.commimentContract
llm.initAccounts(); // Need to run before call llm.accountInfo();
llm.accountInfo(); // Get default account address and it's balance
llm.createCommitment(data); // data = { title: '...', deposit: inWeiValue, daysCount: intValue }
`);
this.currentCommitment.help();
this.guardingCommitment.help();
},
/* general utilities */
initWeb3() {
// Checking if Web3 has been injected by the browser (Mist/MetaMask)
if (typeof web3 !== "undefined") {
// Use Mist/MetaMask's provider
window.web3 = new Web3(web3.currentProvider);
} else {
console.log("No web3? You should consider trying MetaMask!");
}
this.factoryContractInstance = web3.eth.contract(this.abis.LlmFactory).at(this.FactoryContractAddress);
this.commimentContract = web3.eth.contract(this.abis.Commitment);
},
createCommitment(data, func) {
data.value = Math.round(parseFloat(data.deposit) * web3.toWei(1, 'ether'));
console.log('Create commitment using data: ', data);
this.factoryContractInstance.createCommitment(data.title,
parseInt(data.daysCount), {'value': data.value }, function(err, res) {
if (err) {
console.log(err.message);
} else {
console.log(res);
if (func) { func(); }
}
});
},
accountInfo(func) {
var accountAddress = web3.eth.defaultAccount;
web3.eth.getBalance(accountAddress, function(error, balance) {
if (error) {
console.error(error);
} else {
var balanceInEthers = balance.toNumber() / web3.toWei(1, 'ether');
console.log("Acccount " + accountAddress + " has " + balanceInEthers);
if (func) { func(accountAddress, balanceInEthers); }
}
});
},
initAccounts(func) {
// var accounts = await window.promisify(cb => web3.eth.getAccounts(cb));
web3.eth.getAccounts(function(error, accounts) {
if (error) {
console.error(error);
} else {
console.log('Accounts:', accounts);
if (func) { func(); }
}
});
},
isGuarding() {
return !!this.guardingCommitment.getContractAddress();
},
};
/* guardingCommitment utilities */
window.llm.guardingCommitment = {
help() {
console.log(`
[ guardingCommitment utilities ]
llm.guardingCommitment.getContractAddress();
llm.guardingCommitment.getContractInstant();
llm.guardingCommitment.getInfo();
llm.guardingCommitment.becomeGuardian();
llm.guardingCommitment.report(completed); // Report today committer progress. completed = true or false
`);
},
/*
Use https://tamedu.github.io/lazy-loose-money/?guardingCommitment=0xd282007af28c3f8e46fdef6cb3fdb91dfafc2911
to access the commitment you want to be guardian and to report committer daily progress
the contract address is stored in `guardingCommitment` GET parameter
*/
getContractAddress() {
var m = window.location.href.match(/guardingCommitment=(0x([a-z0-9]){40})/);
if (m) { return m[1]; }
},
getContractInstant() {
return window.llm.commimentContract.at(this.getContractAddress());
},
getInfo(func, func2) {
window.llm.currentCommitment.contractInstance = this.getContractInstant();
window.llm.currentCommitment.getInfo(function () {
window.llm.currentCommitment.contractInstance = null;
if (func) { func(); }
}, func2);
},
report(completed, func) {
this.getContractInstant().report(completed, function (err, res) {
if (err) {
console.log(err.message);
} else {
console.log(res);
if (func) { func(); }
}
});
},
becomeGuardian(data, func) {
data.value = Math.round(parseFloat(data.guardianDeposit) * web3.toWei(1, 'ether'));
console.log('Become guardian of: ', data);
this.getContractInstant().becomeGuardian({'value': data.value }, function(err, res) {
if (err) {
console.log(err.message);
} else {
console.log(res);
if (func) { func(); }
}
});
},
};
/* currentCommitment utilities */
window.llm.currentCommitment = {
help() {
console.log(`
[ currentCommitment utilities ]
llm.currentCommitment.getContractInstant();
llm.currentCommitment.getInfo();
llm.currentCommitment.cancel();
llm.currentCommitment.commit();
`);
},
contractInstance: null,
commit(func) {
this.contractInstance.commit(function (err, res) {
if (err) {
console.log(err.message);
} else {
console.log(res);
if (func) { func(); }
}
});
},
cancel(func) {
this.contractInstance.cancel(function (err, res) {
if (err) {
console.log(err.message);
} else {
console.log(res);
if (func) { func(); }
}
});
},
getContractInstant(func) {
window.llm.factoryContractInstance.getCurrentCommitmentAddress(function(err, res) {
if (err) {
console.log(err.message);
} else {
console.log('Current commitment address:', res);
window.llm.currentCommitment.contractInstance = window.llm.commimentContract.at(res);
console.log("Current commitment contract instant assigned to llm.currentCommitment.contractInstance");
if (func) { func(window.llm.currentCommitment.contractInstance); }
var events = window.llm.currentCommitment.contractInstance.allEvents({fromBlock: 0, toBlock: 'latest'});
events.get(function(err, logs) {
if (!err) { console.log("All events:", logs); }
});
}
});
},
getInfo(func, func2) {
if (this.contractInstance == null) {
console.log("Run llm.currentCommitment.getContractInstant() first");
return;
}
this.contractInstance.getInfo(function (err, res) {
if (err) {
console.log(err.message);
} else {
if (res[8] == '0x0000000000000000000000000000000000000000') res[8] = null;
currentCommitment = {
contractAddress: llm.currentCommitment.contractInstance.address,
title: res[1],
deposit: res[2].toNumber() / web3.toWei(1, 'ether'),
guardianDeposit: res[3].toNumber() / web3.toWei(1, 'ether'),
daysCount: res[4].toNumber(),
state: window.llm.State[res[5].toNumber()],
guardian: res[8],
startedAt: new Date(res[6].toNumber()*1000),
finishedAt: new Date(res[7].toNumber()*1000),
};
// https://github.com/ethereum/wiki/wiki/JavaScript-API#contract-events
var events = window.llm.currentCommitment.contractInstance.Reported({}, {fromBlock: 0, toBlock: 'latest'});
events.get(function(err, logs) {
if (err) {
console.log(err.message);
} else {
console.log("Reported events:", logs);
var dailyReports = [];
var m = moment(currentCommitment.startedAt);
var k = 0;
for (var i = 0; i < currentCommitment.daysCount; i++) {
dailyReports[i] = {
date: m.add(i, 'days'),
day: i,
completed: null,
reportedAt: null,
}
var reportedAt = logs[k].args.reportedAt.toNumber();
var startedAt = res[6].toNumber();
var days = Math.round((reportedAt - startedAt) / 86400); // 86400 = 60 * 60 * 24
// console.log(reportedAt, startedAt, days);
if (days == i) {
dailyReports[i].completed = logs[k].args.completed;
dailyReports[i].reportedAt = new Date(reportedAt*1000);
k = k + 1;
if (k >= logs.length) { break; }
}
} // end for
if (func2) { func2(dailyReports); }
}
});
console.log("Current commitment: ", currentCommitment);
if (func) { func(currentCommitment); }
}
});
},
};
/* data */
window.llm.State = [
'Opened',
'Guarded',
'Started',
'Pending',
'Closed'
];
/* run `solc --abi LazyLooseMoney.sol` to get the ABI jsons */
window.llm.abis = {
// ======= contracts/LazyLooseMoney.sol:Commitment =======
Commitment: [{"constant":true,"inputs":[],"name":"getState","outputs":[{"name":"","type":"uint8"}],"payable":false,"stateMutability":"view","type":"function"},{"constant":false,"inputs":[{"name":"encouragement","type":"string"}],"name":"supportFund","outputs":[],"payable":true,"stateMutability":"payable","type":"function"},{"constant":false,"inputs":[],"name":"commit","outputs":[],"payable":false,"stateMutability":"nonpayable","type":"function"},{"constant":true,"inputs":[],"name":"guardian","outputs":[{"name":"","type":"address"}],"payable":false,"stateMutability":"view","type":"function"},{"constant":true,"inputs":[],"name":"title","outputs":[{"name":"","type":"string"}],"payable":false,"stateMutability":"view","type":"function"},{"constant":true,"inputs":[],"name":"getInfo","outputs":[{"name":"","type":"address"},{"name":"","type":"string"},{"name":"","type":"uint256"},{"name":"","type":"uint256"},{"name":"","type":"uint256"},{"name":"","type":"uint8"},{"name":"","type":"uint256"},{"name":"","type":"uint256"},{"name":"","type":"address"}],"payable":false,"stateMutability":"view","type":"function"},{"constant":false,"inputs":[],"name":"release","outputs":[],"payable":false,"stateMutability":"nonpayable","type":"function"},{"constant":true,"inputs":[],"name":"owner","outputs":[{"name":"","type":"address"}],"payable":false,"stateMutability":"view","type":"function"},{"constant":false,"inputs":[{"name":"completed","type":"bool"}],"name":"report","outputs":[],"payable":false,"stateMutability":"nonpayable","type":"function"},{"constant":false,"inputs":[],"name":"cancel","outputs":[],"payable":false,"stateMutability":"nonpayable","type":"function"},{"constant":true,"inputs":[{"name":"","type":"address"}],"name":"supportersFunded","outputs":[{"name":"","type":"uint256"}],"payable":false,"stateMutability":"view","type":"function"},{"constant":false,"inputs":[],"name":"becomeGuardian","outputs":[],"payable":true,"stateMutability":"payable","type":"function"},{"inputs":[{"name":"_creator","type":"address"},{"name":"_title","type":"string"},{"name":"_days","type":"uint256"}],"payable":true,"stateMutability":"payable","type":"constructor"},{"anonymous":false,"inputs":[{"indexed":true,"name":"commitment","type":"address"},{"indexed":true,"name":"owner","type":"address"},{"indexed":false,"name":"deposit","type":"uint256"},{"indexed":false,"name":"title","type":"string"},{"indexed":false,"name":"daysCount","type":"uint256"},{"indexed":false,"name":"createdAt","type":"uint256"}],"name":"Created","type":"event"},{"anonymous":false,"inputs":[{"indexed":true,"name":"commitment","type":"address"},{"indexed":true,"name":"supporter","type":"address"},{"indexed":false,"name":"value","type":"uint256"},{"indexed":false,"name":"encouragement","type":"string"},{"indexed":false,"name":"fundedAt","type":"uint256"}],"name":"fundAdded","type":"event"},{"anonymous":false,"inputs":[{"indexed":true,"name":"commitment","type":"address"},{"indexed":true,"name":"owner","type":"address"},{"indexed":false,"name":"startedAt","type":"uint256"},{"indexed":false,"name":"finishedAt","type":"uint256"}],"name":"Started","type":"event"},{"anonymous":false,"inputs":[{"indexed":true,"name":"commitment","type":"address"},{"indexed":true,"name":"owner","type":"address"},{"indexed":true,"name":"guardian","type":"address"},{"indexed":false,"name":"guardianDeposit","type":"uint256"},{"indexed":false,"name":"guardedAt","type":"uint256"}],"name":"Guarded","type":"event"},{"anonymous":false,"inputs":[{"indexed":true,"name":"commitment","type":"address"},{"indexed":true,"name":"owner","type":"address"},{"indexed":true,"name":"guardian","type":"address"},{"indexed":false,"name":"completed","type":"bool"},{"indexed":false,"name":"reportedAt","type":"uint256"}],"name":"Reported","type":"event"},{"anonymous":false,"inputs":[{"indexed":true,"name":"commitment","type":"address"},{"indexed":true,"name":"owner","type":"address"},{"indexed":false,"name":"dayscount","type":"uint256"},{"indexed":false,"name":"reportedDays","type":"uint256"},{"indexed":false,"name":"completedDays","type":"uint256"},{"indexed":false,"name":"ownerReward","type":"uint256"},{"indexed":false,"name":"guardianReward","type":"uint256"},{"indexed":false,"name":"closedAt","type":"uint256"}],"name":"Closed","type":"event"}],
// ======= contracts/LazyLooseMoney.sol:LlmFactory =======
LlmFactory: [{"constant":false,"inputs":[{"name":"_title","type":"string"},{"name":"_days","type":"uint256"}],"name":"createCommitment","outputs":[],"payable":true,"stateMutability":"payable","type":"function"},{"constant":true,"inputs":[{"name":"","type":"uint256"}],"name":"commitments","outputs":[{"name":"","type":"address"}],"payable":false,"stateMutability":"view","type":"function"},{"constant":true,"inputs":[],"name":"getCurrentCommitmentAddress","outputs":[{"name":"","type":"address"}],"payable":false,"stateMutability":"view","type":"function"},{"constant":true,"inputs":[{"name":"","type":"address"}],"name":"currentCommitment","outputs":[{"name":"","type":"address"}],"payable":false,"stateMutability":"view","type":"function"},{"constant":true,"inputs":[],"name":"getLastCommitmentAddress","outputs":[{"name":"","type":"address"}],"payable":false,"stateMutability":"view","type":"function"},{"anonymous":false,"inputs":[{"indexed":true,"name":"owner","type":"address"},{"indexed":false,"name":"commitment","type":"address"},{"indexed":false,"name":"title","type":"string"},{"indexed":false,"name":"daysCount","type":"uint256"},{"indexed":false,"name":"createdAt","type":"uint256"}],"name":"CommitmentCreated","type":"event"}],
};
})();