|
| 1 | +package paymaster |
| 2 | + |
| 3 | +import ( |
| 4 | + "context" |
| 5 | + "fmt" |
| 6 | + "strconv" |
| 7 | + |
| 8 | + "github.com/NethermindEth/juno/core/felt" |
| 9 | + "github.com/NethermindEth/starknet.go/client/rpcerr" |
| 10 | +) |
| 11 | + |
| 12 | +// TrackingIDToLatestHash gets the latest transaction hash and status for a given tracking ID. |
| 13 | +// Returns a TrackingIdResponse. |
| 14 | +// |
| 15 | +// Parameters: |
| 16 | +// - ctx: The context.Context object for controlling the function call |
| 17 | +// - trackingID: A unique identifier used to track an execution request of a user. |
| 18 | +// This identitifier is returned by the paymaster after a successful call to `execute`. |
| 19 | +// Its purpose is to track the possibly different transaction hashes in the mempool which |
| 20 | +// are associated with a same user request. |
| 21 | +// |
| 22 | +// Returns: |
| 23 | +// - *TrackingIDResponse: The hash of the latest transaction broadcasted by the paymaster |
| 24 | +// corresponding to the requested ID and the status of the ID. |
| 25 | +// - error: An error if any |
| 26 | +func (p *Paymaster) TrackingIDToLatestHash( |
| 27 | + ctx context.Context, |
| 28 | + trackingID *felt.Felt, |
| 29 | +) (TrackingIDResponse, error) { |
| 30 | + var response TrackingIDResponse |
| 31 | + if err := p.c.CallContextWithSliceArgs( |
| 32 | + ctx, |
| 33 | + &response, |
| 34 | + "paymaster_trackingIdToLatestHash", |
| 35 | + trackingID, |
| 36 | + ); err != nil { |
| 37 | + return TrackingIDResponse{}, rpcerr.UnwrapToRPCErr(err, ErrInvalidID) |
| 38 | + } |
| 39 | + |
| 40 | + return response, nil |
| 41 | +} |
| 42 | + |
| 43 | +// TrackingIDResponse is the response for the `paymaster_trackingIdToLatestHash` method. |
| 44 | +type TrackingIDResponse struct { |
| 45 | + // The hash of the most recent tx sent by the paymaster and corresponding to the ID |
| 46 | + TransactionHash *felt.Felt `json:"transaction_hash"` |
| 47 | + // The status of the transaction associated with the ID |
| 48 | + Status TxnStatus `json:"status"` |
| 49 | +} |
| 50 | + |
| 51 | +// An enum representing the status of the transaction associated with a tracking ID |
| 52 | +type TxnStatus int |
| 53 | + |
| 54 | +const ( |
| 55 | + // Indicates that the latest transaction associated with the ID is not yet |
| 56 | + // included in a block but is still being handled and monitored by the paymaster. |
| 57 | + // Represents the "active" string value. |
| 58 | + TxnStatusActive TxnStatus = iota + 1 |
| 59 | + // Indicates that a transaction associated with the ID has been accepted on L2. |
| 60 | + // Represents the "accepted" string value. |
| 61 | + TxnStatusAccepted |
| 62 | + // Indicates that no transaction associated with the ID managed to enter a block |
| 63 | + // and the request has been dropped by the paymaster. |
| 64 | + // Represents the "dropped" string value. |
| 65 | + TxnStatusDropped |
| 66 | +) |
| 67 | + |
| 68 | +// String returns the string representation of the TxnStatus. |
| 69 | +func (t TxnStatus) String() string { |
| 70 | + return []string{"active", "accepted", "dropped"}[t-1] |
| 71 | +} |
| 72 | + |
| 73 | +// MarshalJSON marshals the TxnStatus to JSON. |
| 74 | +func (t TxnStatus) MarshalJSON() ([]byte, error) { |
| 75 | + return strconv.AppendQuote(nil, t.String()), nil |
| 76 | +} |
| 77 | + |
| 78 | +// UnmarshalJSON unmarshals the JSON data into a TxnStatus. |
| 79 | +func (t *TxnStatus) UnmarshalJSON(b []byte) error { |
| 80 | + s, err := strconv.Unquote(string(b)) |
| 81 | + if err != nil { |
| 82 | + return err |
| 83 | + } |
| 84 | + |
| 85 | + switch s { |
| 86 | + case "active": |
| 87 | + *t = TxnStatusActive |
| 88 | + case "accepted": |
| 89 | + *t = TxnStatusAccepted |
| 90 | + case "dropped": |
| 91 | + *t = TxnStatusDropped |
| 92 | + default: |
| 93 | + return fmt.Errorf("invalid transaction status: %s", s) |
| 94 | + } |
| 95 | + |
| 96 | + return nil |
| 97 | +} |
0 commit comments