int32_t bidask_parse(struct destbuf *exchangestr,struct destbuf *name,struct destbuf *base,struct destbuf *rel,struct destbuf *gui,struct InstantDEX_quote *iQ,cJSON *json) { uint64_t basemult,relmult,baseamount,relamount; double price,volume; int32_t exchangeid,keysize,flag; char key[1024],buf[64],*methodstr; memset(iQ,0,sizeof(*iQ)); iQ->s.baseid = j64bits(json,"baseid"); iQ->s.relid = j64bits(json,"relid"); iQ->s.baseamount = j64bits(json,"baseamount"), iQ->s.relamount = j64bits(json,"relamount"); iQ->s.vol = jdouble(json,"volume"); iQ->s.price = jdouble(json,"price"); copy_cJSON(exchangestr,jobj(json,"exchange")); if ( exchangestr->buf[0] == 0 || find_exchange(&exchangeid,exchangestr->buf) == 0 ) exchangeid = -1; iQ->exchangeid = exchangeid; copy_cJSON(base,jobj(json,"base")); copy_cJSON(rel,jobj(json,"rel")); copy_cJSON(name,jobj(json,"name")); methodstr = jstr(json,"method"); if ( methodstr != 0 && (strcmp(methodstr,"placeask") == 0 || strcmp(methodstr,"ask") == 0) ) iQ->s.isask = 1; if ( strcmp(exchangestr->buf,"wallet") == 0 && (iQ->s.baseid == NXT_ASSETID || strcmp(base->buf,"NXT") == 0) ) { flag = 1; if ( strcmp(methodstr,"placeask") == 0 ) methodstr = "placebid"; else if ( strcmp(methodstr,"placebid") == 0 ) methodstr = "placeask"; else if ( strcmp(methodstr,"ask") == 0 ) methodstr = "bid"; else if ( strcmp(methodstr,"bid") == 0 ) methodstr = "ask"; else flag = 0; if ( flag != 0 ) { iQ->s.baseid = iQ->s.relid, iQ->s.relid = NXT_ASSETID; strcpy(base->buf,rel->buf), strcpy(rel->buf,"NXT"); baseamount = iQ->s.baseamount; iQ->s.baseamount = iQ->s.relamount, iQ->s.relamount = baseamount; name->buf[0] = 0; if ( iQ->s.vol > SMALLVAL && iQ->s.price > SMALLVAL ) { iQ->s.vol *= iQ->s.price; iQ->s.price = 1. / iQ->s.price; } iQ->s.isask ^= 1; printf("INVERT\n"); } } if ( (iQ->s.timestamp= juint(json,"timestamp")) == 0 ) iQ->s.timestamp = (uint32_t)time(NULL); copy_cJSON(gui,jobj(json,"gui")), strncpy(iQ->gui,gui->buf,sizeof(iQ->gui)-1); iQ->s.automatch = juint(json,"automatch"); iQ->s.minperc = juint(json,"minperc"); if ( (iQ->s.duration= juint(json,"duration")) == 0 || iQ->s.duration > ORDERBOOK_EXPIRATION ) iQ->s.duration = ORDERBOOK_EXPIRATION; InstantDEX_name(key,&keysize,exchangestr->buf,name->buf,base->buf,&iQ->s.baseid,rel->buf,&iQ->s.relid); //printf(">>>>>>>>>>>> BASE.(%s) REL.(%s)\n",base->buf,rel->buf); iQ->s.basebits = stringbits(base->buf); iQ->s.relbits = stringbits(rel->buf); iQ->s.offerNXT = j64bits(json,"offerNXT"); iQ->s.quoteid = j64bits(json,"quoteid"); if ( strcmp(exchangestr->buf,"shuffle") == 0 ) { if ( iQ->s.price == 0. ) iQ->s.price = 1.; if ( iQ->s.vol == 0. ) iQ->s.vol = 1.; if ( iQ->s.baseamount == 0 ) iQ->s.baseamount = iQ->s.price * SATOSHIDEN; } else { if ( iQ->s.baseamount == 0 || iQ->s.relamount == 0 ) { if ( iQ->s.price <= SMALLVAL || iQ->s.vol <= SMALLVAL ) return(-1); set_best_amounts(&iQ->s.baseamount,&iQ->s.relamount,iQ->s.price,iQ->s.vol); } } if ( iQ->s.quoteid == 0 ) iQ->s.quoteid = calc_quoteid(iQ); else if ( iQ->s.quoteid != calc_quoteid(iQ) ) { printf("bidask_parse quoteid.%llu != calc.%llu\n",(long long)iQ->s.quoteid,(long long)calc_quoteid(iQ)); return(-1); } if ( iQ->s.price > SMALLVAL && iQ->s.vol > SMALLVAL && iQ->s.baseid != 0 && iQ->s.relid != 0 ) { buf[0] = 0, _set_assetname(&basemult,buf,0,iQ->s.baseid); //printf("baseid.%llu -> %s mult.%llu\n",(long long)iQ->baseid,buf,(long long)basemult); buf[0] = 0, _set_assetname(&relmult,buf,0,iQ->s.relid); //printf("relid.%llu -> %s mult.%llu\n",(long long)iQ->relid,buf,(long long)relmult); //basemult = get_assetmult(iQ->baseid), relmult = get_assetmult(iQ->relid); baseamount = (iQ->s.baseamount + basemult/2) / basemult, baseamount *= basemult; relamount = (iQ->s.relamount + relmult/2) / relmult, relamount *= relmult; if ( iQ->s.price != 0. && iQ->s.vol != 0 ) { price = prices777_price_volume(&volume,baseamount,relamount); if ( fabs(iQ->s.price - price)/price > 0.001 ) { printf("cant create accurate price ref.(%f %f) -> (%f %f)\n",iQ->s.price,iQ->s.vol,price,volume); return(-1); } } } return(0); }
struct coin777 *coin777_create(char *coinstr,cJSON *argjson) { char *serverport,*path=0,*conf=0; struct destbuf tmp; struct coin777 *coin = calloc(1,sizeof(*coin)); safecopy(coin->name,coinstr,sizeof(coin->name)); if ( argjson == 0 ) { coin->minconfirms = (strcmp("BTC",coinstr) == 0) ? 3 : 10; coin->estblocktime = (strcmp("BTC",coinstr) == 0) ? 600 : 120; } else { coin->minoutput = get_API_nxt64bits(cJSON_GetObjectItem(argjson,"minoutput")); coin->minconfirms = get_API_int(cJSON_GetObjectItem(argjson,"minconfirms"),(strcmp("BTC",coinstr) == 0) ? 3 : 10); coin->estblocktime = get_API_int(cJSON_GetObjectItem(argjson,"estblocktime"),(strcmp("BTC",coinstr) == 0) ? 600 : 120); coin->jsonstr = cJSON_Print(argjson); coin->argjson = cJSON_Duplicate(argjson,1); if ( (serverport= cJSON_str(cJSON_GetObjectItem(argjson,"rpc"))) != 0 ) safecopy(coin->serverport,serverport,sizeof(coin->serverport)); path = cJSON_str(cJSON_GetObjectItem(argjson,"path")); conf = cJSON_str(cJSON_GetObjectItem(argjson,"conf")); copy_cJSON(&tmp,cJSON_GetObjectItem(argjson,"assetid")), safecopy(coin->mgw.assetidstr,tmp.buf,sizeof(coin->mgw.assetidstr)); coin->mgw.assetidbits = calc_nxt64bits(coin->mgw.assetidstr); copy_cJSON(&tmp,cJSON_GetObjectItem(argjson,"issuer")), safecopy(coin->mgw.issuer,tmp.buf,sizeof(coin->mgw.issuer));; coin->mgw.issuerbits = conv_acctstr(coin->mgw.issuer); printf(">>>>>>>>>>>> a issuer.%s %llu assetid.%llu minoutput.%llu\n",coin->mgw.issuer,(long long)coin->mgw.issuerbits,(long long)coin->mgw.assetidbits,(long long)coin->minoutput); //uint32_t set_assetname(uint64_t *multp,char *name,uint64_t assetbits); _set_assetname(&coin->mgw.ap_mult,coin->mgw.assetname,0,coin->mgw.assetidbits); printf("assetname.(%s) mult.%llu\n",coin->mgw.assetname,coin->mgw.ap_mult); strcpy(coin->mgw.coinstr,coinstr); if ( (coin->mgw.special= cJSON_GetObjectItem(argjson,"special")) == 0 ) coin->mgw.special = cJSON_GetObjectItem(COINS.argjson,"special"); if ( coin->mgw.special != 0 ) coin->mgw.special = NXT_convjson(coin->mgw.special); printf("CONVERTED.(%s)\n",cJSON_Print(coin->mgw.special)); coin->mgw.limbo = cJSON_GetObjectItem(argjson,"limbo"); coin->mgw.dust = get_API_nxt64bits(cJSON_GetObjectItem(argjson,"dust")); coin->mgw.txfee = get_API_nxt64bits(cJSON_GetObjectItem(argjson,"txfee_satoshis")); if ( coin->mgw.txfee == 0 ) coin->mgw.txfee = (uint64_t)(SATOSHIDEN * get_API_float(cJSON_GetObjectItem(argjson,"txfee"))); if ( coin->mgw.txfee == 0 ) coin->mgw.txfee = 10000; coin->mgw.NXTfee_equiv = get_API_nxt64bits(cJSON_GetObjectItem(argjson,"NXTfee_equiv_satoshis")); if ( coin->mgw.NXTfee_equiv == 0 ) coin->mgw.NXTfee_equiv = (uint64_t)(SATOSHIDEN * get_API_float(cJSON_GetObjectItem(argjson,"NXTfee_equiv"))); copy_cJSON(&tmp,cJSON_GetObjectItem(argjson,"opreturnmarker")), safecopy(coin->mgw.opreturnmarker,tmp.buf,sizeof(coin->mgw.opreturnmarker)); printf("OPRETURN.(%s)\n",coin->mgw.opreturnmarker); copy_cJSON(&tmp,cJSON_GetObjectItem(argjson,"marker2")), safecopy(coin->mgw.marker2,tmp.buf,sizeof(coin->mgw.marker2)); coin->mgw.redeemheight = get_API_int(cJSON_GetObjectItem(argjson,"redeemheight"),430000); coin->mgw.use_addmultisig = get_API_int(cJSON_GetObjectItem(argjson,"useaddmultisig"),(strcmp("BTC",coinstr) != 0)); coin->mgw.do_opreturn = get_API_int(cJSON_GetObjectItem(argjson,"do_opreturn"),(strcmp("BTC",coinstr) == 0)); coin->mgw.oldtx_format = get_API_int(cJSON_GetObjectItem(argjson,"oldtx_format"),(strcmp("BTC",coinstr) == 0)); coin->mgw.firstunspentind = get_API_int(cJSON_GetObjectItem(argjson,"firstunspent"),(strcmp("BTCD",coinstr) == 0) ? 2500000 : 0); if ( (coin->mgw.NXTconvrate = get_API_float(cJSON_GetObjectItem(argjson,"NXTconvrate"))) == 0 ) { if ( coin->mgw.NXTfee_equiv != 0 && coin->mgw.txfee != 0 ) coin->mgw.NXTconvrate = ((double)coin->mgw.NXTfee_equiv / coin->mgw.txfee); } copy_cJSON(&tmp,cJSON_GetObjectItem(argjson,"marker")), safecopy(coin->mgw.marker,tmp.buf,sizeof(coin->mgw.marker)); printf("OPRETURN.(%s)\n",coin->mgw.opreturnmarker); } printf("coin777_create %s: (%s) %llu mult.%llu NXTconvrate %.8f minconfirms.%d issuer.(%s) %llu opreturn.%d oldformat.%d\n",coin->mgw.coinstr,coin->mgw.assetidstr,(long long)coin->mgw.assetidbits,(long long)coin->mgw.ap_mult,coin->mgw.NXTconvrate,coin->minconfirms,coin->mgw.issuer,(long long)coin->mgw.issuerbits,coin->mgw.do_opreturn,coin->mgw.oldtx_format); extract_userpass(coin->serverport,coin->userpass,coinstr,SUPERNET.userhome,path,conf); printf("COIN.%s serverport.(%s) userpass.(%s)\n",coin->name,coin->serverport,coin->userpass); COINS.LIST = realloc(COINS.LIST,(COINS.num+1) * sizeof(*coin)); COINS.LIST[COINS.num] = coin, COINS.num++; //ensure_packedptrs(coin); return(coin); }
struct coin777 *coin777_create(char *coinstr,cJSON *argjson) { char *serverport,*path=0,*conf=0; struct destbuf tmp; struct coin777 *coin = calloc(1,sizeof(*coin)); if ( coinstr == 0 || coinstr[0] == 0 ) { printf("null coinstr?\n"); //getchar(); return(0); } safecopy(coin->name,coinstr,sizeof(coin->name)); coin->jvin = -1; if ( argjson == 0 || strcmp(coinstr,"NXT") == 0 ) { coin->usep2sh = 1; coin->minconfirms = (strcmp("BTC",coinstr) == 0) ? 3 : 10; coin->estblocktime = (strcmp("BTC",coinstr) == 0) ? 600 : 120; coin->mgw.use_addmultisig = (strcmp("BTC",coinstr) != 0); } else { coin->minoutput = get_API_nxt64bits(cJSON_GetObjectItem(argjson,"minoutput")); coin->minconfirms = get_API_int(cJSON_GetObjectItem(argjson,"minconfirms"),(strcmp("BTC",coinstr) == 0) ? 3 : 10); coin->estblocktime = get_API_int(cJSON_GetObjectItem(argjson,"estblocktime"),(strcmp("BTC",coinstr) == 0) ? 600 : 120); coin->jsonstr = cJSON_Print(argjson); coin->argjson = cJSON_Duplicate(argjson,1); if ( (serverport= cJSON_str(cJSON_GetObjectItem(argjson,"rpc"))) != 0 ) safecopy(coin->serverport,serverport,sizeof(coin->serverport)); path = cJSON_str(cJSON_GetObjectItem(argjson,"path")); conf = cJSON_str(cJSON_GetObjectItem(argjson,"conf")); copy_cJSON(&tmp,cJSON_GetObjectItem(argjson,"assetid")), safecopy(coin->mgw.assetidstr,tmp.buf,sizeof(coin->mgw.assetidstr)); if ( (coin->mgw.assetidbits= calc_nxt64bits(coin->mgw.assetidstr)) == 0 ) coin->mgw.assetidbits = is_MGWcoin(coinstr); copy_cJSON(&tmp,cJSON_GetObjectItem(argjson,"issuer")), safecopy(coin->mgw.issuer,tmp.buf,sizeof(coin->mgw.issuer));; coin->mgw.issuerbits = conv_acctstr(coin->mgw.issuer); printf(">>>>>>>>>>>> a issuer.%s %llu assetid.%llu minoutput.%llu\n",coin->mgw.issuer,(long long)coin->mgw.issuerbits,(long long)coin->mgw.assetidbits,(long long)coin->minoutput); //uint32_t set_assetname(uint64_t *multp,char *name,uint64_t assetbits); if ( coin->mgw.assetidbits != 0 ) _set_assetname(&coin->mgw.ap_mult,coin->mgw.assetname,0,coin->mgw.assetidbits); printf("assetname.(%s) mult.%llu\n",coin->mgw.assetname,coin->mgw.ap_mult); strcpy(coin->mgw.coinstr,coinstr); if ( (coin->mgw.special= cJSON_GetObjectItem(argjson,"special")) == 0 ) coin->mgw.special = cJSON_GetObjectItem(COINS.argjson,"special"); if ( coin->mgw.special != 0 ) { coin->mgw.special = NXT_convjson(coin->mgw.special); printf("CONVERTED.(%s)\n",cJSON_Print(coin->mgw.special)); } coin->mgw.limbo = cJSON_GetObjectItem(argjson,"limbo"); coin->mgw.dust = get_API_nxt64bits(cJSON_GetObjectItem(argjson,"dust")); coin->mgw.txfee = get_API_nxt64bits(cJSON_GetObjectItem(argjson,"txfee_satoshis")); if ( coin->mgw.txfee == 0 ) coin->mgw.txfee = (uint64_t)(SATOSHIDEN * get_API_float(cJSON_GetObjectItem(argjson,"txfee"))); coin->mgw.NXTfee_equiv = get_API_nxt64bits(cJSON_GetObjectItem(argjson,"NXTfee_equiv_satoshis")); if ( coin->mgw.NXTfee_equiv == 0 ) coin->mgw.NXTfee_equiv = (uint64_t)(SATOSHIDEN * get_API_float(cJSON_GetObjectItem(argjson,"NXTfee_equiv"))); copy_cJSON(&tmp,cJSON_GetObjectItem(argjson,"opreturnmarker")), safecopy(coin->mgw.opreturnmarker,tmp.buf,sizeof(coin->mgw.opreturnmarker)); printf("OPRETURN.(%s)\n",coin->mgw.opreturnmarker); copy_cJSON(&tmp,cJSON_GetObjectItem(argjson,"marker2")), safecopy(coin->mgw.marker2,tmp.buf,sizeof(coin->mgw.marker2)); coin->mgw.redeemheight = get_API_int(cJSON_GetObjectItem(argjson,"redeemheight"),430000); coin->mgw.use_addmultisig = get_API_int(cJSON_GetObjectItem(argjson,"useaddmultisig"),(strcmp("BTC",coinstr) != 0)); coin->mgw.do_opreturn = get_API_int(cJSON_GetObjectItem(argjson,"do_opreturn"),(strcmp("BTC",coinstr) == 0)); coin->mgw.oldtx_format = get_API_int(cJSON_GetObjectItem(argjson,"oldtx_format"),(strcmp("BTC",coinstr) == 0)); coin->mgw.firstunspentind = get_API_int(cJSON_GetObjectItem(argjson,"firstunspent"),(strcmp("BTCD",coinstr) == 0) ? 2500000 : 0); if ( (coin->mgw.NXTconvrate = get_API_float(cJSON_GetObjectItem(argjson,"NXTconvrate"))) == 0 ) { if ( coin->mgw.NXTfee_equiv != 0 && coin->mgw.txfee != 0 ) coin->mgw.NXTconvrate = ((double)coin->mgw.NXTfee_equiv / coin->mgw.txfee); } copy_cJSON(&tmp,cJSON_GetObjectItem(argjson,"marker")), safecopy(coin->mgw.marker,tmp.buf,sizeof(coin->mgw.marker)); printf("OPRETURN.(%s)\n",coin->mgw.opreturnmarker); coin->addrtype = get_API_int(jobj(argjson,"addrtype"),0); coin->p2shtype = get_API_int(jobj(argjson,"p2shtype"),0); coin->usep2sh = get_API_int(jobj(argjson,"usep2sh"),1); } if ( coin->mgw.txfee == 0 ) coin->mgw.txfee = 10000; if ( strcmp(coin->name,"BTC") == 0 ) { coin->addrtype = 0, coin->p2shtype = 5; if ( coin->donationaddress[0] == 0 ) { strcpy(coin->donationaddress,"177MRHRjAxCZc7Sr5NViqHRivDu1sNwkHZ"); sprintf(coin->donationscript,"76a91443044b8d5dc8f3758dbc83374c596e96d25ead4f88ac"); } } else if ( strcmp(coin->name,"LTC") == 0 ) coin->addrtype = 48, coin->p2shtype = 5, coin->minconfirms = 1, coin->mgw.txfee = 100000, coin->usep2sh = 0; else if ( strcmp(coin->name,"BTCD") == 0 ) coin->addrtype = 60, coin->p2shtype = 85, coin->mgw.txfee = 1000000;//, strcpy(coin->donationaddress,"RDRWMSrDdoUcfZRBWUz7KZQSxPS9bZRerM"); else if ( strcmp(coin->name,"DOGE") == 0 ) coin->addrtype = 30, coin->p2shtype = 35; else if ( strcmp(coin->name,"VRC") == 0 ) coin->addrtype = 70, coin->p2shtype = 85; else if ( strcmp(coin->name,"OPAL") == 0 ) coin->addrtype = 115, coin->p2shtype = 28; else if ( strcmp(coin->name,"BITS") == 0 ) coin->addrtype = 25, coin->p2shtype = 8; printf("coin777_create %s: (%s) %llu mult.%llu NXTconvrate %.8f minconfirms.%d issuer.(%s) %llu opreturn.%d oldformat.%d\n",coin->mgw.coinstr,coin->mgw.assetidstr,(long long)coin->mgw.assetidbits,(long long)coin->mgw.ap_mult,coin->mgw.NXTconvrate,coin->minconfirms,coin->mgw.issuer,(long long)coin->mgw.issuerbits,coin->mgw.do_opreturn,coin->mgw.oldtx_format); if ( strcmp(coin->name,"NXT") != 0 ) { extract_userpass(coin->serverport,coin->userpass,coinstr,SUPERNET.userhome,path,conf); set_atomickeys(coin); printf("COIN.%s serverport.(%s) userpass.(%s) %s/%s %s/%s\n",coin->name,coin->serverport,coin->userpass,coin->atomicrecv,coin->atomicrecvpubkey,coin->atomicsend,coin->atomicsendpubkey); } COINS.LIST = realloc(COINS.LIST,(COINS.num+1) * sizeof(*coin)); COINS.LIST[COINS.num] = coin, COINS.num++; //ensure_packedptrs(coin); return(coin); }