diff --git a/deprecated/obsolete.h b/deprecated/obsolete.h index 91f0fc8b3..e9ecf4aa2 100644 --- a/deprecated/obsolete.h +++ b/deprecated/obsolete.h @@ -13192,5 +13192,131 @@ len = 0; jadd(json,"vin",vins); return(json); }*/ + + /* + if ( strcmp(cmdstr+3,"offer") == 0 ) + { + + } + if ( (price= instantdex_acceptable(myinfo,0,refstr,base,rel,volume)) > 0. ) + { + // sends NXT assetid, volume and desired + if ( strcmp(base,"NXT") == 0 || strcmp(base,"nxt") == 0 ) + assetbits = NXT_ASSETID; + else if ( is_decimalstr(base) > 0 ) + assetbits = calc_nxt64bits(base); + if ( assetbits != 0 ) + { + nextcmd = INSTANTDEX_REQUEST; + nextcmdstr = "request"; + } + } + } + else if ( strncmp(cmdstr,"ALT",3) == 0 ) + { + if ( (price= instantdex_acceptable(myinfo,0,refstr,base,rel,volume)) > 0. ) + { + // sends NXT assetid, volume and desired + if ( strcmp(base,"NXT") == 0 || strcmp(base,"nxt") == 0 ) + assetbits = NXT_ASSETID; + else if ( is_decimalstr(base) > 0 ) + assetbits = calc_nxt64bits(base); + if ( assetbits != 0 ) + { + nextcmd = INSTANTDEX_REQUEST; + nextcmdstr = "request"; + } + } + } + else if ( strncmp(cmdstr,"NXT",3) == 0 ) + { + if ( (price= instantdex_acceptable(myinfo,0,refstr,base,rel,volume)) > 0. ) + { + // sends NXT assetid, volume and desired + if ( strcmp(base,"NXT") == 0 || strcmp(base,"nxt") == 0 ) + assetbits = NXT_ASSETID; + else if ( is_decimalstr(base) > 0 ) + assetbits = calc_nxt64bits(base); + if ( assetbits != 0 ) + { + nextcmd = INSTANTDEX_REQUEST; + nextcmdstr = "request"; + } + } + } + { + + } + + if ( strcmp(cmdstr,"request") == 0 ) + { + // request: + // other node sends (othercoin, othercoinaddr, otherNXT and reftx that expires before phasedtx) + if ( (strcmp(rel,"BTC") == 0 || strcmp(base,"BTC") == 0) && (price= instantdex_acceptable(myinfo,0,refstr,base,rel,volume)) > 0. ) + { + //aveprice = instantdex_aveprice(myinfo,sortbuf,(int32_t)(sizeof(sortbuf)/sizeof(*sortbuf)),&totalvol,base,rel,volume,argjson); + set_NXTtx(myinfo,&feeT,assetbits,SATOSHIDEN*3,calc_nxt64bits(INSTANTDEX_ACCT),-1); + if ( (feejson= gen_NXT_tx_json(myinfo,fullhash,&feeT,0,1.)) != 0 ) + free_json(feejson); + nextcmd = INSTANTDEX_PROPOSE; + nextcmdstr = "proposal"; + othercoinaddr = myinfo->myaddr.BTC; + otherNXTaddr = myinfo->myaddr.NXTADDR; + } + } + else + { + if ( strcmp(cmdstr,"proposal") == 0 ) + { + // proposal: + // NXT node submits phasedtx that refers to it, but it wont confirm + nextcmd = INSTANTDEX_ACCEPT; + nextcmdstr = "accept"; + message = ""; + //instantdex_phasetxsubmit(refstr); + } + else if ( strcmp(cmdstr,"accept") == 0 ) + { + // accept: + // other node verifies unconfirmed has phasedtx and broadcasts cltv, also to NXT node, releases trigger + nextcmd = INSTANTDEX_CONFIRM; + nextcmdstr = "confirm"; + message = ""; + //instantdex_phasedtxverify(); + //instantdex_cltvbroadcast(); + //instantdex_releasetrigger(); + } + else if ( strcmp(cmdstr,"confirm") == 0 ) + { + // confirm: + // NXT node verifies bitcoin txbytes has proper payment and cashes in with onetimepubkey + // BTC* node approves phased tx with onetimepubkey + //instantdex_cltvverify(); + //instantdex_phasetxapprove(); + return(clonestr("{\"error\":\"trade confirmed\"}")); + } + } + if ( nextcmd != 0 && (newjson= InstantDEX_argjson(refstr,message,othercoinaddr,otherNXTaddr,nextcmd,duration,flags)) != 0 ) + { + jaddnum(newjson,"price",price); + jaddnum(newjson,"volume",volume); + return(instantdex_sendcmd(myinfo,newjson,nextcmdstr,myinfo->ipaddr,INSTANTDEX_HOPS)); + } + } + return(clonestr("{\"error\":\"request needs argjson\"}")); + } + num = 0; + depth = 30; + request = jstr(argjson,"request"); + base = jstr(argjson,"base"); + rel = jstr(argjson,"rel"); + refstr = jstr(argjson,"refstr"); + volume = jdouble(argjson,"volume"); + duration = juint(argjson,"duration"); + flags = juint(argjson,"flags"); + nextcmd = 0; + nextcmdstr = message = ""; + + */ #endif diff --git a/iguana/exchanges/bitcoin.c b/iguana/exchanges/bitcoin.c index 9b2e79044..ebbe92fb2 100755 --- a/iguana/exchanges/bitcoin.c +++ b/iguana/exchanges/bitcoin.c @@ -1392,7 +1392,8 @@ int32_t is_valid_BTCother(char *other) uint64_t TRADE(int32_t dotrade,char **retstrp,struct exchange_info *exchange,char *base,char *rel,int32_t dir,double price,double volume,cJSON *argjson) { - struct instantdex_accept *ap; char *str; uint64_t txid = 0; cJSON *json; struct supernet_info *myinfo; int32_t hops = 3; + struct instantdex_accept *ap; char *str,coinaddr[64]; uint64_t txid = 0; cJSON *json; + struct supernet_info *myinfo; uint8_t pubkey[33]; struct iguana_info *other; int32_t hops = 3; myinfo = SuperNET_MYINFO(0); if ( strcmp(base,"BTC") == 0 || strcmp(base,"btc") == 0 ) { @@ -1418,7 +1419,7 @@ uint64_t TRADE(int32_t dotrade,char **retstrp,struct exchange_info *exchange,cha { if ( ap != 0 ) { - + // issue matching response } else if ( dir < 0 ) { @@ -1432,6 +1433,13 @@ uint64_t TRADE(int32_t dotrade,char **retstrp,struct exchange_info *exchange,cha jaddstr(json,"rel","BTC"); jaddnum(json,"maxprice",price); jaddnum(json,"volume",volume); + if ( (other= iguana_coinfind(base)) != 0 ) + { + bitcoin_pubkey(pubkey,myinfo->persistent_priv); + bitcoin_address(coinaddr,other->chain->pubtype,pubkey,sizeof(pubkey)); + jaddstr(argjson,base,coinaddr); + } + jaddstr(json,"BTC",myinfo->myaddr.BTC); if ( (str= instantdex_sendcmd(myinfo,json,"BTCoffer",myinfo->ipaddr,hops)) != 0 ) free(str); free_json(json); diff --git a/iguana/exchanges777.h b/iguana/exchanges777.h index 5881213f3..89a32661c 100755 --- a/iguana/exchanges777.h +++ b/iguana/exchanges777.h @@ -94,7 +94,7 @@ struct instantdex_accept { struct queueitem DL; uint64_t orderid; uint32_t dead; struct instantdex_accept *instantdex_acceptablefind(struct exchange_info *exchange,cJSON *bids,cJSON *asks,uint64_t orderid,char *base,char *rel); cJSON *instantdex_acceptjson(struct instantdex_accept *ap); -struct instantdex_accept *instantdex_acceptable(struct exchange_info *exchange,char *refstr,char *base,char *rel,char *offerside,int32_t offerdir,double offerprice,double volume); +struct instantdex_accept *instantdex_acceptable(struct exchange_info *exchange,char *base,char *rel,char *offerside,int32_t offerdir,double offerprice,double volume); void *curl_post(void **cHandlep,char *url,char *userpass,char *postfields,char *hdr0,char *hdr1,char *hdr2,char *hdr3); char *instantdex_sendcmd(struct supernet_info *myinfo,cJSON *argjson,char *cmdstr,char *ipaddr,int32_t hops); @@ -113,5 +113,7 @@ double fxcm_price(struct exchange_info *exchange,char *base,char *rel,struct exc double instaforex_price(struct exchange_info *exchange,char *base,char *rel,struct exchange_quote *bidasks,int32_t maxdepth,double commission,cJSON *argjson,int32_t invert); char *instantdex_queueaccept(struct exchange_info *exchange,char *base,char *rel,double price,double basevolume,int32_t acceptdir,char *myside,int32_t duration); +bits256 bitcoin_pubkey(uint8_t *data,bits256 privkey); +char *bitcoin_address(char *coinaddr,uint8_t addrtype,uint8_t *pubkey,int32_t len); #endif diff --git a/iguana/iguana_instantdex.c b/iguana/iguana_instantdex.c index a338c4f93..e83e63e37 100755 --- a/iguana/iguana_instantdex.c +++ b/iguana/iguana_instantdex.c @@ -272,7 +272,7 @@ struct instantdex_accept *instantdex_acceptablefind(struct exchange_info *exchan return(retap); } -struct instantdex_accept *instantdex_acceptable(struct exchange_info *exchange,char *refstr,char *base,char *rel,char *offerside,int32_t offerdir,double offerprice,double volume) +struct instantdex_accept *instantdex_acceptable(struct exchange_info *exchange,char *base,char *rel,char *offerside,int32_t offerdir,double offerprice,double volume) { struct instantdex_accept PAD,*ap,*retap = 0; double bestprice = 0.; uint32_t now; now = (uint32_t)time(NULL); @@ -299,131 +299,6 @@ struct instantdex_accept *instantdex_acceptable(struct exchange_info *exchange,c return(retap); } -/* - if ( strcmp(cmdstr+3,"offer") == 0 ) -{ - -} -if ( (price= instantdex_acceptable(myinfo,0,refstr,base,rel,volume)) > 0. ) -{ - // sends NXT assetid, volume and desired - if ( strcmp(base,"NXT") == 0 || strcmp(base,"nxt") == 0 ) - assetbits = NXT_ASSETID; - else if ( is_decimalstr(base) > 0 ) - assetbits = calc_nxt64bits(base); - if ( assetbits != 0 ) - { - nextcmd = INSTANTDEX_REQUEST; - nextcmdstr = "request"; - } -} -} -else if ( strncmp(cmdstr,"ALT",3) == 0 ) -{ - if ( (price= instantdex_acceptable(myinfo,0,refstr,base,rel,volume)) > 0. ) - { - // sends NXT assetid, volume and desired - if ( strcmp(base,"NXT") == 0 || strcmp(base,"nxt") == 0 ) - assetbits = NXT_ASSETID; - else if ( is_decimalstr(base) > 0 ) - assetbits = calc_nxt64bits(base); - if ( assetbits != 0 ) - { - nextcmd = INSTANTDEX_REQUEST; - nextcmdstr = "request"; - } - } -} -else if ( strncmp(cmdstr,"NXT",3) == 0 ) -{ - if ( (price= instantdex_acceptable(myinfo,0,refstr,base,rel,volume)) > 0. ) - { - // sends NXT assetid, volume and desired - if ( strcmp(base,"NXT") == 0 || strcmp(base,"nxt") == 0 ) - assetbits = NXT_ASSETID; - else if ( is_decimalstr(base) > 0 ) - assetbits = calc_nxt64bits(base); - if ( assetbits != 0 ) - { - nextcmd = INSTANTDEX_REQUEST; - nextcmdstr = "request"; - } - } -} -{ - -} - -if ( strcmp(cmdstr,"request") == 0 ) -{ - // request: - // other node sends (othercoin, othercoinaddr, otherNXT and reftx that expires before phasedtx) - if ( (strcmp(rel,"BTC") == 0 || strcmp(base,"BTC") == 0) && (price= instantdex_acceptable(myinfo,0,refstr,base,rel,volume)) > 0. ) - { - //aveprice = instantdex_aveprice(myinfo,sortbuf,(int32_t)(sizeof(sortbuf)/sizeof(*sortbuf)),&totalvol,base,rel,volume,argjson); - set_NXTtx(myinfo,&feeT,assetbits,SATOSHIDEN*3,calc_nxt64bits(INSTANTDEX_ACCT),-1); - if ( (feejson= gen_NXT_tx_json(myinfo,fullhash,&feeT,0,1.)) != 0 ) - free_json(feejson); - nextcmd = INSTANTDEX_PROPOSE; - nextcmdstr = "proposal"; - othercoinaddr = myinfo->myaddr.BTC; - otherNXTaddr = myinfo->myaddr.NXTADDR; - } -} -else -{ - if ( strcmp(cmdstr,"proposal") == 0 ) - { - // proposal: - // NXT node submits phasedtx that refers to it, but it wont confirm - nextcmd = INSTANTDEX_ACCEPT; - nextcmdstr = "accept"; - message = ""; - //instantdex_phasetxsubmit(refstr); - } - else if ( strcmp(cmdstr,"accept") == 0 ) - { - // accept: - // other node verifies unconfirmed has phasedtx and broadcasts cltv, also to NXT node, releases trigger - nextcmd = INSTANTDEX_CONFIRM; - nextcmdstr = "confirm"; - message = ""; - //instantdex_phasedtxverify(); - //instantdex_cltvbroadcast(); - //instantdex_releasetrigger(); - } - else if ( strcmp(cmdstr,"confirm") == 0 ) - { - // confirm: - // NXT node verifies bitcoin txbytes has proper payment and cashes in with onetimepubkey - // BTC* node approves phased tx with onetimepubkey - //instantdex_cltvverify(); - //instantdex_phasetxapprove(); - return(clonestr("{\"error\":\"trade confirmed\"}")); - } -} -if ( nextcmd != 0 && (newjson= InstantDEX_argjson(refstr,message,othercoinaddr,otherNXTaddr,nextcmd,duration,flags)) != 0 ) -{ - jaddnum(newjson,"price",price); - jaddnum(newjson,"volume",volume); - return(instantdex_sendcmd(myinfo,newjson,nextcmdstr,myinfo->ipaddr,INSTANTDEX_HOPS)); -} -} -return(clonestr("{\"error\":\"request needs argjson\"}")); -} - num = 0; - depth = 30; - request = jstr(argjson,"request"); - base = jstr(argjson,"base"); - rel = jstr(argjson,"rel"); - refstr = jstr(argjson,"refstr"); - volume = jdouble(argjson,"volume"); - duration = juint(argjson,"duration"); - flags = juint(argjson,"flags"); - nextcmd = 0; - nextcmdstr = message = ""; - -*/ // NXTrequest: // sends NXT assetid, volume and desired // request: @@ -443,7 +318,7 @@ return(clonestr("{\"error\":\"request needs argjson\"}")); char *instantdex_parse(struct supernet_info *myinfo,struct instantdex_msghdr *msg,cJSON *argjson,char *remoteaddr,uint64_t signerbits,uint8_t *data,int32_t datalen) { - char cmdstr[16],*traderip; + char cmdstr[16],*traderip; struct exchange_info *exchange = exchanges777_find("bitcoin"); memset(cmdstr,0,sizeof(cmdstr)), memcpy(cmdstr,msg->cmd,sizeof(msg->cmd)); if ( argjson != 0 ) { @@ -453,13 +328,13 @@ char *instantdex_parse(struct supernet_info *myinfo,struct instantdex_msghdr *ms return(clonestr("{\"result\":\"got my own request\"}")); } if ( strncmp(cmdstr,"BTC",3) == 0 ) - return(instantdex_BTCswap(myinfo,cmdstr+3,msg,argjson,remoteaddr,signerbits,data,datalen)); + return(instantdex_BTCswap(myinfo,exchange,cmdstr+3,msg,argjson,remoteaddr,signerbits,data,datalen)); else if ( strncmp(cmdstr,"NXT",3) == 0 ) - return(instantdex_NXTswap(myinfo,cmdstr+3,msg,argjson,remoteaddr,signerbits,data,datalen)); + return(instantdex_NXTswap(myinfo,exchange,cmdstr+3,msg,argjson,remoteaddr,signerbits,data,datalen)); else if ( strncmp(cmdstr,"ALT",3) == 0 ) - return(instantdex_ALTswap(myinfo,cmdstr+3,msg,argjson,remoteaddr,signerbits,data,datalen)); + return(instantdex_ALTswap(myinfo,exchange,cmdstr+3,msg,argjson,remoteaddr,signerbits,data,datalen)); else if ( strncmp(cmdstr,"PAX",3) == 0 ) - return(instantdex_PAXswap(myinfo,cmdstr+3,msg,argjson,remoteaddr,signerbits,data,datalen)); + return(instantdex_PAXswap(myinfo,exchanges777_find("PAX"),cmdstr+3,msg,argjson,remoteaddr,signerbits,data,datalen)); else return(clonestr("{\"error\":\"unrecognized atomic swap family\"}")); } return(clonestr("{\"error\":\"request needs argjson\"}")); @@ -555,13 +430,21 @@ TWO_STRINGS_AND_TWO_DOUBLES(InstantDEX,minaccept,base,rel,minprice,basevolume) TWO_STRINGS_AND_TWO_DOUBLES(InstantDEX,BTCoffer,othercoin,otherassetid,maxprice,othervolume) { - int32_t hops = INSTANTDEX_HOPS; cJSON *argjson; char *base,*str; + int32_t hops = INSTANTDEX_HOPS; cJSON *argjson; char *base,*str,coinaddr[64]; struct iguana_info *other; + uint8_t pubkey[33]; if ( remoteaddr == 0 ) { argjson = cJSON_CreateObject(); base = othercoin[0] != 0 ? othercoin : otherassetid; + if ( (other= iguana_coinfind(othercoin)) != 0 ) + { + bitcoin_pubkey(pubkey,myinfo->persistent_priv); + bitcoin_address(coinaddr,other->chain->pubtype,pubkey,sizeof(pubkey)); + jaddstr(argjson,othercoin,coinaddr); + } jaddstr(argjson,"base",base); jaddstr(argjson,"rel","BTC"); + jaddstr(argjson,"BTC",myinfo->myaddr.BTC); if ( maxprice > 0. ) { if ( (str= InstantDEX_maxaccept(IGUANA_CALLARGS,base,"BTC",maxprice,othervolume)) != 0 ) diff --git a/iguana/iguana_json.c b/iguana/iguana_json.c index 33e295654..d1fe12e2e 100755 --- a/iguana/iguana_json.c +++ b/iguana/iguana_json.c @@ -490,6 +490,9 @@ cJSON *iguana_peerjson(struct iguana_info *coin,struct iguana_peer *addr) { cJSON *array,*json = cJSON_CreateObject(); jaddstr(json,"ipaddr",addr->ipaddr); + if ( addr->supernet != 0 ) + jaddstr(json,"ipaddr",addr->ipaddr); + jaddstr(json,"supernet","yes"); jaddnum(json,"protover",addr->protover); jaddnum(json,"relay",addr->relayflag); jaddnum(json,"height",addr->height); diff --git a/iguana/swaps/iguana_ALTswap.c b/iguana/swaps/iguana_ALTswap.c index c1299067e..5f53ac35d 100755 --- a/iguana/swaps/iguana_ALTswap.c +++ b/iguana/swaps/iguana_ALTswap.c @@ -25,7 +25,7 @@ // NXT node verifies bitcoin txbytes has proper payment and cashes in with onetimepubkey // BTC* node approves phased tx with onetimepubkey -char *instantdex_ALTswap(struct supernet_info *myinfo,char *cmdstr,struct instantdex_msghdr *msg,cJSON *argjson,char *remoteaddr,uint64_t signerbits,uint8_t *data,int32_t datalen) // receiving side +char *instantdex_ALTswap(struct supernet_info *myinfo,struct exchange_info *exchange,char *cmdstr,struct instantdex_msghdr *msg,cJSON *argjson,char *remoteaddr,uint64_t signerbits,uint8_t *data,int32_t datalen) // receiving side { char *retstr = 0; if ( strcmp(cmdstr,"offer") == 0 ) diff --git a/iguana/swaps/iguana_BTCswap.c b/iguana/swaps/iguana_BTCswap.c index d478dd34f..dc22d1791 100755 --- a/iguana/swaps/iguana_BTCswap.c +++ b/iguana/swaps/iguana_BTCswap.c @@ -32,11 +32,24 @@ struct instantdex_accept *instantdex_acceptable(struct supernet_info *myinfo,cJSON *array,char *refstr,char *base,char *rel,char *offerside,int32_t offerdir,double offerprice,double volume) */ -char *instantdex_BTCswap(struct supernet_info *myinfo,char *cmdstr,struct instantdex_msghdr *msg,cJSON *argjson,char *remoteaddr,uint64_t signerbits,uint8_t *data,int32_t datalen) // receiving side +char *instantdex_BTCswap(struct supernet_info *myinfo,struct exchange_info *exchange,char *cmdstr,struct instantdex_msghdr *msg,cJSON *argjson,char *remoteaddr,uint64_t signerbits,uint8_t *data,int32_t datalen) // receiving side { - char *retstr = 0; + char *base,*rel,*offerside,*retstr = 0; int32_t offerdir = 0; struct instantdex_accept *ap; double offerprice,volume; + if ( exchange == 0 ) + return(clonestr("{\"error\":\"instantdex_BTCswap null exchange ptr\"}")); if ( strcmp(cmdstr,"offer") == 0 ) { + base = jstr(argjson,"base"), rel = jstr(argjson,"rel"); + if ( rel == 0 || strcmp(rel,"BTC") != 0 ) + return(clonestr("{\"error\":\"instantdex_BTCswap offer non BTC rel\"}")); + offerprice = jdouble(argjson,"price"), volume = jdouble(argjson,"volume"); + offerside = "BTC"; + // offerdir = xxx + // printf("got offer.(%s) offerside.%s offerdir.%d\n",jprint(argjson,0),offerside,offerdir); + if ( (ap= instantdex_acceptable(exchange,base,rel,offerside,offerdir,offerprice,volume)) != 0 ) + { + + } } else if ( strcmp(cmdstr,"proposal") == 0 ) { diff --git a/iguana/swaps/iguana_NXTswap.c b/iguana/swaps/iguana_NXTswap.c index d1005ec74..15df61b38 100755 --- a/iguana/swaps/iguana_NXTswap.c +++ b/iguana/swaps/iguana_NXTswap.c @@ -25,7 +25,7 @@ // NXT node verifies bitcoin txbytes has proper payment and cashes in with onetimepubkey // BTC* node approves phased tx with onetimepubkey -char *instantdex_NXTswap(struct supernet_info *myinfo,char *cmdstr,struct instantdex_msghdr *msg,cJSON *argjson,char *remoteaddr,uint64_t signerbits,uint8_t *data,int32_t datalen) // receiving side +char *instantdex_NXTswap(struct supernet_info *myinfo,struct exchange_info *exchange,char *cmdstr,struct instantdex_msghdr *msg,cJSON *argjson,char *remoteaddr,uint64_t signerbits,uint8_t *data,int32_t datalen) // receiving side { char *retstr = 0; if ( strcmp(cmdstr,"offer") == 0 ) diff --git a/iguana/swaps/iguana_PAXswap.c b/iguana/swaps/iguana_PAXswap.c index 94a622d5b..c6dd76364 100755 --- a/iguana/swaps/iguana_PAXswap.c +++ b/iguana/swaps/iguana_PAXswap.c @@ -25,7 +25,7 @@ // NXT node verifies bitcoin txbytes has proper payment and cashes in with onetimepubkey // BTC* node approves phased tx with onetimepubkey -char *instantdex_PAXswap(struct supernet_info *myinfo,char *cmdstr,struct instantdex_msghdr *msg,cJSON *argjson,char *remoteaddr,uint64_t signerbits,uint8_t *data,int32_t datalen) // receiving side +char *instantdex_PAXswap(struct supernet_info *myinfo,struct exchange_info *exchange,char *cmdstr,struct instantdex_msghdr *msg,cJSON *argjson,char *remoteaddr,uint64_t signerbits,uint8_t *data,int32_t datalen) // receiving side { char *retstr = 0; return(clonestr("{\"error\":\"PAX swap is not yet\"}"));