Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

swap #15

Open
wants to merge 6 commits into
base: main
Choose a base branch
from
Open

swap #15

Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion .idea/.name

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 1 addition & 0 deletions .idea/gradle.xml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 1 addition & 0 deletions .idea/misc.xml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 1 addition & 0 deletions apps/wallet/data/stonfi/.gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
/build
35 changes: 35 additions & 0 deletions apps/wallet/data/stonfi/build.gradle.kts
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
plugins {
id("com.android.library")
id("org.jetbrains.kotlin.android")
id("kotlin-parcelize")
}

android {
namespace = Build.namespacePrefix("wallet.data.stonfi")
compileSdk = Build.compileSdkVersion

defaultConfig {
minSdk = Build.minSdkVersion
consumerProguardFiles("consumer-rules.pro")
}

compileOptions {
sourceCompatibility = JavaVersion.VERSION_1_8
targetCompatibility = JavaVersion.VERSION_1_8
}
kotlinOptions {
jvmTarget = "1.8"
}
}

dependencies {
implementation(Dependence.KotlinX.coroutines)
implementation(Dependence.Koin.core)
implementation(Dependence.Squareup.okhttp)
implementation(Dependence.Squareup.logger)
implementation(Dependence.Squareup.retrofit)
implementation(Dependence.Squareup.moshi)
implementation(Dependence.Squareup.moshiAdapters)
implementation(Dependence.Squareup.retrofitMoshiAdapter)
implementation(project(Dependence.Lib.extensions))
}
1 change: 1 addition & 0 deletions apps/wallet/data/stonfi/src/main/AndroidManifest.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
<manifest />
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
package com.tonapps.wallet.data.stonfi

import org.koin.dsl.module

val stonfiModule = module {
single { StonfiRepository(get()) }
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,44 @@
package com.tonapps.wallet.data.stonfi

import android.content.Context
import com.tonapps.wallet.data.stonfi.api.StonfiApi
import com.tonapps.wallet.data.stonfi.entities.StonfiAsset
import com.tonapps.wallet.data.stonfi.entities.StonfiAssetResponse
import com.tonapps.wallet.data.stonfi.entities.StonfiSimulate
import retrofit2.http.Query

class StonfiRepository(
context: Context
) {
private val stonfiApi: StonfiApi = StonfiApi.provideApi(context)
private var cacheAssets: List<StonfiAsset>? = null
private var cachedPairs: Map<String, List<String>>? = null

suspend fun assets(
): List<StonfiAsset> {
return cacheAssets ?: loadAssets()
}

suspend fun pairs(): Map<String, List<String>> {
return cachedPairs ?: loadPairs()
}

private suspend fun loadPairs(): Map<String, List<String>> {
val pairs = stonfiApi.pairs().getPairs()
cachedPairs = pairs
return pairs
}

private suspend fun loadAssets(): List<StonfiAsset> {
val assets = stonfiApi.assets().assets.filter { it.defaultSymbol }
cacheAssets = assets
return assets
}

suspend fun simulate(offersAddress: String,
askAddress: String,
units: String,
slippageTolerance: String): StonfiSimulate {
return stonfiApi.simulate(offersAddress, askAddress, units, slippageTolerance)
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,58 @@
package com.tonapps.wallet.data.stonfi.api

import android.content.Context
import com.squareup.moshi.Moshi
import com.squareup.moshi.kotlin.reflect.KotlinJsonAdapterFactory
import com.tonapps.wallet.data.stonfi.entities.StonfiAssetResponse
import com.tonapps.wallet.data.stonfi.entities.StonfiPairResponse
import com.tonapps.wallet.data.stonfi.entities.StonfiSimulate
import okhttp3.Cache
import okhttp3.OkHttpClient
import okhttp3.logging.HttpLoggingInterceptor
import retrofit2.Retrofit
import retrofit2.converter.moshi.MoshiConverterFactory
import retrofit2.http.GET
import retrofit2.http.POST
import retrofit2.http.Query
import java.io.File
import java.util.concurrent.TimeUnit

interface StonfiApi {

@GET("v1/markets")
suspend fun pairs(): StonfiPairResponse

@GET("/v1/assets")
suspend fun assets(): StonfiAssetResponse


@POST("/v1/swap/simulate")
suspend fun simulate(
@Query("offer_address") offersAddress: String,
@Query("ask_address") askAddress: String,
@Query("units") units: String,
@Query("slippage_tolerance") slippageTolerance: String,
): StonfiSimulate

companion object {

fun provideApi(context: Context): StonfiApi {
val httpClient = OkHttpClient.Builder()
.connectTimeout(40, TimeUnit.SECONDS)
.readTimeout(40, TimeUnit.SECONDS)
.writeTimeout(40, TimeUnit.SECONDS)


return Retrofit.Builder()
.client(httpClient.build())
.baseUrl("https://api.ston.fi")
.addConverterFactory(
MoshiConverterFactory.create(
Moshi.Builder().addLast(KotlinJsonAdapterFactory()).build()
)
)
.build().create(StonfiApi::class.java)

}
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,46 @@
package com.tonapps.wallet.data.stonfi.entities

import com.squareup.moshi.Json
import com.squareup.moshi.JsonClass

data class StonfiAsset(
@Json(name = "balance")
val balance: String?,
@Json(name = "blacklisted")
val blacklisted: Boolean = true,
@Json(name = "community")
val community: Boolean = true,
@Json(name = "contract_address")
val contractAddress: String,
@Json(name = "decimals")
val decimals: Int = 9,
@Json(name = "default_symbol")
val defaultSymbol: Boolean,
@Json(name = "deprecated")
val deprecated: Boolean,
@Json(name = "dex_price_usd")
val dexPriceUsd: String?,
// @Json(name = "dex_usd_price")
// val dexUsdPrice: String?,
@Json(name = "display_name")
val displayName: String?,
@Json(name = "image_url")
val imageUrl: String?,
@Json(name = "kind")
val kind: StonfiAssetKind,
@Json(name = "symbol")
val symbol: String,
@Json(name = "third_party_price_usd")
val thirdPartyPriceUsd: String?,
// @Json(name = "third_party_usd_price")
// val thirdPartyUsdPrice: String?,
@Json(name = "wallet_address")
val walletAddress: String?,
) {
@JsonClass(generateAdapter = false)
enum class StonfiAssetKind(val value: String){
@Json(name = "Jetton") Jetton("Jetton"),
@Json(name = "Wton") Wton("Wton"),
@Json(name = "Ton") Ton("Ton");
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
package com.tonapps.wallet.data.stonfi.entities

import com.squareup.moshi.Json
import com.squareup.moshi.JsonClass

@JsonClass(generateAdapter = false)
enum class StonfiAssetKind {
@Json(name = "Jetton") Jetton,
@Json(name = "Wton") Wton,
@Json(name = "Ton") Ton;
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
package com.tonapps.wallet.data.stonfi.entities

import com.squareup.moshi.Json


data class StonfiAssetResponse(
@Json(name = "asset_list")
val assets: List<StonfiAsset>,
)
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
package com.tonapps.wallet.data.stonfi.entities

data class StonfiPairResponse(val pairs: List<List<String>>) {

fun getPairs(): Map<String, List<String>> {
val map = mutableMapOf<String, MutableList<String>>()
for (pair in pairs) {
val pairDefault = map[pair[0]]
if (pairDefault == null) {
map[pair[0]] = mutableListOf(pair[1])
} else {
pairDefault.add(pair[1])
}


val pairReverse = map[pair[1]]
if (pairReverse == null) {
map[pair[1]] = mutableListOf(pair[0])
} else {
pairReverse.add(pair[0])
}
}
return map
}
}

Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
package com.tonapps.wallet.data.stonfi.entities

import com.squareup.moshi.Json

data class StonfiSimulate(
@Json(name = "offer_address")
val offerAddress: String,
@Json(name = "ask_address")
val askAddress: String,
@Json(name = "router_address")
val routerAddress: String,
@Json(name = "pool_address")
val poolAddress: String,
@Json(name = "offer_units")
val offerUnits: String,
@Json(name = "ask_units")
val askUnits: String,
@Json(name = "slippage_tolerance")
val slippageTolerance: String,
@Json(name = "min_ask_units")
val minAskUnits: String,
@Json(name = "swap_rate")
val swapRate: String,
@Json(name = "price_impact")
val priceImpact: String,
@Json(name = "fee_address")
val feeAddress: String,
@Json(name = "fee_units")
val feeUnits: String,
@Json(name = "fee_percent")
val feePercent: String,
)
2 changes: 2 additions & 0 deletions apps/wallet/instance/build.gradle.kts
Original file line number Diff line number Diff line change
Expand Up @@ -61,6 +61,7 @@ dependencies {

implementation(project(Dependence.Wallet.Data.core))
implementation(project(Dependence.Wallet.Data.tokens))
implementation(project(Dependence.Wallet.Data.stonfi))
implementation(project(Dependence.Wallet.Data.account))
implementation(project(Dependence.Wallet.Data.settings))
implementation(project(Dependence.Wallet.Data.rates))
Expand All @@ -81,6 +82,7 @@ dependencies {
implementation(Dependence.AndroidX.workManager)
implementation(Dependence.AndroidX.biometric)
implementation(Dependence.AndroidX.swiperefreshlayout)
implementation(Dependence.AndroidX.constraintlayout)

implementation(Dependence.guava)

Expand Down
14 changes: 8 additions & 6 deletions apps/wallet/instance/src/main/java/com/tonapps/tonkeeper/App.kt
Original file line number Diff line number Diff line change
Expand Up @@ -13,19 +13,21 @@ import com.tonapps.tonkeeper.core.fiat.Fiat
import com.tonapps.tonkeeper.koin.koinModel
import com.tonapps.wallet.api.apiModule
import com.tonapps.wallet.data.account.accountModule
import com.tonapps.wallet.data.rates.ratesModule
import com.tonapps.wallet.data.settings.SettingsRepository
import com.tonapps.wallet.data.token.tokenModule
import org.koin.android.ext.koin.androidContext
import org.koin.core.context.startKoin
import com.tonapps.wallet.data.account.legacy.WalletManager
import com.tonapps.wallet.data.browser.browserModule
import com.tonapps.wallet.data.collectibles.collectiblesModule
import com.tonapps.wallet.data.core.dataModule
import com.tonapps.wallet.data.events.eventsModule
import com.tonapps.wallet.data.push.pushModule
import com.tonapps.wallet.data.rates.ratesModule
import com.tonapps.wallet.data.settings.SettingsRepository
import com.tonapps.wallet.data.stonfi.stonfiModule
import com.tonapps.wallet.data.token.tokenModule
import com.tonapps.wallet.data.tonconnect.tonConnectModule
import org.koin.android.ext.koin.androidContext
import org.koin.core.component.KoinComponent
import org.koin.core.context.startKoin


class App: Application(), CameraXConfig.Provider, KoinComponent {

Expand Down Expand Up @@ -53,7 +55,7 @@ class App: Application(), CameraXConfig.Provider, KoinComponent {

startKoin {
androidContext(this@App)
modules(koinModel, dataModule, browserModule, pushModule, tonConnectModule, apiModule, accountModule, ratesModule, tokenModule, eventsModule, collectiblesModule)
modules(koinModel, dataModule, browserModule, pushModule, tonConnectModule, apiModule, accountModule, ratesModule, tokenModule, eventsModule, collectiblesModule, stonfiModule)
}

AppCompatDelegate.setDefaultNightMode(AppCompatDelegate.MODE_NIGHT_YES)
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
package com.tonapps.tonkeeper.extensions

val Float.amount :String
get() {
return if (0f >= this) {
""
} else {
this.toString()
}
}
Original file line number Diff line number Diff line change
@@ -1,5 +1,9 @@
package com.tonapps.tonkeeper.extensions

import android.text.Editable
import android.widget.EditText
import com.tonapps.blockchain.Coin

fun String.substringSafe(startIndex: Int, endIndex: Int): String {
return if (startIndex > length) {
""
Expand All @@ -22,4 +26,12 @@ val String.capitalized: String
} else {
""
}
}
}


val EditText.amount: Float
get() {
text?: return 0f
val text = Coin.prepareValue(text.toString())
return text.toFloatOrNull() ?: 0f
}
Loading