-
Notifications
You must be signed in to change notification settings - Fork 7
Expand file tree
/
Copy pathmaker.js
More file actions
52 lines (48 loc) · 1.79 KB
/
maker.js
File metadata and controls
52 lines (48 loc) · 1.79 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
var fs = require("fs");
var Web3 = require("web3");
var web3 = new Web3();
var dappsys = require("./dapple_packages/dappsys/build/js_module.js");
var objects = JSON.parse(fs.readFileSync("./env/objects.json"));
var Maker = function() {
// Need to apply `brfs` transform to bundle this for browser
this.web3 = web3;
this.objects = objects;
this.header = dappsys; // TODO merge other dependencies
this.symbol_to_db_name = {
"MKR": "mkr_db",
"DAI": "dai_db"
}
for( var key of Object.keys(objects) ) {
var classname = this.objects[key].typename;
var abi = JSON.parse(this.header[classname]["interface"]);
var contract = this.web3.eth.contract(abi);
var address = this.objects[key].address;
var instance = contract.at(address);
this.objects[key].instance = instance
}
this.getSupply = function(symbol, cb) {
var objectname = this.symbol_to_db_name[symbol];
var baldb = this.objects[objectname].instance;
baldb.get_supply(function(err, res) {
if (err) { cb(err, null); }
if (!res[1]) { cb(res[1], null); }
var wei_supply = res[0].toNumber();
var supply = wei_supply / Math.pow(10, 18);
cb(null, supply);
});
}
this.getBalance = function(symbol, addr, cb) {
var objectname = this.symbol_to_db_name[symbol];
var baldb = this.objects[objectname].instance;
baldb.get_balance(function(err, res) {
if (err) { cb(err, null); }
if (!res[1]) { cb(res[1], null); }
var wei_balance = res[0].toNumber();
var balance = wei_balance / Math.pow(10, 18);
cb(null, balance);
});
}
};
var maker = new Maker();
module.exports.maker = maker;
window.maker = maker;