-
Notifications
You must be signed in to change notification settings - Fork 18
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
PWN-8904 - Striga API. get_user_wallets #1883
Merged
gslevinkov
merged 2 commits into
feature/PWN-8895-striga-payment-api
from
feature/PWN-8904-striga-api-wallets
Jun 27, 2023
Merged
Changes from 1 commit
Commits
Show all changes
2 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
15 changes: 15 additions & 0 deletions
15
app/src/main/java/org/p2p/wallet/striga/wallet/api/request/StrigaUserWalletsRequest.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 org.p2p.wallet.striga.wallet.api.request | ||
|
||
import com.google.gson.annotations.SerializedName | ||
import org.p2p.core.utils.MillisSinceEpoch | ||
|
||
class StrigaUserWalletsRequest( | ||
@SerializedName("userId") | ||
val userId: String, | ||
@SerializedName("startDate") | ||
val startDate: MillisSinceEpoch, | ||
@SerializedName("endDate") | ||
val endDate: MillisSinceEpoch, | ||
@SerializedName("page") | ||
val page: Long | ||
) |
39 changes: 39 additions & 0 deletions
39
...rc/main/java/org/p2p/wallet/striga/wallet/api/response/StrigaUserWalletAccountResponse.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,39 @@ | ||
package org.p2p.wallet.striga.wallet.api.response | ||
|
||
import com.google.gson.annotations.SerializedName | ||
import java.math.BigInteger | ||
|
||
data class StrigaUserWalletAccountResponse( | ||
@SerializedName("accountId") | ||
val accountId: String, | ||
@SerializedName("parentWalletId") | ||
val parentWalletId: String, | ||
@SerializedName("currency") | ||
val currency: String, | ||
@SerializedName("ownerId") | ||
val ownerId: String, | ||
@SerializedName("rootFiatCurrency") | ||
val rootFiatCurrency: String, | ||
@SerializedName("ownerType") | ||
val ownerType: String, | ||
@SerializedName("createdAt") | ||
val createdAt: String, | ||
@SerializedName("availableBalance") | ||
val availableBalance: AvailableBalanceResponse, | ||
@SerializedName("linkedCardId") | ||
val linkedCardId: String, | ||
@SerializedName("linkedBankAccountId") | ||
val linkedBankAccountId: String, | ||
@SerializedName("status") | ||
val status: String | ||
) { | ||
/** | ||
* @param currencyUnits can be cents, or satoshis or smth else | ||
*/ | ||
data class AvailableBalanceResponse( | ||
@SerializedName("amount") | ||
val amount: BigInteger, | ||
@SerializedName("currency") | ||
val currencyUnits: String | ||
) | ||
} |
24 changes: 24 additions & 0 deletions
24
...rc/main/java/org/p2p/wallet/striga/wallet/api/response/StrigaUserWalletDetailsResponse.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,24 @@ | ||
package org.p2p.wallet.striga.wallet.api.response | ||
|
||
import com.google.gson.annotations.SerializedName | ||
import com.google.gson.internal.LinkedTreeMap | ||
|
||
/** | ||
* @param createdAt - example 2022-07-22T12:39:56.835Z | ||
*/ | ||
data class StrigaUserWalletDetailsResponse( | ||
@SerializedName("walletId") | ||
val walletId: String, | ||
@SerializedName("accounts") | ||
val accountCurrencyToDetails: LinkedTreeMap<String, StrigaUserWalletAccountResponse>, | ||
@SerializedName("rootFiatCurrency") | ||
val rootFiatCurrency: String, | ||
@SerializedName("syncedOwnerId") | ||
val syncedOwnerId: String, | ||
@SerializedName("ownerType") | ||
val ownerType: String, | ||
@SerializedName("createdAt") | ||
val createdAt: String, | ||
@SerializedName("comment") | ||
val comment: String | ||
) |
8 changes: 8 additions & 0 deletions
8
app/src/main/java/org/p2p/wallet/striga/wallet/api/response/StrigaUserWalletsResponse.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 org.p2p.wallet.striga.wallet.api.response | ||
|
||
import com.google.gson.annotations.SerializedName | ||
|
||
class StrigaUserWalletsResponse( | ||
@SerializedName("wallets") | ||
val wallets: List<StrigaUserWalletDetailsResponse> | ||
) |
21 changes: 21 additions & 0 deletions
21
app/src/main/java/org/p2p/wallet/striga/wallet/models/StrigaUserWallet.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,21 @@ | ||
package org.p2p.wallet.striga.wallet.models | ||
|
||
import org.p2p.core.utils.isNotZero | ||
import org.p2p.wallet.striga.wallet.models.ids.StrigaWalletId | ||
|
||
/** | ||
* A wallet on the Striga platform contains accounts. | ||
* Accounts are the lowest divisible unit of value storage and are each represented in one currency only. | ||
* When creating a wallet, accounts for each of your configured currencies are created and linked under that wallet. | ||
*/ | ||
data class StrigaUserWallet( | ||
val walletId: StrigaWalletId, | ||
val userId: String, | ||
val accounts: List<StrigaUserWalletAccount> | ||
) { | ||
val hasAvailableBalance: Boolean | ||
get() = accounts.any { it.availableBalance.isNotZero() } | ||
|
||
val eurAccount: StrigaUserWalletAccount? | ||
get() = accounts.firstOrNull { it.accountCurrency == StrigaWalletAccountCurrency.EUR } | ||
} |
32 changes: 32 additions & 0 deletions
32
app/src/main/java/org/p2p/wallet/striga/wallet/models/StrigaUserWalletAccount.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,32 @@ | ||
package org.p2p.wallet.striga.wallet.models | ||
|
||
import java.math.BigInteger | ||
import org.p2p.wallet.striga.wallet.models.ids.StrigaAccountId | ||
|
||
class StrigaUserWalletAccount( | ||
val accountId: StrigaAccountId, | ||
// todo (leave comment on PR if you see this): convert to enum when all statuses are found | ||
val accountStatus: String, | ||
val accountCurrency: StrigaWalletAccountCurrency, | ||
val parentWalletId: String, | ||
val ownerId: String, | ||
val rootFiatCurrency: String, | ||
val ownerType: String, | ||
val availableBalance: BigInteger, | ||
val balanceUnit: String, | ||
val linkedBankAccount: StrigaWalletAccountBankLink, | ||
) { | ||
fun availableBalanceWithUnits(): String = "$availableBalance $balanceUnit" | ||
} | ||
|
||
sealed interface StrigaWalletAccountBankLink { | ||
data class Linked(val value: String) : StrigaWalletAccountBankLink | ||
object Unlinked : StrigaWalletAccountBankLink | ||
} | ||
|
||
/** | ||
* Striga provides only 1 wallet with two accounts - USDC and EUR | ||
*/ | ||
enum class StrigaWalletAccountCurrency(val currencyName: String) { | ||
USDC("USDC"), EUR("EUR"), OTHER("OTHER") | ||
} |
65 changes: 65 additions & 0 deletions
65
app/src/main/java/org/p2p/wallet/striga/wallet/repository/StrigaUserWalletsMapper.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,65 @@ | ||
package org.p2p.wallet.striga.wallet.repository | ||
|
||
import org.p2p.wallet.striga.wallet.api.response.StrigaUserWalletAccountResponse | ||
import org.p2p.wallet.striga.wallet.api.response.StrigaUserWalletDetailsResponse | ||
import org.p2p.wallet.striga.wallet.api.response.StrigaUserWalletsResponse | ||
import org.p2p.wallet.striga.wallet.models.StrigaUserWallet | ||
import org.p2p.wallet.striga.wallet.models.StrigaUserWalletAccount | ||
import org.p2p.wallet.striga.wallet.models.StrigaWalletAccountBankLink | ||
import org.p2p.wallet.striga.wallet.models.StrigaWalletAccountCurrency | ||
import org.p2p.wallet.striga.wallet.models.ids.StrigaAccountId | ||
import org.p2p.wallet.striga.wallet.models.ids.StrigaWalletId | ||
|
||
class StrigaUserWalletsMapper { | ||
private companion object { | ||
private const val EUR_ACCOUNT_NAME = "EUR" | ||
private const val USDC_ACCOUNT_NAME = "USDC" | ||
private const val UNLINKED_BANK_ACCOUNT_VALUE = "UNLINKED" | ||
} | ||
|
||
fun fromNetwork(userId: String, response: StrigaUserWalletsResponse): StrigaUserWallet? { | ||
if (response.wallets.isEmpty()) { | ||
return null | ||
} | ||
val activeWallet: StrigaUserWalletDetailsResponse = response.wallets.first() | ||
|
||
return StrigaUserWallet( | ||
userId = userId, | ||
walletId = StrigaWalletId(activeWallet.walletId), | ||
// no support for multiple wallets so we get first | ||
accounts = activeWallet.accountCurrencyToDetails.map(::toDomain) | ||
) | ||
} | ||
|
||
private fun toDomain(entry: Map.Entry<String, StrigaUserWalletAccountResponse>): StrigaUserWalletAccount { | ||
val (accountCurrency, accountDetails) = entry | ||
return StrigaUserWalletAccount( | ||
accountId = StrigaAccountId(accountDetails.accountId), | ||
accountStatus = accountDetails.status, | ||
accountCurrency = mapAccountCurrency(accountCurrency), | ||
parentWalletId = accountDetails.parentWalletId, | ||
ownerId = accountDetails.ownerId, | ||
rootFiatCurrency = accountDetails.rootFiatCurrency, | ||
ownerType = accountDetails.ownerType, | ||
availableBalance = accountDetails.availableBalance.amount, | ||
balanceUnit = accountDetails.availableBalance.currencyUnits, | ||
linkedBankAccount = mapLinkedBankAccount(accountDetails.linkedBankAccountId) | ||
) | ||
} | ||
|
||
private fun mapAccountCurrency(currency: String): StrigaWalletAccountCurrency { | ||
return when (currency.uppercase()) { | ||
EUR_ACCOUNT_NAME -> StrigaWalletAccountCurrency.EUR | ||
USDC_ACCOUNT_NAME -> StrigaWalletAccountCurrency.USDC | ||
else -> StrigaWalletAccountCurrency.OTHER | ||
} | ||
} | ||
|
||
private fun mapLinkedBankAccount(linkedBankAccountId: String?): StrigaWalletAccountBankLink { | ||
return if (linkedBankAccountId == null || linkedBankAccountId == UNLINKED_BANK_ACCOUNT_VALUE) { | ||
StrigaWalletAccountBankLink.Unlinked | ||
} else { | ||
StrigaWalletAccountBankLink.Linked(linkedBankAccountId) | ||
} | ||
} | ||
} |
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
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
let's have this function return a non-null type and throw an exception if it can't find any wallets, that would be a pretty weird case, I reckon