Skip to content

Commit

Permalink
PWN-8904 - Striga API. get_estimated_fees (#1884)
Browse files Browse the repository at this point in the history
* PWN-8904 - Striga API. get_estimated_fees

* PWN-8904 - comment fixes

* PWN-8904 - change base url

* PWN-8904 - add StrigaUserWalletsMapper to koin

* PWN-8904 - add ending slashes to new striga urls

---------

Co-authored-by: Eduard Maximovich <[email protected]>
  • Loading branch information
gslevinkov and eduardmaximovich authored Jun 28, 2023
1 parent 018ed0d commit 4310a6c
Show file tree
Hide file tree
Showing 11 changed files with 114 additions and 39 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ import org.p2p.wallet.infrastructure.network.NetworkModule.getRetrofit
import org.p2p.wallet.infrastructure.network.interceptor.StrigaProxyApiInterceptor
import org.p2p.wallet.striga.wallet.api.StrigaWalletApi
import org.p2p.wallet.striga.wallet.interactor.StrigaWalletInteractor
import org.p2p.wallet.striga.wallet.repository.StrigaUserWalletsMapper
import org.p2p.wallet.striga.wallet.repository.StrigaWalletRemoteRepository
import org.p2p.wallet.striga.wallet.repository.StrigaWalletRepository
import org.p2p.wallet.striga.wallet.repository.StrigaWalletRepositoryMapper
Expand All @@ -35,6 +36,7 @@ object StrigaWalletModule : InjectionModule {
).create()
}

factoryOf(::StrigaUserWalletsMapper)
factoryOf(::StrigaWalletRepositoryMapper)
factoryOf(::StrigaWalletRemoteRepository) bind StrigaWalletRepository::class
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,9 +6,11 @@ import org.p2p.wallet.striga.wallet.api.request.StrigaAddWhitelistedAddressReque
import org.p2p.wallet.striga.wallet.api.request.StrigaEnrichAccountRequest
import org.p2p.wallet.striga.wallet.api.request.StrigaGetWhitelistedAddressesRequest
import org.p2p.wallet.striga.wallet.api.request.StrigaInitWithdrawalRequest
import org.p2p.wallet.striga.wallet.api.request.StrigaOnchainWithdrawalFeeRequest
import org.p2p.wallet.striga.wallet.api.request.StrigaUserWalletsRequest
import org.p2p.wallet.striga.wallet.api.response.StrigaEnrichFiatAccountResponse
import org.p2p.wallet.striga.wallet.api.response.StrigaInitWithdrawalResponse
import org.p2p.wallet.striga.wallet.api.response.StrigaOnchainWithdrawalFeeResponse
import org.p2p.wallet.striga.wallet.api.response.StrigaUserWalletsResponse
import org.p2p.wallet.striga.wallet.api.response.StrigaWhitelistedAddressItemResponse
import org.p2p.wallet.striga.wallet.api.response.StrigaWhitelistedAddressesResponse
Expand All @@ -35,8 +37,17 @@ interface StrigaWalletApi {
* So use it for fiat accounts only
*/
@POST("v1/wallets/account/enrich")
suspend fun enrichFiatAccount(@Body body: StrigaEnrichAccountRequest): StrigaEnrichFiatAccountResponse
suspend fun enrichFiatAccount(
@Body body: StrigaEnrichAccountRequest
): StrigaEnrichFiatAccountResponse

@POST("v1/wallets/get/all")
suspend fun getUserWallets(@Body body: StrigaUserWalletsRequest): StrigaUserWalletsResponse
suspend fun getUserWallets(
@Body body: StrigaUserWalletsRequest
): StrigaUserWalletsResponse

@POST("v1/wallets/send/initiate/onchain/fee-estimate")
suspend fun getOnchainWithdrawalFees(
@Body body: StrigaOnchainWithdrawalFeeRequest
): StrigaOnchainWithdrawalFeeResponse
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
package org.p2p.wallet.striga.wallet.api.request

import com.google.gson.annotations.SerializedName

/**
* @param amountInUnits The amount denominated in the smallest divisible unit of the sending currency.
* For example: cents or satoshis
*/
class StrigaOnchainWithdrawalFeeRequest(
@SerializedName("userId")
val userId: String,
@SerializedName("sourceAccountId")
val sourceAccountId: String,
@SerializedName("whitelistedAddressId")
val whitelistedAddressId: String,
@SerializedName("amount")
val amountInUnits: String,
)
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ class StrigaInitWithdrawalResponse(
@SerializedName("transaction")
val transaction: WithdrawalTransactionResponse,
@SerializedName("feeEstimate")
val feeEstimate: WithdrawalFeeEstimateResponse,
val feeEstimate: StrigaOnchainWithdrawalFeeResponse,
) {
class WithdrawalTransactionResponse(
@SerializedName("syncedOwnerId")
Expand All @@ -34,21 +34,4 @@ class StrigaInitWithdrawalResponse(
@SerializedName("transactionCurrency")
val transactionCurrency: String,
)

class WithdrawalFeeEstimateResponse(
@SerializedName("totalFee")
val totalFee: String,
@SerializedName("networkFee")
val networkFee: String,
@SerializedName("ourFee")
val ourFee: String,
@SerializedName("theirFee")
val theirFee: String,
@SerializedName("feeCurrency")
val feeCurrency: String,
@SerializedName("gasLimit")
val gasLimit: String,
@SerializedName("gasPrice")
val gasPrice: String,
)
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
package org.p2p.wallet.striga.wallet.api.response

import com.google.gson.annotations.SerializedName

data class StrigaOnchainWithdrawalFeeResponse(
@SerializedName("totalFee")
val totalFee: String,
@SerializedName("networkFee")
val networkFee: String,
@SerializedName("ourFee")
val ourFee: String,
@SerializedName("theirFee")
val theirFee: String,
@SerializedName("feeCurrency")
val feeCurrency: String,
@SerializedName("gasLimit")
val gasLimit: String,
@SerializedName("gasPrice")
val gasPrice: String
)
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@ package org.p2p.wallet.striga.wallet.models

import android.os.Parcelable
import org.threeten.bp.ZonedDateTime
import java.math.BigDecimal
import java.math.BigInteger
import kotlinx.parcelize.Parcelize
import org.p2p.wallet.striga.wallet.models.ids.StrigaAccountId
Expand All @@ -14,7 +13,7 @@ data class StrigaInitWithdrawalDetails(
val challengeId: StrigaWithdrawalChallengeId,
val dateExpires: ZonedDateTime,
val transaction: WithdrawalTransactionDetails,
val feeEstimate: WithdrawalFeeEstimateDetails,
val feeEstimate: StrigaOnchainWithdrawalFees,
) : Parcelable {

/**
Expand All @@ -34,15 +33,4 @@ data class StrigaInitWithdrawalDetails(
val blockchainNetwork: StrigaBlockchainNetworkInfo,
val transactionCurrency: StrigaNetworkCurrency,
) : Parcelable

@Parcelize
class WithdrawalFeeEstimateDetails(
val totalFee: BigInteger,
val networkFee: BigInteger,
val ourFee: BigInteger,
val theirFee: BigInteger,
val feeCurrency: StrigaNetworkCurrency,
val gasLimit: BigInteger,
val gasPrice: BigDecimal,
) : Parcelable
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
package org.p2p.wallet.striga.wallet.models

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

@Parcelize
data class StrigaOnchainWithdrawalFees(
val totalFee: BigInteger,
val networkFee: BigInteger,
val ourFee: BigInteger,
val theirFee: BigInteger,
val feeCurrency: StrigaNetworkCurrency,
val gasLimit: BigInteger,
val gasPrice: BigDecimal,
) : Parcelable
Original file line number Diff line number Diff line change
Expand Up @@ -11,10 +11,12 @@ import org.p2p.wallet.striga.wallet.api.request.StrigaAddWhitelistedAddressReque
import org.p2p.wallet.striga.wallet.api.request.StrigaEnrichAccountRequest
import org.p2p.wallet.striga.wallet.api.request.StrigaGetWhitelistedAddressesRequest
import org.p2p.wallet.striga.wallet.api.request.StrigaInitWithdrawalRequest
import org.p2p.wallet.striga.wallet.api.request.StrigaOnchainWithdrawalFeeRequest
import org.p2p.wallet.striga.wallet.api.request.StrigaUserWalletsRequest
import org.p2p.wallet.striga.wallet.models.StrigaFiatAccountDetails
import org.p2p.wallet.striga.wallet.models.StrigaInitWithdrawalDetails
import org.p2p.wallet.striga.wallet.models.StrigaNetworkCurrency
import org.p2p.wallet.striga.wallet.models.StrigaOnchainWithdrawalFees
import org.p2p.wallet.striga.wallet.models.StrigaUserWallet
import org.p2p.wallet.striga.wallet.models.StrigaWhitelistedAddressItem
import org.p2p.wallet.striga.wallet.models.ids.StrigaAccountId
Expand Down Expand Up @@ -49,6 +51,28 @@ class StrigaWalletRemoteRepository(
}
}

override suspend fun getOnchainWithdrawalFees(
sourceAccountId: StrigaAccountId,
whitelistedAddressId: StrigaWhitelistedAddressId,
amount: BigInteger,
): StrigaDataLayerResult<StrigaOnchainWithdrawalFees> {
return try {
val request = StrigaOnchainWithdrawalFeeRequest(
userId = strigaUserIdProvider.getUserIdOrThrow(),
sourceAccountId = sourceAccountId.value,
whitelistedAddressId = whitelistedAddressId.value,
amountInUnits = amount.toString()
)
val response = api.getOnchainWithdrawalFees(request)
return mapper.fromNetwork(response).toSuccessResult()
} catch (error: Throwable) {
StrigaDataLayerError.from(
error = error,
default = StrigaDataLayerError.InternalError(error)
)
}
}

override suspend fun whitelistAddress(
address: String,
currency: StrigaNetworkCurrency,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ import org.p2p.wallet.striga.model.StrigaDataLayerResult
import org.p2p.wallet.striga.wallet.models.StrigaFiatAccountDetails
import org.p2p.wallet.striga.wallet.models.StrigaInitWithdrawalDetails
import org.p2p.wallet.striga.wallet.models.StrigaNetworkCurrency
import org.p2p.wallet.striga.wallet.models.StrigaOnchainWithdrawalFees
import org.p2p.wallet.striga.wallet.models.StrigaUserWallet
import org.p2p.wallet.striga.wallet.models.StrigaWhitelistedAddressItem
import org.p2p.wallet.striga.wallet.models.ids.StrigaAccountId
Expand Down Expand Up @@ -63,4 +64,10 @@ interface StrigaWalletRepository {
): StrigaDataLayerResult<StrigaFiatAccountDetails>

suspend fun getUserWallet(): StrigaDataLayerResult<StrigaUserWallet>

suspend fun getOnchainWithdrawalFees(
sourceAccountId: StrigaAccountId,
whitelistedAddressId: StrigaWhitelistedAddressId,
amount: BigInteger
): StrigaDataLayerResult<StrigaOnchainWithdrawalFees>
}
Original file line number Diff line number Diff line change
Expand Up @@ -6,15 +6,17 @@ import org.p2p.core.utils.toBigIntegerOrZero
import org.p2p.wallet.striga.wallet.api.response.StrigaBlockchainNetworkResponse
import org.p2p.wallet.striga.wallet.api.response.StrigaEnrichFiatAccountResponse
import org.p2p.wallet.striga.wallet.api.response.StrigaInitWithdrawalResponse
import org.p2p.wallet.striga.wallet.api.response.StrigaOnchainWithdrawalFeeResponse
import org.p2p.wallet.striga.wallet.api.response.StrigaWhitelistedAddressItemResponse
import org.p2p.wallet.striga.wallet.api.response.StrigaWhitelistedAddressesResponse
import org.p2p.wallet.striga.wallet.models.StrigaFiatAccountStatus
import org.p2p.wallet.striga.wallet.models.StrigaBlockchainNetworkInfo
import org.p2p.wallet.striga.wallet.models.StrigaFiatAccountDetails
import org.p2p.wallet.striga.wallet.models.StrigaFiatAccountStatus
import org.p2p.wallet.striga.wallet.models.StrigaInitWithdrawalDetails
import org.p2p.wallet.striga.wallet.models.StrigaNetworkCurrency
import org.p2p.wallet.striga.wallet.models.StrigaOnchainTxStatus
import org.p2p.wallet.striga.wallet.models.StrigaOnchainTxType
import org.p2p.wallet.striga.wallet.models.StrigaOnchainWithdrawalFees
import org.p2p.wallet.striga.wallet.models.StrigaWhitelistedAddressItem
import org.p2p.wallet.striga.wallet.models.ids.StrigaAccountId
import org.p2p.wallet.striga.wallet.models.ids.StrigaWalletId
Expand Down Expand Up @@ -56,6 +58,10 @@ class StrigaWalletRepositoryMapper {
)
}

fun fromNetwork(response: StrigaOnchainWithdrawalFeeResponse): StrigaOnchainWithdrawalFees {
return response.toDetailsFeeEstimate()
}

fun fromNetwork(item: StrigaWhitelistedAddressItemResponse): StrigaWhitelistedAddressItem {
return StrigaWhitelistedAddressItem(
id = StrigaWhitelistedAddressId(item.id),
Expand All @@ -74,9 +80,8 @@ class StrigaWalletRepositoryMapper {
)
}

private fun StrigaInitWithdrawalResponse.WithdrawalFeeEstimateResponse.toDetailsFeeEstimate():
StrigaInitWithdrawalDetails.WithdrawalFeeEstimateDetails {
return StrigaInitWithdrawalDetails.WithdrawalFeeEstimateDetails(
private fun StrigaOnchainWithdrawalFeeResponse.toDetailsFeeEstimate(): StrigaOnchainWithdrawalFees {
return StrigaOnchainWithdrawalFees(
totalFee = totalFee.toBigIntegerOrZero(),
networkFee = networkFee.toBigIntegerOrZero(),
ourFee = ourFee.toBigIntegerOrZero(),
Expand Down
4 changes: 2 additions & 2 deletions app/src/main/res/values/environment_config.xml
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,7 @@

<string name="alarmErrorsServiceBaseUrl">https://oncall.keyapp.org/</string>

<string name="strigaProxyServiceBaseUrl">https://payment.keyapp.org/striga/api/</string>
<string name="strigaProxyServiceProdBaseUrl">https://payment.key.app/striga/api/</string>
<string name="strigaProxyServiceBaseUrl">https://payment-rust.keyapp.org/striga/api/v1/</string>
<string name="strigaProxyServiceProdBaseUrl">https://payment-rust.keyapp.org/striga/api/v1/</string>
<!--TODO PWN-4266 migrate googleClientId to CI for secure-->
</resources>

0 comments on commit 4310a6c

Please sign in to comment.