diff --git a/src/main/kotlin/org/ethereum/lists/chains/Env.kt b/src/main/kotlin/org/ethereum/lists/chains/Env.kt index 2d8824d9..f96c8d02 100644 --- a/src/main/kotlin/org/ethereum/lists/chains/Env.kt +++ b/src/main/kotlin/org/ethereum/lists/chains/Env.kt @@ -1,7 +1,9 @@ package org.ethereum.lists.chains +import com.squareup.moshi.JsonAdapter import com.squareup.moshi.Moshi import com.squareup.moshi.kotlin.reflect.KotlinJsonAdapterFactory +import org.ethereum.lists.chains.model.Chain val mandatory_fields = listOf( "name", @@ -21,3 +23,4 @@ val optionalFields = listOf( ) val moshi: Moshi = Moshi.Builder().add(KotlinJsonAdapterFactory()).build() +val chainAdapter: JsonAdapter = moshi.adapter(Chain::class.java) diff --git a/src/main/kotlin/org/ethereum/lists/chains/Main.kt b/src/main/kotlin/org/ethereum/lists/chains/Main.kt index 952e2e9b..aafde6bd 100644 --- a/src/main/kotlin/org/ethereum/lists/chains/Main.kt +++ b/src/main/kotlin/org/ethereum/lists/chains/Main.kt @@ -8,6 +8,9 @@ import org.kethereum.erc55.isValid import org.kethereum.model.Address import org.kethereum.rpc.HttpEthereumRPC +val parsedShortNames= mutableSetOf() +val parsedNames= mutableSetOf() + fun main(args: Array) { File("_data/chains").listFiles()?.forEach { @@ -83,7 +86,16 @@ 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()) + val parsedChain = chainAdapter.fromJson(fileToParse.readText()) + if (parsedNames.contains(parsedChain!!.name)) { + throw NameMustBeUnique(parsedChain.name) + } + parsedNames.add(parsedChain.name) + + if (parsedShortNames.contains(parsedChain.shortName)) { + throw ShortNameMustBeUnique(parsedChain.shortName) + } + parsedShortNames.add(parsedChain.shortName) } private fun getNumber(jsonObject: JsonObject, field: String): Long { diff --git a/src/main/kotlin/org/ethereum/lists/chains/model/Exceptions.kt b/src/main/kotlin/org/ethereum/lists/chains/model/Exceptions.kt index e02fc556..39b89769 100644 --- a/src/main/kotlin/org/ethereum/lists/chains/model/Exceptions.kt +++ b/src/main/kotlin/org/ethereum/lists/chains/model/Exceptions.kt @@ -9,3 +9,5 @@ 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") +class NameMustBeUnique(dup: String): Exception(" name must be unique - but found `$dup` more than once") +class ShortNameMustBeUnique(dup: String): Exception("short name must be unique - but found `$dup` more than once") \ No newline at end of file diff --git a/src/test/kotlin/TheChainChecker.kt b/src/test/kotlin/TheChainChecker.kt index c1c2ccf6..8f8b9910 100644 --- a/src/test/kotlin/TheChainChecker.kt +++ b/src/test/kotlin/TheChainChecker.kt @@ -1,11 +1,18 @@ import com.squareup.moshi.JsonEncodingException import org.ethereum.lists.chains.* import org.ethereum.lists.chains.model.* +import org.junit.Before import org.junit.Test import java.io.File class TheChainChecker { + @Before + fun setup() { + parsedNames.clear() + parsedShortNames.clear() + } + @Test fun shouldPassForValidChain() { val file = getFile("valid/1.json") @@ -84,6 +91,23 @@ class TheChainChecker { checkChain(file, false) } + @Test(expected = NameMustBeUnique::class) + fun shouldFailOnNonUniqueName() { + checkChain(getFile("valid/1.json"), false) + checkChain(getFile("valid/1.json"), false) + } + + @Test(expected = ShortNameMustBeUnique::class) + fun shouldFailOnNonUniqueShortName() { + checkChain(getFile("invalid/sameshortname/5.json"), false) + checkChain(getFile("invalid/sameshortname/1.json"), false) + } + + @Test + fun canParse2chains() { + checkChain(getFile("valid/1.json"), false) + checkChain(getFile("valid/5.json"), false) + } private fun getFile(s: String) = File(javaClass.classLoader.getResource("test_chains/$s").file) diff --git a/src/test/resources/test_chains/invalid/sameshortname/1.json b/src/test/resources/test_chains/invalid/sameshortname/1.json new file mode 100644 index 00000000..d3f9eee1 --- /dev/null +++ b/src/test/resources/test_chains/invalid/sameshortname/1.json @@ -0,0 +1,14 @@ +{ + "name": "Ethereum Mainnet", + "shortName": "willfail", + "chain": "ETH", + "network": "mainnet", + "chainId": 1, + "networkId": 1, + "rpc": [ + "https://mainnet.infura.io/v3/${INFURA_API_KEY}", + "https://api.mycryptoapi.com/eth" + ], + "faucets": [], + "infoURL": "https://ethereum.org" +} diff --git a/src/test/resources/test_chains/invalid/sameshortname/5.json b/src/test/resources/test_chains/invalid/sameshortname/5.json new file mode 100644 index 00000000..d479df95 --- /dev/null +++ b/src/test/resources/test_chains/invalid/sameshortname/5.json @@ -0,0 +1,26 @@ +{ + "name": "Ethereum Testnet Görli", + "chain": "ETH", + "network": "goerli", + "rpc": [ + "https://rpc.goerli.mudit.blog/", + "https://rpc.slock.it/goerli ", + "https://goerli.prylabs.net" + ], + "faucets": [ + "https://goerli-faucet.slock.it/?address=${ADDRESS}", + "https://faucet.goerli.mudit.blog" + ], + "nativeCurrency": { + "name": "Görli Ether", + "symbol": "GOR", + "decimals": 18 + }, + "infoURL": "https://goerli.net/#about", + "shortName": "willfail", + "chainId": 5, + "networkId": 5, + "ens": { + "registry":"0x112234455c3a32fd11230c42e7bccd4a84e02010" + } +}