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

add: no_send option to prevent transaction sending on-chain #12

Merged
merged 3 commits into from
Dec 23, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
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 @@ -18,6 +18,10 @@ post:
properties:
tx_data:
type: string
no_send:
type: bool
description: Flag indicates whether transaction should be sent on-chain
example: true
destination:
type: string
example: "0xC0B09085Fa2ad3A8BbF96494B8d5cd10702FE20d"
Expand Down
13 changes: 9 additions & 4 deletions internal/service/handlers/registration.go
Original file line number Diff line number Diff line change
Expand Up @@ -84,15 +84,13 @@ func Registration(w http.ResponseWriter, r *http.Request) {
return
}

tx, err := sendTx(r, &txd, &registrationAddress)
tx, err := sendTx(r, &txd, &registrationAddress, req.Data.NoSend)
if err != nil {
Log(r).WithError(err).Error("failed to send tx")
ape.RenderErr(w, problems.InternalError())
return
}

RelayerConfig(r).IncrementNonce()

ape.Render(w, newTxResponse(tx))
}

Expand Down Expand Up @@ -130,12 +128,17 @@ func confGas(r *http.Request, txd *txData, receiver *common.Address) (err error)
return nil
}

func sendTx(r *http.Request, txd *txData, receiver *common.Address) (tx *types.Transaction, err error) {
func sendTx(r *http.Request, txd *txData, receiver *common.Address, noSend bool) (tx *types.Transaction, err error) {
tx, err = signTx(r, txd, receiver)
if err != nil {
return nil, fmt.Errorf("failed to sign new tx: %w", err)
}

if noSend {
Log(r).WithField("hash", tx.Hash().String()).Warn("transaction sending disabled")
return tx, nil
}

if err = RelayerConfig(r).RPC.SendTransaction(r.Context(), tx); err != nil {
if strings.Contains(err.Error(), "nonce") {
if err = RelayerConfig(r).ResetNonce(RelayerConfig(r).RPC); err != nil {
Expand All @@ -155,6 +158,8 @@ func sendTx(r *http.Request, txd *txData, receiver *common.Address) (tx *types.T
}
}

RelayerConfig(r).IncrementNonce()

return tx, nil
}

Expand Down
2 changes: 1 addition & 1 deletion internal/service/handlers/transit_state.go
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,7 @@ func TransitState(w http.ResponseWriter, r *http.Request) {
return
}

tx, err := sendTx(r, &txd, RelayerConfig(r).LightweightStateAddress)
tx, err := sendTx(r, &txd, RelayerConfig(r).LightweightStateAddress, false)
if err != nil {
Log(r).WithError(err).Error("failed to send tx")
ape.RenderErr(w, problems.InternalError())
Expand Down
1 change: 1 addition & 0 deletions internal/service/requests/registration.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@ var (

type RegistrationRequestData struct {
TxData string `json:"tx_data"`
NoSend bool `json:"no_send"`
Destination *string `json:"destination,omitempty"`
}

Expand Down
Loading