Skip to content

Commit

Permalink
chore(blockbook): rename type to standard
Browse files Browse the repository at this point in the history
  • Loading branch information
enjojoy committed Jan 13, 2025
1 parent 70f34ce commit 5db31d1
Show file tree
Hide file tree
Showing 9 changed files with 95 additions and 80 deletions.
52 changes: 28 additions & 24 deletions api/types.go
Original file line number Diff line number Diff line change
Expand Up @@ -158,21 +158,23 @@ type MultiTokenValue struct {

// Token contains info about tokens held by an address
type Token struct {
Type bchain.TokenTypeName `json:"type" ts_type:"'XPUBAddress' | 'ERC20' | 'ERC721' | 'ERC1155'"`
Name string `json:"name"`
Path string `json:"path,omitempty"`
Contract string `json:"contract,omitempty"`
Transfers int `json:"transfers"`
Symbol string `json:"symbol,omitempty"`
Decimals int `json:"decimals,omitempty"`
BalanceSat *Amount `json:"balance,omitempty"`
BaseValue float64 `json:"baseValue,omitempty"` // value in the base currency (ETH for Ethereum)
SecondaryValue float64 `json:"secondaryValue,omitempty"` // value in secondary (fiat) currency, if specified
Ids []Amount `json:"ids,omitempty"` // multiple ERC721 tokens
MultiTokenValues []MultiTokenValue `json:"multiTokenValues,omitempty"` // multiple ERC1155 tokens
TotalReceivedSat *Amount `json:"totalReceived,omitempty"`
TotalSentSat *Amount `json:"totalSent,omitempty"`
ContractIndex string `json:"-"`
// Deprecated: Use Standard instead.
Type bchain.TokenStandardName `json:"type" ts_type:"'XPUBAddress' | 'ERC20' | 'ERC721' | 'ERC1155'"`
Standard bchain.TokenStandardName `json:"standard" ts_type:"'XPUBAddress' | 'ERC20' | 'ERC721' | 'ERC1155'"`
Name string `json:"name"`
Path string `json:"path,omitempty"`
Contract string `json:"contract,omitempty"`
Transfers int `json:"transfers"`
Symbol string `json:"symbol,omitempty"`
Decimals int `json:"decimals,omitempty"`
BalanceSat *Amount `json:"balance,omitempty"`
BaseValue float64 `json:"baseValue,omitempty"` // value in the base currency (ETH for Ethereum)
SecondaryValue float64 `json:"secondaryValue,omitempty"` // value in secondary (fiat) currency, if specified
Ids []Amount `json:"ids,omitempty"` // multiple ERC721 tokens
MultiTokenValues []MultiTokenValue `json:"multiTokenValues,omitempty"` // multiple ERC1155 tokens
TotalReceivedSat *Amount `json:"totalReceived,omitempty"`
TotalSentSat *Amount `json:"totalSent,omitempty"`
ContractIndex string `json:"-"`
}

// Tokens is array of Token
Expand Down Expand Up @@ -204,15 +206,17 @@ func (a Tokens) Less(i, j int) bool {

// TokenTransfer contains info about a token transfer done in a transaction
type TokenTransfer struct {
Type bchain.TokenTypeName `json:"type"`
From string `json:"from"`
To string `json:"to"`
Contract string `json:"contract"`
Name string `json:"name,omitempty"`
Symbol string `json:"symbol,omitempty"`
Decimals int `json:"decimals,omitempty"`
Value *Amount `json:"value,omitempty"`
MultiTokenValues []MultiTokenValue `json:"multiTokenValues,omitempty"`
// Deprecated: Use Standard instead.
Type bchain.TokenStandardName `json:"type"`
Standard bchain.TokenStandardName `json:"standard"`
From string `json:"from"`
To string `json:"to"`
Contract string `json:"contract"`
Name string `json:"name,omitempty"`
Symbol string `json:"symbol,omitempty"`
Decimals int `json:"decimals,omitempty"`
Value *Amount `json:"value,omitempty"`
MultiTokenValues []MultiTokenValue `json:"multiTokenValues,omitempty"`
}

type EthereumInternalTransfer struct {
Expand Down
24 changes: 12 additions & 12 deletions api/worker.go
Original file line number Diff line number Diff line change
Expand Up @@ -622,15 +622,15 @@ func (w *Worker) GetTransactionFromMempoolTx(mempoolTx *bchain.MempoolTx) (*Tx,
return r, nil
}

func (w *Worker) GetContractInfo(contract string, typeFromContext bchain.TokenTypeName) (*bchain.ContractInfo, bool, error) {
func (w *Worker) GetContractInfo(contract string, typeFromContext bchain.TokenStandardName) (*bchain.ContractInfo, bool, error) {
cd, err := w.chainParser.GetAddrDescFromAddress(contract)
if err != nil {
return nil, false, err
}
return w.getContractDescriptorInfo(cd, typeFromContext)
}

func (w *Worker) getContractDescriptorInfo(cd bchain.AddressDescriptor, typeFromContext bchain.TokenTypeName) (*bchain.ContractInfo, bool, error) {
func (w *Worker) getContractDescriptorInfo(cd bchain.AddressDescriptor, typeFromContext bchain.TokenStandardName) (*bchain.ContractInfo, bool, error) {
var err error
validContract := true
contractInfo, err := w.db.GetContractInfo(cd, typeFromContext)
Expand All @@ -647,22 +647,22 @@ func (w *Worker) getContractDescriptorInfo(cd bchain.AddressDescriptor, typeFrom
glog.Errorf("GetContractInfo from chain error %v, contract %v", err, cd)
}
if contractInfo == nil {
contractInfo = &bchain.ContractInfo{Type: bchain.UnknownTokenType, Decimals: w.chainParser.AmountDecimals()}
contractInfo = &bchain.ContractInfo{Standard: bchain.UnknownTokenType, Decimals: w.chainParser.AmountDecimals()}
addresses, _, _ := w.chainParser.GetAddressesFromAddrDesc(cd)
if len(addresses) > 0 {
contractInfo.Contract = addresses[0]
}

validContract = false
} else {
if typeFromContext != bchain.UnknownTokenType && contractInfo.Type == bchain.UnknownTokenType {
contractInfo.Type = typeFromContext
if typeFromContext != bchain.UnknownTokenType && contractInfo.Standard == bchain.UnknownTokenType {
contractInfo.Standard = typeFromContext
}
if err = w.db.StoreContractInfo(contractInfo); err != nil {
glog.Errorf("StoreContractInfo error %v, contract %v", err, cd)
}
}
} else if (contractInfo.Type == bchain.UnhandledTokenType || len(contractInfo.Name) > 0 && contractInfo.Name[0] == 0) || (len(contractInfo.Symbol) > 0 && contractInfo.Symbol[0] == 0) {
} else if (contractInfo.Standard == bchain.UnhandledTokenType || len(contractInfo.Name) > 0 && contractInfo.Name[0] == 0) || (len(contractInfo.Symbol) > 0 && contractInfo.Symbol[0] == 0) {
// fix contract name/symbol that was parsed as a string consisting of zeroes
blockchainContractInfo, err := w.chain.GetContractInfo(cd)
if err != nil {
Expand All @@ -681,9 +681,9 @@ func (w *Worker) getContractDescriptorInfo(cd bchain.AddressDescriptor, typeFrom
if blockchainContractInfo != nil {
contractInfo.Decimals = blockchainContractInfo.Decimals
}
if contractInfo.Type == bchain.UnhandledTokenType {
if contractInfo.Standard == bchain.UnhandledTokenType {
glog.Infof("Contract %v %v [%s] handled", cd, typeFromContext, contractInfo.Name)
contractInfo.Type = typeFromContext
contractInfo.Standard = typeFromContext
}
if err = w.db.StoreContractInfo(contractInfo); err != nil {
glog.Errorf("StoreContractInfo error %v, contract %v", err, cd)
Expand All @@ -700,7 +700,7 @@ func (w *Worker) getEthereumTokensTransfers(transfers bchain.TokenTransfers, add
contractCache := make(contractInfoCache)
for i := range transfers {
t := transfers[i]
typeName := bchain.EthereumTokenTypeMap[t.Type]
typeName := bchain.EthereumTokenStandardMap[t.Standard]
var contractInfo *bchain.ContractInfo
if info, ok := contractCache[t.Contract]; ok {
contractInfo = info
Expand All @@ -715,7 +715,7 @@ func (w *Worker) getEthereumTokensTransfers(transfers bchain.TokenTransfers, add
}
var value *Amount
var values []MultiTokenValue
if t.Type == bchain.MultiToken {
if t.Standard == bchain.MultiToken {
values = make([]MultiTokenValue, len(t.MultiTokenValues))
for j := range values {
values[j].Id = (*Amount)(&t.MultiTokenValues[j].Id)
Expand Down Expand Up @@ -957,7 +957,7 @@ func computePaging(count, page, itemsOnPage int) (Paging, int, int, int) {
}

func (w *Worker) getEthereumContractBalance(addrDesc bchain.AddressDescriptor, index int, c *db.AddrContract, details AccountDetails, ticker *common.CurrencyRatesTicker, secondaryCoin string) (*Token, error) {
typeName := bchain.EthereumTokenTypeMap[c.Type]
typeName := bchain.EthereumTokenStandardMap[c.Standard]
ci, validContract, err := w.getContractDescriptorInfo(c.Contract, typeName)
if err != nil {
return nil, errors.Annotatef(err, "getEthereumContractBalance %v", c.Contract)
Expand Down Expand Up @@ -1468,7 +1468,7 @@ func (w *Worker) GetAddress(address string, page int, txsOnPage int, option Acco
StakingPools: ed.stakingPools,
}
// keep address backward compatible, set deprecated Erc20Contract value if ERC20 token
if ed.contractInfo != nil && ed.contractInfo.Type == bchain.ERC20TokenType {
if ed.contractInfo != nil && ed.contractInfo.Standard == bchain.ERC20TokenStandard {
r.Erc20Contract = ed.contractInfo
}
glog.Info("GetAddress-", option, " ", address, ", ", time.Since(start))
Expand Down
2 changes: 2 additions & 0 deletions api/xpub.go
Original file line number Diff line number Diff line change
Expand Up @@ -267,7 +267,9 @@ func (w *Worker) tokenFromXpubAddress(data *xpubData, ad *xpubAddress, changeInd
}
}
return Token{
// Deprecated: Use Standard instead.
Type: bchain.XPUBAddressTokenType,
Standard: bchain.XPUBAddressTokenType,
Name: address,
Decimals: w.chainParser.AmountDecimals(),
BalanceSat: (*Amount)(balance),
Expand Down
8 changes: 4 additions & 4 deletions bchain/coins/bsc/bscrpc.go
Original file line number Diff line number Diff line change
Expand Up @@ -15,9 +15,9 @@ const (
MainNet eth.Network = 56

// bsc token type names
BEP20TokenType bchain.TokenTypeName = "BEP20"
BEP721TokenType bchain.TokenTypeName = "BEP721"
BEP1155TokenType bchain.TokenTypeName = "BEP1155"
BEP20TokenStandard bchain.TokenStandardName = "BEP20"
BEP721TokenStandard bchain.TokenStandardName = "BEP721"
BEP1155TokenStandard bchain.TokenStandardName = "BEP1155"
)

// BNBSmartChainRPC is an interface to JSON-RPC bsc service.
Expand All @@ -33,7 +33,7 @@ func NewBNBSmartChainRPC(config json.RawMessage, pushHandler func(bchain.Notific
}

// overwrite EthereumTokenTypeMap with bsc specific token type names
bchain.EthereumTokenTypeMap = []bchain.TokenTypeName{BEP20TokenType, BEP721TokenType, BEP1155TokenType}
bchain.EthereumTokenStandardMap = []bchain.TokenStandardName{BEP20TokenStandard, BEP721TokenStandard, BEP1155TokenStandard}

s := &BNBSmartChainRPC{
EthereumRPC: c.(*eth.EthereumRPC),
Expand Down
2 changes: 1 addition & 1 deletion bchain/coins/eth/ethrpc.go
Original file line number Diff line number Diff line change
Expand Up @@ -622,7 +622,7 @@ func (b *EthereumRPC) getCreationContractInfo(contract string, height uint32) *b
Contract: contract,
}
// }
ci.Type = bchain.UnhandledTokenType
ci.Standard = bchain.UnhandledTokenType
ci.CreatedInBlock = height
return ci
}
Expand Down
25 changes: 14 additions & 11 deletions bchain/types.go
Original file line number Diff line number Diff line change
Expand Up @@ -116,26 +116,29 @@ type MempoolTx struct {
CoinSpecificData interface{} `json:"-"`
}

// TokenType - type of token
type TokenType int
// // Deprecated: Use TokenStandard instead.
// type TokenType int

// TokenType enumeration
// TokenStandard - standard of token
type TokenStandard int

// TokenStandard enumeration
const (
FungibleToken = TokenType(iota) // ERC20/BEP20
NonFungibleToken // ERC721/BEP721
MultiToken // ERC1155/BEP1155
FungibleToken = TokenStandard(iota) // ERC20/BEP20
NonFungibleToken // ERC721/BEP721
MultiToken // ERC1155/BEP1155
)

// TokenTypeName specifies type of token
type TokenTypeName string
// TokenStandardName specifies type of token
type TokenStandardName string

// Token types
const (
UnknownTokenType TokenTypeName = ""
UnhandledTokenType TokenTypeName = "-"
UnknownTokenType TokenStandardName = ""
UnhandledTokenType TokenStandardName = "-"

// XPUBAddressTokenType is address derived from xpub
XPUBAddressTokenType TokenTypeName = "XPUBAddress"
XPUBAddressTokenType TokenStandardName = "XPUBAddress"
)

// TokenTransfers is array of TokenTransfer
Expand Down
28 changes: 16 additions & 12 deletions bchain/types_ethereum_type.go
Original file line number Diff line number Diff line change
Expand Up @@ -59,25 +59,27 @@ type EthereumInternalData struct {

// ContractInfo contains info about a contract
type ContractInfo struct {
Type TokenTypeName `json:"type"`
Contract string `json:"contract"`
Name string `json:"name"`
Symbol string `json:"symbol"`
Decimals int `json:"decimals"`
CreatedInBlock uint32 `json:"createdInBlock,omitempty"`
DestructedInBlock uint32 `json:"destructedInBlock,omitempty"`
// Deprecated: Use Standard instead.
Type TokenStandardName `json:"type"`
Standard TokenStandardName `json:"standard"`
Contract string `json:"contract"`
Name string `json:"name"`
Symbol string `json:"symbol"`
Decimals int `json:"decimals"`
CreatedInBlock uint32 `json:"createdInBlock,omitempty"`
DestructedInBlock uint32 `json:"destructedInBlock,omitempty"`
}

// Ethereum token type names
const (
ERC20TokenType TokenTypeName = "ERC20"
ERC771TokenType TokenTypeName = "ERC721"
ERC1155TokenType TokenTypeName = "ERC1155"
ERC20TokenStandard TokenStandardName = "ERC20"
ERC771TokenStandard TokenStandardName = "ERC721"
ERC1155TokenStandard TokenStandardName = "ERC1155"
)

// EthereumTokenTypeMap maps bchain.TokenType to TokenTypeName
// the map must match all bchain.TokenType to avoid index out of range panic
var EthereumTokenTypeMap = []TokenTypeName{ERC20TokenType, ERC771TokenType, ERC1155TokenType}
var EthereumTokenStandardMap = []TokenStandardName{ERC20TokenStandard, ERC771TokenStandard, ERC1155TokenStandard}

type MultiTokenValue struct {
Id big.Int
Expand All @@ -86,7 +88,9 @@ type MultiTokenValue struct {

// TokenTransfer contains a single token transfer
type TokenTransfer struct {
Type TokenType
// Deprecated: Use Standard instead.
Type TokenStandard
Standard TokenStandard
Contract string
From string
To string
Expand Down
18 changes: 10 additions & 8 deletions db/rocksdb_ethereumtype.go
Original file line number Diff line number Diff line change
Expand Up @@ -108,7 +108,9 @@ func (s *MultiTokenValues) upsert(m bchain.MultiTokenValue, index int32, aggrega

// AddrContract is Contract address with number of transactions done by given address
type AddrContract struct {
Type bchain.TokenType
// Deprecated: Use Standard instead.
Type bchain.TokenStandard
Standard bchain.TokenStandard
Contract bchain.AddressDescriptor
Txs uint
Value big.Int // single value of ERC20
Expand Down Expand Up @@ -177,7 +179,7 @@ func unpackAddrContracts(buf []byte, addrDesc bchain.AddressDescriptor) (*AddrCo
contract := append(bchain.AddressDescriptor(nil), buf[:eth.EthereumTypeAddressDescriptorLen]...)
txs, l := unpackVaruint(buf[eth.EthereumTypeAddressDescriptorLen:])
buf = buf[eth.EthereumTypeAddressDescriptorLen+l:]
ttt := bchain.TokenType(txs & 3)
ttt := bchain.TokenStandard(txs & 3)
txs >>= 2
ac := AddrContract{
Type: ttt,
Expand Down Expand Up @@ -391,7 +393,7 @@ func (d *RocksDB) addToAddressesAndContractsEthereumType(addrDesc bchain.Address

type ethBlockTxContract struct {
from, to, contract bchain.AddressDescriptor
transferType bchain.TokenType
transferType bchain.TokenStandard
value big.Int
idValues []bchain.MultiTokenValue
}
Expand Down Expand Up @@ -868,7 +870,7 @@ func unpackContractInfo(buf []byte) (*bchain.ContractInfo, error) {
contractInfo.Symbol, l = unpackString(buf)
buf = buf[l:]
s, l = unpackString(buf)
contractInfo.Type = bchain.TokenTypeName(s)
contractInfo.Standard = bchain.TokenStandardName(s)
buf = buf[l:]
ui, l = unpackVaruint(buf)
contractInfo.Decimals = int(ui)
Expand All @@ -891,7 +893,7 @@ func (d *RocksDB) GetContractInfoForAddress(address string) (*bchain.ContractInf

// GetContractInfo gets contract from cache or DB and possibly updates the type from typeFromContext
// it is hard to guess the type of the contract using API, it is easier to set it the first time the contract is processed in a tx
func (d *RocksDB) GetContractInfo(contract bchain.AddressDescriptor, typeFromContext bchain.TokenTypeName) (*bchain.ContractInfo, error) {
func (d *RocksDB) GetContractInfo(contract bchain.AddressDescriptor, typeFromContext bchain.TokenStandardName) (*bchain.ContractInfo, error) {
cacheKey := string(contract)
cachedContractsMux.Lock()
contractInfo, found := cachedContracts[cacheKey]
Expand All @@ -912,8 +914,8 @@ func (d *RocksDB) GetContractInfo(contract bchain.AddressDescriptor, typeFromCon
contractInfo.Contract = addresses[0]
}
// if the type is specified and stored contractInfo has unknown type, set and store it
if typeFromContext != bchain.UnknownTokenType && contractInfo.Type == bchain.UnknownTokenType {
contractInfo.Type = typeFromContext
if typeFromContext != bchain.UnknownTokenStandard && contractInfo.Standard == bchain.UnknownTokenStandard {
contractInfo.Standard = typeFromContext
err = d.db.PutCF(d.wo, d.cfh[cfContracts], contract, packContractInfo(contractInfo))
if err != nil {
return nil, err
Expand Down Expand Up @@ -1142,7 +1144,7 @@ func unpackBlockTx(buf []byte, pos int) (*ethBlockTx, int, error) {
return nil, 0, err
}
cc, l = unpackVaruint(buf[pos:])
c.transferType = bchain.TokenType(cc)
c.transferType = bchain.TokenStandard(cc)
pos += l
if c.transferType == bchain.MultiToken {
cc, l = unpackVaruint(buf[pos:])
Expand Down
Loading

0 comments on commit 5db31d1

Please sign in to comment.