Skip to content

Commit

Permalink
fix: transactions
Browse files Browse the repository at this point in the history
  • Loading branch information
HashMapsData2Value committed Oct 24, 2024
1 parent 0c9bca6 commit b97e143
Show file tree
Hide file tree
Showing 4 changed files with 61 additions and 74 deletions.
90 changes: 38 additions & 52 deletions ui/pages/transaction/controller.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package transaction

import (
"encoding/base64"
"fmt"

"github.com/algorand/go-algorand-sdk/v2/types"
Expand All @@ -19,15 +20,11 @@ func (m ViewModel) Update(msg tea.Msg) (tea.Model, tea.Cmd) {
}

func (m *ViewModel) UpdateTxnURLAndQRCode() error {

accountStatus := m.State.Accounts[m.Data.Address].Status

m.hint = ""

senderAddress, err := types.DecodeAddress(m.Data.Address)
if err != nil {
return err
}

var isOnline bool
switch accountStatus {
case "Online":
Expand All @@ -37,65 +34,54 @@ func (m *ViewModel) UpdateTxnURLAndQRCode() error {
case "NotParticipating": // This status means the account can never participate in consensus
m.urlTxn = ""
m.asciiQR = ""
m.hint = fmt.Sprintf("%s is NotParticipating. Cannot register key.", senderAddress)
m.hint = fmt.Sprintf("%s is NotParticipating. Cannot register key.", m.Data.Address)
return nil
}

// Construct Transaction
tx := types.Transaction{}
fee := uint64(1000)

kr := &encoder.AUrlTxn{}

if !isOnline {

if !isOnline { // TX take account online
var stateProofPk types.MerkleVerifier
copy(stateProofPk[:], (*m.Data.Key.StateProofKey)[:])
// TX take account online

tx = types.Transaction{
Type: types.KeyRegistrationTx,
Header: types.Header{
Sender: senderAddress,
Fee: 1000, //TODO: get proper fee
votePartKey := base64.RawURLEncoding.EncodeToString(m.Data.Key.VoteParticipationKey)
selPartKey := base64.RawURLEncoding.EncodeToString(m.Data.Key.SelectionParticipationKey)
spKey := base64.RawURLEncoding.EncodeToString(*m.Data.Key.StateProofKey)
firstValid := uint64(m.Data.Key.VoteFirstValid)
lastValid := uint64(m.Data.Key.VoteLastValid)
vkDilution := uint64(m.Data.Key.VoteKeyDilution)

kr = &encoder.AUrlTxn{
AUrlTxnKeyCommon: encoder.AUrlTxnKeyCommon{
Sender: m.Data.Address,
Type: string(types.KeyRegistrationTx),
Fee: &fee,
},
KeyregTxnFields: types.KeyregTxnFields{
VotePK: types.VotePK(m.Data.Key.VoteParticipationKey),
SelectionPK: types.VRFPK(m.Data.Key.SelectionParticipationKey),
StateProofPK: types.MerkleVerifier(stateProofPk),
VoteFirst: types.Round(m.Data.Key.VoteFirstValid),
VoteLast: types.Round(m.Data.Key.VoteLastValid),
VoteKeyDilution: uint64(m.Data.Key.VoteKeyDilution),
AUrlTxnKeyreg: encoder.AUrlTxnKeyreg{
VotePK: &votePartKey,
SelectionPK: &selPartKey,
StateProofPK: &spKey,
VoteFirst: &firstValid,
VoteLast: &lastValid,
VoteKeyDilution: &vkDilution,
},
}

// Update hint if no error
defer func() {
if err == nil {
m.hint = fmt.Sprintf("Scan this QR code to take %s Online.", senderAddress)
}
}()

} else { // TX to take account offline
tx = types.Transaction{
Type: types.KeyRegistrationTx,
Header: types.Header{
Sender: senderAddress,
Fee: 1000, //TODO: get proper fee
},
}
m.hint = fmt.Sprintf("Scan this QR code to take %s Online.", m.Data.Address)

// Update hint if no error
defer func() {
if err == nil {
m.hint = fmt.Sprintf("Scan this QR code to take %s Offline.", senderAddress)
}
}()
}
} else {

// Construct QR Code
kr, err := encoder.MakeQRKeyRegRequest(
encoder.RawTxn{
Txn: tx,
})
// TX to take account offline
kr = &encoder.AUrlTxn{
AUrlTxnKeyCommon: encoder.AUrlTxnKeyCommon{
Sender: m.Data.Address,
Type: string(types.KeyRegistrationTx),
Fee: &fee,
}}

if err != nil {
return err
m.hint = fmt.Sprintf("Scan this QR code to take %s Offline.", m.Data.Address)
}

qrCode, err := kr.ProduceQRCode()
Expand Down
2 changes: 1 addition & 1 deletion ui/pages/transaction/model.go
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,6 @@ type ViewModel struct {
func New(state *internal.StateModel) ViewModel {
return ViewModel{
State: state,
controls: controls.New(" (a)ccounts | (k)eys | (t)xn "),
controls: controls.New(" (a)ccounts | (k)eys | shift+tab: back "),
}
}
17 changes: 17 additions & 0 deletions ui/pages/transaction/style.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
package transaction

import "github.com/charmbracelet/lipgloss"

var qrStyle = lipgloss.NewStyle().
Foreground(lipgloss.Color("15")).
Background(lipgloss.Color("0"))

var urlStyle = lipgloss.NewStyle().Foreground(lipgloss.Color("#2596be"))

var red = lipgloss.NewStyle().
Foreground(lipgloss.Color("9"))

var yellow = lipgloss.NewStyle().
Foreground(lipgloss.Color("11"))

var Padding1 = lipgloss.NewStyle().Padding()
26 changes: 5 additions & 21 deletions ui/pages/transaction/view.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,23 +6,6 @@ import (

func (m ViewModel) View() string {

qrStyle := lipgloss.NewStyle().
Foreground(lipgloss.Color("15")).
Background(lipgloss.Color("0"))

urlStyle := lipgloss.NewStyle().
Width(m.Width - 2)

red := lipgloss.NewStyle().
Foreground(lipgloss.Color("9")).
Width(m.Width - 2)

yellow := lipgloss.NewStyle().
Foreground(lipgloss.Color("11")).
Width(m.Width - 2)

var Padding1 = lipgloss.NewStyle().Padding().Render

if m.asciiQR == "" || m.urlTxn == "" {
return lipgloss.JoinVertical(
lipgloss.Left,
Expand All @@ -34,14 +17,15 @@ func (m ViewModel) View() string {
if m.QRWontFit {
return lipgloss.JoinVertical(
lipgloss.Left,
red.Render("QR Code too large to display... Please adjust terminal dimensions or font."),
red.Width(m.Width-2).Render("QR Code too large to display... Please adjust terminal dimensions or font."),
m.controls.View())
}

return lipgloss.JoinVertical(
lipgloss.Left,
yellow.Render(m.hint),
Padding1(),
yellow.Width(m.Width-2).Render(m.hint),
Padding1.Render(),
qrStyle.Render(m.asciiQR),
urlStyle.Render(m.urlTxn),
urlStyle.Width(m.Width-2).Render(m.urlTxn),
m.controls.View())
}

0 comments on commit b97e143

Please sign in to comment.