|
| 1 | +package io.openfuture.state.component |
| 2 | + |
| 3 | +import io.openfuture.state.config.property.EthereumProperties |
| 4 | +import io.openfuture.state.controller.domain.dto.TransactionDto |
| 5 | +import io.openfuture.state.controller.domain.dto.pow |
| 6 | +import io.openfuture.state.service.StateTrackingService |
| 7 | +import io.reactivex.disposables.Disposable |
| 8 | +import io.reactivex.functions.Consumer |
| 9 | +import org.slf4j.LoggerFactory |
| 10 | +import org.springframework.stereotype.Component |
| 11 | +import org.web3j.protocol.Web3j |
| 12 | +import org.web3j.protocol.core.DefaultBlockParameterName |
| 13 | +import org.web3j.protocol.core.methods.response.EthBlock |
| 14 | +import java.math.BigInteger |
| 15 | +import javax.annotation.PostConstruct |
| 16 | +import javax.annotation.PreDestroy |
| 17 | + |
| 18 | + |
| 19 | +@Component |
| 20 | +class Web3Wrapper( |
| 21 | + private val web3j: Web3j, |
| 22 | + private val properties: EthereumProperties, |
| 23 | + private val stateService: StateTrackingService |
| 24 | +) { |
| 25 | + |
| 26 | + private var blockSubscriber: Disposable? = null |
| 27 | + |
| 28 | + |
| 29 | + @PostConstruct |
| 30 | + fun subscribe() { |
| 31 | + if (!properties.eventSubscription) return |
| 32 | + |
| 33 | + blockSubscriber = web3j.blockFlowable(true).subscribe( |
| 34 | + Consumer { processBlock(it.block) }, |
| 35 | + onErrorSubscription() |
| 36 | + ) |
| 37 | + } |
| 38 | + |
| 39 | + @PreDestroy |
| 40 | + fun destroy() { |
| 41 | + unsubscribe() |
| 42 | + } |
| 43 | + |
| 44 | + fun getEthBalance(address: String): Long = web3j.ethGetBalance(address, DefaultBlockParameterName.LATEST) |
| 45 | + .sendAsync() |
| 46 | + .get() |
| 47 | + .balance |
| 48 | + .toLong() |
| 49 | + |
| 50 | + private fun unsubscribe() { |
| 51 | + blockSubscriber?.dispose() |
| 52 | + } |
| 53 | + |
| 54 | + private fun processBlock(block: EthBlock.Block?) { |
| 55 | + if (block == null) return |
| 56 | + |
| 57 | + val transactions = block.transactions |
| 58 | + |
| 59 | + transactions.forEach { txObject -> |
| 60 | + val tx = txObject.get() as EthBlock.TransactionObject |
| 61 | + |
| 62 | + if ((tx.from != null && stateService.isTrackedAddress(tx.from, 1)) || |
| 63 | + (tx.to != null && stateService.isTrackedAddress(tx.to, 1))) { |
| 64 | + stateService.processTransaction(getTransaction(tx, block.timestamp)) |
| 65 | + } |
| 66 | + } |
| 67 | + } |
| 68 | + |
| 69 | + private fun onErrorSubscription(): Consumer<Throwable> = Consumer { |
| 70 | + log.warn("Error subscription: $it") |
| 71 | + subscribe() |
| 72 | + } |
| 73 | + |
| 74 | + private fun getTransaction(tx: EthBlock.TransactionObject, timestamp: BigInteger): TransactionDto { |
| 75 | + return TransactionDto(1, tx.hash, tx.from, tx.to, tx.value.toLong(), |
| 76 | + tx.gas.toLong() * 10.pow(10).toLong(), |
| 77 | + timestamp.toLong(), tx.blockNumber.toLong(), tx.blockHash) |
| 78 | + } |
| 79 | + |
| 80 | + |
| 81 | + companion object { |
| 82 | + private val log = LoggerFactory.getLogger(Web3Wrapper::class.java) |
| 83 | + } |
| 84 | + |
| 85 | +} |
0 commit comments