From 9cf5e88a3036c3db9326a3107de947b54f422f17 Mon Sep 17 00:00:00 2001 From: Artem Pikulin Date: Fri, 20 Jul 2018 14:12:25 +0700 Subject: [PATCH] #104. Expose ETH transactions data on WS events. --- iguana/exchanges/LP_commands.c | 2 +- iguana/exchanges/LP_etomic.c | 7 ++--- iguana/exchanges/LP_include.h | 4 +-- iguana/exchanges/LP_remember.c | 56 +++++++++++++++++++++++++++++++++- iguana/exchanges/LP_swap.c | 14 +++++++++ 5 files changed, 75 insertions(+), 8 deletions(-) diff --git a/iguana/exchanges/LP_commands.c b/iguana/exchanges/LP_commands.c index 95295dc74..871d04195 100644 --- a/iguana/exchanges/LP_commands.c +++ b/iguana/exchanges/LP_commands.c @@ -612,7 +612,7 @@ version\n\ jaddstr(retjson,"coin",coin); return(jprint(retjson,1)); } -#ifndef NOT_ETOMIC +#ifndef NOTETOMIC if (strcmp(coin, "ETOMIC") == 0) { if (get_etomic_from_faucet(ptr->smartaddr) != 1) { return(clonestr("{\"error\":\"Could not get ETOMIC from faucet!\"}")); diff --git a/iguana/exchanges/LP_etomic.c b/iguana/exchanges/LP_etomic.c index 3b7c7aa8f..3d03bec75 100644 --- a/iguana/exchanges/LP_etomic.c +++ b/iguana/exchanges/LP_etomic.c @@ -42,6 +42,7 @@ char *LP_etomicalice_send_fee(struct basilisk_swap *swap) { char amount[100], secretKey[70], dexaddr[50]; satoshisToWei(amount, LP_DEXFEE(swap->I.alicerealsat)); + swap->myfee.I.eth_amount = LP_DEXFEE(swap->I.alicerealsat); uint8arrayToHex(secretKey, swap->persistent_privkey.bytes, 32); LP_etomic_pubkeystr_to_addr(INSTANTDEX_PUBKEY, dexaddr); if (strcmp(swap->I.alicestr,"ETH") == 0 ) { @@ -94,7 +95,7 @@ uint8_t LP_etomic_verify_alice_fee(struct basilisk_swap *swap) char *LP_etomicalice_send_payment(struct basilisk_swap *swap) { AliceSendsEthPaymentInput input; AliceSendsErc20PaymentInput input20; BasicTxData txData; - + swap->alicepayment.I.eth_amount = swap->I.alicerealsat; // set input and txData fields from the swap data structure memset(&txData,0,sizeof(txData)); if ( strcmp(swap->I.alicestr,"ETH") == 0 ) @@ -168,7 +169,6 @@ uint8_t LP_etomic_verify_alice_payment(struct basilisk_swap *swap, char *txId) return(0); } AliceSendsEthPaymentInput input; AliceSendsErc20PaymentInput input20; - if ( strcmp(swap->I.alicestr,"ETH") == 0 ) { uint64_t paymentAmount = weiToSatoshi(data.valueHex); if (paymentAmount != swap->I.alicerealsat) { @@ -533,7 +533,6 @@ uint8_t LP_etomic_verify_bob_payment(struct basilisk_swap *swap, char *txId) BobSendsErc20PaymentInput input20; memset(&input,0,sizeof(input)); memset(&input20,0,sizeof(input20)); - if ( strcmp(swap->I.bobstr,"ETH") == 0 ) { uint64_t paymentAmount = weiToSatoshi(data.valueHex); if (paymentAmount != swap->I.bobrealsat) { @@ -767,7 +766,7 @@ int32_t LP_etomic_pub2addr(char *coinaddr,uint8_t pub64[64]) uint8_t LP_etomic_is_empty_tx_id(char *txId) { - if (strcmp(txId, EMPTY_ETH_TX_ID) == 0) { + if (txId[0] == 0 || strcmp(txId, EMPTY_ETH_TX_ID) == 0) { return 1; } return 0; diff --git a/iguana/exchanges/LP_include.h b/iguana/exchanges/LP_include.h index 8503a65f9..b8ff8a194 100644 --- a/iguana/exchanges/LP_include.h +++ b/iguana/exchanges/LP_include.h @@ -206,7 +206,7 @@ struct basilisk_rawtxinfo { char destaddr[64],ethTxid[75]; bits256 txid,signedtxid,actualtxid; - int64_t amount,change,inputsum; + int64_t amount,change,inputsum,eth_amount; int32_t redeemlen,datalen,completed,vintype,vouttype,numconfirms,spendlen,secretstart,suppress_pubkeys; uint32_t locktime,crcs[2]; uint8_t addrtype,pubkey33[33],rmd160[20]; @@ -278,7 +278,7 @@ struct LP_swap_remember uint32_t finishtime,tradeid,requestid,quoteid,plocktime,dlocktime,expiration,state,otherstate; int32_t iambob,finishedflag,origfinishedflag,Predeemlen,Dredeemlen,sentflags[sizeof(txnames)/sizeof(*txnames)]; uint8_t secretAm[20],secretAm256[32],secretBn[20],secretBn256[32],Predeemscript[1024],Dredeemscript[1024],pubkey33[33],other33[33]; - char uuidstr[65],Agui[65],Bgui[65],gui[65],src[65],dest[65],bobtomic[128],alicetomic[128],etomicsrc[65],etomicdest[65],destaddr[64],Adestaddr[64],Sdestaddr[64],alicepaymentaddr[64],bobpaymentaddr[64],bobdepositaddr[64],alicecoin[65],bobcoin[65],*txbytes[sizeof(txnames)/sizeof(*txnames)],bobDepositEthTx[75],bobPaymentEthTx[75],alicePaymentEthTx[75]; + char uuidstr[65],Agui[65],Bgui[65],gui[65],src[65],dest[65],bobtomic[128],alicetomic[128],etomicsrc[65],etomicdest[65],destaddr[64],Adestaddr[64],Sdestaddr[64],alicepaymentaddr[64],bobpaymentaddr[64],bobdepositaddr[64],alicecoin[65],bobcoin[65],*txbytes[sizeof(txnames)/sizeof(*txnames)],bobDepositEthTx[75],bobPaymentEthTx[75],aliceFeeEthTx[75],alicePaymentEthTx[75]; }; struct LP_outpoint diff --git a/iguana/exchanges/LP_remember.c b/iguana/exchanges/LP_remember.c index 732d0f73b..1772b3d05 100644 --- a/iguana/exchanges/LP_remember.c +++ b/iguana/exchanges/LP_remember.c @@ -70,6 +70,12 @@ void basilisk_dontforget(struct basilisk_swap *swap,struct basilisk_rawtx *rawtx fprintf(fp,",\"bobtomic\":\"%s\"",swap->I.bobtomic); if ( swap->I.etomicsrc[0] != 0 ) fprintf(fp,",\"etomicsrc\":\"%s\"",swap->I.etomicsrc); + if (swap->myfee.I.ethTxid[0] != 0) { + fprintf(fp,",\"aliceFeeEthTx\":\"%s\"", swap->myfee.I.ethTxid); + } + if (swap->otherfee.I.ethTxid[0] != 0) { + fprintf(fp,",\"aliceFeeEthTx\":\"%s\"", swap->otherfee.I.ethTxid); + } if (swap->bobdeposit.I.ethTxid[0] != 0) { fprintf(fp,",\"bobDepositEthTx\":\"%s\"", swap->bobdeposit.I.ethTxid); } @@ -253,6 +259,22 @@ void basilisk_dontforget_update(struct basilisk_swap *swap,struct basilisk_rawtx { if ( (reqjson= cJSON_Parse(fstr)) != 0 ) { +#ifndef NOTETOMIC + if (strcmp(rawtx->symbol,"ETOMIC") == 0) { + jdelete(reqjson,"txid"); + jdelete(reqjson,"amount"); + jaddstr(reqjson,"txid", rawtx->I.ethTxid); + jaddnum(reqjson,"amount", dstr(rawtx->I.eth_amount)); + jdelete(reqjson, "coin"); + if (rawtx == &swap->myfee || rawtx == &swap->otherfee || rawtx == &swap->alicepayment || rawtx == &swap->bobspend || rawtx == &swap->alicereclaim) { + jaddstr(reqjson,"coin", swap->I.alicestr); + } + + if (rawtx == &swap->bobdeposit || rawtx == &swap->bobrefund || rawtx == &swap->aliceclaim || rawtx == &swap->bobpayment || rawtx == &swap->bobreclaim || rawtx == &swap->alicespend) { + jaddstr(reqjson,"coin", swap->I.bobstr); + } + } +#endif if ( jobj(reqjson,"method") != 0 ) jdelete(reqjson,"method"); jaddstr(reqjson,"method","update"); @@ -685,7 +707,35 @@ cJSON *LP_swap_json(struct LP_swap_remember *rswap) jaddbits256(item,"Apaymentspent",rswap->Apaymentspent); jaddbits256(item,"depositspent",rswap->depositspent); jaddbits256(item,"alicedexfee",rswap->iambob == 0 ? rswap->txids[BASILISK_MYFEE] : rswap->txids[BASILISK_OTHERFEE]); - + if ( rswap->bobtomic[0] != 0 || rswap->alicetomic[0] != 0) { + cJSON *eth_info = cJSON_CreateObject(); + if (rswap->alicetomic[0] != 0) { + cJSON *a_fee = cJSON_CreateObject(); + jaddstr(a_fee, "txid", rswap->aliceFeeEthTx); + jaddnum(a_fee, "amount", dstr(LP_DEXFEE(rswap->alicerealsat))); + cJSON_AddItemToObject(eth_info, "alicefee", a_fee); + + cJSON *a_payment = cJSON_CreateObject(); + jaddstr(a_payment, "txid", rswap->alicePaymentEthTx); + jaddnum(a_payment, "amount", dstr(rswap->alicerealsat)); + cJSON_AddItemToObject(eth_info, "alicepayment", a_payment); + } + + if (rswap->bobtomic[0] != 0) { + cJSON *b_deposit = cJSON_CreateObject(); + jaddstr(b_deposit, "txid", rswap->bobDepositEthTx); + jaddnum(b_deposit, "amount", dstr(LP_DEPOSITSATOSHIS(rswap->bobrealsat))); + cJSON_AddItemToObject(eth_info, "bobdeposit", b_deposit); + + cJSON *b_payment = cJSON_CreateObject(); + jaddstr(b_payment, "txid", rswap->bobPaymentEthTx); + jaddnum(b_payment, "amount", dstr(rswap->bobrealsat)); + cJSON_AddItemToObject(eth_info, "bobpayment", b_payment); + } + + cJSON_AddItemToObject(item, "eth_info", eth_info); + } + return(item); } @@ -906,6 +956,10 @@ int32_t LP_swap_load(struct LP_swap_remember *rswap,int32_t forceflag) strcpy(rswap->etomicdest,jstr(txobj,"etomicdest")); } + if (jstr(txobj,"aliceFeeEthTx") != 0) { + strcpy(rswap->aliceFeeEthTx, jstr(txobj,"aliceFeeEthTx")); + } + if (jstr(txobj,"bobDepositEthTx") != 0) { strcpy(rswap->bobDepositEthTx, jstr(txobj,"bobDepositEthTx")); } diff --git a/iguana/exchanges/LP_swap.c b/iguana/exchanges/LP_swap.c index 8d6a9c1e5..b4113840f 100644 --- a/iguana/exchanges/LP_swap.c +++ b/iguana/exchanges/LP_swap.c @@ -1342,6 +1342,20 @@ struct basilisk_swap *bitcoin_swapinit(bits256 privkey,uint8_t *pubkey33,bits256 swap->bobpayment.utxotxid = qp->txid, swap->bobpayment.utxovout = qp->vout; swap->bobdeposit.utxotxid = qp->txid2, swap->bobdeposit.utxovout = qp->vout2; swap->alicepayment.utxotxid = qp->desttxid, swap->alicepayment.utxovout = qp->destvout; +#ifndef NOTETOMIC + if (strcmp(alicestr, "ETOMIC") == 0) { + swap->alicepayment.I.eth_amount = swap->I.alicerealsat; + if (swap->I.iambob == 1) { + swap->otherfee.I.eth_amount = LP_DEXFEE(swap->I.alicerealsat); + } else { + swap->myfee.I.eth_amount = LP_DEXFEE(swap->I.alicerealsat); + } + } + if (strcmp(bobstr, "ETOMIC") == 0) { + swap->bobpayment.I.eth_amount = swap->I.bobrealsat; + swap->bobdeposit.I.eth_amount = LP_DEPOSITSATOSHIS(swap->I.bobrealsat); + } +#endif LP_mark_spent(bobstr,qp->txid,qp->vout); LP_mark_spent(bobstr,qp->txid2,qp->vout2); LP_mark_spent(alicestr,qp->desttxid,qp->destvout);