Skip to content

Commit

Permalink
simplified swap pool fetching
Browse files Browse the repository at this point in the history
  • Loading branch information
Joey Harward committed May 13, 2021
1 parent f0f3749 commit bc1699e
Show file tree
Hide file tree
Showing 4 changed files with 57 additions and 34 deletions.
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,7 @@ Then add the following dependency to your module's build.gradle
```gradle
dependencies {
...
implementation "com.metallicus:protonsdk:1.0.2"
implementation "com.metallicus:protonsdk:{latest_version}"
}
```

Expand Down
37 changes: 19 additions & 18 deletions protonsdk/src/main/java/com/metallicus/protonsdk/Proton.kt
Original file line number Diff line number Diff line change
Expand Up @@ -918,7 +918,7 @@ class Proton private constructor(context: Context) {
}
}

fun getSwapPoolMap(): LiveData<Resource<Map<String, SwapPoolMapEntry>>> = liveData {
fun getSwapPools(): LiveData<Resource<SwapPoolData>> = liveData {
emit(Resource.loading())

try {
Expand Down Expand Up @@ -954,7 +954,8 @@ class Proton private constructor(context: Context) {
)
when (tableRowsResource.status) {
Status.SUCCESS -> {
val poolMap = mutableMapOf<String, SwapPool>()
val swapPools = mutableListOf<SwapPool>()
val swapPoolTokens = mutableListOf<TokenCurrencyBalance>()

val gson = Gson()

Expand All @@ -965,26 +966,26 @@ class Proton private constructor(context: Context) {

val swapPool = gson.fromJson(swapPoolJsonObject, SwapPool::class.java)

poolMap[swapPool.getPool1Symbol()] = swapPool
}
}
val pool1 = "${swapPool.getPool1Contract()}:${swapPool.getPool1Symbol()}"
val pool2 = "${swapPool.getPool2Contract()}:${swapPool.getPool2Symbol()}"

val poolGraph = mutableMapOf<String, SwapPoolMapEntry>()
tokenCurrencyBalancesMap[pool1]?.let { tokenCurrencyBalance ->
swapPoolTokens.add(tokenCurrencyBalance)
}
tokenCurrencyBalancesMap[pool2]?.let { tokenCurrencyBalance ->
swapPoolTokens.add(tokenCurrencyBalance)
}

poolMap.forEach {
val symbol = it.key
val rate = it.value.getPool1Rate()
val contract = it.value.getPool1Contract()

tokenCurrencyBalancesMap["$contract:$symbol"]?.let { tokenCurrencyBalance ->
poolGraph[symbol] = SwapPoolMapEntry(rate, tokenCurrencyBalance)
swapPools.add(swapPool)
}
}

emit(Resource.success(poolGraph))
val swapPoolData = SwapPoolData(swapPools, swapPoolTokens.distinct())

emit(Resource.success(swapPoolData))
}
Status.ERROR -> {
val error: Resource<Map<String, SwapPoolMapEntry>> =
val error: Resource<SwapPoolData> =
Resource.error(tableRowsResource.message.orEmpty(), tableRowsResource.code ?: -1)
emit(error)
}
Expand All @@ -993,17 +994,17 @@ class Proton private constructor(context: Context) {
}
}
Status.ERROR -> {
val error: Resource<Map<String, SwapPoolMapEntry>> =
val error: Resource<SwapPoolData> =
Resource.error(tokenBalancesResource.message.orEmpty(), tokenBalancesResource.code ?: -1)
emit(error)
}
Status.LOADING -> { }
}
} catch (e: ProtonException) {
val error: Resource<Map<String, SwapPoolMapEntry>> = Resource.error(e)
val error: Resource<SwapPoolData> = Resource.error(e)
emit(error)
} catch (e: Exception) {
val error: Resource<Map<String, SwapPoolMapEntry>> = Resource.error(e.localizedMessage.orEmpty())
val error: Resource<SwapPoolData> = Resource.error(e.localizedMessage.orEmpty())
emit(error)
}
}
Expand Down
48 changes: 33 additions & 15 deletions protonsdk/src/main/java/com/metallicus/protonsdk/model/SwapPool.kt
Original file line number Diff line number Diff line change
Expand Up @@ -27,21 +27,13 @@ data class SwapPool(
@SerializedName("lt_symbol") val symbol: String,
@SerializedName("creator") val creator: String,
@SerializedName("memo") val memo: String,
@SerializedName("pool1") val pool1: SwapPoolAsset,
@SerializedName("pool2") val pool2: SwapPoolAsset,
@SerializedName("pool1") var pool1: SwapPoolAsset,
@SerializedName("pool2") var pool2: SwapPoolAsset,
@SerializedName("hash") val hash: String,
@SerializedName("fee") val fee: SwapPoolFee,
@SerializedName("active") val active: Int,
@SerializedName("reserved") val reserved: Int
) {
fun getPool1Symbol(): String {
return memo.split("<>")[0]
}

fun getPool2Symbol(): String {
return memo.split("<>")[1]
}

fun getPool1Amount(): Double {
return pool1.quantity.substringBefore(" ").toDouble()
}
Expand All @@ -50,13 +42,39 @@ data class SwapPool(
return pool2.quantity.substringBefore(" ").toDouble()
}

fun getPool1Rate(): Double {
return getPool1Amount() / getPool2Amount()
fun getPool1Symbol(): String {
return pool1.quantity.substringAfter(" ")
}

fun getPool2Symbol(): String {
return pool2.quantity.substringAfter(" ")
}

fun getPool1Contract(): String {
return pool1.contract
}

fun getPool2Contract(): String {
return pool2.contract
}

fun getFee(): Int {
return fee.exchangeFee.toInt()
}

fun deepCopy(): SwapPool {
return SwapPool(
symbol,
creator,
memo,
SwapPoolAsset(pool1.quantity, pool1.contract),
SwapPoolAsset(pool2.quantity, pool2.contract),
hash,
SwapPoolFee(fee.exchangeFee, fee.addLiquidityFee, fee.removeLiquidityFee),
active,
reserved
)
}
}

data class SwapPoolAsset(
Expand All @@ -70,7 +88,7 @@ data class SwapPoolFee(
@SerializedName("remove_liquidity_fee") val removeLiquidityFee: Long
)

data class SwapPoolMapEntry(
val rate: Double,
val tokenCurrencyBalance: TokenCurrencyBalance
data class SwapPoolData(
val swapPools: List<SwapPool>,
val swapPoolTokens: List<TokenCurrencyBalance>
)
4 changes: 4 additions & 0 deletions protonsdk/src/main/res/values/strings.xml
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,10 @@
<string name="kycProvidersTableCode" translatable="false">eosio.proton</string>
<string name="kycProvidersTableName" translatable="false">kycproviders</string>

<string name="protonSwapPoolsTableScope" translatable="false">proton.swaps</string>
<string name="protonSwapPoolsTableCode" translatable="false">proton.swaps</string>
<string name="protonSwapPoolsTableName" translatable="false">pools</string>

<string name="secureStorageKeyPairAlreadyExists">KeyPair already exists!</string>
<string name="secureStorageKeyPairDoesNotExist">KeyPair does not exist in Keystore</string>
<string name="secureStorageEncryptionError">Problem during encryption</string>
Expand Down

0 comments on commit bc1699e

Please sign in to comment.