From 453fa12d1c1b7c912d291f635b93f35d2f906e5d Mon Sep 17 00:00:00 2001 From: Artem Pikulin Date: Wed, 30 May 2018 18:53:53 +0700 Subject: [PATCH] Rework faucet integration, abort ETH/ERC20 swap if ETH balance < 0.005. --- iguana/exchanges/LP_commands.c | 22 +++++++++------------- iguana/exchanges/LP_swap.c | 23 +++++++++++++++++++++-- iguana/exchanges/etomicswap/etomiccurl.c | 5 +---- iguana/exchanges/etomicswap/etomiccurl.h | 2 +- 4 files changed, 32 insertions(+), 20 deletions(-) diff --git a/iguana/exchanges/LP_commands.c b/iguana/exchanges/LP_commands.c index 84b6a0984..7279e13fa 100644 --- a/iguana/exchanges/LP_commands.c +++ b/iguana/exchanges/LP_commands.c @@ -581,24 +581,20 @@ jpg(srcfile, destfile, power2=7, password, data="", required, ind=0)\n\ return(jprint(retjson,1)); } #ifndef NOT_ETOMIC + if (strcmp(coin, "ETOMIC") == 0) { + char eth_addr[100]; + bits256 privkey = LP_privkey("ETOMIC", ptr->smartaddr, ptr->taddr); + LP_etomic_priv2addr(eth_addr, privkey); + if (get_etomic_from_faucet(eth_addr, ptr->smartaddr) != 1) { + return(clonestr("{\"error\":\"Could not get ETOMIC from faucet!\"}")); + } + } + if (ptr->etomic[0] != 0) { struct iguana_info *etomic_coin = LP_coinsearch("ETOMIC"); if (etomic_coin->inactive != 0) { return(clonestr("{\"error\":\"Enable ETOMIC first to use ETH/ERC20!\"}")); } - - if (ptr->inactive != 0) { - uint64_t balance = LP_etomic_get_balance(ptr, ptr->smartaddr); - uint64_t new_required = G.LP_required_etomic_balance + (balance * 9) / 4; - uint64_t etomic_balance = LP_RTsmartbalance(etomic_coin); - if (etomic_balance < new_required) { - printf("Need additional ETOMIC amount: %" PRIu64 "\n", new_required - etomic_balance); - if (get_etomic_from_faucet(ptr->smartaddr, etomic_coin->smartaddr, ptr->etomic) != 1) { - return(clonestr("{\"error\":\"Could not get ETOMIC from faucet!\"}")); - } - } - G.LP_required_etomic_balance = new_required; - } } #endif if ( LP_conflicts_find(ptr) == 0 ) diff --git a/iguana/exchanges/LP_swap.c b/iguana/exchanges/LP_swap.c index c28f06495..1a080839d 100644 --- a/iguana/exchanges/LP_swap.c +++ b/iguana/exchanges/LP_swap.c @@ -854,7 +854,16 @@ void LP_bobloop(void *_swap) expiration = (uint32_t)time(NULL) + LP_SWAPSTEP_TIMEOUT; bobwaittimeout = LP_calc_waittimeout(bobstr); alicewaittimeout = LP_calc_waittimeout(alicestr); - if ( swap != 0 ) +#ifndef NOTETOMIC + if (swap->I.bobtomic[0] != 0 || swap->I.alicetomic != 0) { + uint64_t eth_balance = getEthBalance(swap->I.etomicsrc); + if (eth_balance < 500000) { + err = -5000, printf("Bob ETH balance too low, aborting swap!\n"); + } + } +#endif + + if ( swap != 0 && err == 0) { if ( LP_waitsend("pubkeys",120,swap->N.pair,swap,data,maxlen,LP_pubkeys_verify,LP_pubkeys_data) < 0 ) err = -2000, printf("error waitsend pubkeys\n"); @@ -947,7 +956,17 @@ void LP_aliceloop(void *_swap) expiration = (uint32_t)time(NULL) + LP_SWAPSTEP_TIMEOUT; bobwaittimeout = LP_calc_waittimeout(bobstr); alicewaittimeout = LP_calc_waittimeout(alicestr); - if ( swap != 0 ) + +#ifndef NOTETOMIC + if (swap->I.bobtomic[0] != 0 || swap->I.alicetomic != 0) { + uint64_t eth_balance = getEthBalance(swap->I.etomicdest); + if (eth_balance < 500000) { + err = -5001, printf("Alice ETH balance too low, aborting swap!\n"); + } + } +#endif + + if ( swap != 0 && err == 0) { printf("start swap iamalice pair.%d\n",swap->N.pair); if ( LP_sendwait("pubkeys",120,swap->N.pair,swap,data,maxlen,LP_pubkeys_verify,LP_pubkeys_data) < 0 ) diff --git a/iguana/exchanges/etomicswap/etomiccurl.c b/iguana/exchanges/etomicswap/etomiccurl.c index 1db357b7f..9618eb200 100644 --- a/iguana/exchanges/etomicswap/etomiccurl.c +++ b/iguana/exchanges/etomicswap/etomiccurl.c @@ -373,15 +373,12 @@ void unlock_send_tx_mutex() pthread_mutex_unlock(&sendTxMutex); } -uint8_t get_etomic_from_faucet(char *eth_addr, char *etomic_addr, char *token_addr) +uint8_t get_etomic_from_faucet(char *eth_addr, char *etomic_addr) { char* string; cJSON *request = cJSON_CreateObject(); cJSON_AddStringToObject(request, "ethAddress", eth_addr); cJSON_AddStringToObject(request, "etomicAddress", etomic_addr); - if (strcmp(token_addr, "0x0000000000000000000000000000000000000000") != 0) { - cJSON_AddStringToObject(request, "tokenAddress", token_addr); - } string = cJSON_PrintUnformatted(request); char* requestResult = sendRequest(string, FAUCET_URL); free(string); diff --git a/iguana/exchanges/etomicswap/etomiccurl.h b/iguana/exchanges/etomicswap/etomiccurl.h index a340a604c..6e5036739 100644 --- a/iguana/exchanges/etomicswap/etomiccurl.h +++ b/iguana/exchanges/etomicswap/etomiccurl.h @@ -50,7 +50,7 @@ uint64_t getEthBlockNumber(); uint64_t getGasPriceFromStation(uint8_t defaultOnErr); int32_t waitForConfirmation(char *txId); void unlock_send_tx_mutex(); -uint8_t get_etomic_from_faucet(char *eth_addr, char *etomic_addr, char *token_addr); +uint8_t get_etomic_from_faucet(char *eth_addr, char *etomic_addr); #ifdef __cplusplus }