mirror of
https://github.com/Instadapp/chains.git
synced 2024-07-29 22:37:19 +00:00
Enforce unique names and shortNames
This commit is contained in:
parent
a43a9b2a4d
commit
fc8a0f9bd8
|
@ -1,7 +1,9 @@
|
||||||
package org.ethereum.lists.chains
|
package org.ethereum.lists.chains
|
||||||
|
|
||||||
|
import com.squareup.moshi.JsonAdapter
|
||||||
import com.squareup.moshi.Moshi
|
import com.squareup.moshi.Moshi
|
||||||
import com.squareup.moshi.kotlin.reflect.KotlinJsonAdapterFactory
|
import com.squareup.moshi.kotlin.reflect.KotlinJsonAdapterFactory
|
||||||
|
import org.ethereum.lists.chains.model.Chain
|
||||||
|
|
||||||
val mandatory_fields = listOf(
|
val mandatory_fields = listOf(
|
||||||
"name",
|
"name",
|
||||||
|
@ -21,3 +23,4 @@ val optionalFields = listOf(
|
||||||
)
|
)
|
||||||
|
|
||||||
val moshi: Moshi = Moshi.Builder().add(KotlinJsonAdapterFactory()).build()
|
val moshi: Moshi = Moshi.Builder().add(KotlinJsonAdapterFactory()).build()
|
||||||
|
val chainAdapter: JsonAdapter<Chain> = moshi.adapter(Chain::class.java)
|
||||||
|
|
|
@ -8,6 +8,9 @@ import org.kethereum.erc55.isValid
|
||||||
import org.kethereum.model.Address
|
import org.kethereum.model.Address
|
||||||
import org.kethereum.rpc.HttpEthereumRPC
|
import org.kethereum.rpc.HttpEthereumRPC
|
||||||
|
|
||||||
|
val parsedShortNames= mutableSetOf<String>()
|
||||||
|
val parsedNames= mutableSetOf<String>()
|
||||||
|
|
||||||
fun main(args: Array<String>) {
|
fun main(args: Array<String>) {
|
||||||
|
|
||||||
File("_data/chains").listFiles()?.forEach {
|
File("_data/chains").listFiles()?.forEach {
|
||||||
|
@ -83,7 +86,16 @@ moshi fails for extra commas
|
||||||
https://github.com/ethereum-lists/chains/issues/126
|
https://github.com/ethereum-lists/chains/issues/126
|
||||||
*/
|
*/
|
||||||
private fun parseWithMoshi(fileToParse: File) {
|
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 {
|
private fun getNumber(jsonObject: JsonObject, field: String): Long {
|
||||||
|
|
|
@ -9,3 +9,5 @@ class RPCMustBeListOfStrings : Exception("rpc must be a list of strings")
|
||||||
class ENSMustBeObject: Exception("ens must be an object")
|
class ENSMustBeObject: Exception("ens must be an object")
|
||||||
class ENSMustHaveOnlyRegistry: Exception("ens can only have a registry currently")
|
class ENSMustHaveOnlyRegistry: Exception("ens can only have a registry currently")
|
||||||
class ENSRegistryAddressMustBeValid: Exception("ens registry must have valid address")
|
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")
|
|
@ -1,11 +1,18 @@
|
||||||
import com.squareup.moshi.JsonEncodingException
|
import com.squareup.moshi.JsonEncodingException
|
||||||
import org.ethereum.lists.chains.*
|
import org.ethereum.lists.chains.*
|
||||||
import org.ethereum.lists.chains.model.*
|
import org.ethereum.lists.chains.model.*
|
||||||
|
import org.junit.Before
|
||||||
import org.junit.Test
|
import org.junit.Test
|
||||||
import java.io.File
|
import java.io.File
|
||||||
|
|
||||||
class TheChainChecker {
|
class TheChainChecker {
|
||||||
|
|
||||||
|
@Before
|
||||||
|
fun setup() {
|
||||||
|
parsedNames.clear()
|
||||||
|
parsedShortNames.clear()
|
||||||
|
}
|
||||||
|
|
||||||
@Test
|
@Test
|
||||||
fun shouldPassForValidChain() {
|
fun shouldPassForValidChain() {
|
||||||
val file = getFile("valid/1.json")
|
val file = getFile("valid/1.json")
|
||||||
|
@ -84,6 +91,23 @@ class TheChainChecker {
|
||||||
checkChain(file, false)
|
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)
|
private fun getFile(s: String) = File(javaClass.classLoader.getResource("test_chains/$s").file)
|
||||||
|
|
||||||
|
|
14
src/test/resources/test_chains/invalid/sameshortname/1.json
Normal file
14
src/test/resources/test_chains/invalid/sameshortname/1.json
Normal file
|
@ -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"
|
||||||
|
}
|
26
src/test/resources/test_chains/invalid/sameshortname/5.json
Normal file
26
src/test/resources/test_chains/invalid/sameshortname/5.json
Normal file
|
@ -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"
|
||||||
|
}
|
||||||
|
}
|
Loading…
Reference in New Issue
Block a user