-
Notifications
You must be signed in to change notification settings - Fork 10
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
23 changed files
with
747 additions
and
101 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
341 changes: 278 additions & 63 deletions
341
blockchain/src/main/java/com/tangem/blockchain/blockchains/kaspa/KaspaTransactionBuilder.kt
Large diffs are not rendered by default.
Oops, something went wrong.
280 changes: 251 additions & 29 deletions
280
blockchain/src/main/java/com/tangem/blockchain/blockchains/kaspa/KaspaWalletManager.kt
Large diffs are not rendered by default.
Oops, something went wrong.
9 changes: 9 additions & 0 deletions
9
blockchain/src/main/java/com/tangem/blockchain/blockchains/kaspa/krc20/KaspaKRC20Api.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,9 @@ | ||
package com.tangem.blockchain.blockchains.kaspa.krc20 | ||
|
||
import com.tangem.blockchain.blockchains.kaspa.network.KaspaBalanceResponse | ||
import retrofit2.http.* | ||
|
||
interface KaspaKRC20Api { | ||
@GET("krc20/address/{address}/token/{token}") | ||
suspend fun getBalance(@Path("address") address: String, @Path("token") token: String): KaspaBalanceResponse | ||
} |
15 changes: 15 additions & 0 deletions
15
.../src/main/java/com/tangem/blockchain/blockchains/kaspa/krc20/KaspaKRC20NetworkProvider.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,15 @@ | ||
package com.tangem.blockchain.blockchains.kaspa.krc20 | ||
|
||
import com.tangem.blockchain.common.NetworkProvider | ||
import com.tangem.blockchain.common.Token | ||
import com.tangem.blockchain.extensions.Result | ||
import java.math.BigDecimal | ||
|
||
interface KaspaKRC20NetworkProvider : NetworkProvider { | ||
suspend fun getBalances(address: String, tokens: List<Token>): Result<List<KaspaKRC20InfoResponse>> | ||
} | ||
|
||
data class KaspaKRC20InfoResponse( | ||
val token: Token, | ||
val balance: BigDecimal, | ||
) |
16 changes: 16 additions & 0 deletions
16
...n/src/main/java/com/tangem/blockchain/blockchains/kaspa/krc20/KaspaKRC20NetworkService.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,16 @@ | ||
package com.tangem.blockchain.blockchains.kaspa.krc20 | ||
|
||
import com.tangem.blockchain.common.Token | ||
import com.tangem.blockchain.extensions.Result | ||
import com.tangem.blockchain.network.MultiNetworkProvider | ||
|
||
class KaspaKRC20NetworkService(providers: List<KaspaKRC20NetworkProvider>) : KaspaKRC20NetworkProvider { | ||
|
||
private val multiNetworkProvider = MultiNetworkProvider(providers) | ||
override val baseUrl: String | ||
get() = multiNetworkProvider.currentProvider.baseUrl | ||
|
||
override suspend fun getBalances(address: String, tokens: List<Token>): Result<List<KaspaKRC20InfoResponse>> { | ||
return multiNetworkProvider.performRequest { getBalances(address, tokens) } | ||
} | ||
} |
45 changes: 45 additions & 0 deletions
45
...in/java/com/tangem/blockchain/blockchains/kaspa/krc20/KaspaKRC20RestApiNetworkProvider.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,45 @@ | ||
package com.tangem.blockchain.blockchains.kaspa.krc20 | ||
|
||
import com.tangem.blockchain.common.Blockchain | ||
import com.tangem.blockchain.common.Token | ||
import com.tangem.blockchain.common.toBlockchainSdkError | ||
import com.tangem.blockchain.extensions.Result | ||
import com.tangem.blockchain.extensions.retryIO | ||
import com.tangem.blockchain.network.createRetrofitInstance | ||
import kotlinx.coroutines.async | ||
import kotlinx.coroutines.coroutineScope | ||
|
||
open class KaspaKRC20RestApiNetworkProvider(override val baseUrl: String) : KaspaKRC20NetworkProvider { | ||
|
||
private val api: KaspaKRC20Api by lazy { | ||
createRetrofitInstance(baseUrl).create(KaspaKRC20Api::class.java) | ||
} | ||
private val decimals = Blockchain.Kaspa.decimals() | ||
|
||
override suspend fun getBalances(address: String, tokens: List<Token>): Result<List<KaspaKRC20InfoResponse>> { | ||
return try { | ||
coroutineScope { | ||
val tokenBalancesDeferred = tokens.associateWith { token -> | ||
async { retryIO { api.getBalance(address, token.contractAddress) } } | ||
} | ||
|
||
val tokenBalanceResponses = tokenBalancesDeferred.mapValues { it.value.await() } | ||
|
||
Result.Success( | ||
tokenBalanceResponses.map { | ||
try { | ||
KaspaKRC20InfoResponse( | ||
token = it.key, | ||
balance = it.value.balance!!.toBigDecimal().movePointLeft(decimals), | ||
) | ||
} catch (exception: Exception) { | ||
throw exception.toBlockchainSdkError() | ||
} | ||
}, | ||
) | ||
} | ||
} catch (exception: Exception) { | ||
Result.Failure(exception.toBlockchainSdkError()) | ||
} | ||
} | ||
} |
8 changes: 8 additions & 0 deletions
8
...rc/main/java/com/tangem/blockchain/blockchains/kaspa/krc20/KaspaKRC20TransactionExtras.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,8 @@ | ||
package com.tangem.blockchain.blockchains.kaspa.krc20 | ||
|
||
import com.tangem.blockchain.blockchains.kaspa.krc20.model.IncompleteTokenTransactionParams | ||
import com.tangem.blockchain.common.TransactionExtras | ||
|
||
data class KaspaKRC20TransactionExtras( | ||
val incompleteTokenTransactionParams: IncompleteTokenTransactionParams, | ||
) : TransactionExtras |
11 changes: 11 additions & 0 deletions
11
...in/src/main/java/com/tangem/blockchain/blockchains/kaspa/krc20/model/CommitTransaction.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,11 @@ | ||
package com.tangem.blockchain.blockchains.kaspa.krc20.model | ||
|
||
import com.tangem.blockchain.blockchains.kaspa.KaspaTransaction | ||
|
||
data class CommitTransaction( | ||
val transaction: KaspaTransaction, | ||
val hashes: List<ByteArray>, | ||
val redeemScript: RedeemScript, | ||
val sourceAddress: String, | ||
val params: IncompleteTokenTransactionParams, | ||
) |
13 changes: 13 additions & 0 deletions
13
blockchain/src/main/java/com/tangem/blockchain/blockchains/kaspa/krc20/model/Envelope.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,13 @@ | ||
package com.tangem.blockchain.blockchains.kaspa.krc20.model | ||
|
||
import com.squareup.moshi.Json | ||
import com.squareup.moshi.JsonClass | ||
|
||
@JsonClass(generateAdapter = true) | ||
data class Envelope( | ||
@Json(name = "p") val p: String, | ||
@Json(name = "op") val op: String, | ||
@Json(name = "amt") val amt: String, | ||
@Json(name = "to") val to: String, | ||
@Json(name = "tick") val tick: String, | ||
) |
10 changes: 10 additions & 0 deletions
10
...a/com/tangem/blockchain/blockchains/kaspa/krc20/model/IncompleteTokenTransactionParams.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,10 @@ | ||
package com.tangem.blockchain.blockchains.kaspa.krc20.model | ||
|
||
import java.math.BigDecimal | ||
|
||
data class IncompleteTokenTransactionParams( | ||
val transactionId: String, | ||
val amount: BigDecimal, | ||
val targetOutputAmount: BigDecimal, | ||
val envelope: Envelope, | ||
) |
20 changes: 20 additions & 0 deletions
20
...in/java/com/tangem/blockchain/blockchains/kaspa/krc20/model/KaspaKRC20ProvidersBuilder.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,20 @@ | ||
package com.tangem.blockchain.blockchains.kaspa.krc20.model | ||
|
||
import com.tangem.blockchain.blockchains.kaspa.krc20.KaspaKRC20NetworkProvider | ||
import com.tangem.blockchain.blockchains.kaspa.krc20.KaspaKRC20RestApiNetworkProvider | ||
import com.tangem.blockchain.common.Blockchain | ||
import com.tangem.blockchain.common.network.providers.NetworkProvidersBuilder | ||
import com.tangem.blockchain.common.network.providers.ProviderType | ||
|
||
internal class KaspaKRC20ProvidersBuilder( | ||
override val providerTypes: List<ProviderType>, | ||
) : NetworkProvidersBuilder<KaspaKRC20NetworkProvider>() { | ||
|
||
override fun createProviders(blockchain: Blockchain): List<KaspaKRC20NetworkProvider> { | ||
return listOf(KaspaKRC20RestApiNetworkProvider("https://api.kasplex.org/v1/")) | ||
} | ||
|
||
override fun createTestnetProviders(blockchain: Blockchain): List<KaspaKRC20NetworkProvider> { | ||
return listOf(KaspaKRC20RestApiNetworkProvider("https://tn10api.kasplex.org/v1")) | ||
} | ||
} |
6 changes: 6 additions & 0 deletions
6
blockchain/src/main/java/com/tangem/blockchain/blockchains/kaspa/krc20/model/RedeemScript.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,6 @@ | ||
package com.tangem.blockchain.blockchains.kaspa.krc20.model | ||
|
||
data class RedeemScript( | ||
val publicKey: ByteArray, | ||
val envelope: Envelope, | ||
) |
9 changes: 9 additions & 0 deletions
9
...in/src/main/java/com/tangem/blockchain/blockchains/kaspa/krc20/model/RevealTransaction.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,9 @@ | ||
package com.tangem.blockchain.blockchains.kaspa.krc20.model | ||
|
||
import com.tangem.blockchain.blockchains.kaspa.KaspaTransaction | ||
|
||
internal data class RevealTransaction( | ||
val transaction: KaspaTransaction, | ||
val hashes: List<ByteArray>, | ||
val redeemScript: RedeemScript, | ||
) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters