mirror of
https://github.com/Instadapp/chains.git
synced 2024-07-29 22:37:19 +00:00
Refactor - prepare #121
This commit is contained in:
parent
87d90de9f4
commit
a43a9b2a4d
|
@ -1,117 +0,0 @@
|
|||
package org.ethereum.lists.chains
|
||||
|
||||
import com.beust.klaxon.JsonObject
|
||||
import com.beust.klaxon.Klaxon
|
||||
import com.squareup.moshi.Moshi
|
||||
import com.squareup.moshi.kotlin.reflect.KotlinJsonAdapterFactory
|
||||
import org.ethereum.lists.chains.model.Chain
|
||||
import org.kethereum.erc55.isValid
|
||||
import org.kethereum.model.Address
|
||||
import org.kethereum.rpc.HttpEthereumRPC
|
||||
import java.io.File
|
||||
|
||||
val mandatory_fields = listOf(
|
||||
"name",
|
||||
"shortName",
|
||||
"chain",
|
||||
"network",
|
||||
"chainId",
|
||||
"networkId",
|
||||
"rpc",
|
||||
"faucets",
|
||||
"infoURL",
|
||||
"nativeCurrency"
|
||||
)
|
||||
val optionalFields = listOf(
|
||||
"slip44",
|
||||
"ens"
|
||||
)
|
||||
|
||||
class FileNameMustMatchChainId : Exception("chainId must match the filename")
|
||||
class ExtensionMustBeJSON : Exception("filename extension must be json")
|
||||
class ShouldHaveNoExtraFields(fields: Set<String>) : Exception("should have no extra field $fields")
|
||||
class ShouldHaveNoMissingFields(fields: Set<String>) : Exception("missing field(s) $fields")
|
||||
class RPCMustBeList : Exception("rpc must be a list")
|
||||
class RPCMustBeListOfStrings : Exception("rpc must be a list of strings")
|
||||
class ENSMustBeObject: Exception("ens must be an object")
|
||||
class ENSMustHaveOnlyRegistry: Exception("ens can only have a registry currently")
|
||||
class ENSRegistryAddressMustBeValid: Exception("ens registry must have valid address")
|
||||
|
||||
fun checkChain(it: File, connectRPC: Boolean) {
|
||||
println("processing $it")
|
||||
|
||||
parseWithMoshi(it)
|
||||
|
||||
val jsonObject = Klaxon().parseJsonObject(it.reader())
|
||||
val chainAsLong = getNumber(jsonObject, "chainId")
|
||||
|
||||
if (chainAsLong != it.nameWithoutExtension.toLongOrNull()) {
|
||||
throw(FileNameMustMatchChainId())
|
||||
}
|
||||
|
||||
if (it.extension != "json") {
|
||||
throw(ExtensionMustBeJSON())
|
||||
}
|
||||
|
||||
getNumber(jsonObject, "networkId")
|
||||
|
||||
val extraFields = jsonObject.map.keys.subtract(mandatory_fields).subtract(optionalFields)
|
||||
if (extraFields.isNotEmpty()) {
|
||||
throw ShouldHaveNoExtraFields(extraFields)
|
||||
}
|
||||
|
||||
val missingFields = mandatory_fields.subtract(jsonObject.map.keys)
|
||||
if (missingFields.isNotEmpty()) {
|
||||
throw ShouldHaveNoMissingFields(missingFields)
|
||||
}
|
||||
|
||||
jsonObject["ens"]?.let {
|
||||
if (it !is JsonObject) {
|
||||
throw ENSMustBeObject()
|
||||
}
|
||||
if (it.keys != mutableSetOf("registry")) {
|
||||
throw ENSMustHaveOnlyRegistry()
|
||||
}
|
||||
|
||||
val address = Address(it["registry"] as String)
|
||||
if (!address.isValid()) {
|
||||
throw ENSRegistryAddressMustBeValid()
|
||||
}
|
||||
}
|
||||
|
||||
if (connectRPC) {
|
||||
if (jsonObject["rpc"] is List<*>) {
|
||||
(jsonObject["rpc"] as List<*>).forEach {
|
||||
if (it !is String) {
|
||||
throw(RPCMustBeListOfStrings())
|
||||
} else {
|
||||
println("connecting to $it")
|
||||
val ethereumRPC = HttpEthereumRPC(it)
|
||||
println("Client:" + ethereumRPC.clientVersion())
|
||||
println("BlockNumber:" + ethereumRPC.blockNumber())
|
||||
println("GasPrice:" + ethereumRPC.gasPrice())
|
||||
}
|
||||
}
|
||||
println()
|
||||
} else {
|
||||
throw(RPCMustBeList())
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
/*
|
||||
moshi fails for extra commas
|
||||
https://github.com/ethereum-lists/chains/issues/126
|
||||
*/
|
||||
private fun parseWithMoshi(fileToParse: File) {
|
||||
val moshi = Moshi.Builder().add(KotlinJsonAdapterFactory()).build()
|
||||
moshi.adapter(Chain::class.java).fromJson(fileToParse.readText())
|
||||
}
|
||||
|
||||
private fun getNumber(jsonObject: JsonObject, field: String): Long {
|
||||
return when (val chainId = jsonObject[field]) {
|
||||
is Int -> chainId.toLong()
|
||||
is Long -> chainId
|
||||
else -> throw(Exception("chain_id must be a number"))
|
||||
}
|
||||
}
|
23
src/main/kotlin/org/ethereum/lists/chains/Env.kt
Normal file
23
src/main/kotlin/org/ethereum/lists/chains/Env.kt
Normal file
|
@ -0,0 +1,23 @@
|
|||
package org.ethereum.lists.chains
|
||||
|
||||
import com.squareup.moshi.Moshi
|
||||
import com.squareup.moshi.kotlin.reflect.KotlinJsonAdapterFactory
|
||||
|
||||
val mandatory_fields = listOf(
|
||||
"name",
|
||||
"shortName",
|
||||
"chain",
|
||||
"network",
|
||||
"chainId",
|
||||
"networkId",
|
||||
"rpc",
|
||||
"faucets",
|
||||
"infoURL",
|
||||
"nativeCurrency"
|
||||
)
|
||||
val optionalFields = listOf(
|
||||
"slip44",
|
||||
"ens"
|
||||
)
|
||||
|
||||
val moshi: Moshi = Moshi.Builder().add(KotlinJsonAdapterFactory()).build()
|
|
@ -1,7 +1,12 @@
|
|||
package org.ethereum.lists.chains
|
||||
|
||||
import java.io.File
|
||||
|
||||
import com.beust.klaxon.JsonObject
|
||||
import com.beust.klaxon.Klaxon
|
||||
import org.ethereum.lists.chains.model.*
|
||||
import org.kethereum.erc55.isValid
|
||||
import org.kethereum.model.Address
|
||||
import org.kethereum.rpc.HttpEthereumRPC
|
||||
|
||||
fun main(args: Array<String>) {
|
||||
|
||||
|
@ -10,3 +15,81 @@ fun main(args: Array<String>) {
|
|||
}
|
||||
|
||||
}
|
||||
|
||||
fun checkChain(it: File, connectRPC: Boolean) {
|
||||
println("processing $it")
|
||||
|
||||
parseWithMoshi(it)
|
||||
|
||||
val jsonObject = Klaxon().parseJsonObject(it.reader())
|
||||
val chainAsLong = getNumber(jsonObject, "chainId")
|
||||
|
||||
if (chainAsLong != it.nameWithoutExtension.toLongOrNull()) {
|
||||
throw(FileNameMustMatchChainId())
|
||||
}
|
||||
|
||||
if (it.extension != "json") {
|
||||
throw(ExtensionMustBeJSON())
|
||||
}
|
||||
|
||||
getNumber(jsonObject, "networkId")
|
||||
|
||||
val extraFields = jsonObject.map.keys.subtract(mandatory_fields).subtract(optionalFields)
|
||||
if (extraFields.isNotEmpty()) {
|
||||
throw ShouldHaveNoExtraFields(extraFields)
|
||||
}
|
||||
|
||||
val missingFields = mandatory_fields.subtract(jsonObject.map.keys)
|
||||
if (missingFields.isNotEmpty()) {
|
||||
throw ShouldHaveNoMissingFields(missingFields)
|
||||
}
|
||||
|
||||
jsonObject["ens"]?.let {
|
||||
if (it !is JsonObject) {
|
||||
throw ENSMustBeObject()
|
||||
}
|
||||
if (it.keys != mutableSetOf("registry")) {
|
||||
throw ENSMustHaveOnlyRegistry()
|
||||
}
|
||||
|
||||
val address = Address(it["registry"] as String)
|
||||
if (!address.isValid()) {
|
||||
throw ENSRegistryAddressMustBeValid()
|
||||
}
|
||||
}
|
||||
|
||||
if (connectRPC) {
|
||||
if (jsonObject["rpc"] is List<*>) {
|
||||
(jsonObject["rpc"] as List<*>).forEach {
|
||||
if (it !is String) {
|
||||
throw(RPCMustBeListOfStrings())
|
||||
} else {
|
||||
println("connecting to $it")
|
||||
val ethereumRPC = HttpEthereumRPC(it)
|
||||
println("Client:" + ethereumRPC.clientVersion())
|
||||
println("BlockNumber:" + ethereumRPC.blockNumber())
|
||||
println("GasPrice:" + ethereumRPC.gasPrice())
|
||||
}
|
||||
}
|
||||
println()
|
||||
} else {
|
||||
throw(RPCMustBeList())
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
/*
|
||||
moshi fails for extra commas
|
||||
https://github.com/ethereum-lists/chains/issues/126
|
||||
*/
|
||||
private fun parseWithMoshi(fileToParse: File) {
|
||||
moshi.adapter(Chain::class.java).fromJson(fileToParse.readText())
|
||||
}
|
||||
|
||||
private fun getNumber(jsonObject: JsonObject, field: String): Long {
|
||||
return when (val chainId = jsonObject[field]) {
|
||||
is Int -> chainId.toLong()
|
||||
is Long -> chainId
|
||||
else -> throw(Exception("chain_id must be a number"))
|
||||
}
|
||||
}
|
|
@ -0,0 +1,11 @@
|
|||
package org.ethereum.lists.chains.model
|
||||
|
||||
class FileNameMustMatchChainId : Exception("chainId must match the filename")
|
||||
class ExtensionMustBeJSON : Exception("filename extension must be json")
|
||||
class ShouldHaveNoExtraFields(fields: Set<String>) : Exception("should have no extra field $fields")
|
||||
class ShouldHaveNoMissingFields(fields: Set<String>) : Exception("missing field(s) $fields")
|
||||
class RPCMustBeList : Exception("rpc must be a list")
|
||||
class RPCMustBeListOfStrings : Exception("rpc must be a list of strings")
|
||||
class ENSMustBeObject: Exception("ens must be an object")
|
||||
class ENSMustHaveOnlyRegistry: Exception("ens can only have a registry currently")
|
||||
class ENSRegistryAddressMustBeValid: Exception("ens registry must have valid address")
|
|
@ -1,5 +1,6 @@
|
|||
import com.squareup.moshi.JsonEncodingException
|
||||
import org.ethereum.lists.chains.*
|
||||
import org.ethereum.lists.chains.model.*
|
||||
import org.junit.Test
|
||||
import java.io.File
|
||||
|
||||
|
|
Loading…
Reference in New Issue
Block a user