Skip to content

Commit

Permalink
add messages from exchange protos dynamically
Browse files Browse the repository at this point in the history
  • Loading branch information
nullpointer0x00 committed Mar 11, 2024
1 parent 7043784 commit 3cbd480
Show file tree
Hide file tree
Showing 3 changed files with 28 additions and 3 deletions.
2 changes: 2 additions & 0 deletions buildSrc/src/main/kotlin/Dependencies.kt
Original file line number Diff line number Diff line change
Expand Up @@ -54,6 +54,7 @@ object Versions {
const val ProvProto = "1.18.0-rc2"
const val Postgres = "42.2.23"
const val Protobuf = "3.21.9"
const val Reflections = "0.9.12"

// Testing
const val Jupiter = "5.9.1"
Expand Down Expand Up @@ -89,6 +90,7 @@ object Libraries {
const val KtorClientContentNeg = "io.ktor:ktor-client-content-negotiation:${Versions.Ktor}"
const val KaseChange = "net.pearx.kasechange:kasechange:${Versions.KaseChange}"
const val Json = "org.json:json:${Versions.Json}"
const val Reflections = "org.reflections:reflections:${Versions.Reflections}"

// Protobuf
const val GrpcNetty = "io.grpc:grpc-netty:${Versions.Grpc}"
Expand Down
1 change: 1 addition & 0 deletions service/build.gradle.kts
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@ dependencies {
implementation(Libraries.KotlinStdlib)
implementation(Libraries.ProtobufKotlin)
implementation(Libraries.ProvenanceProto)
implementation(Libraries.Reflections)

implementation(Libraries.SpringBootStarterWeb)
implementation(Libraries.SpringBootStarterJdbc)
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
package io.provenance.explorer.config

import com.google.protobuf.Descriptors

Check failure on line 3 in service/src/main/kotlin/io/provenance/explorer/config/RestConfig.kt

View workflow job for this annotation

GitHub Actions / ktlint

Imports must be ordered in lexicographic order without any empty lines in-between with "java", "javax", "kotlin" and aliases in the end
import com.google.protobuf.util.JsonFormat
import cosmos.auth.v1beta1.Auth
import cosmos.authz.v1beta1.Authz
Expand Down Expand Up @@ -148,6 +149,8 @@ import io.provenance.reward.v1.MsgClaimAllRewardsRequest
import io.provenance.reward.v1.MsgClaimRewardsRequest
import io.provenance.reward.v1.MsgCreateRewardProgramRequest
import io.provenance.reward.v1.MsgEndRewardProgramRequest
import io.provenance.exchange.v1.MsgGovCreateMarketRequest

Check failure on line 152 in service/src/main/kotlin/io/provenance/explorer/config/RestConfig.kt

View workflow job for this annotation

GitHub Actions / ktlint

Unused import
import org.reflections.Reflections
import org.springframework.context.annotation.Bean
import org.springframework.context.annotation.Configuration
import org.springframework.context.annotation.Primary
Expand All @@ -157,6 +160,7 @@ import org.springframework.web.client.RestTemplate
import org.springframework.web.servlet.config.annotation.CorsRegistry
import org.springframework.web.servlet.config.annotation.WebMvcConfigurer


Check failure on line 163 in service/src/main/kotlin/io/provenance/explorer/config/RestConfig.kt

View workflow job for this annotation

GitHub Actions / ktlint

Needless blank line(s)
@Configuration
class RestConfig {

Expand Down Expand Up @@ -233,8 +237,8 @@ fun pubKeyDescriptors() =
cosmos.crypto.multisig.Keys.LegacyAminoPubKey.getDescriptor()
)

fun msgDescriptors() =
listOf(
fun msgDescriptors(): List<Descriptors.Descriptor> {
val descriptors = mutableListOf(
TxOuterClass.Tx.getDescriptor(),
Tx.MsgSend.getDescriptor(),
Tx.MsgMultiSend.getDescriptor(),
Expand Down Expand Up @@ -357,9 +361,27 @@ fun msgDescriptors() =
MsgVote.getDescriptor(),
MsgWithdrawProposal.getDescriptor(),
MsgExec.getDescriptor(),
MsgLeaveGroup.getDescriptor()
MsgLeaveGroup.getDescriptor(),

Check failure on line 364 in service/src/main/kotlin/io/provenance/explorer/config/RestConfig.kt

View workflow job for this annotation

GitHub Actions / ktlint

Unnecessary trailing comma before ")"
)

descriptors.addAll(findMsgDescriptorsInPackage("io.provenance.exchange.v1"))

return descriptors
}

private fun findMsgDescriptorsInPackage(packageName: String): List<Descriptors.Descriptor> {
val reflections = Reflections(packageName)
val messageClasses = reflections.getSubTypesOf(com.google.protobuf.Message::class.java)

return messageClasses.filter { it.simpleName.startsWith("Msg") }.mapNotNull {
try {
it.getMethod("getDescriptor").invoke(null) as Descriptors.Descriptor
} catch (e: Exception) {
null // or log error
}
}
}

fun contentDescriptors() =
listOf(
Gov.TextProposal.getDescriptor(),
Expand Down

0 comments on commit 3cbd480

Please sign in to comment.