diff --git a/storage/client/client.go b/storage/client/client.go index 7e70fa4e3..4dde6ebce 100644 --- a/storage/client/client.go +++ b/storage/client/client.go @@ -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" @@ -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)] @@ -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 tokenType sql.NullInt32 if err := res.rows.Scan( &e.Round, &e.TxIndex, @@ -1290,11 +1303,14 @@ func (c *StorageClient) RuntimeEvents(ctx context.Context, p apiTypes.GetRuntime &e.EvmLogName, &e.EvmLogParams, &et.Symbol, - &et.Type, + &tokenType, &et.Decimals, ); err != nil { return nil, wrapError(err) } + if tokenType.Valid { + et.Type = common.Ptr(translateTokenType(evm.EVMTokenType(tokenType.Int32))) + } if et != (apiTypes.EvmEventToken{}) { e.EvmToken = &et } @@ -1376,18 +1392,20 @@ func (c *StorageClient) RuntimeAccount(ctx context.Context, address staking.Addr for runtimeEvmRows.Next() { b := RuntimeEvmBalance{} var addrPreimage []byte + var tokenType evm.EVMTokenType if err = runtimeEvmRows.Scan( &b.Balance, &b.TokenContractAddr, &addrPreimage, &b.TokenSymbol, &b.TokenName, - &b.TokenType, + &tokenType, &b.TokenDecimals, ); err != nil { return nil, wrapError(err) } b.TokenContractAddrEth = ethCommon.BytesToAddress(addrPreimage).String() + b.TokenType = translateTokenType(tokenType) a.EvmBalances = append(a.EvmBalances, b) } @@ -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 tokenType evm.EVMTokenType if err2 := res.rows.Scan( &t.ContractAddr, &addrPreimage, @@ -1481,7 +1500,7 @@ func (c *StorageClient) RuntimeTokens(ctx context.Context, p apiTypes.GetRuntime &t.Decimals, &t.TotalSupply, &t.NumTransfers, - &t.Type, + &tokenType, &t.NumHolders, &t.IsVerified, ); err2 != nil { @@ -1489,6 +1508,7 @@ func (c *StorageClient) RuntimeTokens(ctx context.Context, p apiTypes.GetRuntime } t.EthContractAddr = ethCommon.BytesToAddress(addrPreimage).String() + t.Type = translateTokenType(tokenType) ts.EvmTokens = append(ts.EvmTokens, t) } diff --git a/storage/client/queries/queries.go b/storage/client/queries/queries.go index bce571e96..067a3ae61 100644 --- a/storage/client/queries/queries.go +++ b/storage/client/queries/queries.go @@ -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. @@ -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 @@ -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)