Skip to content

Commit

Permalink
migrate config to separate repo
Browse files Browse the repository at this point in the history
  • Loading branch information
t0lia committed Jul 7, 2024
1 parent e3e4971 commit 305c8e7
Show file tree
Hide file tree
Showing 9 changed files with 112 additions and 130 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,9 @@ import org.springframework.messaging.simp.config.MessageBrokerRegistry
import org.springframework.scheduling.annotation.EnableScheduling
import org.springframework.web.servlet.config.annotation.CorsRegistry
import org.springframework.web.servlet.config.annotation.WebMvcConfigurer
import org.springframework.web.socket.config.annotation.*
import org.springframework.web.socket.config.annotation.EnableWebSocketMessageBroker
import org.springframework.web.socket.config.annotation.StompEndpointRegistry
import org.springframework.web.socket.config.annotation.WebSocketMessageBrokerConfigurer

@SpringBootApplication
@EnableWebSocketMessageBroker
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,8 @@ import com.fasterxml.jackson.annotation.JsonProperty
data class ChainItemConfig @JsonCreator constructor(
@JsonProperty("id") val id: Int,
@JsonProperty("name") val name: String,
@JsonProperty("infura") val infura: Boolean,
@JsonProperty("rpc") val rpc: String,
@JsonProperty("contract") val contract: String

)
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
package com.retypeme.project.chain

import org.springframework.boot.context.properties.ConfigurationProperties

@ConfigurationProperties(prefix = "w3")
class ChainSettings {
var chains: List<ChainItemConfig> = mutableListOf()
}
Original file line number Diff line number Diff line change
@@ -1,25 +1,16 @@
package com.retypeme.project.chain

import com.fasterxml.jackson.databind.ObjectMapper
import com.fasterxml.jackson.dataformat.yaml.YAMLFactory
import org.springframework.beans.factory.annotation.Value
import org.springframework.stereotype.Service

@Service
class ConfigReaderService {
val config: String =
"""
chains:
class ConfigReaderService(private val settings: ChainSettings) {

- id: 84532
name: Base Sepolia
rpc: "https://sepolia.base.org"
contract: "0xb4eb30e7f583d788a1611f4b7022bdda4bd4af81"
""".trimIndent()
@Value("\${contract.chains}")
private lateinit var contractChains: List<Int>

fun readChainConfig(): ChainConfig {
val objectMapper = ObjectMapper(YAMLFactory())
return objectMapper.readValue(config, ChainConfig::class.java)
return ChainConfig(settings.chains.filter { contractChains.contains(it.id) })
}

}
10 changes: 10 additions & 0 deletions backend/src/main/kotlin/com/retypeme/project/chain/ExtConfig.kt
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
package com.retypeme.project.chain

import org.springframework.boot.context.properties.EnableConfigurationProperties
import org.springframework.context.annotation.Configuration


@Configuration
@EnableConfigurationProperties(ChainSettings::class)
class ExtConfig {
}
4 changes: 2 additions & 2 deletions backend/src/main/resources/application-dev.yml
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ contract:
apikey:
infura: ENC(gRoG5skQ/eF7QPrux4WSukPwhscEnEe6tmxJyv3D0cAps7WY3cClcJwqN6W5vReyAaN9HceM0U2mEOffCSnM4gNKT/lzVI1RodU72xOomfw=)
chainstack: ENC(hmXkq30AsMWQVcRI/H1UVk6GIfnf2TvRy4V4YucYr0iJP7Uus4NI2+Y6YBcsyV1TjY8GZuF8uLNO5lGWME3gU/iyrLB0hhglfN9Rp/4yRlY=)
chains: 80002,168587773,534351,5611
chains: 84532

management:
endpoints:
Expand All @@ -13,4 +13,4 @@ management:

application:
cors:
allowed-origins: "http://localhost:3000,http://localhost:3001"
allowed-origins: "http://localhost:3000,http://localhost:3001"
31 changes: 0 additions & 31 deletions backend/src/main/resources/chains.yml

This file was deleted.

Original file line number Diff line number Diff line change
Expand Up @@ -10,14 +10,14 @@ import org.springframework.boot.test.context.SpringBootTest
@SpringBootTest
class BackendApplicationTest() {

@Autowired
lateinit var smartContractService: SmartContractService
@Autowired
lateinit var chainService: ChainService

@Test
fun testNetwork() {
Assertions.assertTrue(true)
}

// @Autowired
// lateinit var smartContractService: SmartContractService
// @Autowired
// lateinit var chainService: ChainService
//
// @Test
// fun testNetwork() {
// Assertions.assertTrue(true)
// }
//
}
Original file line number Diff line number Diff line change
Expand Up @@ -21,85 +21,85 @@ class SmartContractWrapperTest {
val mEth = BigInteger.valueOf(1000_000_000_000_000)//0.001 ether
val dEth = BigInteger.valueOf(100_000_000_000_000)//0.01 ether

companion object {
@JvmStatic
fun chainIdProvider() = listOf(80002, 534351, 5611, 168587773).stream()
}
// companion object {
// @JvmStatic
// fun chainIdProvider() = listOf(80002, 534351, 5611, 168587773).stream()
// }

// @ParameterizedTest
// @MethodSource("chainIdProvider")
fun testContractReady(chainId: Long) {

val chain = ConfigReaderService().readChainConfig().chains.first { it.id == chainId.toInt() }
val scAddress = chain.contract
val rpc = chain.rpc
val web3j = Web3jBuilder(apiInfuraKey, apiChainstackKey).getWeb3j(rpc, chainId)

val owner = SmartContractWrapper(scAddress, scOwnerPrivateKey, chainId, web3j)

// check owner address
val ownerAddress: String = owner.getOwner()
assertEquals(this.scOwnerAddress, ownerAddress.lowercase(), "Owner address is not correct")

// check balance
val fstBalance = web3j.ethGetBalance(fstPlayerAddress, LATEST).send().balance
val sndBalance = web3j.ethGetBalance(sndPlayerAddress, LATEST).send().balance

println("fstBalance mEth: ${fstBalance / mEth}")
println("sndBalance mEth: ${sndBalance / mEth}")
assertTrue(fstBalance > dEth, "Fst player balance is not enough")
assertTrue(sndBalance > dEth, "Snd player balance is not enough")
// print in game balance in mEth
println("fstInGameBalance mEth: ${owner.getInGameBalance(fstPlayerAddress) / mEth}")
println("sndInGameBalance mEth: ${owner.getInGameBalance(sndPlayerAddress) / mEth}")
// fun testContractReady(chainId: Long) {
//
// val chain = ConfigReaderService().readChainConfig().chains.first { it.id == chainId.toInt() }
// val scAddress = chain.contract
// val rpc = chain.rpc
// val web3j = Web3jBuilder(apiInfuraKey, apiChainstackKey).getWeb3j(rpc, chainId)
//
// val owner = SmartContractWrapper(scAddress, scOwnerPrivateKey, chainId, web3j)
//
// check owner address
// val ownerAddress: String = owner.getOwner()
// assertEquals(this.scOwnerAddress, ownerAddress.lowercase(), "Owner address is not correct")
//
// check balance
// val fstBalance = web3j.ethGetBalance(fstPlayerAddress, LATEST).send().balance
// val sndBalance = web3j.ethGetBalance(sndPlayerAddress, LATEST).send().balance
//
// println("fstBalance mEth: ${fstBalance / mEth}")
// println("sndBalance mEth: ${sndBalance / mEth}")
// assertTrue(fstBalance > dEth, "Fst player balance is not enough")
// assertTrue(sndBalance > dEth, "Snd player balance is not enough")
// print in game balance in mEth
// println("fstInGameBalance mEth: ${owner.getInGameBalance(fstPlayerAddress) / mEth}")
// println("sndInGameBalance mEth: ${owner.getInGameBalance(sndPlayerAddress) / mEth}")
}

// @ParameterizedTest
// @MethodSource("chainIdProvider")
fun testSmoke(chainId: Long) {

val chain = ConfigReaderService().readChainConfig().chains.first { it.id == chainId.toInt() }
val scAddress = chain.contract
val rpc = chain.rpc

val sessionId = BigInteger.valueOf((Math.random() * 1000_000).toLong())

val web3j = Web3jBuilder(apiInfuraKey, apiChainstackKey).getWeb3j(rpc, chainId)

val owner = SmartContractWrapper(scAddress, scOwnerPrivateKey, chainId, web3j)
val fstPlayer = SmartContractWrapper(scAddress, fstPlayerPrivateKey, chainId, web3j)
val sndPlayer = SmartContractWrapper(scAddress, sndPlayerPrivateKey, chainId, web3j)

val fstInGameBalance = fstPlayer.getInGameBalance(fstPlayerAddress)
val sndInGameBalance = sndPlayer.getInGameBalance(sndPlayerAddress)

// deposit 10x mEth if not enough
if (fstInGameBalance <= mEth) {
fstPlayer.deposit(dEth)
}
if (sndInGameBalance <= mEth) {
sndPlayer.deposit(dEth)
}

assertTrue(fstInGameBalance > mEth)
assertTrue(sndInGameBalance > mEth)

// join game
fstPlayer.joinGame(sessionId)
sndPlayer.joinGame(sessionId)

owner.endGame(sessionId, fstPlayerAddress)

val fstInGameBalanceAfter = fstPlayer.getInGameBalance(fstPlayerAddress)
val sndInGameBalanceAfter = sndPlayer.getInGameBalance(sndPlayerAddress)

println("fstInGameBalance: $fstInGameBalance")
println("sndInGameBalance: $sndInGameBalance")
println("sessionId: $sessionId")
println("fstInGameBalanceAfter: $fstInGameBalanceAfter")
println("sndInGameBalanceAfter: $sndInGameBalanceAfter")

assertTrue(fstInGameBalanceAfter > fstInGameBalance)
assertTrue(sndInGameBalanceAfter < sndInGameBalance)
}
// val chain = ConfigReaderService().readChainConfig().chains.first { it.id == chainId.toInt() }
// val scAddress = chain.contract
// val rpc = chain.rpc
//
// val sessionId = BigInteger.valueOf((Math.random() * 1000_000).toLong())
//
// val web3j = Web3jBuilder(apiInfuraKey, apiChainstackKey).getWeb3j(rpc, chainId)
//
// val owner = SmartContractWrapper(scAddress, scOwnerPrivateKey, chainId, web3j)
// val fstPlayer = SmartContractWrapper(scAddress, fstPlayerPrivateKey, chainId, web3j)
// val sndPlayer = SmartContractWrapper(scAddress, sndPlayerPrivateKey, chainId, web3j)
//
// val fstInGameBalance = fstPlayer.getInGameBalance(fstPlayerAddress)
// val sndInGameBalance = sndPlayer.getInGameBalance(sndPlayerAddress)
//
// deposit 10x mEth if not enough
// if (fstInGameBalance <= mEth) {
// fstPlayer.deposit(dEth)
// }
// if (sndInGameBalance <= mEth) {
// sndPlayer.deposit(dEth)
// }
//
// assertTrue(fstInGameBalance > mEth)
// assertTrue(sndInGameBalance > mEth)
//
// join game
// fstPlayer.joinGame(sessionId)
// sndPlayer.joinGame(sessionId)
//
// owner.endGame(sessionId, fstPlayerAddress)
//
// val fstInGameBalanceAfter = fstPlayer.getInGameBalance(fstPlayerAddress)
// val sndInGameBalanceAfter = sndPlayer.getInGameBalance(sndPlayerAddress)
//
// println("fstInGameBalance: $fstInGameBalance")
// println("sndInGameBalance: $sndInGameBalance")
// println("sessionId: $sessionId")
// println("fstInGameBalanceAfter: $fstInGameBalanceAfter")
// println("sndInGameBalanceAfter: $sndInGameBalanceAfter")
//
// assertTrue(fstInGameBalanceAfter > fstInGameBalance)
// assertTrue(sndInGameBalanceAfter < sndInGameBalance)
// }
}

0 comments on commit 305c8e7

Please sign in to comment.