Skip to content

Commit

Permalink
GH-2046 added two new transaction types
Browse files Browse the repository at this point in the history
  • Loading branch information
DmitryBespalov committed Jul 1, 2024
1 parent a9ba2a3 commit 698e27f
Show file tree
Hide file tree
Showing 12 changed files with 395 additions and 3 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,10 @@ enum class TransactionViewType {
REJECTION_QUEUED,
SWAP_ORDER,
SWAP_ORDER_QUEUED,
SWAP_TRANSFER,
SWAP_TRANSFER_QUEUED,
TWAP_ORDER,
TWAP_ORDER_QUEUED,
CONFLICT
}

Expand All @@ -66,6 +70,10 @@ class TransactionViewHolderFactory : BaseFactory<BaseTransactionViewHolder<Trans
TransactionViewType.REJECTION_QUEUED.ordinal -> RejectionQueuedViewHolder(viewBinding as ItemTxQueuedRejectionBinding)
TransactionViewType.SWAP_ORDER.ordinal -> SwapOrderViewHolder(viewBinding as ItemTxSwapOrderBinding)
TransactionViewType.SWAP_ORDER_QUEUED.ordinal -> SwapOrderQueuedViewHolder(viewBinding as ItemTxQueuedSwapOrderBinding)
TransactionViewType.SWAP_TRANSFER.ordinal -> SwapTransferViewHolder(viewBinding as ItemTxSwapOrderBinding)
TransactionViewType.SWAP_TRANSFER_QUEUED.ordinal -> SwapTransferQueuedViewHolder(viewBinding as ItemTxQueuedSwapOrderBinding)
TransactionViewType.TWAP_ORDER.ordinal -> TwapOrderViewHolder(viewBinding as ItemTxSwapOrderBinding)
TransactionViewType.TWAP_ORDER_QUEUED.ordinal -> TwapOrderQueuedViewHolder(viewBinding as ItemTxQueuedSwapOrderBinding)
else -> throw UnsupportedViewType(javaClass.name)
} as BaseTransactionViewHolder<TransactionView>

Expand All @@ -86,6 +94,11 @@ class TransactionViewHolderFactory : BaseFactory<BaseTransactionViewHolder<Trans
TransactionViewType.REJECTION_QUEUED.ordinal -> ItemTxQueuedRejectionBinding.inflate(layoutInflater, parent, false)
TransactionViewType.SWAP_ORDER.ordinal -> ItemTxSwapOrderBinding.inflate(layoutInflater, parent, false)
TransactionViewType.SWAP_ORDER_QUEUED.ordinal -> ItemTxQueuedSwapOrderBinding.inflate(layoutInflater, parent, false)
TransactionViewType.SWAP_TRANSFER.ordinal -> ItemTxSwapOrderBinding.inflate(layoutInflater, parent, false)
TransactionViewType.SWAP_TRANSFER_QUEUED.ordinal -> ItemTxQueuedSwapOrderBinding.inflate(layoutInflater, parent, false)
TransactionViewType.TWAP_ORDER.ordinal -> ItemTxSwapOrderBinding.inflate(layoutInflater, parent, false)
TransactionViewType.TWAP_ORDER_QUEUED.ordinal -> ItemTxQueuedSwapOrderBinding.inflate(layoutInflater, parent, false)

else -> throw UnsupportedViewType(javaClass.name)
}

Expand All @@ -107,6 +120,10 @@ class TransactionViewHolderFactory : BaseFactory<BaseTransactionViewHolder<Trans
is TransactionView.RejectionTransactionQueued -> TransactionViewType.REJECTION_QUEUED
is TransactionView.SwapOrderTransaction -> TransactionViewType.SWAP_ORDER
is TransactionView.SwapOrderTransactionQueued -> TransactionViewType.SWAP_ORDER_QUEUED
is TransactionView.SwapTransferTransaction -> TransactionViewType.SWAP_TRANSFER
is TransactionView.SwapTransferTransactionQueued -> TransactionViewType.SWAP_TRANSFER_QUEUED
is TransactionView.TwapOrderTransaction -> TransactionViewType.TWAP_ORDER
is TransactionView.TwapOrderTransactionQueued -> TransactionViewType.TWAP_ORDER_QUEUED
}.ordinal
}

Expand Down Expand Up @@ -382,6 +399,119 @@ class SwapOrderViewHolder(private val viewBinding: ItemTxSwapOrderBinding) :
}
}

class SwapTransferQueuedViewHolder(private val viewBinding: ItemTxQueuedSwapOrderBinding) :
BaseTransactionViewHolder<TransactionView.SwapTransferTransactionQueued>(viewBinding) {

@OptIn(ExperimentalTime::class)
override fun bind(viewTransfer: TransactionView.SwapTransferTransactionQueued, payloads: List<Any>) {
val resources = viewBinding.root.context.resources
val theme = viewBinding.root.context.theme

with(viewBinding) {
status.setText(viewTransfer.statusText)
status.setTextColor(ResourcesCompat.getColor(resources, viewTransfer.statusColorRes, theme))

dateTime.text = viewTransfer.dateTime.elapsedIntervalTo(Date.from(Instant.now())).format(resources)
addressName.text = resources.getString(R.string.tx_list_swap_transfer)

confirmationsIcon.setImageDrawable(ResourcesCompat.getDrawable(resources, viewTransfer.confirmationsIcon, theme))
confirmations.setTextColor(ResourcesCompat.getColor(resources, viewTransfer.confirmationsTextColor, theme))
confirmations.text = resources.getString(R.string.tx_list_confirmations, viewTransfer.confirmations, viewTransfer.threshold)

nonce.text = viewTransfer.nonce

root.setOnClickListener {
navigateToTxDetails(it, viewTransfer.chain, viewTransfer.id)
}
}
}
}

class SwapTransferViewHolder(private val viewBinding: ItemTxSwapOrderBinding) :
BaseTransactionViewHolder<TransactionView.SwapTransferTransaction>(viewBinding) {

override fun bind(viewTransfer: TransactionView.SwapTransferTransaction, payloads: List<Any>) {
val resources = viewBinding.root.context.resources
val theme = viewBinding.root.context.theme

with(viewBinding) {
finalStatus.setText(viewTransfer.statusText)
finalStatus.setTextColor(ResourcesCompat.getColor(resources, viewTransfer.statusColorRes, theme))
dateTime.text = viewTransfer.dateTimeText

nonce.text = viewTransfer.nonce
addressName.text = resources.getString(R.string.tx_list_swap_transfer)

addressLogo.alpha = viewTransfer.alpha
addressName.alpha = viewTransfer.alpha
finalStatus.alpha = OPACITY_FULL
dateTime.alpha = viewTransfer.alpha
nonce.alpha = viewTransfer.alpha

root.setOnClickListener {
navigateToTxDetails(it, viewTransfer.chain, viewTransfer.id)
}
}
}
}

class TwapOrderQueuedViewHolder(private val viewBinding: ItemTxQueuedSwapOrderBinding) :
BaseTransactionViewHolder<TransactionView.TwapOrderTransactionQueued>(viewBinding) {

@OptIn(ExperimentalTime::class)
override fun bind(viewTransfer: TransactionView.TwapOrderTransactionQueued, payloads: List<Any>) {
val resources = viewBinding.root.context.resources
val theme = viewBinding.root.context.theme

with(viewBinding) {
status.setText(viewTransfer.statusText)
status.setTextColor(ResourcesCompat.getColor(resources, viewTransfer.statusColorRes, theme))

dateTime.text = viewTransfer.dateTime.elapsedIntervalTo(Date.from(Instant.now())).format(resources)
addressName.text = resources.getString(R.string.tx_list_twap_order)

confirmationsIcon.setImageDrawable(ResourcesCompat.getDrawable(resources, viewTransfer.confirmationsIcon, theme))
confirmations.setTextColor(ResourcesCompat.getColor(resources, viewTransfer.confirmationsTextColor, theme))
confirmations.text = resources.getString(R.string.tx_list_confirmations, viewTransfer.confirmations, viewTransfer.threshold)

nonce.text = viewTransfer.nonce

root.setOnClickListener {
navigateToTxDetails(it, viewTransfer.chain, viewTransfer.id)
}
}
}
}

class TwapOrderViewHolder(private val viewBinding: ItemTxSwapOrderBinding) :
BaseTransactionViewHolder<TransactionView.TwapOrderTransaction>(viewBinding) {

override fun bind(viewTransfer: TransactionView.TwapOrderTransaction, payloads: List<Any>) {
val resources = viewBinding.root.context.resources
val theme = viewBinding.root.context.theme

with(viewBinding) {
finalStatus.setText(viewTransfer.statusText)
finalStatus.setTextColor(ResourcesCompat.getColor(resources, viewTransfer.statusColorRes, theme))
dateTime.text = viewTransfer.dateTimeText

nonce.text = viewTransfer.nonce
addressName.text = resources.getString(R.string.tx_list_twap_order)

addressLogo.alpha = viewTransfer.alpha
addressName.alpha = viewTransfer.alpha
finalStatus.alpha = OPACITY_FULL
dateTime.alpha = viewTransfer.alpha
nonce.alpha = viewTransfer.alpha

root.setOnClickListener {
navigateToTxDetails(it, viewTransfer.chain, viewTransfer.id)
}
}
}
}


private fun Resources.getAction(methodName: String?, actionCount: Int?): String? =
if (actionCount != null) {
if (actionCount > 0) this.getQuantityString(R.plurals.tx_list_actions, actionCount, actionCount) else methodName
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -187,6 +187,8 @@ class TransactionListViewModel
toHistoryCreation(chain, txInfo, owner)
}
is TransactionInfo.SwapOrder -> toSwapOrderTransactionView(chain, txInfo, needsYourConfirmation, isConflict)
is TransactionInfo.SwapTransfer -> toSwapTransferTransactionView(chain, txInfo, needsYourConfirmation, isConflict)
is TransactionInfo.TwapOrder -> toTwapOrderTransactionView(chain, txInfo, needsYourConfirmation, isConflict)
TransactionInfo.Unknown -> TransactionView.Unknown
}
}
Expand Down Expand Up @@ -421,6 +423,112 @@ class TransactionListViewModel
)
}

private fun Transaction.toSwapTransferTransactionView(
chain: Chain,
txInfo: TransactionInfo.SwapTransfer,
needsYourConfirmation: Boolean,
isConflict: Boolean
): TransactionView =
if (!isCompleted(txStatus)) queuedSwapTransferTransaction(chain, txInfo, needsYourConfirmation, isConflict)
else historicSwapTransferTransaction(chain, txInfo)

private fun Transaction.historicSwapTransferTransaction(
chain: Chain,
txInfo: TransactionInfo.SwapTransfer
): TransactionView.SwapTransferTransaction {

return TransactionView.SwapTransferTransaction(
chain = chain,
id = id,
status = txStatus,
statusText = displayString(txStatus),
statusColorRes = statusTextColor(txStatus),
dateTimeText = timestamp.formatBackendTimeOfDay(),
alpha = alpha(txStatus),
nonce = executionInfo?.nonce?.toString() ?: "",
explorerUrl = txInfo.explorerUrl
)
}

private fun Transaction.queuedSwapTransferTransaction(
chain: Chain,
txInfo: TransactionInfo.SwapTransfer,
needsYourConfirmation: Boolean,
isConflict: Boolean
): TransactionView.SwapTransferTransactionQueued {

//FIXME this wouldn't make sense for incoming Ethereum TXs
val threshold = executionInfo?.confirmationsRequired ?: -1
val thresholdMet = checkThreshold(threshold, executionInfo?.confirmationsSubmitted)

return TransactionView.SwapTransferTransactionQueued(
chain = chain,
id = id,
status = txStatus,
statusText = displayString(txStatus, needsYourConfirmation),
statusColorRes = statusTextColor(txStatus),
dateTime = timestamp,
confirmations = executionInfo?.confirmationsSubmitted ?: 0,
threshold = threshold,
confirmationsTextColor = if (thresholdMet) R.color.success else R.color.icon,
confirmationsIcon = if (thresholdMet) R.drawable.ic_confirmations_green_16dp else R.drawable.ic_confirmations_grey_16dp,
nonce = if (isConflict) "" else executionInfo?.nonce?.toString() ?: "",
explorerUrl = txInfo.explorerUrl
)
}

private fun Transaction.toTwapOrderTransactionView(
chain: Chain,
txInfo: TransactionInfo.TwapOrder,
needsYourConfirmation: Boolean,
isConflict: Boolean
): TransactionView =
if (!isCompleted(txStatus)) queuedTwapOrderTransaction(chain, txInfo, needsYourConfirmation, isConflict)
else historicTwapOrderTransaction(chain, txInfo)

private fun Transaction.historicTwapOrderTransaction(
chain: Chain,
txInfo: TransactionInfo.TwapOrder
): TransactionView.TwapOrderTransaction {

return TransactionView.TwapOrderTransaction(
chain = chain,
id = id,
status = txStatus,
statusText = displayString(txStatus),
statusColorRes = statusTextColor(txStatus),
dateTimeText = timestamp.formatBackendTimeOfDay(),
alpha = alpha(txStatus),
nonce = executionInfo?.nonce?.toString() ?: ""
)
}

private fun Transaction.queuedTwapOrderTransaction(
chain: Chain,
txInfo: TransactionInfo.TwapOrder,
needsYourConfirmation: Boolean,
isConflict: Boolean
): TransactionView.TwapOrderTransactionQueued {

//FIXME this wouldn't make sense for incoming Ethereum TXs
val threshold = executionInfo?.confirmationsRequired ?: -1
val thresholdMet = checkThreshold(threshold, executionInfo?.confirmationsSubmitted)

return TransactionView.TwapOrderTransactionQueued(
chain = chain,
id = id,
status = txStatus,
statusText = displayString(txStatus, needsYourConfirmation),
statusColorRes = statusTextColor(txStatus),
dateTime = timestamp,
confirmations = executionInfo?.confirmationsSubmitted ?: 0,
threshold = threshold,
confirmationsTextColor = if (thresholdMet) R.color.success else R.color.icon,
confirmationsIcon = if (thresholdMet) R.drawable.ic_confirmations_green_16dp else R.drawable.ic_confirmations_grey_16dp,
nonce = if (isConflict) "" else executionInfo?.nonce?.toString() ?: ""
)
}

private fun Transaction.toRejectionTransactionView(
chain: Chain,
awaitingYourConfirmation: Boolean,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -142,6 +142,58 @@ sealed class TransactionView(
val explorerUrl: String
) : TransactionView(status, id, chain)

data class SwapTransferTransaction(
override val id: String,
override val status: TransactionStatus,
override val chain: Chain,
@StringRes val statusText: Int,
@ColorRes val statusColorRes: Int,
val dateTimeText: String,
val alpha: Float,
val nonce: String,
val explorerUrl: String
) : TransactionView(status, id, chain)

data class SwapTransferTransactionQueued(
override val id: String,
override val status: TransactionStatus,
override val chain: Chain,
@StringRes val statusText: Int,
@ColorRes val statusColorRes: Int,
val dateTime: Date,
val confirmations: Int,
val threshold: Int,
@ColorRes val confirmationsTextColor: Int,
@DrawableRes val confirmationsIcon: Int,
val nonce: String,
val explorerUrl: String
) : TransactionView(status, id, chain)

data class TwapOrderTransaction(
override val id: String,
override val status: TransactionStatus,
override val chain: Chain,
@StringRes val statusText: Int,
@ColorRes val statusColorRes: Int,
val dateTimeText: String,
val alpha: Float,
val nonce: String,
) : TransactionView(status, id, chain)

data class TwapOrderTransactionQueued(
override val id: String,
override val status: TransactionStatus,
override val chain: Chain,
@StringRes val statusText: Int,
@ColorRes val statusColorRes: Int,
val dateTime: Date,
val confirmations: Int,
val threshold: Int,
@ColorRes val confirmationsTextColor: Int,
@DrawableRes val confirmationsIcon: Int,
val nonce: String,
) : TransactionView(status, id, chain)

data class Creation(
override val id: String,
override val status: TransactionStatus,
Expand Down
Loading

0 comments on commit 698e27f

Please sign in to comment.