Files
prime-sdk-go/transaction/transaction.go
Andrew Miller d945029f33 add GetInterchain: trace a transaction/block to validator blocks + interchain anchors
New Transaction.GetInterchain and Block.GetInterchain call the prime-node
/api/v1/{transaction,block}/{id}/interchain endpoints, returning an
InterchainTrace {blockId, validatorBlocks, interchainTransactions}. Adds local
VerificationBlock / InterchainTransaction / InterchainTrace model types.
2026-06-02 14:12:49 -04:00

78 lines
2.8 KiB
Go
Executable File

package transaction
import (
"context"
"fmt"
"git.dragonchain.com/dragonchain/prime-sdk-go/client"
"git.dragonchain.com/dragonchain/prime-sdk-go/models"
)
type TransactionClient struct {
client *client.Client
}
func NewTransactionClient(c *client.Client) *TransactionClient {
return &TransactionClient{client: c}
}
// Create submits a new transaction and returns immediately with the assigned transaction ID.
// It does NOT wait for the transaction to be included in a block. Block processing happens
// asynchronously on a ~5-second cycle. If you need the block ID (e.g. for interchain
// verification), poll Get until Header.BlockId is populated.
func (tc *TransactionClient) Create(ctx context.Context, req *models.TransactionCreateRequest) (*models.TransactionCreateResponse, error) {
var resp models.TransactionCreateResponse
err := tc.client.Post(ctx, "/api/v1/transaction", models.ContentTypeJSON, req, &resp)
if err != nil {
return nil, err
}
return &resp, nil
}
// CreateBulk submits multiple transactions and returns immediately with the assigned transaction IDs.
// It does NOT wait for the transactions to be included in a block. Block processing happens
// asynchronously on a ~5-second cycle. If you need block IDs, poll Get for each transaction
// until Header.BlockId is populated.
func (tc *TransactionClient) CreateBulk(ctx context.Context, req *models.TransactionBulkRequest) (*models.TransactionBulkResponse, error) {
var resp models.TransactionBulkResponse
err := tc.client.Post(ctx, "/api/v1/transaction/bulk", models.ContentTypeJSON, req, &resp)
if err != nil {
return nil, err
}
return &resp, nil
}
func (tc *TransactionClient) Get(ctx context.Context, transactionID string) (*models.Transaction, error) {
var resp models.Transaction
path := fmt.Sprintf("/api/v1/transaction/%s", transactionID)
err := tc.client.Get(ctx, path, &resp)
if err != nil {
return nil, err
}
return &resp, nil
}
// GetInterchain traces a transaction to the validator (verification) blocks that
// validated its prime block and the public-chain interchain anchors those
// validator blocks were bundled into. If the transaction is still pending (not
// yet in a block) the trace's slices are empty.
func (tc *TransactionClient) GetInterchain(ctx context.Context, transactionID string) (*models.InterchainTrace, error) {
var resp models.InterchainTrace
path := fmt.Sprintf("/api/v1/transaction/%s/interchain", transactionID)
err := tc.client.Get(ctx, path, &resp)
if err != nil {
return nil, err
}
return &resp, nil
}
func (tc *TransactionClient) List(ctx context.Context) (*models.ListTransactionsResponse, error) {
var resp models.ListTransactionsResponse
path := "/api/v1/transaction/"
err := tc.client.Get(ctx, path, &resp)
if err != nil {
return nil, err
}
return &resp, nil
}