-
Notifications
You must be signed in to change notification settings - Fork 22
Expand file tree
/
Copy pathbtc38.c
More file actions
executable file
·321 lines (302 loc) · 12.2 KB
/
btc38.c
File metadata and controls
executable file
·321 lines (302 loc) · 12.2 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
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
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
/******************************************************************************
* Copyright © 2014-2018 The SuperNET Developers. *
* *
* See the AUTHORS, DEVELOPER-AGREEMENT and LICENSE files at *
* the top-level directory of this distribution for the individual copyright *
* holder information and the developer policies on copyright and licensing. *
* *
* Unless otherwise agreed in a custom licensing agreement, no part of the *
* SuperNET software, including this file may be copied, modified, propagated *
* or distributed except according to the terms contained in the LICENSE file *
* *
* Removal or modification of this copyright notice is prohibited. *
* *
******************************************************************************/
#define EXCHANGE_NAME "btc38"
#define UPDATE prices777_ ## btc38
#define SUPPORTS btc38 ## _supports
#define SIGNPOST btc38 ## _signpost
#define TRADE btc38 ## _trade
#define ORDERSTATUS btc38 ## _orderstatus
#define CANCELORDER btc38 ## _cancelorder
#define OPENORDERS btc38 ## _openorders
#define TRADEHISTORY btc38 ## _tradehistory
#define BALANCES btc38 ## _balances
#define PARSEBALANCE btc38 ## _parsebalance
#define WITHDRAW btc38 ## _withdraw
#define EXCHANGE_AUTHURL "http://www.btc38.com/trade/t_api"
#define CHECKBALANCE btc38 ## _checkbalance
double UPDATE(struct prices777 *prices,int32_t maxdepth)
{
if ( prices->url[0] == 0 )
{
if ( strcmp(prices->lbase,"cny") == 0 && strcmp(prices->lrel,"btc") == 0 )
sprintf(prices->url,"http://api.btc38.com/v1/depth.php?c=%s&mk_type=%s","btc","cny");
else sprintf(prices->url,"http://api.btc38.com/v1/depth.php?c=%s&mk_type=%s",prices->lbase,prices->lrel);
}
return(prices777_standard("btc38",prices->url,prices,0,0,maxdepth,0));
}
int32_t SUPPORTS(char *_base,char *_rel)
{
char *cnypairs[] = { "BTC", "LTC", "DOGE", "XRP", "BTS", "STR", "NXT", "BLK", "BC", "VPN", "BILS", "BOST", "PPC", "APC", "ZCC", "XPM", "DGC", "MEC", "WDC", "QRK", "BEC", "ANC", "UNC", "RIC", "SRC", "TAG" };
char *btcpairs[] = { "TMC", "LTC", "DOGE", "XRP", "BTS", "XEM", "VPN", "XCN", "VOOT", "SYS", "NRS", "NAS", "SYNC", "MED", "EAC" };
int32_t i; char base[64],rel[64];
strcpy(base,_base), strcpy(rel,_rel);
touppercase(base), touppercase(rel);
if ( strlen(base) > 5 || strlen(rel) > 5 )
return(0);
if ( strcmp(base,"BTC") == 0 && strcmp(rel,"CNY") == 0 )
return(1);
else if ( strcmp(base,"CNY") == 0 && strcmp(rel,"BTC") == 0 )
return(-1);
else if ( strcmp(base,"BTC") == 0 )
{
for (i=0; i<sizeof(btcpairs)/sizeof(*btcpairs); i++)
if ( strcmp(btcpairs[i],rel) == 0 )
return(-1);
}
else if ( strcmp(rel,"BTC") == 0 )
{
for (i=0; i<sizeof(btcpairs)/sizeof(*btcpairs); i++)
if ( strcmp(btcpairs[i],base) == 0 )
return(1);
}
else if ( strcmp(base,"CNY") == 0 )
{
for (i=0; i<sizeof(cnypairs)/sizeof(*cnypairs); i++)
if ( strcmp(cnypairs[i],rel) == 0 )
return(-1);
}
else if ( strcmp(rel,"CNY") == 0 )
{
for (i=0; i<sizeof(cnypairs)/sizeof(*cnypairs); i++)
if ( strcmp(cnypairs[i],base) == 0 )
return(1);
}
printf("BTC38 doesnt support (%s/%s)\n",base,rel);
return(0);
}
cJSON *SIGNPOST(void **cHandlep,int32_t dotrade,char **retstrp,struct exchange_info *exchange,char *payload,char *path)
{
char cmdbuf[2048],url[1024],buf[1024],hdr1[512],hdr2[512],hdr3[512],hdr4[512],digest[33],*data;
cJSON *json; uint64_t nonce;
hdr1[0] = hdr2[0] = hdr3[0] = hdr4[0] = 0;
json = 0;
nonce = exchange_nonce(exchange);
sprintf(buf,"%s_%s_%s_%llu",exchange->apikey,exchange->userid,exchange->apisecret,(long long)nonce);
//printf("MD5.(%s)\n",buf);
calc_md5(digest,buf,(int32_t)strlen(buf));
sprintf(cmdbuf,"key=%s&time=%llu&md5=%s%s",exchange->apikey,(long long)nonce,digest,payload);
sprintf(url,"%s/%s",EXCHANGE_AUTHURL,path);
if ( dotrade == 0 )
data = exchange_would_submit(payload,hdr1,hdr2,hdr3,hdr4);
else if ( (data= curl_post(cHandlep,url,0,cmdbuf,hdr1,hdr2,hdr3,hdr4)) != 0 )
json = cJSON_Parse(data);
if ( retstrp != 0 )
*retstrp = data;
else if ( data != 0 )
free(data);
return(json);
}
/* $ Stamp = $ date-> getTimestamp ();
type, 1 for the purchase of Entry, 2 entry order to sell, can not be empty / the type of the order
$ Mdt = "_ public here to write here write here to write user ID_ private _" $ stamp.;
$ Mdt = md5 ($ mdt);
$ Data = array ("key" => "here to write public", "time" => $ stamp, "md5" => $ mdt, "type" => 1, "mk_type" => "cny",
"Price" => "0.0001", "amount" => "100", "coinname" => "XRP");
// $ Data_string = json_encode ($ data);
$ Ch = curl_init ();
curl_setopt ($ ch, CURLOPT_URL, 'http://www.btc38.com/trade/t_api/submitOrder.php');
curl_setopt ($ ch, CURLOPT_POST, 1);
curl_setopt ($ ch, CURLOPT_POSTFIELDS, $ data);
curl_setopt ($ ch, CURLOPT_RETURNTRANSFER, 1);
curl_setopt ($ ch, CURLOPT_HEADER, 0); */
/*
static CURL *cHandle;
char *data,*path,url[1024],cmdbuf[8192],buf[512],digest[33],market[16],base[64],rel[64],coinname[16],fmtstr[512],*pricefmt,*volfmt = "%.3f";
cJSON *json,*resultobj; uint64_t nonce,txid = 0;
if ( _base != 0 && _rel != 0 )
{
strcpy(base,_base), strcpy(rel,_rel);
touppercase(base), touppercase(rel);
if ( btc38_supports(base,rel) == 0 )
{
*retstrp = clonestr("{\"error\":\"invalid contract pair\"}");
return(0);
}
}
nonce = exchange_nonce(exchange);
sprintf(buf,"%s_%s_%s_%llu",exchange->apikey,exchange->userid,exchange->apisecret,(long long)nonce);
//printf("MD5.(%s)\n",buf);
calc_md5(digest,buf,(int32_t)strlen(buf));
*retstrp = 0;
if ( dir == 0 )
{
path = "getMyBalance.php";
sprintf(cmdbuf,"key=%s&time=%llu&md5=%s",exchange->apikey,(long long)nonce,digest);
}
else
{
if ( (data= curl_post(&cHandle,url,0,cmdbuf,0,0,0,0)) != 0 )
{
//printf("submit cmd.(%s) [%s]\n",cmdbuf,data);
if ( (json= cJSON_Parse(data)) != 0 )
{
if ( juint(json,"success") > 0 && (resultobj= cJSON_GetObjectItem(json,"return")) != 0 )
{
if ( (txid= get_API_nxt64bits(cJSON_GetObjectItem(resultobj,"order_id"))) == 0 )
{
if ( get_API_nxt64bits(cJSON_GetObjectItem(resultobj,"remains")) == 0 )
txid = _crc32(0,cmdbuf,strlen(cmdbuf));
}
}
free_json(json);
}
} else fprintf(stderr,"submit err cmd.(%s)\n",cmdbuf);
if ( retstrp != 0 && data != 0 )
{
if ( (json= cJSON_Parse(data)) == 0 )
{
json = cJSON_CreateObject();
jaddstr(json,"result",data);
data = jprint(json,1);
} else free_json(json);
//printf("btc38 returning.(%s) in %p\n",data,data);
*retstrp = data;
}
else if ( data != 0 )
free(data);
return(txid);
*/
cJSON *BALANCES(void **cHandlep,struct exchange_info *exchange)
{
return(SIGNPOST(cHandlep,1,0,exchange,"","getMyBalance.php"));
}
char *PARSEBALANCE(struct exchange_info *exchange,double *balancep,char *coinstr)
{
char field[128],*str,*itemstr = 0; cJSON *obj; double lockbalance,imma;
*balancep = 0.;
strcpy(field,coinstr);
tolowercase(field);
strcat(field,"_balance");
if ( exchange->balancejson != 0 && (str= jstr(exchange->balancejson,field)) != 0 )
{
*balancep = jdouble(exchange->balancejson,field);
strcpy(field,coinstr), tolowercase(field), strcat(field,"_balance_lock");
lockbalance = jdouble(exchange->balancejson,field);
strcpy(field,coinstr), tolowercase(field), strcat(field,"_balance_imma");
imma = jdouble(exchange->balancejson,field);
obj = cJSON_CreateObject();
jaddnum(obj,"balance",*balancep);
jaddnum(obj,"locked_balance",lockbalance);
jaddnum(obj,"imma_balance",imma);
itemstr = jprint(obj,1);
}
if ( itemstr == 0 )
return(clonestr("{\"error\":\"cant find coin balance\"}"));
return(itemstr);
}
#include "checkbalance.c"
uint64_t TRADE(void **cHandlep,int32_t dotrade,char **retstrp,struct exchange_info *exchange,char *base,char *rel,int32_t dir,double price,double volume)
{
char payload[1024],market[16],coinname[16],fmtstr[512],*pricefmt,*extra,*volfmt = "%.3f";
cJSON *json,*resultobj; uint64_t txid = 0;
if ( (extra= *retstrp) != 0 )
*retstrp = 0;
if ( (dir= cny_flip(market,coinname,base,rel,dir,&price,&volume)) == 0 )
{
fprintf(stderr,"btc38_trade illegal base.(%s) or rel.(%s)\n",base,rel);
return(0);
}
if ( strcmp(market,"cny") == 0 )
pricefmt = "%.5f";
else pricefmt = "%.6f";
//sprintf(fmtstr,"key=%%s&time=%%llu&md5=%%s&type=%%s&mk_type=%%s&coinname=%%s&price=%s&amount=%s",pricefmt,volfmt);
//sprintf(payload,fmtstr,exchange->apikey,(long long)nonce,digest,dir>0?"1":"2",market,coinname,price,volume);
sprintf(fmtstr,"&type=%%s&mk_type=%%s&coinname=%%s&price=%s&amount=%s",pricefmt,volfmt);
sprintf(payload,fmtstr,dir>0?"1":"2",market,coinname,price,volume);
if ( CHECKBALANCE(retstrp,dotrade,exchange,dir,base,rel,price,volume) == 0 && (json= SIGNPOST(cHandlep,dotrade,retstrp,exchange,payload,"submitOrder.php")) != 0 )
{
if ( juint(json,"success") > 0 && (resultobj= jobj(json,"return")) != 0 )
{
if ( (txid= j64bits(resultobj,"order_id")) == 0 )
{
if ( j64bits(resultobj,"remains") == 0 )
txid = _crc32(0,payload,strlen(payload));
}
}
free_json(json);
if ( retstrp != 0 && *retstrp != 0 )
{
if ( (json= cJSON_Parse(*retstrp)) == 0 )
{
json = cJSON_CreateObject();
jaddstr(json,"result",*retstrp);
free(*retstrp);
*retstrp = jprint(json,1);
} else free_json(json);
}
}
return(txid);
}
char *CANCELORDER(void **cHandlep,struct exchange_info *exchange,cJSON *argjson,uint64_t quoteid)
{
char payload[1024],*rel,*retstr = 0; cJSON *json;
if ( (rel= jstr(argjson,"rel")) == 0 )
rel = "cny";
sprintf(payload,"&mk_type=%s&order_id=%llu",rel,(long long)quoteid);
if ( (json= SIGNPOST(cHandlep,1,&retstr,exchange,payload,"cancelOrder.php")) != 0 )
{
free_json(json);
}
return(retstr); // return standardized cancelorder
}
char *OPENORDERS(void **cHandlep,struct exchange_info *exchange,cJSON *argjson)
{
char payload[1024],*base,*rel,*retstr = 0; cJSON *json;
if ( (rel= jstr(argjson,"rel")) == 0 )
rel = "cny";
sprintf(payload,"&mk_type=%s",rel);
if ( (base= jstr(argjson,"base")) != 0 )
sprintf(payload + strlen(payload),"&coinname=%s",base);
if ( (json= SIGNPOST(cHandlep,1,&retstr,exchange,payload,"getOrderList.php")) != 0 )
{
free_json(json);
}
return(retstr); // return standardized open orders
}
char *TRADEHISTORY(void **cHandlep,struct exchange_info *exchange,cJSON *argjson)
{
return(clonestr("{\"error\":\"btc38 doesnt seem to have trade history api!\"}"));
}
char *WITHDRAW(void **cHandlep,struct exchange_info *exchange,cJSON *argjson)
{
return(clonestr("{\"error\":\"btc38 doesnt seem to have withdraw api!\"}"));
}
char *ORDERSTATUS(void **cHandlep,struct exchange_info *exchange,cJSON *argjson,uint64_t quoteid)
{
char *status,*retstr;
status = OPENORDERS(cHandlep,exchange,argjson);
if ( (retstr= exchange_extractorderid(0,status,quoteid,"order_id")) != 0 )
{
free(status);
return(retstr);
}
free(status);
return(clonestr("{\"result\":\"order not pending\"}"));
}
struct exchange_funcs btc38_funcs = EXCHANGE_FUNCS(btc38,EXCHANGE_NAME);
#undef UPDATE
#undef SUPPORTS
#undef SIGNPOST
#undef TRADE
#undef ORDERSTATUS
#undef CANCELORDER
#undef OPENORDERS
#undef TRADEHISTORY
#undef BALANCES
#undef PARSEBALANCE
#undef WITHDRAW
#undef EXCHANGE_NAME
#undef EXCHANGE_AUTHURL
#undef CHECKBALANCE