Skip to content

Commit

Permalink
Implement SubscribePendingTransactions
Browse files Browse the repository at this point in the history
  • Loading branch information
thiagodeev committed Jan 22, 2025
1 parent f386d32 commit 4d61a71
Show file tree
Hide file tree
Showing 2 changed files with 59 additions and 0 deletions.
36 changes: 36 additions & 0 deletions rpc/types_transaction.go
Original file line number Diff line number Diff line change
Expand Up @@ -401,3 +401,39 @@ func (v *TransactionVersion) BigInt() (*big.Int, error) {
return big.NewInt(-1), errors.New(fmt.Sprint("TransactionVersion %i not supported", *v))
}
}

// SubPendingTxnsInput is the optional input of the starknet_subscribePendingTransactions subscription.
type SubPendingTxnsInput struct {
// Get all transaction details, and not only the hash. If not provided, only hash is returned. Default is false
TransactionDetails bool `json:"transaction_details,omitempty"`
// Filter transactions to only receive notification from address list
SenderAddress *felt.Felt `json:"sender_address,omitempty"`
}

// SubPendingTxns is the response of the starknet_subscribePendingTransactions subscription.
type SubPendingTxns struct {
// The hashes of the pending transactions. Only present if transactionDetails is false.
TransactionHashes []*felt.Felt
// The full transaction details. Only present if transactionDetails is true.
Transactions []*BlockTransaction
}

// UnmarshalJSON unmarshals the JSON data into a SubPendingTxns object.
//
// Parameters:
// - data: The JSON data to be unmarshalled
// Returns:
// - error: An error if the unmarshalling process fails
func (s *SubPendingTxns) UnmarshalJSON(data []byte) error {
var txnsHashes []*felt.Felt
if err := json.Unmarshal(data, &txnsHashes); err == nil {
s.TransactionHashes = txnsHashes
return nil
}
var txns []*BlockTransaction
if err := json.Unmarshal(data, &txns); err == nil {
s.Transactions = txns
return nil
}
return errors.New("failed to unmarshal SubPendingTxns")
}
23 changes: 23 additions & 0 deletions rpc/websocket.go
Original file line number Diff line number Diff line change
Expand Up @@ -91,3 +91,26 @@ func (provider *WsProvider) SubscribeTransactionStatus(ctx context.Context, newS
// }
return sub, nil
}

// New Pending Transactions subscription
// Creates a WebSocket stream which will fire events when a new pending transaction is added.
// While there is no mempool, this notifies of transactions in the pending block.
//
// Parameters:
// - ctx: The context.Context object for controlling the function call
// - pendingTxns: The channel to send the new pending transactions to
// - options: The optional input struct containing the optional filters. Set to nil if no filters are needed.
// Returns:
// - clientSubscription: The client subscription object, used to unsubscribe from the stream and to get errors
// - error: An error, if any
func (provider *WsProvider) SubscribePendingTransactions(ctx context.Context, pendingTxns chan<- *SubPendingTxns, options *SubPendingTxnsInput) (*client.ClientSubscription, error) {
if options == nil {
options = &SubPendingTxnsInput{}
}

sub, err := provider.c.Subscribe(ctx, "starknet", "_subscribePendingTransactions", pendingTxns, options)
if err != nil {
return nil, tryUnwrapToRPCErr(err, ErrTooManyAddressesInFilter)
}
return sub, nil
}

0 comments on commit 4d61a71

Please sign in to comment.