Skip to content

Commit

Permalink
Merge pull request #106 from artemii235/etomic
Browse files Browse the repository at this point in the history
#97 Check ETOMIC balance before faucet. #104 Expose ETH data on WS.
  • Loading branch information
artemii235 authored Jul 23, 2018
2 parents 64bccb4 + 4093b4b commit a724c41
Show file tree
Hide file tree
Showing 9 changed files with 137 additions and 25 deletions.
6 changes: 3 additions & 3 deletions crypto777/cJSON.c
Original file line number Diff line number Diff line change
Expand Up @@ -902,10 +902,10 @@ char *get_cJSON_fieldname(cJSON *obj)
{
if ( obj != 0 )
{
if ( obj->child != 0 && obj->child->string != 0 )
return(obj->child->string);
else if ( obj->string != 0 )
if ( obj->string != 0 )
return(obj->string);
if ( obj->child != 0 && obj->child->string != 0 )
return(obj->child->string);
}
return((char *)"<no cJSON string field>");
}
Expand Down
13 changes: 2 additions & 11 deletions iguana/exchanges/LP_commands.c
Original file line number Diff line number Diff line change
Expand Up @@ -612,8 +612,8 @@ version\n\
jaddstr(retjson,"coin",coin);
return(jprint(retjson,1));
}
#ifndef NOT_ETOMIC
if (strcmp(coin, "ETOMIC") == 0) {
#ifndef NOTETOMIC
if (strcmp(coin, "ETOMIC") == 0 && LP_RTsmartbalance(ptr) < 20 * SATOSHIDEN) {
if (get_etomic_from_faucet(ptr->smartaddr) != 1) {
return(clonestr("{\"error\":\"Could not get ETOMIC from faucet!\"}"));
}
Expand Down Expand Up @@ -641,8 +641,6 @@ version\n\
{
cJSON *array;
ptr->inactive = 0;
if ( ptr->smartaddr[0] != 0 )
LP_unspents_load(coin,ptr->smartaddr);
LP_unspents_load(coin,ptr->smartaddr);
if ( strcmp(ptr->symbol,"KMD") == 0 )
LP_importaddress("KMD",BOTS_BONDADDRESS);
Expand Down Expand Up @@ -708,13 +706,6 @@ version\n\
{
if ( (ptr= LP_coinsearch(coin)) != 0 )
{
#ifndef NOTETOMIC
if (strcmp(coin, "ETOMIC") == 0) {
if (get_etomic_from_faucet(ptr->smartaddr) != 1) {
return(clonestr("{\"error\":\"Could not get ETOMIC from faucet!\"}"));
}
}
#endif
ptr->inactive = 0;
return(jprint(LP_electrumserver(ptr,jstr(argjson,"ipaddr"),juint(argjson,"port")),1));
} else return(clonestr("{\"error\":\"cant find coind\"}"));
Expand Down
7 changes: 3 additions & 4 deletions iguana/exchanges/LP_etomic.c
Original file line number Diff line number Diff line change
Expand Up @@ -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 ) {
Expand Down Expand Up @@ -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 )
Expand Down Expand Up @@ -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) {
Expand Down Expand Up @@ -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) {
Expand Down Expand Up @@ -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;
Expand Down
5 changes: 3 additions & 2 deletions iguana/exchanges/LP_include.h
Original file line number Diff line number Diff line change
Expand Up @@ -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];
Expand Down Expand Up @@ -278,7 +278,8 @@ 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)];
char bobDepositEthTx[75],bobPaymentEthTx[75],aliceFeeEthTx[75],alicePaymentEthTx[75],bobPaymentSpentEth[75],alicePaymentSpentEth[75],bobDepositSpentEth[75];
};

struct LP_outpoint
Expand Down
10 changes: 9 additions & 1 deletion iguana/exchanges/LP_mmjson.c
Original file line number Diff line number Diff line change
Expand Up @@ -259,7 +259,15 @@ int32_t MMJSON_decodeitem(cJSON *lineobj,uint8_t *linebuf,int32_t i,int32_t len,
arbstr[j++] = c;
}
arbstr[j] = 0;
jaddstr(lineobj,fieldstr,arbstr);
#ifndef NOTETOMIC
if (strcmp(fieldstr, "eth_info") == 0) {
cJSON_AddItemToObject(lineobj, fieldstr, cJSON_Parse(arbstr));
} else {
#endif
jaddstr(lineobj, fieldstr, arbstr);
#ifndef NOTETOMIC
}
#endif
break;
default:
if ( valind < MMJSON_BOUNDARY )
Expand Down
2 changes: 1 addition & 1 deletion iguana/exchanges/LP_nativeDEX.c
Original file line number Diff line number Diff line change
Expand Up @@ -1226,7 +1226,7 @@ void queue_loop(void *ctx)
}
if ( (json= cJSON_Parse((char *)ptr->msg)) != 0 )
{
if ( 1 && ptr->msglen < sizeof(linebuf) )
if ( ptr->msglen < sizeof(linebuf) )
{
if ( (k= MMJSON_encode(linebuf,(char *)ptr->msg)) > 0 )
{
Expand Down
89 changes: 87 additions & 2 deletions iguana/exchanges/LP_remember.c
Original file line number Diff line number Diff line change
Expand Up @@ -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);
}
Expand Down Expand Up @@ -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");
Expand Down Expand Up @@ -685,7 +707,56 @@ 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]);

#ifndef NOTETOMIC
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->alicePaymentSpentEth[0] != 0) {
cJSON *a_payment_spent = cJSON_CreateObject();
jaddstr(a_payment_spent, "txid", rswap->alicePaymentSpentEth);
jaddnum(a_payment_spent, "amount", dstr(rswap->alicerealsat));
cJSON_AddItemToObject(eth_info, "alicepaymentspent", a_payment_spent);
}
}

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);
if (rswap->bobDepositSpentEth[0] != 0) {
cJSON *b_deposit_spent = cJSON_CreateObject();
jaddstr(b_deposit_spent, "txid", rswap->bobDepositSpentEth);
jaddnum(b_deposit_spent, "amount", dstr(LP_DEPOSITSATOSHIS(rswap->bobrealsat)));
cJSON_AddItemToObject(eth_info, "bobdepositspent", b_deposit_spent);
}

if (rswap->bobPaymentSpentEth[0] != 0) {
cJSON *b_payment_spent = cJSON_CreateObject();
jaddstr(b_payment_spent, "txid", rswap->bobPaymentSpentEth);
jaddnum(b_payment_spent, "amount", dstr(rswap->bobrealsat));
cJSON_AddItemToObject(eth_info, "bobpaymentspent", b_payment_spent);
}
}

cJSON_AddItemToObject(item, "eth_info", eth_info);
}
#endif
return(item);
}

Expand Down Expand Up @@ -906,6 +977,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"));
}
Expand Down Expand Up @@ -1283,6 +1358,7 @@ cJSON *basilisk_remember(int32_t fastflag,int64_t *KMDtotals,int64_t *BTCtotals,
{
char *aliceSpendEthTxId = LP_etomicalice_spends_bob_payment(&rswap);
if (aliceSpendEthTxId != NULL) {
strcpy(rswap.bobPaymentSpentEth, aliceSpendEthTxId);
free(aliceSpendEthTxId);
} else {
printf("Alice spend ETH tx send failed!\n");
Expand Down Expand Up @@ -1332,6 +1408,7 @@ cJSON *basilisk_remember(int32_t fastflag,int64_t *KMDtotals,int64_t *BTCtotals,
{
char *aliceClaimsEthTxId = LP_etomicalice_claims_bob_deposit(&rswap);
if (aliceClaimsEthTxId != NULL) {
strcpy(rswap.bobDepositSpentEth, aliceClaimsEthTxId);
free(aliceClaimsEthTxId);
} else {
printf("Alice Bob deposit claim ETH tx failed!\n");
Expand Down Expand Up @@ -1364,7 +1441,12 @@ cJSON *basilisk_remember(int32_t fastflag,int64_t *KMDtotals,int64_t *BTCtotals,
if ( rswap.alicetomic[0] != 0 )
{
char *aliceReclaimEthTx = LP_etomicalice_reclaims_payment(&rswap);
free(aliceReclaimEthTx);
if (aliceReclaimEthTx != NULL) {
strcpy(rswap.alicePaymentSpentEth, aliceReclaimEthTx);
free(aliceReclaimEthTx);
} else {
printf("Alice could not reclaim ETH/ERC20 payment!\n");
}
}
#endif
}
Expand Down Expand Up @@ -1408,6 +1490,7 @@ cJSON *basilisk_remember(int32_t fastflag,int64_t *KMDtotals,int64_t *BTCtotals,
{
char *bobSpendEthTx = LP_etomicbob_spends_alice_payment(&rswap);
if (bobSpendEthTx != NULL) {
strcpy(rswap.alicePaymentSpentEth, bobSpendEthTx);
free(bobSpendEthTx);
} else {
printf("Bob spends Alice payment ETH tx send failed!\n");
Expand Down Expand Up @@ -1445,6 +1528,7 @@ cJSON *basilisk_remember(int32_t fastflag,int64_t *KMDtotals,int64_t *BTCtotals,
{
char *bobReclaimEthTx = LP_etomicbob_reclaims_payment(&rswap);
if (bobReclaimEthTx != NULL) {
strcpy(rswap.bobPaymentSpentEth, bobReclaimEthTx);
free(bobReclaimEthTx);
} else {
printf("Bob reclaims payment ETH tx send failed!\n");
Expand Down Expand Up @@ -1498,6 +1582,7 @@ cJSON *basilisk_remember(int32_t fastflag,int64_t *KMDtotals,int64_t *BTCtotals,
{
char *bobRefundsEthTx = LP_etomicbob_refunds_deposit(&rswap);
if (bobRefundsEthTx != NULL) {
strcpy(rswap.bobDepositSpentEth, bobRefundsEthTx);
free(bobRefundsEthTx);
} else {
printf("Bob refunds deposit ETH tx send failed!\n");
Expand Down
16 changes: 15 additions & 1 deletion iguana/exchanges/LP_socket.c
Original file line number Diff line number Diff line change
Expand Up @@ -1277,7 +1277,21 @@ cJSON *LP_electrumserver(struct iguana_info *coin,char *ipaddr,uint16_t port)
jaddnum(retjson,"restart",kickval);
}
}
#ifndef NOTETOMIC
if (strcmp(coin->symbol, "ETOMIC") == 0) {
cJSON *balance = cJSON_CreateObject();
electrum_address_getbalance(coin->symbol, ep, &balance, coin->smartaddr);
int64_t confirmed = get_cJSON_int(balance, "confirmed");
int64_t unconfirmed = get_cJSON_int(balance, "unconfirmed");
if ((confirmed + unconfirmed) < 20 * SATOSHIDEN && get_etomic_from_faucet(coin->smartaddr) != 1) {
coin->inactive = (uint32_t)time(NULL);
coin->electrum = ep->prev;
cJSON_Delete(balance);
return(cJSON_Parse("{\"error\":\"Could not get ETOMIC from faucet!\"}"));
}
cJSON_Delete(balance);
}
#endif
//printf("(%s)\n",jprint(retjson,0));
return(retjson);
}

14 changes: 14 additions & 0 deletions iguana/exchanges/LP_swap.c
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand Down

0 comments on commit a724c41

Please sign in to comment.