/
deposit-cash-to-address.js
115 lines (102 loc) · 3.71 KB
/
deposit-cash-to-address.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
#!/usr/bin/env node
"use strict";
var chalk = require("chalk");
var speedomatic = require("speedomatic");
function showCashBalance(augur, address, callback) {
augur.api.Cash.balanceOf({ _owner: address }, function (err, cashBalance) {
if (err) {
console.log(chalk.red(err));
return callback(err);
}
var bnCashBalance = speedomatic.bignum(cashBalance);
var totalCashBalance = speedomatic.unfix(bnCashBalance, "string");
console.log("Cash:", chalk.green(totalCashBalance));
callback(null);
});
}
function getNextFeeWindow(augur, universe, auth, callback) {
augur.api.Universe.getOrCreateNextFeeWindow({
meta: auth,
tx: { to: universe },
onSent: result => {
console.log("Deposit Cash to address:", JSON.stringify(result));
},
onSuccess: function (result) {
callback(null, result.callReturn);
},
onFailed: function (result) {
console.log("Failed Deposit:", JSON.stringify(result));
callback(result);
},
});
}
function getCurrentFeeWindow(augur, universe, callback) {
augur.api.Universe.getCurrentFeeWindow({ tx: { to: universe } }, function (err, feeWindow) {
if (err) {
console.log(chalk.red(err));
return callback(err);
}
callback(err, feeWindow);
});
}
function help() {
console.log(chalk.red("deposit-cash, will simply deposits the amount of CASH givn in ETH on the address provided"));
console.log(chalk.red("default address is next fee window"));
console.log(chalk.red("default amount is 1 ETH"));
}
function depositCashToAddress(augur, args, auth, callback) {
if (args === "help" || args.opt.help) {
help();
return callback(null);
}
var address = args.opt.address;
var amount = args.opt.amount || 1000000000000000000;
var currentFeeWindowFlag = args.opt.current;
var nextFeeWindowFlag = args.opt.next;
var cash = augur.contracts.addresses[augur.rpc.getNetworkID()].Cash;
var universe = augur.contracts.addresses[augur.rpc.getNetworkID()].Universe;
getNextFeeWindow(augur, universe, auth, (err, nextFeeWindow) => {
if (err) {
console.log(chalk.red(err));
return callback(err);
}
getCurrentFeeWindow(augur, universe, (err, currentFeeWindow) => {
if (err) {
console.log(chalk.red(err));
return callback(err);
}
console.log("next fee window", nextFeeWindow);
console.log("current fee window", currentFeeWindow);
const fw = currentFeeWindowFlag ? currentFeeWindow : nextFeeWindow;
const nfw = nextFeeWindowFlag ? nextFeeWindow : fw;
const feeWindow = address ? address : nfw;
console.log(chalk.green.dim("address:"), chalk.green(feeWindow));
console.log(chalk.green.dim("amount:"), chalk.green(amount));
var cashPayload = {
meta: auth,
tx: { to: cash, value: augur.utils.convertBigNumberToHexString(amount) },
_to: feeWindow,
onSent: result => {
console.log(chalk.yellow.dim("Deposit Cash to address:"), chalk.yellow(JSON.stringify(result)));
console.log(chalk.yellow.dim("Waiting for reply ...."));
},
onSuccess: function (result) {
console.log(chalk.green.dim("Success Deposited:"), chalk.green(JSON.stringify(result)));
showCashBalance(augur, feeWindow, function (err) {
if (err) {
console.log(chalk.red(err));
return callback(JSON.stringify(err));
}
callback(null);
});
},
onFailed: function (result) {
console.log(chalk.red.dim("Failed Deposit:"), chalk.red(JSON.stringify(result)));
callback(result);
},
};
augur.api.Cash.depositEtherFor(cashPayload);
});
});
}
module.exports = depositCashToAddress;