From b488abc51a4d3a452ac7be2a26a18f1399bf7acb Mon Sep 17 00:00:00 2001 From: Valentin Stavetski Date: Thu, 3 May 2018 20:21:27 +0300 Subject: [PATCH] #173 update isAlive implementation for etherdelta --- .../connector/trade/EtherdeltaTradeConnector.kt | 13 ++++++------- 1 file changed, 6 insertions(+), 7 deletions(-) diff --git a/exchanges-connector/src/main/kotlin/fund/cyber/markets/connector/trade/EtherdeltaTradeConnector.kt b/exchanges-connector/src/main/kotlin/fund/cyber/markets/connector/trade/EtherdeltaTradeConnector.kt index a1383b99..330a9be9 100644 --- a/exchanges-connector/src/main/kotlin/fund/cyber/markets/connector/trade/EtherdeltaTradeConnector.kt +++ b/exchanges-connector/src/main/kotlin/fund/cyber/markets/connector/trade/EtherdeltaTradeConnector.kt @@ -40,6 +40,7 @@ import java.util.concurrent.TimeUnit private const val ETHERDELTA_CONTRACT_ADDRESS = "0x8d12a197cb00d4747a1fe03395095ce2a5cc6819" private const val ETH_SYMBOL = "ETH" +private const val ETHERDELTA_CONNECTION_TIMEOUT_MS: Long = 5 * 60 * 1000 @Component class EtherdeltaTradeConnector : Connector { @@ -48,6 +49,7 @@ class EtherdeltaTradeConnector : Connector { private val exchangeName = "ETHERDELTA" private val tradesTopicName by lazy { TRADES_TOPIC_PREFIX + exchangeName } private lateinit var etherdeltaContract: EtherdeltaContract + private var lastTradeTimestamp: Long? = null @Autowired private lateinit var web3j: Web3j @@ -86,15 +88,11 @@ class EtherdeltaTradeConnector : Connector { } override fun isAlive(): Boolean { - val test: String? + val currentTimestamp = Date().time - try { - test = etherdeltaContract.accountLevelsAddr().send() - } catch (e: Throwable) { - return false - } + return lastTradeTimestamp != null && + currentTimestamp - lastTradeTimestamp!! < ETHERDELTA_CONNECTION_TIMEOUT_MS - return test != null } /** @@ -180,6 +178,7 @@ class EtherdeltaTradeConnector : Connector { } val timestamp = Numeric.toBigInt(block.block.timestampRaw).multiply(BigInteger.valueOf(1000)).toLong() + lastTradeTimestamp = timestamp if (tokenGive.symbol == ETH_SYMBOL) { val price = amountGive.divide(amountGet, tokenGive.decimals, RoundingMode.HALF_EVEN)