Skip to content

Commit

Permalink
storage: move token type translation to go
Browse files Browse the repository at this point in the history
  • Loading branch information
pro-wh committed Sep 28, 2023
1 parent 6754434 commit 6c815e1
Show file tree
Hide file tree
Showing 2 changed files with 26 additions and 18 deletions.
26 changes: 23 additions & 3 deletions storage/client/client.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@ import (
oasisConfig "github.com/oasisprotocol/oasis-sdk/client-sdk/go/config"
sdkTypes "github.com/oasisprotocol/oasis-sdk/client-sdk/go/types"

"github.com/oasisprotocol/nexus/analyzer/runtime/evm"
"github.com/oasisprotocol/nexus/analyzer/util"
apiCommon "github.com/oasisprotocol/nexus/api"
apiTypes "github.com/oasisprotocol/nexus/api/v1/types"
Expand Down Expand Up @@ -46,6 +47,17 @@ type StorageClient struct {
logger *log.Logger
}

func translateTokenType(tokenType evm.EVMTokenType) apiTypes.EvmTokenType {
switch tokenType {
case evm.EVMTokenTypeERC20:
return apiTypes.EvmTokenTypeERC20
case evm.EVMTokenTypeERC721:
return apiTypes.EvmTokenTypeERC721
default:
return "unexpected_other_type"
}
}

// runtimeNameToID returns the runtime ID for the given network and runtime name.
func runtimeNameToID(chainName common.ChainName, name common.Runtime) (string, error) {
network, exists := oasisConfig.DefaultNetworks.All[string(chainName)]
Expand Down Expand Up @@ -1279,6 +1291,7 @@ func (c *StorageClient) RuntimeEvents(ctx context.Context, p apiTypes.GetRuntime
for res.rows.Next() {
var e RuntimeEvent
var et apiTypes.EvmEventToken
var tt sql.NullInt32
if err := res.rows.Scan(
&e.Round,
&e.TxIndex,
Expand All @@ -1290,11 +1303,14 @@ func (c *StorageClient) RuntimeEvents(ctx context.Context, p apiTypes.GetRuntime
&e.EvmLogName,
&e.EvmLogParams,
&et.Symbol,
&et.Type,
&tt,
&et.Decimals,
); err != nil {
return nil, wrapError(err)
}
if tt.Valid {
et.Type = common.Ptr(translateTokenType(evm.EVMTokenType(tt.Int32)))
}
if et != (apiTypes.EvmEventToken{}) {
e.EvmToken = &et
}
Expand Down Expand Up @@ -1376,18 +1392,20 @@ func (c *StorageClient) RuntimeAccount(ctx context.Context, address staking.Addr
for runtimeEvmRows.Next() {
b := RuntimeEvmBalance{}
var addrPreimage []byte
var tt evm.EVMTokenType
if err = runtimeEvmRows.Scan(
&b.Balance,
&b.TokenContractAddr,
&addrPreimage,
&b.TokenSymbol,
&b.TokenName,
&b.TokenType,
&tt,
&b.TokenDecimals,
); err != nil {
return nil, wrapError(err)
}
b.TokenContractAddrEth = ethCommon.BytesToAddress(addrPreimage).String()
b.TokenType = translateTokenType(tt)
a.EvmBalances = append(a.EvmBalances, b)
}

Expand Down Expand Up @@ -1473,6 +1491,7 @@ func (c *StorageClient) RuntimeTokens(ctx context.Context, p apiTypes.GetRuntime
for res.rows.Next() {
var t EvmToken
var addrPreimage []byte
var tt evm.EVMTokenType
if err2 := res.rows.Scan(
&t.ContractAddr,
&addrPreimage,
Expand All @@ -1481,14 +1500,15 @@ func (c *StorageClient) RuntimeTokens(ctx context.Context, p apiTypes.GetRuntime
&t.Decimals,
&t.TotalSupply,
&t.NumTransfers,
&t.Type,
&tt,
&t.NumHolders,
&t.IsVerified,
); err2 != nil {
return nil, wrapError(err2)
}

t.EthContractAddr = ethCommon.BytesToAddress(addrPreimage).String()
t.Type = translateTokenType(tt)
ts.EvmTokens = append(ts.EvmTokens, t)
}

Expand Down
18 changes: 3 additions & 15 deletions storage/client/queries/queries.go
Original file line number Diff line number Diff line change
Expand Up @@ -383,11 +383,7 @@ const (
evs.evm_log_name,
evs.evm_log_params,
tokens.symbol,
CASE -- NOTE: There are three queries that use this CASE via copy-paste; edit both if changing.
WHEN tokens.token_type = 20 THEN 'ERC20'
WHEN tokens.token_type = 721 THEN 'ERC721'
ELSE NULL
END AS token_type,
tokens.token_type,
tokens.decimals
FROM chain.runtime_events as evs
-- Look up the oasis-style address derived from evs.body.address.
Expand Down Expand Up @@ -465,11 +461,7 @@ const (
tokens.decimals,
tokens.total_supply,
tokens.num_transfers,
CASE -- NOTE: There are three queries that use this CASE via copy-paste; edit both if changing.
WHEN tokens.token_type = 20 THEN 'ERC20'
WHEN tokens.token_type = 721 THEN 'ERC721'
ELSE 'unexpected_other_type' -- Our openapi spec doesn't allow us to output this, but better this than a null value (which causes nil dereference)
END AS type,
tokens.token_type AS type,
holders.cnt AS num_holders,
(contracts.verification_info_downloaded_at IS NOT NULL) AS is_verified
FROM chain.evm_tokens AS tokens
Expand Down Expand Up @@ -520,11 +512,7 @@ const (
preimages.address_data AS token_address_eth,
tokens.symbol AS token_symbol,
tokens.token_name AS token_name,
CASE -- NOTE: There are three queries that use this CASE via copy-paste; edit both if changing.
WHEN tokens.token_type = 20 THEN 'ERC20'
WHEN tokens.token_type = 721 THEN 'ERC721'
ELSE 'unexpected_other_type' -- Our openapi spec doesn't allow us to output this, but better this than a null value (which causes nil dereference)
END AS token_type,
tokens.token_type,
tokens.decimals AS token_decimals
FROM chain.evm_token_balances AS balances
JOIN chain.address_preimages AS preimages ON (preimages.address = balances.token_address AND preimages.context_identifier = 'oasis-runtime-sdk/address: secp256k1eth' AND preimages.context_version = 0)
Expand Down

0 comments on commit 6c815e1

Please sign in to comment.