Skip to content

Commit

Permalink
Merge branch 'chary/multisig-sign-update' of github.com:vitwit/resolu…
Browse files Browse the repository at this point in the history
…te into chary/multisig-sign-update
  • Loading branch information
charymalloju committed Nov 12, 2024
2 parents 6d212a6 + e437e91 commit 4182e14
Showing 1 changed file with 37 additions and 18 deletions.
55 changes: 37 additions & 18 deletions server/handler/transactions.go
Original file line number Diff line number Diff line change
Expand Up @@ -246,11 +246,11 @@ func (h *Handler) GetAllMultisigTxns(c echo.Context) error {

var rows *sql.Rows
if status == "PENDING" {
rows, err = h.DB.Query(`SELECT t.id,t.multisig_address,t.status,t.created_at,t.last_updated,t.memo,t.signatures,t.messages,t.hash,t.err_msg,t.fee, m.threshold,
rows, err = h.DB.Query(`SELECT t.id, COALESCE(t.signed_at, '0001-01-01 00:00:00'::timestamp) AS signed_at, t.multisig_address,t.status,t.created_at,t.last_updated,t.memo,t.signatures,t.messages,t.hash,t.err_msg,t.fee, m.threshold,
json_agg(jsonb_build_object('pubkey', p.pubkey, 'address', p.address, 'multisig_address',p.multisig_address)) AS pubkeys FROM transactions t JOIN multisig_accounts m ON t.multisig_address = m.address JOIN pubkeys p ON t.multisig_address = p.multisig_address WHERE t.multisig_address=$1 and t.status='PENDING' GROUP BY t.id, t.multisig_address, m.threshold, t.messages LIMIT $2 OFFSET $3`,
multisigAddress, limit, (page-1)*limit)
} else {
rows, err = h.DB.Query(`SELECT t.id,t.multisig_address,t.status,t.created_at,t.last_updated,t.memo,t.signatures,t.messages,t.hash,t.err_msg,t.fee, m.threshold,
rows, err = h.DB.Query(`SELECT t.id, COALESCE(t.signed_at, '0001-01-01 00:00:00'::timestamp) AS signed_at, t.multisig_address,t.status,t.created_at,t.last_updated,t.memo,t.signatures,t.messages,t.hash,t.err_msg,t.fee, m.threshold,
json_agg(jsonb_build_object('pubkey', p.pubkey, 'address', p.address, 'multisig_address',p.multisig_address)) AS pubkeys FROM transactions t JOIN multisig_accounts m ON t.multisig_address = m.address JOIN pubkeys p ON t.multisig_address = p.multisig_address WHERE t.multisig_address=$1 and t.status <> 'PENDING' GROUP BY t.id, t.multisig_address, m.threshold, t.messages LIMIT $2 OFFSET $3`,
multisigAddress, limit, (page-1)*limit)
}
Expand All @@ -272,8 +272,11 @@ func (h *Handler) GetAllMultisigTxns(c echo.Context) error {

for rows.Next() {
var transaction schema.AllTransactionResult
var signedAt time.Time

if err := rows.Scan(
&transaction.ID,
&signedAt,
&transaction.MultisigAddress,
&transaction.Status,
&transaction.CreatedAt,
Expand All @@ -293,6 +296,11 @@ func (h *Handler) GetAllMultisigTxns(c echo.Context) error {
Log: err.Error(),
})
}
if signedAt.IsZero() {
transaction.SignedAt = time.Time{}
} else {
transaction.SignedAt = signedAt
}
transactions = append(transactions, transaction)
}
rows.Close()
Expand Down Expand Up @@ -509,35 +517,46 @@ func (h *Handler) DeleteTransaction(c echo.Context) error {
if err != nil {
return c.JSON(http.StatusBadRequest, model.ErrorResponse{
Status: "error",
Message: "invalid transaction id ",
Message: "invalid transaction id",
})
}

_, err = h.DB.Exec(`DELETE from transactions WHERE id=$1 AND multisig_address=$2`, txId, address)
// Fetch signed_at before attempting to delete, to avoid issues if the transaction does not exist
var signedAt time.Time
row := h.DB.QueryRow(`SELECT signed_at FROM transactions WHERE id=$1 AND multisig_address=$2`, txId, address)
err = row.Scan(&signedAt)
if err != nil {
if err == sql.ErrNoRows {
return c.JSON(http.StatusNotFound, model.ErrorResponse{
Status: "error",
Message: "transaction not found",
})
}
return c.JSON(http.StatusInternalServerError, model.ErrorResponse{
Status: "error",
Message: "failed to fetch transaction details",
Log: err.Error(),
})
}

// Delete the transaction
_, err = h.DB.Exec(`DELETE FROM transactions WHERE id=$1 AND multisig_address=$2`, txId, address)
if err != nil {
return c.JSON(http.StatusBadRequest, model.ErrorResponse{
return c.JSON(http.StatusInternalServerError, model.ErrorResponse{
Status: "error",
Message: "failed to delete transaction",
Log: err.Error(),
})
}

row := h.DB.QueryRow(`SELECT signed_at FROM transactions WHERE id=$1 AND multisig_address=$2`, txId, address)

var transaction schema.Transaction
row.Scan(
&transaction.Signatures,
)

if !transaction.SignedAt.IsZero() {
txSignedAt := transaction.SignedAt

_, err = h.DB.Exec("UPDATE transactions SET signatures='[]'::jsonb WHERE multisig_address=$1 AND signed_at > $2", address, txSignedAt)
// Clear signatures for transactions with signed_at > txSignedAt
if !signedAt.IsZero() {
_, err = h.DB.Exec(`UPDATE transactions SET signatures='[]'::jsonb WHERE multisig_address=$1 AND signed_at > $2`, address, signedAt)
if err != nil {
return c.JSON(http.StatusBadRequest, model.ErrorResponse{
return c.JSON(http.StatusInternalServerError, model.ErrorResponse{
Status: "error",
Message: err.Error(),
Message: "failed to update transaction signatures",
Log: err.Error(),
})
}
}
Expand Down

0 comments on commit 4182e14

Please sign in to comment.