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

[All features] Contest #13

Open
wants to merge 24 commits into
base: main
Choose a base branch
from
Open
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
3 changes: 3 additions & 0 deletions .idea/gradle.xml

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

106 changes: 102 additions & 4 deletions apps/wallet/api/src/main/java/com/tonapps/wallet/api/API.kt
Original file line number Diff line number Diff line change
@@ -1,12 +1,11 @@
package com.tonapps.wallet.api

import android.content.Context
import android.net.Uri
import android.util.ArrayMap
import android.util.Log
import com.tonapps.blockchain.Coin
import com.tonapps.blockchain.ton.extensions.base64
import com.tonapps.blockchain.ton.extensions.isValid
import com.tonapps.extensions.ifPunycodeToUnicode
import com.tonapps.extensions.locale
import com.tonapps.extensions.unicodeToPunycode
import com.tonapps.network.SSEvent
Expand All @@ -17,8 +16,11 @@ import com.tonapps.network.post
import com.tonapps.network.postJSON
import com.tonapps.network.sse
import com.tonapps.wallet.api.entity.AccountDetailsEntity
import com.tonapps.wallet.api.entity.AssetEntity
import com.tonapps.wallet.api.entity.BalanceEntity
import com.tonapps.wallet.api.entity.ConfigEntity
import com.tonapps.wallet.api.entity.StakePoolsEntity
import com.tonapps.wallet.api.entity.SwapDetailsEntity
import com.tonapps.wallet.api.entity.TokenEntity
import com.tonapps.wallet.api.internal.ConfigRepository
import com.tonapps.wallet.api.internal.InternalApi
Expand All @@ -43,6 +45,7 @@ import org.json.JSONArray
import org.json.JSONObject
import org.ton.api.pub.PublicKeyEd25519
import org.ton.cell.Cell
import java.math.BigDecimal
import java.util.Locale
import java.util.concurrent.TimeUnit

Expand Down Expand Up @@ -79,6 +82,100 @@ class API(

fun rates() = provider.rates.get(false)

fun stake(testnet: Boolean) = provider.staking.get(testnet)

fun stonfi(testnet: Boolean) = provider.stonfi.get(testnet)

fun getStakingPools(walletAddress: String, testnet: Boolean): StakePoolsEntity {
return stake(testnet).getStakingPools(walletAddress, false).let {
StakePoolsEntity(
pools = it.pools.map {
StakePoolsEntity.PoolInfo(
address = it.address,
name = it.name,
totalAmount = it.totalAmount,
implementation = StakePoolsEntity.PoolImplementationType.find(it.implementation.value),
apy = it.apy,
minStake = it.minStake,
cycleStart = it.cycleStart,
cycleEnd = it.cycleEnd,
verified = it.verified,
currentNominators = it.currentNominators,
maxNominators = it.maxNominators,
nominatorsStake = it.nominatorsStake,
validatorStake = it.validatorStake,
liquidJettonMaster = it.liquidJettonMaster,
cycleLength = it.cycleLength
)
},
implementations = it.implementations.entries.associate {
it.key to StakePoolsEntity.PoolImplementation(
name = it.value.name,
description = it.value.description,
url = it.value.url,
socials = it.value.socials
)
},
)
}
}

fun getWalletAssets(walletAddress: String, testnet: Boolean): List<AssetEntity> {
val entityList = mutableListOf<AssetEntity>()
val assetList = stonfi(testnet).getWalletAssets(walletAddress).assetList
for (asset in assetList) {
if (asset.blacklisted || asset.community || asset.deprecated) {
continue
}
entityList.add(
AssetEntity(
token = TokenEntity(
address = asset.contractAddress,
name = asset.displayName,
symbol = asset.symbol,
imageUri = Uri.parse(asset.imageUrl.orEmpty()),
decimals = asset.decimals,
verification = TokenEntity.Verification.whitelist
),
value = BigDecimal(asset.balance ?: "0").movePointLeft(asset.decimals)
.toFloat(),
walletAddress = asset.walletAddress.orEmpty(),
usdPrice = asset.dexPriceUsd?.toFloatOrNull() ?: 0f,
kind = asset.kind
)
)
}
return entityList
}

fun simulateSwap(
offerAddress: String,
askAddress: String,
units: String,
tolerance: String,
testnet: Boolean,
reverse: Boolean
): SwapDetailsEntity {
val response =
stonfi(testnet).simulateSwap(offerAddress, askAddress, units, tolerance, reverse)

return SwapDetailsEntity(
offerUnits = response.offerUnits,
askUnits = response.askUnits,
priceImpact = response.priceImpact,
minReceived = response.minAskUnits,
routerAddress = response.routerAddress,
poolAddress = response.poolAddress,
providerFeeUnits = response.feeUnits,
feeAddress = response.feeAddress,
swapRate = response.swapRate
)
}

fun getJettonAddress(ownerAddress: String, jettonAddress: String, testnet: Boolean): String {
return stonfi(testnet).getJettonAddress(ownerAddress, jettonAddress).address
}

fun getEvents(
accountId: String,
testnet: Boolean,
Expand Down Expand Up @@ -182,7 +279,8 @@ class API(
config.tonapiMainnetHost
}
// val mempool = okHttpClient.sse("$endpoint/v2/sse/mempool?accounts=${accountId}")
val tx = tonAPIHttpClient.sse("$endpoint/v2/sse/accounts/transactions?accounts=${accountId}")
val tx =
tonAPIHttpClient.sse("$endpoint/v2/sse/accounts/transactions?accounts=${accountId}")
// return merge(mempool, tx)
return tx
}
Expand Down Expand Up @@ -387,7 +485,7 @@ class API(

private fun baseOkHttpClientBuilder(): OkHttpClient.Builder {
return OkHttpClient().newBuilder()
.retryOnConnectionFailure(false)
.retryOnConnectionFailure(true)
.connectTimeout(10, TimeUnit.SECONDS)
.readTimeout(10, TimeUnit.SECONDS)
.writeTimeout(10, TimeUnit.SECONDS)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -40,4 +40,6 @@ internal class Provider(
val traces = SourceAPI(main.traces, test.traces)

val wallet = SourceAPI(main.wallet, test.wallet)

val stonfi = SourceAPI(main.stonfi, test.stonfi)
}
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ import io.tonapi.apis.LiteServerApi
import io.tonapi.apis.NFTApi
import io.tonapi.apis.RatesApi
import io.tonapi.apis.StakingApi
import io.tonapi.apis.StonFiApi
import io.tonapi.apis.StorageApi
import io.tonapi.apis.TracesApi
import io.tonapi.apis.WalletApi
Expand Down Expand Up @@ -49,4 +50,6 @@ class BaseAPI(

val wallet: WalletApi by lazy { WalletApi(basePath, okHttpClient) }

val stonfi: StonFiApi by lazy { StonFiApi() }

}
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
package com.tonapps.wallet.api.entity

import android.os.Parcelable
import kotlinx.parcelize.Parcelize

@Parcelize
data class AssetEntity(
val token: TokenEntity,
val value: Float,
val walletAddress: String,
val usdPrice: Float,
val kind: String
) : Parcelable
Original file line number Diff line number Diff line change
@@ -0,0 +1,54 @@
package com.tonapps.wallet.api.entity

import android.os.Parcelable
import kotlinx.parcelize.Parcelize
import java.math.BigDecimal

@Parcelize
data class StakePoolsEntity(
val pools: List<PoolInfo>,
val implementations: Map<String, PoolImplementation>
) : Parcelable {
@Parcelize
data class PoolInfo(
val address: String,
val name: String,
val totalAmount: Long,
val implementation: PoolImplementationType,
val apy: BigDecimal,
val minStake: Long,
val cycleStart: Long,
val cycleEnd: Long,
val verified: Boolean,
val currentNominators: Int,
val maxNominators: Int,
val nominatorsStake: Long,
val validatorStake: Long,
val liquidJettonMaster: String? = null,
val cycleLength: Long? = null
) : Parcelable

@Parcelize
data class PoolImplementation(
val name: String,
val description: String,
val url: String,
val socials: List<String>
) : Parcelable

@Parcelize
enum class PoolImplementationType(val value: String) : Parcelable {
whales("whales"),
tf("tf"),
liquidTF("liquidTF");

companion object {

fun find(s: String): PoolImplementationType = entries.first { it.value == s }
}
}

companion object {
val Empty = StakePoolsEntity(emptyList(), emptyMap())
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
package com.tonapps.wallet.api.entity

import android.os.Parcelable
import kotlinx.parcelize.Parcelize

@Parcelize
data class SwapDetailsEntity(
val offerUnits: String,
val askUnits: String,
val priceImpact: String,
val minReceived: String,
val routerAddress: String,
val poolAddress: String,
val providerFeeUnits: String,
val feeAddress: String,
val swapRate: String
) : Parcelable
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
package com.tonapps.wallet.data.account

import com.tonapps.wallet.api.API
import com.tonapps.wallet.data.account.legacy.WalletLegacy
import org.ton.block.StateInit

object SeqnoHelper {
private var lastSeqno = -1

suspend fun getStateInitIfNeed(wallet: WalletLegacy, api: API): StateInit? {
if (lastSeqno == -1) {
lastSeqno = getSeqno(wallet, api)
}
if (lastSeqno == 0) {
return wallet.contract.stateInit
}
return null
}

private suspend fun getSeqno(wallet: WalletLegacy, api: API): Int {
if (lastSeqno == 0) {
lastSeqno = wallet.getSeqnoRemote(api)
}
return lastSeqno
}

private suspend fun WalletLegacy.getSeqnoRemote(api: API): Int {
return try {
api.getAccountSeqno(accountId, testnet)
} catch (e: Throwable) {
0
}
}
}
37 changes: 37 additions & 0 deletions apps/wallet/data/buysell/build.gradle.kts
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
plugins {
id("com.android.library")
id("org.jetbrains.kotlin.android")
id("kotlin-parcelize")
}

android {
namespace = Build.namespacePrefix("wallet.data.buysell")
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(project(Dependence.Module.tonApi))
implementation(project(Dependence.Module.ton))
implementation(project(Dependence.Wallet.Data.core))
implementation(project(Dependence.Wallet.Data.rates))
implementation(project(Dependence.Wallet.Data.account))
implementation(project(Dependence.Wallet.api))
implementation(project(Dependence.Lib.extensions))
implementation(project(Dependence.Lib.security))
implementation(project(Dependence.UIKit.list))
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
package com.tonapps.wallet.data.buysell

import com.tonapps.wallet.api.API

class BuySellRepository(
private val api: API
) {

}
Loading