Skip to content

Commit

Permalink
Merge pull request #289 from oasisprotocol/mitjat/mvp_missing_api_fields
Browse files Browse the repository at this point in the history
api: Implement missing/empty fields for MVP
  • Loading branch information
mitjat authored Jan 26, 2023
2 parents 811c05e + 4390ae3 commit 987a446
Show file tree
Hide file tree
Showing 12 changed files with 377 additions and 124 deletions.
1 change: 1 addition & 0 deletions analyzer/consensus/consensus.go
Original file line number Diff line number Diff line change
Expand Up @@ -373,6 +373,7 @@ func (m *Main) queueBlockInserts(batch *storage.QueryBatch, data *storage.Consen
data.BlockHeader.Height,
data.BlockHeader.Hash.Hex(),
data.BlockHeader.Time.UTC(),
len(data.Transactions),
data.BlockHeader.StateRoot.Namespace.String(),
int64(data.BlockHeader.StateRoot.Version),
data.BlockHeader.StateRoot.Type.String(),
Expand Down
9 changes: 5 additions & 4 deletions analyzer/emerald/emerald.go
Original file line number Diff line number Diff line change
Expand Up @@ -149,6 +149,7 @@ func (m *Main) Start() {

for m.cfg.Range.To == 0 || round <= m.cfg.Range.To {
backoff.Wait()
m.logger.Info("attempting block", "round", round)

if err := m.processRound(ctx, round); err != nil {
if err == analyzer.ErrOutOfRange {
Expand All @@ -166,9 +167,13 @@ func (m *Main) Start() {
continue
}

m.logger.Info("processed block", "round", round)
backoff.Success()
round++
}
m.logger.Info(
fmt.Sprintf("finished processing all blocks in the configured range [%d, %d]",
m.cfg.Range.From, m.cfg.Range.To))
}

// Name returns the name of the Main.
Expand Down Expand Up @@ -216,10 +221,6 @@ func (m *Main) prework() error {
// from source storage and committing an atomically-executed batch of queries
// to target storage.
func (m *Main) processRound(ctx context.Context, round uint64) error {
m.logger.Info("processing round",
"round", round,
)

ctxWithTimeout, cancel := context.WithTimeout(ctx, ProcessRoundTimeout)
defer cancel()
group, groupCtx := errgroup.WithContext(ctxWithTimeout)
Expand Down
8 changes: 4 additions & 4 deletions analyzer/queries.go
Original file line number Diff line number Diff line change
Expand Up @@ -46,8 +46,8 @@ func (qf QueryFactory) GenesisIndexingProgressQuery() string {

func (qf QueryFactory) ConsensusBlockInsertQuery() string {
return fmt.Sprintf(`
INSERT INTO %s.blocks (height, block_hash, time, namespace, version, type, root_hash)
VALUES ($1, $2, $3, $4, $5, $6, $7)`, qf.chainID)
INSERT INTO %s.blocks (height, block_hash, time, num_txs, namespace, version, type, root_hash)
VALUES ($1, $2, $3, $4, $5, $6, $7, $8)`, qf.chainID)
}

func (qf QueryFactory) ConsensusEpochInsertQuery() string {
Expand Down Expand Up @@ -411,10 +411,10 @@ func (qf QueryFactory) RuntimeWithdrawInsertQuery() string {

func (qf QueryFactory) RuntimeNativeBalanceUpdateQuery() string {
return fmt.Sprintf(`
INSERT INTO %[1]s.runtime_native_balances (runtime, account_address, symbol, balance)
INSERT INTO %[1]s.runtime_sdk_balances (runtime, account_address, symbol, balance)
VALUES ('%[2]s', $1, $2, $3)
ON CONFLICT (runtime, account_address, symbol) DO
UPDATE SET balance = %[1]s.runtime_native_balances.balance + $3`, qf.chainID, qf.runtime)
UPDATE SET balance = %[1]s.runtime_sdk_balances.balance + $3`, qf.chainID, qf.runtime)
}

func (qf QueryFactory) RuntimeGasUsedInsertQuery() string {
Expand Down
131 changes: 90 additions & 41 deletions api/spec/v1.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -788,20 +788,19 @@ paths:
$ref: '#/components/schemas/RuntimeTransaction'
<<: *common_error_responses

/emerald/tokens:
/emerald/evm_tokens:
get:
summary: Returns a list of ERC-20 tokens on Emerald.
summary: Returns a list of EVM (ERC-20, ...) tokens on Emerald.
parameters:
- *limit
- *offset
responses:
'200':
description: |
A JSON object containing a list of ERC-20 tokens on Emerald.
description: The requested tokens.
content:
application/json:
schema:
$ref: '#/components/schemas/RuntimeTokenList'
$ref: '#/components/schemas/EvmTokenList'
<<: *common_error_responses

/{layer}/stats/tx_volume:
Expand Down Expand Up @@ -1354,12 +1353,12 @@ components:
description: Version of the `context`.
address_data:
type: string
format: hex
format: byte
description: |
The hex-encoded data from which the oasis address was derived.
The base64-encoded data from which the oasis address was derived.
When `context = "oasis-runtime-sdk/address: secp256k1eth"`, this
is the Ethereum address (without the leading `0x`). All-lowercase.
example: '9907a0cf64ec9fbf6ed8fd4971090de88222a9ac'
is the Ethereum address (in base64, not hex!).
example: 'INLp2Ih3YIdcA+zFNhM+SIGyFgKsYYc9SKQeKRKe2uI='

RuntimeName:
description: |
Expand All @@ -1370,31 +1369,55 @@ components:
- emerald
example: emerald

RuntimeBalance:
description: Balance of an account in a runtime.
RuntimeSdkBalance:
description: Balance of an account for a specific runtime and oasis-sdk token (e.g. ROSE).
type: object
required: [amount, runtime, token_id, token_symbol]
required: [balance, runtime, token_symbol, token_decimals]
properties:
amount:
balance:
<<: *BigIntType
description: Number of tokens held, in base units.
runtime:
$ref: "#/components/schemas/RuntimeName"
token_symbol:
type: string
description: The token ticker symbol. Unique across all oasis-sdk tokens in the same runtime.
example: ROSE
token_decimals:
type: integer
description: The number of decimals of precision for this token.
example: 18

RuntimeEvmBalance:
description: Balance of an account for a specific runtime and EVM token.
type: object
required: [balance, runtime, token_contract_addr, token_decimals]
properties:
balance:
<<: *BigIntType
description: Number of base units held; as a string.
description: Number of tokens held, in base units.
runtime:
$ref: "#/components/schemas/RuntimeName"
# TODO:
# - How do we handle NFTs? Add another field?
# - We do not handle non-native tokens from the `accounts` SDK module. The plan is to use
# their denomination name for both `token_id` and `token_symbol`, or update the SDK to have tickers.
token_id:
x-go-name: TokenID
token_contract_addr:
type: string
description: Unique identifier for the token. For EVM tokens, this is their eth address.
description: The EVM address of this token's contract. Encoded as a lowercase hex string.
example: 'dc19a122e268128b5ee20366299fc7b5b199c8e3'
token_symbol:
type: string
description: The token ticker symbol. Not guaranteed to be unique across distinct tokens.
description: The token ticker symbol. Not guaranteed to be unique across distinct EVM tokens.
token_name:
type: string
description: The name of the token. Not guaranteed to be unique across distinct EVM tokens.
token_type:
$ref: "#/components/schemas/EvmTokenType"
token_decimals:
type: integer
description: The number of decimals of precision for this token.
example: 18

Account:
type: object
required: [address, address_preimage, nonce, runtime_balances, available, escrow, debonding, delegations_balance, debonding_delegations_balance, allowances]
required: [address, nonce, available, escrow, debonding, allowances]
properties:
address:
type: string
Expand All @@ -1407,12 +1430,22 @@ components:
format: int64
description: A nonce used to prevent replay.
example: 0
# TODO: limit this to 1000 entries. If folks have more,
# we can eventually open up a separate, paginable endpoint just for balances.
runtime_balances:
runtime_sdk_balances:
description: |
The balances of this account in each runtime, as managed by oasis-sdk.
NOTE 1: This field is omitted for efficiency when listing multiple accounts.
NOTE 2: This field is limited to 1000 entries. If you need more, please let us know in a GitHub issue.
type: array
items:
$ref: '#/components/schemas/RuntimeSdkBalance'
runtime_evm_balances:
description: |
The balances of this account in each runtime, as managed by EVM smart contracts (notably, ERC-20).
NOTE 1: This field is omitted for efficiency when listing multiple accounts.
NOTE 2: This field is limited to 1000 entries. If you need more, please let us know in a GitHub issue.
type: array
items:
$ref: '#/components/schemas/RuntimeBalance'
$ref: '#/components/schemas/RuntimeEvmBalance'
available:
<<: *BigIntType
description: The available balance, in base units.
Expand All @@ -1424,10 +1457,14 @@ components:
description: The debonding escrow balance, in base units.
delegations_balance:
<<: *BigIntType
description: The delegations balance, in base units.
description: |
The delegations balance, in base units.
For efficiency, this field is omitted when listing multiple-accounts.
debonding_delegations_balance:
<<: *BigIntType
description: The debonding delegations balance, in base units.
description: |
The debonding delegations balance, in base units.
For efficiency, this field is omitted when listing multiple-accounts.
example: 10000000000
allowances:
type: array
Expand Down Expand Up @@ -1504,7 +1541,7 @@ components:
runtime_committee_protocol:
type: string
description: The target propotocol versions for this upgrade proposal.

Proposal:
type: object
# TODO: Revise required fields. Most fields are required for actual proposals, and only absent
Expand Down Expand Up @@ -1653,13 +1690,18 @@ components:
RuntimeTransaction:
type: object
# NOTE: Not guaranteed to be present: eth_hash, to, amount.
required: [round, timestamp, hash, sender_0, nonce_0, fee, gas_limit, method, body, success]
required: [round, index, timestamp, hash, sender_0, nonce_0, fee, gas_limit, method, body, success]
properties:
round:
type: integer
format: int64
description: The block round at which this transaction was executed.
example: 3379702
index:
type: integer
format: int64
description: The 0-based index of this transaction in the block.
example: 0
timestamp:
type: string
format: date-time
Expand Down Expand Up @@ -1741,34 +1783,41 @@ components:
- ERC20
- ERC721
- ERC1155
- OasisSdk
description: |
The type of a EVM token.
RuntimeTokenList:
EvmTokenList:
type: object
required: [tokens]
required: [evm_tokens]
properties:
tokens:
evm_tokens:
type: array
items:
$ref: '#/components/schemas/RuntimeToken'
description: |
A list of ERC-20 tokens on a runtime.
$ref: '#/components/schemas/EvmToken'
description: A list of L2 EVM tokens (ERC-20, ERC-271, ...).
description: A list of tokens in a runtime.

RuntimeToken:
EvmToken:
type: object
required: [contract_addr, num_holders, type]
required: [contract_addr, evm_contract_addr, num_holders, type]
properties:
contract_addr:
type: string
description: The Oasis address of this token's contract.
example: 'oasis1qp2hssandc7dekjdr6ygmtzt783k3gn38uupdeys'
evm_contract_addr:
type: string
description: The EVM address of this token's contract. Encoded as a lowercase hex string.
example: 'dc19a122e268128b5ee20366299fc7b5b199c8e3'
name:
type: string
description: Name of the token, as provided by token contract's `name()` method.
example: Uniswap
symbol:
type: string
description: Symbol of the token, as provided by token contract's `symbol()` method.
example: UNI
example: USDT
decimals:
type: integer
description: |
Expand All @@ -1784,7 +1833,7 @@ components:
ERC-1363 token might be labeled as ERC-20 here. If the type cannot be
detected or is not supported, this field will be null/absent.
total_supply:
type: string
<<: *BigIntType
description: The total number of base units available.
num_holders:
type: integer
Expand Down
1 change: 1 addition & 0 deletions api/v1/logic.go
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,7 @@ func renderRuntimeTransaction(storageTransaction client.RuntimeTransaction) (api
}
apiTransaction := apiTypes.RuntimeTransaction{
Round: storageTransaction.Round,
Index: storageTransaction.Index,
Hash: storageTransaction.Hash,
EthHash: storageTransaction.EthHash,
// TODO: Get timestamp from that round's block
Expand Down
4 changes: 2 additions & 2 deletions api/v1/strict_server.go
Original file line number Diff line number Diff line change
Expand Up @@ -227,12 +227,12 @@ func (srv *StrictServerImpl) GetEmeraldBlocks(ctx context.Context, request apiTy
return apiTypes.GetEmeraldBlocks200JSONResponse(*blocks), nil
}

func (srv *StrictServerImpl) GetEmeraldTokens(ctx context.Context, request apiTypes.GetEmeraldTokensRequestObject) (apiTypes.GetEmeraldTokensResponseObject, error) {
func (srv *StrictServerImpl) GetEmeraldEvmTokens(ctx context.Context, request apiTypes.GetEmeraldEvmTokensRequestObject) (apiTypes.GetEmeraldEvmTokensResponseObject, error) {
tokens, err := srv.dbClient.RuntimeTokens(ctx, request.Params)
if err != nil {
return nil, err
}
return apiTypes.GetEmeraldTokens200JSONResponse(*tokens), nil
return apiTypes.GetEmeraldEvmTokens200JSONResponse(*tokens), nil
}

func (srv *StrictServerImpl) GetEmeraldTransactions(ctx context.Context, request apiTypes.GetEmeraldTransactionsRequestObject) (apiTypes.GetEmeraldTransactionsResponseObject, error) {
Expand Down
Loading

0 comments on commit 987a446

Please sign in to comment.