-
Notifications
You must be signed in to change notification settings - Fork 2
/
ts.js
88 lines (74 loc) · 3.12 KB
/
ts.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
var Mobinsure = artifacts.require("./Mobinsure.sol")
var mobinsure = Mobinsure.at(Mobinsure.address)
var XMLHttpRequest = require("xmlhttprequest").XMLHttpRequest;
var utils = require("./utils.js")
var policyValue = web3.toWei(0.007, 'ether') * 5;
// Copied from some StackOverflow post.
function httpGetAsync(theUrl, callback) {
var xmlHttp = new XMLHttpRequest();
xmlHttp.onreadystatechange = function() {
if (xmlHttp.readyState == 4 && xmlHttp.status == 200)
callback(xmlHttp.responseText);
}
xmlHttp.open("GET", theUrl, true); // true for asynchronous
xmlHttp.send(null);
}
var watchForPolicyCreation = imeiString => {
var buyPolicySuccessfullyMade = mobinsure.LogBuyPolicySuccess({ imei: imeiString })
buyPolicySuccessfullyMade.watch((error, response) => {
if (!error) {
console.log("Policy Purchased Imei: " + response.args.imei + " Policy owner: " + response.args.policyOwner + " Time invalid: " + response.args.timePolicyInvalid)
buyPolicySuccessfullyMade.stopWatching()
} else {
console.log(error)
}
})
}
var watchForClaimMade = imeiString => {
var claimSuccessfullyMade = mobinsure.LogMakeClaimSuccess({ imei: imeiString })
claimSuccessfullyMade.watch((error, response) => {
console.log("Policy owner: " + response.args.imei + " Payout (eth): " + web3.fromWei(response.args.payout, 'ether'))
claimSuccessfullyMade.stopWatching()
})
}
var buyPolicy = (fromAccount, imei, blockedStatus) =>
mobinsure.buyPolicy(imei, blockedStatus, { from: web3.eth.accounts[fromAccount], value: policyValue })
.then(tx => {
watchForPolicyCreation(imei)
console.log("Buy policy tx made from account " + fromAccount + " with IMEI: " + imei)
})
var makeClaim = (fromAccount, imei, blockedStatus) =>
mobinsure.makeClaim(imei, blockedStatus, { from: web3.eth.accounts[fromAccount] })
.then(tx => {
watchForClaimMade(imei)
console.log("Claim tx made from account " + fromAccount + " for IMEI: " + imei)
})
var getOraclizeFee = () => mobinsure.getOraclizeFee()
.then(fee => console.log("Oraclize fee: " + fee))
var contractBalance = () => console.log("Mobinsure contract balance: " + utils.balance(Mobinsure.address))
// mobinsure.PRICE_CHECK_URL().then(cryptoCompare =>
// mobinsure.PREMIUM_VALUE_GBP()
// .then(premium => httpGetAsync("https://min-api.cryptocompare.com/data/price?fsym=GBP&tsyms=ETH",
// response => console.log("£" + premium + " in Eth: " + ((JSON.parse(response).ETH) * premium)))))
// Note, it appears I do not understand indexed events properly so all the following tx's logs are called at once.
// Keeps crashing when I use indexed logs.
// UNCOMMENT THE BELOW TO TEST POLICY BUYING
// utils.balances(2)
// contractBalance()
// buyPolicy(0, "1", "unblocked")
// .then(() => buyPolicy(1, "2", "unblocked"))
// .then(() => buyPolicy(0, "3", "unblocked"))
// .then(() => buyPolicy(1, "4", "unblocked"))
// .then(() => {
// utils.balances(2)
// contractBalance()
// })
// UNCOMMENT THE BELOW TO TEST POLICY CLAIMING
utils.balances(1)
contractBalance()
makeClaim(0, "1", "blocked")
.then(() => {
utils.balances(1)
contractBalance()
})
module.exports = callback => {}