Skip to content

Commit

Permalink
Merge pull request #286 from oasisprotocol/mitjat/emerald_tx_endpoint
Browse files Browse the repository at this point in the history
Implement `/emerald/transactions/{tx_hash}`
  • Loading branch information
mitjat authored Jan 19, 2023
2 parents 158fb2b + 94eb4a9 commit d6272d2
Show file tree
Hide file tree
Showing 4 changed files with 78 additions and 3 deletions.
20 changes: 20 additions & 0 deletions api/spec/v1.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -768,6 +768,26 @@ paths:
$ref: '#/components/schemas/RuntimeTransactionList'
<<: *common_error_responses

/emerald/transactions/{tx_hash}:
get:
summary: Returns an Emerald transaction with the given transaction hash.
parameters:
- in: path
name: tx_hash
required: true
schema:
type: string
description: The transaction hash of the transaction to return.
example: *tx_hash_1
responses:
'200':
description: The requested Emerald transactions.
content:
application/json:
schema:
$ref: '#/components/schemas/RuntimeTransaction'
<<: *common_error_responses

/emerald/tokens:
get:
summary: Returns a list of ERC-20 tokens on Emerald.
Expand Down
14 changes: 14 additions & 0 deletions api/v1/strict_server.go
Original file line number Diff line number Diff line change
Expand Up @@ -248,3 +248,17 @@ func (srv *StrictServerImpl) GetEmeraldTransactions(ctx context.Context, request

return apiTypes.GetEmeraldTransactions200JSONResponse(apiTransactions), nil
}

func (srv *StrictServerImpl) GetEmeraldTransactionsTxHash(ctx context.Context, request apiTypes.GetEmeraldTransactionsTxHashRequestObject) (apiTypes.GetEmeraldTransactionsTxHashResponseObject, error) {
storageTx, err := srv.dbClient.RuntimeTransaction(ctx, request.TxHash)
if err != nil {
return nil, err
}

apiTx, err := renderRuntimeTransaction(*storageTx)
if err != nil {
return nil, fmt.Errorf("rendering emerald tx %s: %w", request.TxHash, err)
}

return apiTypes.GetEmeraldTransactionsTxHash200JSONResponse(apiTx), nil
}
40 changes: 40 additions & 0 deletions storage/client/client.go
Original file line number Diff line number Diff line change
Expand Up @@ -1328,6 +1328,7 @@ func (c *StorageClient) RuntimeTransactions(ctx context.Context, p apiTypes.GetE
ctx,
qf.RuntimeTransactionsQuery(),
p.Block,
nil, // tx_hash; filter not supported by this endpoint
p.Limit,
p.Offset,
)
Expand Down Expand Up @@ -1366,6 +1367,45 @@ func (c *StorageClient) RuntimeTransactions(ctx context.Context, p apiTypes.GetE
return &ts, nil
}

// RuntimeTransaction returns a single runtime transaction.
func (c *StorageClient) RuntimeTransaction(ctx context.Context, txHash string) (*RuntimeTransaction, error) {
cid, ok := ctx.Value(common.ChainIDContextKey).(string)
if !ok {
return nil, apiCommon.ErrBadChainID
}
runtime, ok := ctx.Value(common.RuntimeContextKey).(string)
if !ok {
return nil, apiCommon.ErrBadRuntime
}
qf := NewQueryFactory(cid, runtime)

t := RuntimeTransaction{}
err := c.db.QueryRow(
ctx,
qf.RuntimeTransactionsQuery(),
nil, // block; filter not supported by this endpoint
txHash,
1, // limit
0, // offset
).Scan(
&t.Round,
&t.Index,
&t.Hash,
&t.EthHash,
&t.Raw,
&t.ResultRaw,
)
if err != nil {
c.logger.Info("query failed",
"request_id", ctx.Value(common.RequestIDContextKey),
"err", err.Error(),
)
return nil, apiCommon.ErrStorageError
}

return &t, nil
}

func (c *StorageClient) RuntimeTokens(ctx context.Context, p apiTypes.GetEmeraldTokensParams) (*RuntimeTokenList, error) {
cid, ok := ctx.Value(common.ChainIDContextKey).(string)
if !ok {
Expand Down
7 changes: 4 additions & 3 deletions storage/client/queries.go
Original file line number Diff line number Diff line change
Expand Up @@ -337,10 +337,11 @@ func (qf QueryFactory) RuntimeTransactionsQuery() string {
return fmt.Sprintf(`
SELECT round, tx_index, tx_hash, tx_eth_hash, raw, result_raw
FROM %s.%s_transactions
WHERE $1::bigint IS NULL OR round = $1::bigint
WHERE ($1::bigint IS NULL OR round = $1::bigint) AND
($2::text IS NULL OR tx_hash = $2::text)
ORDER BY round DESC, tx_index DESC
LIMIT $2::bigint
OFFSET $3::bigint`, qf.chainID, qf.runtime)
LIMIT $3::bigint
OFFSET $4::bigint`, qf.chainID, qf.runtime)
}

func (qf QueryFactory) RuntimeTokensQuery() string {
Expand Down

0 comments on commit d6272d2

Please sign in to comment.