-
Notifications
You must be signed in to change notification settings - Fork 1
/
PoloAPIWrapper.gs
137 lines (111 loc) · 4.9 KB
/
PoloAPIWrapper.gs
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
/*************************************************************************
The point of wrapper functions is many fold:
* to extend the baseAPI functionality
* keep the cluttering away from the basic API functions
* to validate the parameters of baseAPI calls before sending them
* trapping errors
*********/
var poloniexApi = function(apiKey,secretKey) {
//Inherit all functions from the exchange API
exchangePoloApi.call(this,apiKey,secretKey);
/*************************************************************************
* Wrapper functions in addition to default API functions - extends API
*********/
/*************************************************************************
* Wrapper functions : Base API
*********/
this.createLoanOffer = function(currency, amount, duration, autorenew, lendingrate){
/******
* Perform basic validation on parameters before wasting API call
*/
if (currency == undefined) { // argument not passed or undefined
Logger.warning('Function createLoanOffer in PoloAPIWrapper has no currency parameter. Operation aborted.');
return;
}
if (amount == undefined) { // argument not passed or undefined
Logger.warning('Function createLoanOffer in PoloAPIWrapper has no amount parameter. Operation aborted.');
return;
} else {
if(amount < 0){
Logger.warning('Function createLoanOffer in PoloAPIWrapper has negative loan amount. Operation aborted.');
return;
}
}
if (duration == undefined) { // argument not passed or undefined
Logger.warning('Function createLoanOffer in PoloAPIWrapper has no duration parameter. Operation aborted.');
return;
} else {
if(duration < MIN_NUM_LOAN_DAYS){
Logger.warning('Function createLoanOffer in PoloAPIWrapper duration parameter is too low. Minimum number of days is ' + MIN_NUM_LOAN_DAYS +'. Operation aborted.');
return;
}
if(duration > MAX_NUM_LOAN_DAYS){
Logger.warning('Function createLoanOffer in PoloAPIWrapper duration parameter is too high. Maximum number of days is ' + MAX_NUM_LOAN_DAYS +'. Operation aborted.');
return;
}
}
if (autorenew == undefined) { // argument not passed or undefined
Logger.warning('Function createLoanOffer in PoloAPIWrapper has no autorenew parameter. Operation aborted.');
return;
} else {
if(!(autorenew == 0 || autorenew == 1)){
Logger.warning('Function createLoanOffer in PoloAPIWrapper has an unsupported value for autorenew parameter. Only values 0 or 1 is allowed. Operation aborted.');
return;
}
}
if (lendingrate == undefined) { // argument not passed or undefined
Logger.warning('Function createLoanOffer in PoloAPIWrapper has no lendingrate parameter. Operation aborted.');
return;
} else {
if(lendingrate <= 0){
Logger.warning('Function createLoanOffer in PoloAPIWrapper has negative or zero lendingrate amount. Operation aborted.');
return;
}
}
var parcedData = this.baseApiCreateLoanOffer(currency, amount, duration, autorenew, lendingrate)
return parcedData['orderID']
}
this.returnAvailableAccountBalances = function(){
var t = Date.now();
var value = this.baseApiReturnAvailableAccountBalances();
Logger.fine('"returnAvailableAccountBalances" runtime: ' + (Date.now() - t)/1000 + ' seconds.');
return value;
}
this.returnOpenLoanOffers = function(){
var t = Date.now();
var value = this.baseApiReturnOpenLoanOffers()
Logger.fine('"returnOpenLoanOffers" runtime: ' + (Date.now() - t)/1000 + ' seconds.')
return value;
}
/*************************************************************************
* Wrapper functions : Extend Base API
*********/
//Illustration of renaming an existing function
this.returnAllOpenOrders = function (){
return this.returnOpenOrders();
}
//return a parced list of all deposit addresses
this.returnAllDepositAddresses = function(){
var tempArray = []
var parcedData = this.returnDepositAddresses();
for(var key in parcedData){
tempArray.push([key, parcedData[key]])}
return tempArray;
};
//generate a new deposit address for all currencies
this.generateNewAddressAllCurrencies = function(){
var parcedData = this.exchInstance.returnBalances();
for(var key in parcedData){
this.exchInstance.generateNewAddress(key);
Utilities.sleep(1000); //Need to sleep to avoid nonce errors - generation of addresses extremely fast
}
}
//return the available balance by wallet and currency - this is an API parser
this.returnAvailableBalance = function(balances, wallet, currency){
try {
if (balances[wallet][currency.toString()] == null) throw 0;
return balances[wallet][currency.toString()];
}
catch(err){return 0}
}
}