Skip to content

Commit

Permalink
api: Add /{runtime}/evm_tokens/{address} endpoint
Browse files Browse the repository at this point in the history
  • Loading branch information
mitjat committed Jun 16, 2023
1 parent e298352 commit 20b97e2
Show file tree
Hide file tree
Showing 4 changed files with 40 additions and 4 deletions.
20 changes: 20 additions & 0 deletions api/spec/v1.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -986,6 +986,26 @@ paths:
$ref: '#/components/schemas/EvmTokenList'
<<: *common_error_responses

/{runtime}/evm_tokens/{address}:
get:
summary: Returns info on an EVM (ERC-20, ...) token on the runtime.
parameters:
- *runtime
- in: path
name: address
required: true
schema:
<<: *StakingAddressType
description: The staking address of the account to return.
responses:
'200':
description: The requested token.
content:
application/json:
schema:
$ref: '#/components/schemas/EvmToken'
<<: *common_error_responses

/{runtime}/accounts/{address}:
get:
summary: Returns a runtime account.
Expand Down
14 changes: 13 additions & 1 deletion api/v1/strict_server.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ import (
"fmt"

apiTypes "github.com/oasisprotocol/oasis-indexer/api/v1/types"
"github.com/oasisprotocol/oasis-indexer/common"
"github.com/oasisprotocol/oasis-indexer/log"
"github.com/oasisprotocol/oasis-indexer/storage/client"
)
Expand Down Expand Up @@ -260,13 +261,24 @@ func (srv *StrictServerImpl) GetRuntimeBlocks(ctx context.Context, request apiTy
}

func (srv *StrictServerImpl) GetRuntimeEvmTokens(ctx context.Context, request apiTypes.GetRuntimeEvmTokensRequestObject) (apiTypes.GetRuntimeEvmTokensResponseObject, error) {
tokens, err := srv.dbClient.RuntimeTokens(ctx, request.Params)
tokens, err := srv.dbClient.RuntimeTokens(ctx, request.Params, nil)
if err != nil {
return nil, err
}
return apiTypes.GetRuntimeEvmTokens200JSONResponse(*tokens), nil
}

func (srv *StrictServerImpl) GetRuntimeEvmTokensAddress(ctx context.Context, request apiTypes.GetRuntimeEvmTokensAddressRequestObject) (apiTypes.GetRuntimeEvmTokensAddressResponseObject, error) {
tokens, err := srv.dbClient.RuntimeTokens(ctx, apiTypes.GetRuntimeEvmTokensParams{Limit: common.Ptr(uint64(1))}, &request.Address)
if err != nil {
return nil, err
}
if len(tokens.EvmTokens) != 1 {
return apiTypes.GetRuntimeEvmTokensAddress404JSONResponse{}, nil
}
return apiTypes.GetRuntimeEvmTokensAddress200JSONResponse(tokens.EvmTokens[0]), nil
}

func (srv *StrictServerImpl) GetRuntimeTransactions(ctx context.Context, request apiTypes.GetRuntimeTransactionsRequestObject) (apiTypes.GetRuntimeTransactionsResponseObject, error) {
transactions, err := srv.dbClient.RuntimeTransactions(ctx, request.Params, nil)
if err != nil {
Expand Down
5 changes: 4 additions & 1 deletion storage/client/client.go
Original file line number Diff line number Diff line change
Expand Up @@ -1423,11 +1423,14 @@ func (c *StorageClient) RuntimeAccount(ctx context.Context, address staking.Addr
return &a, nil
}

func (c *StorageClient) RuntimeTokens(ctx context.Context, p apiTypes.GetRuntimeEvmTokensParams) (*EvmTokenList, error) {
// If `address` is non-nil, it is used to filter the results to at most 1 token: the one
// with the correcponding contract address.
func (c *StorageClient) RuntimeTokens(ctx context.Context, p apiTypes.GetRuntimeEvmTokensParams, address *staking.Address) (*EvmTokenList, error) {
res, err := c.withTotalCount(
ctx,
queries.EvmTokens,
runtimeFromCtx(ctx),
address,
p.Limit,
p.Offset,
)
Expand Down
5 changes: 3 additions & 2 deletions storage/client/queries/queries.go
Original file line number Diff line number Diff line change
Expand Up @@ -458,10 +458,11 @@ const (
JOIN holders USING (token_address)
WHERE
(tokens.runtime = $1) AND
($2::oasis_addr IS NULL OR tokens.token_address = $2::oasis_addr) AND
tokens.token_type != 0 -- exclude unknown-type tokens; they're often just contracts that emitted Transfer events but don't expose the token ticker, name, balance etc.
ORDER BY num_holders DESC
LIMIT $2::bigint
OFFSET $3::bigint`
LIMIT $3::bigint
OFFSET $4::bigint`

AccountRuntimeSdkBalances = `
SELECT
Expand Down

0 comments on commit 20b97e2

Please sign in to comment.