Skip to content

Commit

Permalink
add: no_send option to prevent transaction sending on-chain (#12)
Browse files Browse the repository at this point in the history
* add: no_send option to prevent transaction sending on-chain

* remove: no_send option from service config

* refactor: pass no_send option in request body for each request
  • Loading branch information
mhrynenko authored Dec 23, 2024
1 parent fecac50 commit e80278f
Show file tree
Hide file tree
Showing 4 changed files with 15 additions and 5 deletions.
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

0 comments on commit e80278f

Please sign in to comment.