Skip to content

Commit

Permalink
write most tests. stuck on weird bug
Browse files Browse the repository at this point in the history
  • Loading branch information
yogurtandjam committed May 29, 2024
1 parent e593757 commit fb4b822
Show file tree
Hide file tree
Showing 4 changed files with 664 additions and 9 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ internal class SkipProcessor(parser: ParserProtocol) : BaseProcessor(parser), IR
// actual type of the tokens payload is Map<str, Map<str, List<Map<str, Any>>>>
override var tokens: List<Any>? = null

private var skipTokens: Map<String, Map<String, List<Map<String, Any>>>>? = null
var skipTokens: Map<String, Map<String, List<Map<String, Any>>>>? = null
override var exchangeDestinationChainId: String? = null
val sharedRouterProcessor = SharedRouterProcessor(parser)

Expand Down Expand Up @@ -60,6 +60,7 @@ internal class SkipProcessor(parser: ParserProtocol) : BaseProcessor(parser), IR
}

val chainToAssetsMap = payload["chain_to_assets_map"] as Map<String, Map<String, List<Map<String, Any>>>>?

var modified = mutableMapOf<String, Any>()
existing?.let {
modified = it.mutable()
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -202,7 +202,7 @@ class V4SquidTests : V4BaseTests() {
val stateChange = perp.routerTokens(mock.squidTokensMock.payload)
assertNotNull(stateChange)

assertTrue(perp.squidProcessor.selectedTokenSymbol("0xEeeeeEeeeEeEeeEeEeEeeEEEeeeeEeeeeeeeEEeE") == "ETH")
assertTrue(perp.squidProcessor.selectedTokenSymbol("0xEeeeeEeeeEeEeeEeEeEeeEEEeeeeEeeeeeeeEEeE", "should_not_matter") == "ETH")
}

@Test
Expand All @@ -212,7 +212,7 @@ class V4SquidTests : V4BaseTests() {
val stateChange = perp.routerTokens(mock.squidTokensMock.payload)
assertNotNull(stateChange)

assertTrue(perp.squidProcessor.selectedTokenDecimals("0xEeeeeEeeeEeEeeEeEeEeeEEEeeeeEeeeeeeeEEeE") == "18")
assertTrue(perp.squidProcessor.selectedTokenDecimals("0xEeeeeEeeeEeEeeEeEeEeeEEEeeeeEeeeeeeeEEeE", "should_not_matter") == "18")
}

@Test
Expand Down
Original file line number Diff line number Diff line change
@@ -1,27 +1,103 @@
package exchange.dydx.abacus.processor.router.skip
import exchange.dydx.abacus.tests.payloads.SkipChainsMock
import exchange.dydx.abacus.tests.payloads.SkipTokensMock
import exchange.dydx.abacus.utils.Parser
import kotlinx.serialization.json.Json
import kotlinx.serialization.json.jsonObject
import kotlinx.serialization.json.*
import kotlin.test.BeforeTest
import kotlin.test.Test
import kotlin.test.assertEquals

internal fun templateToJson(template: String): Map<String, Any> {

return Json.parseToJsonElement(template.trimIndent()).jsonObject.toMap()
}

class SkipProcessorTests {

internal val skipProcessor = SkipProcessor(parser = Parser())
internal val skipChainsMock = SkipChainsMock()
internal val skipTokensMock = SkipTokensMock()
internal val selectedChainId = "osmosis-1"
internal val selectedTokenAddress = "selectedTokenDenom"
internal val selectedTokenSymbol = "selectedTokenSymbol"
internal val selectedTokenDecimals = "15"
internal val selectedChainAssets = listOf(
mapOf(
"denom" to selectedTokenAddress,
"symbol" to selectedTokenSymbol,
"decimals" to selectedTokenDecimals,
),
mapOf(
"denom" to "testTokenKeyValue2",
"symbol" to "ARB",
"decimals" to 8
),
mapOf(
"denom" to "testTokenKeyValue3",
"symbol" to "ETH",
"decimals" to 5
),
)

/**
* Adds tokens to the skipProcessor instance
* The test tokens fixture looks like this:
* {
* "osmosis-1": {
* "assets": [
* {
* "denom": "selectedTokenDenom",
* "symbol": "selectedTokenSymbol",
* "decimals": 15
* },
* {
* "denom": "testTokenKeyValue2",
* "symbol": "ARB",
* "decimals": 8
* },
* {
* "denom": "testTokenKeyValue3"
* "symbol": "ETH",
* "decimals": 5
* }
* ]
* },
* "dont-select": {
* "assets": [
* {"denom": "shouldNotBeSelectedValue1"},
* {"denom": "shouldNotBeSelectedValue2"},
* {"denom": "shouldNotBeSelectedValue3"}
* ]
* }
* }
*
* This makes it easy to know what the filteredTokens output should be
* which in turn helps us know the results of the funs that depend on it.
*/
internal fun addTokens() {
skipProcessor.skipTokens = mapOf(
selectedChainId to mapOf("assets" to selectedChainAssets),
"dont-select" to mapOf("assets" to listOf(
mapOf("shouldNotBeSelected1" to "shouldNotBeSelectedValue1"),
mapOf("shouldNotBeSelected2" to "shouldNotBeSelectedValue2"),
mapOf("shouldNotBeSelected3" to "shouldNotBeSelectedValue3"),
))
)
}

@BeforeTest
internal fun setUp() {
addTokens()
}

@Test
fun testReceivedChains() {
val payload = templateToJson(
skipChainsMock.payload,
)
val modified = skipProcessor.receivedChains(
existing = mapOf(),
payload = templateToJson(
skipChainsMock.payload,
),
payload = payload,
)

val expected = mapOf(
Expand All @@ -46,6 +122,121 @@ class SkipProcessorTests {
),
),
)
assertEquals(modified, expected)
assertEquals(expected, modified)
assertEquals(payload["chains"], skipProcessor.chains)
}

@Test
fun testFilteredTokens() {
val result = skipProcessor.filteredTokens(chainId = selectedChainId)
val expected = selectedChainAssets
assertEquals(expected, result)
}

@Test
fun testSelectedTokenSymbol() {
val result = skipProcessor.selectedTokenSymbol(tokenAddress = selectedTokenAddress, selectedChainId = selectedChainId)
val expected = selectedTokenSymbol
assertEquals(expected, result)
}

@Test
fun testSelectedTokenDecimals() {
val result = skipProcessor.selectedTokenDecimals(tokenAddress = selectedTokenAddress, selectedChainId = selectedChainId)
val expected = selectedTokenDecimals
assertEquals(expected, result)
}

@Test
fun testDefaultTokenAddress() {
val result = skipProcessor.defaultTokenAddress(selectedChainId)
val expected = selectedTokenAddress
assertEquals(expected, result)
}

@Test
fun testTokenResources() {
// TODO: implement
}

@Test
fun testTokenOptions() {
// TODO: implement
}

@Test
fun testReceivedTokens() {
val payload = templateToJson(skipTokensMock.payload)
skipProcessor.skipTokens = null
skipProcessor.chains = listOf(
mapOf(
"chain_name" to "aura",
"chain_id" to "1",
"pfm_enabled" to false,
"supports_memo" to true,
"logo_uri" to "https ://raw.githubusercontent.com/chainapsis/keplr-chain-registry/main/images/xstaxy/chain.png",
"bech32_prefix" to "aura",
"chain_type" to "cosmos",
"is_testnet" to false
),
)

val modified = skipProcessor.receivedTokens(
existing = mapOf(),
payload = payload
)

val assets = mapOf(
"assets" to listOf(
mapOf(
"stringKey" to "Euro Coin",
"type" to "0x1aBaEA1f7C830bD89Acc67eC4af516284b1bC33c",
"iconUrl" to "https://raw.githubusercontent.com/axelarnetwork/axelar-configs/main/images/tokens/euroc.svg"
),
mapOf(
"stringKey" to "Real Yield USD",
"type" to "0x97e6E0a40a3D02F12d1cEC30ebfbAE04e37C119E",
"iconUrl" to "https://raw.githubusercontent.com/axelarnetwork/axelar-configs/main/images/tokens/yieldusd.svg",
),
mapOf(
"stringKey" to "Umee native token",
"type" to "0x923e030f951A2401426a3407a9bcc7EB715d9a0b",
"iconUrl" to "https://raw.githubusercontent.com/axelarnetwork/axelar-configs/main/images/tokens/umee.svg",
)
)
)
val expected = mapOf(
"transfer" to mapOf(
"depositOptions" to assets,
"withdrawalOptions" to assets,
"token" to "0x97e6E0a40a3D02F12d1cEC30ebfbAE04e37C119E",
"resources" to mapOf(
"tokenResources" to mapOf(
"0x97e6E0a40a3D02F12d1cEC30ebfbAE04e37C119E" to mapOf(
"name" to "Real Yield USD",
"address" to "0x97e6E0a40a3D02F12d1cEC30ebfbAE04e37C119E",
"symbol" to "YieldUSD",
"decimals" to 18,
"iconUrl" to "https://raw.githubusercontent.com/axelarnetwork/axelar-configs/main/images/tokens/yieldusd.svg"
),
"0x1aBaEA1f7C830bD89Acc67eC4af516284b1bC33c" to mapOf(
"name" to "Euro Coin",
"address" to "0x1aBaEA1f7C830bD89Acc67eC4af516284b1bC33c",
"symbol" to "EUROC",
"decimals" to 6,
"iconUrl" to "https://raw.githubusercontent.com/axelarnetwork/axelar-configs/main/images/tokens/euroc.svg"
),
"0x923e030f951A2401426a3407a9bcc7EB715d9a0b" to mapOf(
"name" to "Umee native token",
"address" to "0x923e030f951A2401426a3407a9bcc7EB715d9a0b",
"symbol" to "UMEE",
"decimals" to 6,
"iconUrl" to "https://raw.githubusercontent.com/axelarnetwork/axelar-configs/main/images/tokens/umee.svg"
)
)
)
)
)
assertEquals(expected, modified)
}
}
Loading

0 comments on commit fb4b822

Please sign in to comment.