Skip to content

Commit

Permalink
wip-vote
Browse files Browse the repository at this point in the history
  • Loading branch information
dkoukoul committed Feb 19, 2024
1 parent ad29522 commit 2899424
Show file tree
Hide file tree
Showing 7 changed files with 45 additions and 19 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -354,6 +354,21 @@ class WalletRepositoryImpl @Inject constructor() : WalletRepository {
}
}

override suspend fun sendVote(fromAddresses: List<String>, voteFor: String, isCandidate: Boolean): Result<SendVoteResponse> {
val request = SendVoteRequest(voteFor, fromAddresses, isCandidate)
val response = walletAPI.sendVote(request)
if (response.message.isNotEmpty()) {
Timber.e("sendVote: Failed: ${response.message}")
return Result.failure(Exception("Failed to send vote: ${response.message}"))
} else if (response.txHash.isNotEmpty()){
Timber.d("sendVote: success")
return Result.success(response)
} else {
Timber.e("sendVote: Failed, empty response")
return Result.failure(Exception("Failed to send vote"))
}
}

override suspend fun createTransaction(fromAddresses: List<String>, amount: Double, toAddress: String): Result<CreateTransactionResponse> {
val request = CreateTransactionRequest(toAddress, amount, fromAddresses, true)
val response = walletAPI.createTransaction(request)
Expand Down Expand Up @@ -441,5 +456,4 @@ class WalletRepositoryImpl @Inject constructor() : WalletRepository {
override suspend fun decodeTransaction(binTx: String): String {
return walletAPI.decodeTransaction(binTx)
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -37,8 +37,6 @@ class VoteBottomSheet : StateBottomSheet<VoteState>(R.layout.bottom_sheet_vote)
voteButton.doOnClick {
viewModel.onVoteClick()
}

updateAmountInputPadding()
}
}

Expand All @@ -53,21 +51,6 @@ class VoteBottomSheet : StateBottomSheet<VoteState>(R.layout.bottom_sheet_vote)
}
}

private fun updateAmountInputPadding() {
with(viewBinding) {
/*if (maxCheckbox.width > 0) {
val newPadding = amountInput.paddingStart + maxCheckbox.width
Timber.d("newPadding: $newPadding")
amountInput.updatePaddingRelative(end = newPadding)
} else {
maxCheckbox.doOnNextLayout {
updateAmountInputPadding()
}
}*/
}
}

override fun handleState(state: VoteState) {
with(viewBinding) {
candidateVoteCheckbox.apply {
Expand Down Expand Up @@ -98,7 +81,6 @@ class VoteBottomSheet : StateBottomSheet<VoteState>(R.layout.bottom_sheet_vote)
viewModel.onWithdrawVoteCheckChanged(isChecked)
}
}
// maxLabel.isVisible = state.maxValueSelected
voteButton.isEnabled = state.voteButtonEnabled
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -65,6 +65,7 @@ class SendTransactionViewModel @Inject constructor(
toaddress = walletRepository.isPKTAddressValid(toaddress).getOrThrow()
}.onSuccess {
Timber.i("VoteViewModel onVoteClick| PKT address is valid")

//TODO: update this event
/*sendNavigation(
AppNavigation.OpenSendConfirm(
Expand Down
16 changes: 16 additions & 0 deletions pkt_domain/src/main/java/com/pkt/domain/dto/PldRequests.kt
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,14 @@ data class SendTransactionRequest(
val from_address: List<String>
)

@Keep
@Serializable
data class SendVoteRequest(
val vote_for: String,
val from_address: List<String>,
val is_candidate: Boolean
)

@Keep
@Serializable
data class CreateTransactionRequest(
Expand All @@ -59,6 +67,14 @@ data class SendTransactionResponse(
val stack: String = ""
)

@Keep
@Serializable
data class SendVoteResponse(
val txHash: String,
var message: String = "",
val stack: String = ""
)

@Keep
@Serializable
data class DecodeTransactionRequest(
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,8 @@ interface WalletAPI {
suspend fun createWallet(@Body recoverWalletRequest: RecoverWalletRequest): CreateWalletResponse
@POST("wallet/transaction/sendfrom")
suspend fun sendTransaction(@Body sendRequest: SendTransactionRequest): SendTransactionResponse
@POST("wallet/transaction/sendvote")
suspend fun sendVote(@Body sendRequest: SendVoteRequest): SendVoteResponse
@POST("wallet/transaction/create")
suspend fun createTransaction(@Body createRequest: CreateTransactionRequest): CreateTransactionResponse
@POST("wallet/checkpassphrase")
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -127,6 +127,15 @@ class WalletAPIService {
}
}

suspend fun sendVote(request: SendVoteRequest): SendVoteResponse {
try {
return api.sendVote(request)
} catch (e: Exception) {
Timber.d("sendVote: failed with message ${e.message}")
return SendVoteResponse("","${e.message}", e.stackTraceToString())
}
}

suspend fun createTransaction(request: CreateTransactionRequest): CreateTransactionResponse {
return try {
api.createTransaction(request)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -76,4 +76,6 @@ interface WalletRepository {
fun getActiveWalletUri(): Uri?

suspend fun decodeTransaction(binTx: String): String

suspend fun sendVote(fromAddresses: List<String>, voteFor: String, isCandidate: Boolean): Result<SendVoteResponse>
}

0 comments on commit 2899424

Please sign in to comment.