Esempio n. 1
0
uint64_t TRADE(char **retstrp,struct exchange_info *exchange,char *base,char *rel,int32_t dir,double price,double volume)
{
    char payload[1024],pairstr[512],*typestr,*method,*extra; cJSON *json; uint64_t txid = 0;
    if ( (extra= *retstrp) != 0 )
        *retstrp = 0;
    if ( (dir= flipstr_for_exchange(exchange,pairstr,"%s%s",dir,&price,&volume,base,rel)) == 0 )
    {
        printf("cant find baserel (%s/%s)\n",base,rel);
        return(0);
    }
    method = "order/new";
    //Either "market" / "limit" / "stop" / "trailing-stop" / "fill-or-kill" / "exchange market" / "exchange limit" / "exchange stop" / "exchange trailing-stop" / "exchange fill-or-kill". (type starting by "exchange " are exchange orders, others are margin trading orders)
    if ( (typestr= extra) == 0 )
        typestr = "exchange limit";
    sprintf(payload,"{\"request\":\"/v1/%s\",\"nonce\":\"%llu\",\"exchange\":\"bitfinex\",\"side\":\"%s\",\"type\":\"%s\",\"price\":\"%.8f\",\"amount\":\"%.8f\",\"symbol\":\"%s\"}",method,(long long)exchange_nonce(exchange),dir>0?"buy":"sell",typestr,price,volume,pairstr);
    if ( (json= SIGNPOST(retstrp,exchange,payload,method)) != 0 )
    {
        if ( (txid= j64bits(json,"order_id")) == 0 )
        {
            if ( dir != 0 )
                printf("bitfinex: no txid error\n");
        }
        free_json(json);
    }
    return(txid);
}
Esempio n. 2
0
char *WITHDRAW(struct exchange_info *exchange,cJSON *argjson)
{
    char payload[1024],*method,*base,*destaddr,*type,*retstr = 0; cJSON *json; double amount;
    amount = jdouble(argjson,"amount");
    if ( (destaddr= jstr(argjson,"destaddr")) == 0 || amount < SMALLVAL )
        return(clonestr("{\"error\":\"no destaddr specified or amount too small\"}"));
    if ( (base= jstr(argjson,"base")) == 0 )
        base = "bitcoin";
    else if ( strcmp(base,"BTC") == 0 )
        base = "bitcoin";
    else if ( strcmp(base,"LTC") == 0 )
        base = "litecoin";
    else if ( strcmp(base,"DRK") == 0 )
        base = "darkcoin";
    else return(clonestr("{\"error\":\"invalid base specified\"}"));
    if ( (type= jstr(argjson,"extra")) == 0 )
        type = "exchange";
    else if ( strcmp(type,"exchange") != 0 && strcmp(type,"trading") != 0 && strcmp(type,"deposit") != 0 )
        return(clonestr("{\"error\":\"invalid wallet type specified\"}"));
    method = "withdraw";
    sprintf(payload,"{\"request\":\"/v1/%s\",\"nonce\":\"%llu\",\"amount\":\"%.6f\",\"withdraw_type\":\"%s\",\"walletselected\":\"%s\",\"address\":\"%s\"}",method,(long long)exchange_nonce(exchange),amount,base,type,destaddr);
    if ( (json= SIGNPOST(&retstr,exchange,payload,method)) != 0 )
    {
        free_json(json);
    }
    return(retstr); // return standardized withdraw
}
Esempio n. 3
0
cJSON *BALANCES(struct exchange_info *exchange)
{
    char payload[1024],*method;
    method = "balances";
    sprintf(payload,"{\"request\":\"/v1/%s\",\"nonce\":\"%llu\"}",method,(long long)exchange_nonce(exchange));
    return(SIGNPOST(0,exchange,payload,method));
}
Esempio n. 4
0
uint64_t TRADE(char **retstrp,struct exchange_info *exchange,char *base,char *rel,int32_t dir,double price,double volume)
{
    char payload[1024],buf[1024],url[1024],digest[512],pairstr[512],pricestr[64],*extra,*typestr;
    cJSON *json; uint64_t txid = 0;
    if ( (extra= *retstrp) != 0 )
        *retstrp = 0;
    if ( (dir= flipstr_for_exchange(exchange,pairstr,"%s_%s",dir,&price,&volume,base,rel)) == 0 )
    {
        printf("cant find baserel (%s/%s)\n",base,rel);
        return(0);
    }
    if ( extra != 0 && strcmp(extra,"market") == 0 )
        typestr = (dir > 0) ? "buy_market" : "sell_market", sprintf(pricestr,"&price=%.2f",price); // docs say market orders put volume in price
    else typestr = (dir > 0) ? "buy" : "sell";
    sprintf(pricestr,"&price=%.2f",price);
    sprintf(buf,"amount=%.4f&api_key=%s%ssymbol=%s&type=%s&secret_key=%s",volume,exchange->apikey,pricestr,pairstr,typestr,exchange->apisecret);
    calc_md5(digest,buf,(int32_t)strlen(buf));
    touppercase(digest);
    sprintf(payload,"amount=%.4f&api_key=%s%s&symbol=%s&type=%s&sign=%s",volume,exchange->apikey,pricestr,pairstr,typestr,digest);
    sprintf(url,"%s/%s",EXCHANGE_AUTHURL,"trade.do");
    if ( (json= SIGNPOST(retstrp,exchange,url,payload)) != 0 )
    {
        txid = j64bits(json,"order_id");
        free_json(json);
    }
    return(txid);
}
Esempio n. 5
0
uint64_t TRADE(char **retstrp,struct exchange_info *exchange,char *base,char *rel,int32_t dir,double price,double volume)
{
    char payload[1024],pairstr[64],*extra,*path; cJSON *json; uint64_t txid = 0;
    if ( (extra= *retstrp) != 0 )
        *retstrp = 0;
    if ( (dir= flipstr_for_exchange(exchange,pairstr,"%s_%s",dir,&price,&volume,base,rel)) == 0 )
    {
        printf("cant find baserel (%s/%s)\n",base,rel);
        return(0);
    }
    path = (dir > 0) ? "buy" : "sell";
    //key - API key
    //signature - signature
    //nonce - nonce
    //amount - amount of major currency
    //price - price to buy at
    //book - optional, if not specified, will default to btc_cad
    sprintf(payload,"\"amount\":%.6f,\"price\":%.3f,\"book\":\"%s_%s\",",volume,price,base,rel);
    if ( (json= SIGNPOST(retstrp,exchange,payload,path)) != 0 )
    {
        // parse json and set txid
        free_json(json);
    }
    return(txid);
}
Esempio n. 6
0
char *CANCELORDER(void **cHandlep,struct exchange_info *exchange,cJSON *argjson,uint64_t quoteid)
{
    char payload[1024],*retstr = 0; cJSON *json;
    sprintf(payload,"command=cancelOrder&nonce=%llu&orderNumber=%llu",(long long)exchange_nonce(exchange),(long long)quoteid);
    if ( (json= SIGNPOST(cHandlep,1,&retstr,exchange,EXCHANGE_AUTHURL,payload)) != 0 )
    {
        free_json(json);
    }
    return(retstr); // return standardized cancelorder
}
Esempio n. 7
0
char *CANCELORDER(struct exchange_info *exchange,cJSON *argjson,uint64_t quoteid)
{
    char payload[1024],*method,*retstr = 0; cJSON *json;
    method = "order/cancel";
    sprintf(payload,"{\"request\":\"/v1/%s\",\"nonce\":\"%llu\",\"order_id\":%llu}",method,(long long)exchange_nonce(exchange),(long long)quoteid);
    if ( (json= SIGNPOST(&retstr,exchange,payload,method)) != 0 )
    {
        free_json(json);
    }
    return(retstr); // return standardized cancelorder
}
Esempio n. 8
0
char *OPENORDERS(struct exchange_info *exchange,cJSON *argjson)
{
    char payload[1024],*method,*retstr = 0; cJSON *json;
    method = "orders";
    sprintf(payload,"{\"request\":\"/v1/%s\",\"nonce\":\"%llu\"}",method,(long long)exchange_nonce(exchange));
    if ( (json= SIGNPOST(&retstr,exchange,payload,method)) != 0 )
    {
        free_json(json);
    }
    return(retstr); // return standardized open orders
}
Esempio n. 9
0
char *OPENORDERS(void **cHandlep,struct exchange_info *exchange,cJSON *argjson)
{
    char payload[1024],pair[64],*retstr = 0; cJSON *json;
    poloniex_setpair(pair,argjson);
    sprintf(payload,"command=returnOpenOrders&nonce=%llu&currencyPair=%s",(long long)exchange_nonce(exchange),pair);
    if ( (json= SIGNPOST(cHandlep,1,&retstr,exchange,EXCHANGE_AUTHURL,payload)) != 0 )
    {
        free_json(json);
    }
    return(retstr); // return standardized open orders
}
Esempio n. 10
0
cJSON *huobi_issue_auth(struct exchange_info *exchange,char *method,char *buf)
{
    char payload[1024],digest[33],tmp[1024]; uint64_t nonce;
    nonce = exchange_nonce(exchange);
    sprintf(tmp,"access_key=%s&created=%llu&method=%s%s",exchange->apikey,(long long)nonce,method,buf);
    sprintf(payload,"%s&secret_key=%s",tmp,exchange->apisecret);
    //printf("tmp.(%s) payload.(%s)\n",tmp,payload);
    calc_md5(digest,payload,(int32_t)strlen(payload));
    sprintf(payload,"%s&sign=%s",tmp,digest);
    //printf("-> (%s)\n",payload);
    return(SIGNPOST(0,exchange,payload));
}
Esempio n. 11
0
char *WITHDRAW(void **cHandlep,struct exchange_info *exchange,cJSON *argjson)
{
    char buf[1024],*base,*destaddr; double amount;
    if ( (base= jstr(argjson,"base")) == 0 || strcmp(base,"BTC") != 0 )
        return(clonestr("{\"error\":\"base not specified or base != BTC\"}"));
    if ( (destaddr= jstr(argjson,"destaddr")) == 0 )
        return(clonestr("{\"error\":\"destaddr not specified\"}"));
    if ( (amount= jdouble(argjson,"amount")) < SMALLVAL )
        return(clonestr("{\"error\":\"amount not specified\"}"));
    sprintf(buf,"\"amount\":%.4f,\"address\":\"%s\",",amount,destaddr);
    printf("submit.(%s)\n",buf);
    return(jprint(SIGNPOST(cHandlep,1,0,exchange,"","bitcoin_withdrawal"),1));
}
Esempio n. 12
0
cJSON *okcoin_issue_auth(void **cHandlep,struct exchange_info *exchange,char *method,char *buf)
{
    char payload[1024],tmp[1024],digest[512],url[512];
    sprintf(tmp,"api_key=%s%s",exchange->apikey,buf);
    
    sprintf(payload,"%s&secret_key=%s",tmp,exchange->apisecret);
    //printf("tmp.(%s) payload.(%s)\n",tmp,payload);
    calc_md5(digest,payload,(int32_t)strlen(payload));
    touppercase(digest);
    sprintf(payload,"%s&sign=%s",tmp,digest);
    sprintf(url,"%s/%s",EXCHANGE_AUTHURL,method);
    return(SIGNPOST(cHandlep,1,0,exchange,url,payload));
}
Esempio n. 13
0
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
}
Esempio n. 14
0
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
}
Esempio n. 15
0
char *TRADEHISTORY(void **cHandlep,struct exchange_info *exchange,cJSON *argjson)
{
    char payload[1024],pair[64],*retstr = 0; cJSON *json; uint32_t timestamp,endstamp;
    poloniex_setpair(pair,argjson);
    timestamp = juint(argjson,"start");
    endstamp = juint(argjson,"end");
    sprintf(payload,"command=returnTradeHistory&nonce=%llu&currencyPair=%s",(long long)exchange_nonce(exchange),pair);
    if ( timestamp != 0 )
        sprintf(payload + strlen(payload),"&start=%u",timestamp);
    if ( endstamp != 0 )
        sprintf(payload + strlen(payload),"&end=%u",endstamp);
    if ( (json= SIGNPOST(cHandlep,1,&retstr,exchange,EXCHANGE_AUTHURL,payload)) != 0 )
    {
        free_json(json);
    }
    return(retstr); // return standardized tradehistory
}
Esempio n. 16
0
char *WITHDRAW(void **cHandlep,struct exchange_info *exchange,cJSON *argjson)
{
    char payload[1024],*destaddr,*paymentid,*base,*retstr = 0; cJSON *json; double amount;
    if ( (base= jstr(argjson,"base")) == 0 )
        return(clonestr("{\"error\":\"base not specified\"}"));
    if ( (destaddr= jstr(argjson,"destaddr")) == 0 )
        return(clonestr("{\"error\":\"destaddr not specified\"}"));
    if ( (amount= jdouble(argjson,"amount")) < SMALLVAL )
        return(clonestr("{\"error\":\"amount not specified\"}"));
    paymentid = jstr(argjson,"paymentid");
    sprintf(payload,"command=withdraw&nonce=%llu&currency=%s&amount=%.6f&address=%s",(long long)exchange_nonce(exchange),base,amount,destaddr);
    if ( paymentid != 0 )
        sprintf(payload + strlen(payload),"&paymentId=%s",paymentid);
    if ( (json= SIGNPOST(cHandlep,1,&retstr,exchange,EXCHANGE_AUTHURL,payload)) != 0 )
    {
        free_json(json);
    }
    return(retstr); // return standardized withdraw
}
Esempio n. 17
0
char *TRADEHISTORY(struct exchange_info *exchange,cJSON *argjson)
{
    char payload[1024],baserel[16],*method,*base,*rel,*retstr = 0; uint32_t timestamp; cJSON *json;
    method = "mytrades";
    base = jstr(argjson,"base");
    rel = jstr(argjson,"rel");
    if ( base == 0 || rel == 0 )
    {
        base = "BTC";
        rel = "USD";
    }
    sprintf(baserel,"%s%s",base,rel);
    timestamp = juint(argjson,"start");
    sprintf(payload,"{\"request\":\"/v1/%s\",\"nonce\":\"%llu\",\"symbol\":\"%s\",\"timestamp\":%u}",method,(long long)exchange_nonce(exchange),baserel,timestamp);
    //printf("TRADEHISTORY.(%s)\n",payload);
    if ( (json= SIGNPOST(&retstr,exchange,payload,method)) != 0 )
    {
        free_json(json);
    }
    return(retstr); // return standardized tradehistory
}
Esempio n. 18
0
char *OPENORDERS(void **cHandlep,struct exchange_info *exchange,cJSON *argjson)
{
    return(jprint(SIGNPOST(cHandlep,1,0,exchange,"open_orders",""),1));
}
Esempio n. 19
0
char *CANCELORDER(void **cHandlep,struct exchange_info *exchange,cJSON *argjson,uint64_t quoteid)
{
    char jsonbuf[128];
    sprintf(jsonbuf,"&id=%llu",(long long)quoteid);
    return(jprint(SIGNPOST(cHandlep,1,0,exchange,"cancel_order",jsonbuf),1));
}
Esempio n. 20
0
char *ORDERSTATUS(void **cHandlep,struct exchange_info *exchange,cJSON *argjson,uint64_t quoteid)
{
    char jsonbuf[128];
    sprintf(jsonbuf,"&id=%llu",(long long)quoteid);
    return(jprint(SIGNPOST(cHandlep,1,0,exchange,"order_status",jsonbuf),1));
}
Esempio n. 21
0
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],url[512],pairstr[512],*extra; cJSON *json; uint64_t txid = 0;
    if ( (extra= *retstrp) != 0 )
        *retstrp = 0;
    if ( (dir= flipstr_for_exchange(exchange,pairstr,"%s%s",dir,&price,&volume,base,rel)) == 0 )
    {
        printf("cant find baserel (%s/%s)\n",base,rel);
        return(0);
    }
    sprintf(url,"%s/%s/",EXCHANGE_AUTHURL,dir>0 ? "buy" : "sell");
    if ( CHECKBALANCE(retstrp,dotrade,exchange,dir,base,rel,price,volume) == 0 && (json= SIGNPOST(cHandlep,dotrade,retstrp,exchange,url,payload)) != 0 )
    {
        // parse json and set txid
        free_json(json);
    }
    return(txid);
}
Esempio n. 22
0
char *TRADEHISTORY(struct exchange_info *exchange,cJSON *argjson)
{
    return(jprint(SIGNPOST(0,exchange,"","user_transactions"),1));
}
Esempio n. 23
0
char *CANCELORDER(void **cHandlep,struct exchange_info *exchange,cJSON *argjson,uint64_t quoteid)
{
    char buf[64];
    sprintf(buf,"\"id\":%llu,",(long long)quoteid);
    return(jprint(SIGNPOST(cHandlep,1,0,exchange,buf,"cancel_order"),1));
}
Esempio n. 24
0
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],pairstr[64],*extra,*typestr; cJSON *json; uint64_t nonce,txid = 0;
    nonce = exchange_nonce(exchange);
    if ( (extra= *retstrp) != 0 )
        *retstrp = 0;
    dir = flip_for_exchange(pairstr,"%s_%s","BTC",dir,&price,&volume,base,rel);
    if ( extra != 0 && strcmp(extra,"margin") == 0 )
        typestr = (dir > 0) ? "marginBuy":"marginSell";
    else typestr = (dir > 0) ? "buy":"sell";
    sprintf(payload,"command=%s&nonce=%lld&currencyPair=%s&rate=%.8f&amount=%.8f",typestr,(long long)nonce,pairstr,price,volume);
    if ( CHECKBALANCE(retstrp,dotrade,exchange,dir,base,rel,price,volume) == 0 && (json= SIGNPOST(cHandlep,dotrade,retstrp,exchange,EXCHANGE_AUTHURL,payload)) != 0 )
    {
        txid = (get_API_nxt64bits(cJSON_GetObjectItem(json,"orderNumber")) << 32) | get_API_nxt64bits(cJSON_GetObjectItem(json,"tradeID"));
        free_json(json);
    }
    return(txid);
}
Esempio n. 25
0
cJSON *BALANCES(void **cHandlep,struct exchange_info *exchange)
{
    char payload[1024];
    sprintf(payload,"command=returnCompleteBalances&nonce=%llu",(long long)exchange_nonce(exchange));
    return(SIGNPOST(cHandlep,1,0,exchange,EXCHANGE_AUTHURL,payload));
}
Esempio n. 26
0
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);
}
Esempio n. 27
0
char *OPENORDERS(struct exchange_info *exchange,cJSON *argjson)
{
    return(jprint(SIGNPOST(0,exchange,"","open_orders"),1));
}
Esempio n. 28
0
char *TRADEHISTORY(void **cHandlep,struct exchange_info *exchange,cJSON *argjson)
{
    return(jprint(SIGNPOST(cHandlep,1,0,exchange,"user_transactions",""),1));
}
Esempio n. 29
0
cJSON *BALANCES(void **cHandlep,struct exchange_info *exchange)
{
    return(SIGNPOST(cHandlep,1,0,exchange,"balance",""));
}
Esempio n. 30
0
char *ORDERSTATUS(struct exchange_info *exchange,cJSON *argjson,uint64_t quoteid)
{
    char buf[64];
    sprintf(buf,"\"id\":%llu,",(long long)quoteid);
    return(jprint(SIGNPOST(0,exchange,buf,"lookup_order"),1));
}