Skip to content

Commit

Permalink
Run account update when unshielding completes
Browse files Browse the repository at this point in the history
  • Loading branch information
Radiokot committed Jun 5, 2024
1 parent 8a705e5 commit 5f3201f
Showing 1 changed file with 27 additions and 29 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@ import com.concordium.wallet.data.room.Account
import com.concordium.wallet.data.room.Transfer
import com.concordium.wallet.data.room.WalletDatabase
import com.concordium.wallet.ui.account.common.accountupdater.AccountUpdater
import com.concordium.wallet.ui.account.common.accountupdater.TotalBalancesData
import com.concordium.wallet.ui.common.BackendErrorHandler
import com.concordium.wallet.ui.transaction.sendfunds.SendFundsViewModel
import com.concordium.wallet.util.DateTimeUtil
Expand Down Expand Up @@ -243,9 +244,6 @@ class UnshieldingViewModel(application: Application) : AndroidViewModel(applicat
amount = amount,
submissionId = submissionId
)

_isUnshieldEnabledLiveData.postValue(true)
_waitingLiveData.postValue(false)
}

private suspend fun submitTransfer(
Expand All @@ -266,26 +264,13 @@ class UnshieldingViewModel(application: Application) : AndroidViewModel(applicat
amount: BigInteger,
submissionId: String,
) {
var newSelfEncryptedAmount: String? = null

if (createTransferOutput.remaining != null) {
newSelfEncryptedAmount = createTransferOutput.remaining
val remainingAmount =
accountUpdater.decryptAndSaveAmount(
credentialsOutput.encryptionSecretKey,
createTransferOutput.remaining
)
val newSelfEncryptedAmount: String? = createTransferOutput.remaining

account.finalizedEncryptedBalance?.let { encBalance ->
val oldDecryptedAmount =
accountUpdater.lookupMappedAmount(encBalance.selfAmount)
oldDecryptedAmount?.let {
accountUpdater.saveDecryptedAmount(
createTransferOutput.remaining,
remainingAmount.toString()
)
}
}
if (newSelfEncryptedAmount != null) {
accountUpdater.decryptAndSaveAmount(
credentialsOutput.encryptionSecretKey,
newSelfEncryptedAmount
)
}

val newTransfer = Transfer(
Expand All @@ -308,14 +293,27 @@ class UnshieldingViewModel(application: Application) : AndroidViewModel(applicat
)
transferRepository.insert(newTransfer)

_finishWithResultLiveData.postValue(
Event(
UnshieldingResult(
unshieldedAmount = amount,
accountAddress = account.address,
accountUpdater.setUpdateListener(object : AccountUpdater.UpdateListener {
override fun onError(stringRes: Int) {
_errorLiveData.postValue(Event(stringRes))
_isUnshieldEnabledLiveData.postValue(true)
_waitingLiveData.postValue(false)
}

override fun onDone(totalBalances: TotalBalancesData) {
_finishWithResultLiveData.postValue(
Event(
UnshieldingResult(
unshieldedAmount = amount,
accountAddress = account.address,
)
)
)
)
)
}

override fun onNewAccountFinalized(accountName: String) {}
})
accountUpdater.updateForAccount(account)
}

private fun getBalanceAtDisposal(): BigInteger =
Expand Down

0 comments on commit 5f3201f

Please sign in to comment.