Browse Source

#71 Fix crash on failing ETH/ERC20 balance requests.

pass-iguana-arg
Artem Pikulin 7 years ago
parent
commit
7eeb533b74
  1. 38
      iguana/exchanges/etomicswap/etomiclib.cpp

38
iguana/exchanges/etomicswap/etomiclib.cpp

@ -554,10 +554,14 @@ char* getPubKeyFromPriv(char* privKey)
uint64_t getEthBalance(char* address) uint64_t getEthBalance(char* address)
{ {
char* hexBalance = getEthBalanceRequest(address); char* hexBalance = getEthBalanceRequest(address);
// convert wei to satoshi if (hexBalance != NULL) {
u256 balance = jsToU256(hexBalance) / boost::multiprecision::pow(u256(10), 10); // convert wei to satoshi
free(hexBalance); u256 balance = jsToU256(hexBalance) / boost::multiprecision::pow(u256(10), 10);
return static_cast<uint64_t>(balance); free(hexBalance);
return static_cast<uint64_t>(balance);
} else {
return 0;
}
} }
uint64_t getErc20BalanceSatoshi(char *address, char *tokenAddress, uint8_t setDecimals) uint64_t getErc20BalanceSatoshi(char *address, char *tokenAddress, uint8_t setDecimals)
@ -569,19 +573,23 @@ uint64_t getErc20BalanceSatoshi(char *address, char *tokenAddress, uint8_t setDe
char* hexBalance = ethCall(tokenAddress, ss.str().c_str()); char* hexBalance = ethCall(tokenAddress, ss.str().c_str());
// convert wei to satoshi // convert wei to satoshi
uint8_t decimals; uint8_t decimals;
if (setDecimals > 0) { if (hexBalance != NULL) {
decimals = setDecimals; if (setDecimals > 0) {
} else { decimals = setDecimals;
decimals = getErc20Decimals(tokenAddress); } else {
} decimals = getErc20Decimals(tokenAddress);
}
u256 balance = jsToU256(hexBalance); u256 balance = jsToU256(hexBalance);
if (decimals < 18) { if (decimals < 18) {
balance *= boost::multiprecision::pow(u256(10), 18 - decimals); balance *= boost::multiprecision::pow(u256(10), 18 - decimals);
}
balance /= boost::multiprecision::pow(u256(10), 10);
free(hexBalance);
return static_cast<uint64_t>(balance);
} else {
return 0;
} }
balance /= boost::multiprecision::pow(u256(10), 10);
free(hexBalance);
return static_cast<uint64_t>(balance);
} }
char *getErc20BalanceHexWei(char *address, char *tokenAddress) char *getErc20BalanceHexWei(char *address, char *tokenAddress)

Loading…
Cancel
Save