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

feat(crowdnode): limit withdrawals to once per block #1229

Merged
merged 5 commits into from
Nov 28, 2023
Merged
Show file tree
Hide file tree
Changes from 1 commit
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
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,7 @@ import org.dash.wallet.common.data.Resource
import org.dash.wallet.common.data.ServiceName
import org.dash.wallet.common.data.Status
import org.dash.wallet.common.data.TaxCategory
import org.dash.wallet.common.services.BlockchainStateProvider
import org.dash.wallet.common.services.LeftoverBalanceException
import org.dash.wallet.common.services.NotificationService
import org.dash.wallet.common.services.TransactionMetadataProvider
Expand Down Expand Up @@ -94,6 +95,7 @@ class CrowdNodeApiAggregator @Inject constructor(
private val config: CrowdNodeConfig,
private val globalConfig: Configuration,
private val transactionMetadataProvider: TransactionMetadataProvider,
private val blockchainStateProvider: BlockchainStateProvider,
@ApplicationContext private val appContext: Context
) : CrowdNodeApi {
companion object {
Expand Down Expand Up @@ -126,6 +128,7 @@ class CrowdNodeApiAggregator @Inject constructor(
private set
override var notificationIntent: Intent? = null
override var showNotificationOnResult = false
var currentBlockHeight = -1

init {
walletDataProvider.attachOnWalletWipedListener {
Expand Down Expand Up @@ -155,6 +158,12 @@ class CrowdNodeApiAggregator @Inject constructor(
}
}
.launchIn(configScope)

blockchainStateProvider.observeState()
.onEach {
it?.run { currentBlockHeight = bestChainHeight }
}
.launchIn(statusScope)
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It might make sense to expose currentBlockHeight as a property (if possible) or suspend function to avoid dancing with observables. Here and in many other places, we might only need to get it once.

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Good point.

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

We have a such a suspend function already.

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This has been resolved.

}

override suspend fun restoreStatus() {
Expand Down Expand Up @@ -299,6 +308,7 @@ class CrowdNodeApiAggregator @Inject constructor(
if (result.messageStatus.lowercase() == MESSAGE_RECEIVED_STATUS) {
log.info("Withdrawal request sent successfully")
refreshBalance(retries = 3, afterWithdrawal = true)
config.set(CrowdNodeConfig.LAST_WITHDRAWAL_BLOCK, currentBlockHeight)
true
} else {
log.info("Withdrawal request not received, status: ${result.messageStatus}. Result: ${result.result}")
Expand Down Expand Up @@ -331,6 +341,9 @@ class CrowdNodeApiAggregator @Inject constructor(
config.get(CrowdNodeConfig.WITHDRAWAL_LIMIT_PER_DAY)
?: CrowdNodeConstants.WithdrawalLimits.DEFAULT_LIMIT_PER_DAY.value
}
else -> {
0L
}
}
)
}
Expand Down Expand Up @@ -824,6 +837,10 @@ class CrowdNodeApiAggregator @Inject constructor(
if (withdrawalsLast24h.add(value) > perDayLimit) {
throw WithdrawalLimitsException(perDayLimit, WithdrawalLimitPeriod.PerDay)
}
val lastWithrawBlock = config.get(CrowdNodeConfig.LAST_WITHDRAWAL_BLOCK)
if (lastWithrawBlock != null && lastWithrawBlock >= currentBlockHeight) {
throw WithdrawalLimitsException(value, WithdrawalLimitPeriod.PerBlock)
}
}

private fun refreshWithdrawalLimits() {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,8 @@ data class WithdrawalLimit(
enum class WithdrawalLimitPeriod {
PerTransaction,
PerHour,
PerDay
PerDay,
PerBlock
Comment on lines -33 to +34
Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

We add PerBlock limit period.

}

data class WithdrawalLimitsException(
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,7 @@ import org.dash.wallet.common.ui.viewBinding
import org.dash.wallet.integrations.crowdnode.R
import org.dash.wallet.integrations.crowdnode.databinding.DialogWithdrawalLimitsBinding
import org.dash.wallet.integrations.crowdnode.model.WithdrawalLimitPeriod
import java.lang.IllegalArgumentException

class WithdrawalLimitsInfoDialog(
private val limitPerTx: Coin,
Expand Down Expand Up @@ -79,6 +80,9 @@ class WithdrawalLimitsInfoDialog(
binding.perDayLimit.setTextColor(warningColor)
TextViewCompat.setCompoundDrawableTintList(binding.perDayLimit, colorStateLit)
}
else -> {
throw IllegalArgumentException("highlightedLimit $highlightedLimit not supported")
}
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -339,30 +339,51 @@ class TransferFragment : Fragment(R.layout.fragment_transfer) {
}

private suspend fun showWithdrawalLimitsError(period: WithdrawalLimitPeriod) {
val limits = viewModel.getWithdrawalLimits()
val okButtonText = if (period == WithdrawalLimitPeriod.PerTransaction) {
if (viewModel.onlineAccountStatus == OnlineAccountStatus.Done) {
getString(R.string.read_withdrawal_policy)
} else {
getString(R.string.online_account_create)
if (period == WithdrawalLimitPeriod.PerBlock) {
AdaptiveDialog.create(
R.drawable.ic_warning,
"Please wait before initiating the next withdrawal",
"Please wait 5 minutes before initiating another withdrawal",
getString(R.string.button_okay)
).show(requireActivity()) {
// TODO: what do we do here?
// close this page?
// findNavController().popBackStack()
// show an error?
safeNavigate(
TransferFragmentDirections.transferToResult(
true,
getString(R.string.crowdnode_withdraw_error),
""
)
)
Copy link
Member

@Syn-McJ Syn-McJ Nov 23, 2023

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

To properly handle the progress dialog, we need to invoke the suspend method for showing the dialog which is called showAsync (not sure if it's a good name). The return value can be discarded.

Suggested change
).show(requireActivity()) {
// TODO: what do we do here?
// close this page?
// findNavController().popBackStack()
// show an error?
safeNavigate(
TransferFragmentDirections.transferToResult(
true,
getString(R.string.crowdnode_withdraw_error),
""
)
)
).showAsync(requireActivity())

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Fixed

}
} else {
""
}

val doAction = WithdrawalLimitsInfoDialog(
limits[0],
limits[1],
limits[2],
highlightedLimit = period,
okButtonText = okButtonText
).showAsync(requireActivity())

if (doAction == true) {
if (viewModel.onlineAccountStatus == OnlineAccountStatus.Done) {
openWithdrawalPolicy()
val limits = viewModel.getWithdrawalLimits()
val okButtonText = if (period == WithdrawalLimitPeriod.PerTransaction) {
if (viewModel.onlineAccountStatus == OnlineAccountStatus.Done) {
getString(R.string.read_withdrawal_policy)
} else {
getString(R.string.online_account_create)
}
} else {
safeNavigate(TransferFragmentDirections.transferToOnlineAccountEmail())
""
}

val doAction = WithdrawalLimitsInfoDialog(
limits[0],
limits[1],
limits[2],
highlightedLimit = period,
okButtonText = okButtonText
).showAsync(requireActivity())

if (doAction == true) {
if (viewModel.onlineAccountStatus == OnlineAccountStatus.Done) {
openWithdrawalPolicy()
} else {
safeNavigate(TransferFragmentDirections.transferToOnlineAccountEmail())
}
}
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -46,5 +46,6 @@ open class CrowdNodeConfig @Inject constructor(
val WITHDRAWAL_LIMIT_PER_TX = longPreferencesKey("withdrawal_limit_per_tx")
val WITHDRAWAL_LIMIT_PER_HOUR = longPreferencesKey("withdrawal_limit_per_hour")
val WITHDRAWAL_LIMIT_PER_DAY = longPreferencesKey("withdrawal_limit_per_day")
val LAST_WITHDRAWAL_BLOCK = intPreferencesKey("last_withdrawal_block")
}
}
Loading