This repository has been archived by the owner on Jan 29, 2019. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 14
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Showing
5 changed files
with
171 additions
and
99 deletions.
There are no files selected for viewing
88 changes: 88 additions & 0 deletions
88
supply/common/src/main/kotlin/fund/cyber/supply/CommonConfiguration.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,88 @@ | ||
package fund.cyber.supply | ||
|
||
import fund.cyber.common.kafka.JsonSerializer | ||
import fund.cyber.common.kafka.defaultProducerConfig | ||
import fund.cyber.common.with | ||
import fund.cyber.search.configuration.CHAIN_FAMILY | ||
import fund.cyber.search.configuration.CHAIN_NAME | ||
import fund.cyber.search.configuration.KAFKA_BROKERS | ||
import fund.cyber.search.configuration.KAFKA_BROKERS_DEFAULT | ||
import fund.cyber.search.model.chains.ChainFamily | ||
import fund.cyber.search.model.chains.ChainInfo | ||
import fund.cyber.search.model.events.PumpEvent | ||
import fund.cyber.search.model.events.supplyTopic | ||
import org.apache.kafka.clients.admin.AdminClient | ||
import org.apache.kafka.clients.admin.AdminClientConfig | ||
import org.apache.kafka.clients.admin.NewTopic | ||
import org.apache.kafka.clients.producer.ProducerConfig | ||
import org.apache.kafka.common.config.TopicConfig | ||
import org.springframework.beans.factory.annotation.Value | ||
import org.springframework.context.annotation.Bean | ||
import org.springframework.context.annotation.ComponentScan | ||
import org.springframework.context.annotation.Configuration | ||
import org.springframework.kafka.core.DefaultKafkaProducerFactory | ||
import org.springframework.kafka.core.KafkaAdmin | ||
import org.springframework.kafka.core.KafkaTemplate | ||
import org.springframework.kafka.core.ProducerFactory | ||
import org.springframework.kafka.transaction.KafkaTransactionManager | ||
import javax.annotation.PostConstruct | ||
|
||
|
||
@Configuration | ||
@ComponentScan("fund.cyber.supply") | ||
class CommonConfiguration { | ||
|
||
@Value("\${$KAFKA_BROKERS:$KAFKA_BROKERS_DEFAULT}") | ||
private lateinit var kafkaBrokers: String | ||
|
||
@Value("\${$CHAIN_FAMILY:}") | ||
private lateinit var chainFamily: String | ||
|
||
@Value("\${$CHAIN_NAME:}") | ||
private lateinit var chainName: String | ||
|
||
|
||
@Bean | ||
fun chainInfo() = ChainInfo(ChainFamily.valueOf(chainFamily), chainName) | ||
|
||
@Bean | ||
fun producerFactory(): ProducerFactory<PumpEvent, Any> { | ||
|
||
val config = defaultProducerConfig().with( | ||
ProducerConfig.BOOTSTRAP_SERVERS_CONFIG to kafkaBrokers | ||
) | ||
return DefaultKafkaProducerFactory<PumpEvent, Any>(config, JsonSerializer(), JsonSerializer()) | ||
.apply { setTransactionIdPrefix(chainInfo().fullName + "_SUPPLY") } | ||
} | ||
|
||
@Bean | ||
fun transactionManager() = KafkaTransactionManager(producerFactory()) | ||
|
||
@Bean | ||
fun kafkaTemplate() = KafkaTemplate(producerFactory()) | ||
|
||
|
||
@Bean | ||
fun kafkaAdmin(): KafkaAdmin { | ||
val configs = mapOf(AdminClientConfig.BOOTSTRAP_SERVERS_CONFIG to kafkaBrokers) | ||
return KafkaAdmin(configs).apply { this.setFatalIfBrokerNotAvailable(true) } | ||
} | ||
|
||
@PostConstruct | ||
fun createTopics() { | ||
|
||
val kafkaClient = AdminClient.create(kafkaAdmin().config) | ||
|
||
val supplyTopicConfig = mapOf( | ||
TopicConfig.RETENTION_BYTES_CONFIG to "52428800", | ||
TopicConfig.CLEANUP_POLICY_CONFIG to TopicConfig.CLEANUP_POLICY_DELETE | ||
) | ||
|
||
val supplyTopic = NewTopic(chainInfo().supplyTopic, 1, 1).configs(supplyTopicConfig) | ||
|
||
kafkaClient.createTopics(listOf(supplyTopic)) | ||
kafkaClient.close() | ||
} | ||
|
||
|
||
} |
50 changes: 50 additions & 0 deletions
50
supply/common/src/main/kotlin/fund/cyber/supply/common/CurrentSupplyProvider.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,50 @@ | ||
package fund.cyber.supply.common | ||
|
||
import fund.cyber.common.kafka.reader.SinglePartitionTopicLastItemsReader | ||
import fund.cyber.search.configuration.GENESIS_SUPPLY | ||
import fund.cyber.search.configuration.KAFKA_BROKERS | ||
import fund.cyber.search.configuration.KAFKA_BROKERS_DEFAULT | ||
import fund.cyber.search.model.chains.ChainInfo | ||
import fund.cyber.search.model.events.supplyTopic | ||
import org.slf4j.LoggerFactory | ||
import org.springframework.beans.factory.annotation.Autowired | ||
import org.springframework.beans.factory.annotation.Value | ||
import org.springframework.stereotype.Component | ||
import java.math.BigDecimal | ||
|
||
|
||
private val log = LoggerFactory.getLogger(CurrentSupplyProvider::class.java)!! | ||
|
||
@Component | ||
class CurrentSupplyProvider { | ||
|
||
@Value("\${$KAFKA_BROKERS:$KAFKA_BROKERS_DEFAULT}") | ||
private lateinit var kafkaBrokers: String | ||
|
||
@Value("\${$GENESIS_SUPPLY:}") | ||
private lateinit var genesisSupply: String | ||
|
||
@Autowired | ||
lateinit var chainInfo: ChainInfo | ||
|
||
fun <T> getLastCalculatedSupply(supplyClass: Class<T>, genesisSupplyCreator: (genesis: BigDecimal) -> T): T { | ||
|
||
val topicReader = SinglePartitionTopicLastItemsReader( | ||
kafkaBrokers = kafkaBrokers, topic = chainInfo.supplyTopic, | ||
keyClass = Any::class.java, valueClass = supplyClass | ||
) | ||
|
||
val keyToValue = topicReader.readLastRecords(1).firstOrNull() | ||
|
||
return if (keyToValue != null) { | ||
keyToValue.second | ||
} else { | ||
if (genesisSupply.trim().isEmpty()) { | ||
log.error("Please specify env variable `GENESIS_SUPPLY`. " + | ||
"For example, initial Ethereum supply is 72009990.50") | ||
throw RuntimeException("`GENESIS_SUPPLY` is not provided") | ||
} | ||
genesisSupplyCreator(BigDecimal(genesisSupply)) | ||
} | ||
} | ||
} |
2 changes: 1 addition & 1 deletion
2
...yber/supply/ethereum/SupplyApplication.kt → ...in/fund/cyber/supply/SupplyApplication.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
3 changes: 2 additions & 1 deletion
3
supply/ethereum/src/test/kotlin/fund/cyber/supply/ethereum/EthereumSupplyBaseTest.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,10 +1,11 @@ | ||
package fund.cyber.supply.ethereum | ||
|
||
import fund.cyber.common.kafka.BaseKafkaIntegrationTestWithStartedKafka | ||
import fund.cyber.supply.CommonConfiguration | ||
import org.springframework.test.context.ContextConfiguration | ||
import org.springframework.test.context.TestPropertySource | ||
|
||
|
||
@TestPropertySource(properties = ["CHAIN_FAMILY:ETHEREUM", "GENESIS_SUPPLY:72009990.50"]) | ||
@ContextConfiguration(classes = [ApplicationConfiguration::class]) | ||
@ContextConfiguration(classes = [ApplicationConfiguration::class, CommonConfiguration::class]) | ||
abstract class EthereumSupplyBaseTest : BaseKafkaIntegrationTestWithStartedKafka() |