diff --git a/api/worker.go b/api/worker.go index 799c90e8..6954c99b 100644 --- a/api/worker.go +++ b/api/worker.go @@ -172,9 +172,18 @@ func (w *Worker) getAddressAliases(addresses map[string]struct{}) AddressAliases } for a := range addresses { if w.chainType == bchain.ChainEthereumType { - ci, err := w.db.GetContractInfoForAddress(a) - if err == nil && ci != nil && ci.Name != "" { - aliases[a] = AddressAlias{Type: "Contract", Alias: ci.Name} + addrDesc, err := w.chainParser.GetAddrDescFromAddress(a) + if err != nil || addrDesc == nil { + continue + } + ci, err := w.db.GetContractInfo(addrDesc, bchain.UnknownTokenType) + if err == nil && ci != nil { + if ci.Type == bchain.UnhandledTokenType { + ci, _, err = w.getContractDescriptorInfo(addrDesc, bchain.UnknownTokenType) + } + if err == nil && ci != nil && ci.Name != "" { + aliases[a] = AddressAlias{Type: "Contract", Alias: ci.Name} + } } } n := w.db.GetAddressAlias(a) @@ -608,7 +617,7 @@ 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.TokenTypeName) (*bchain.ContractInfo, bool, error) { cd, err := w.chainParser.GetAddrDescFromAddress(contract) if err != nil { return nil, false, err @@ -648,7 +657,7 @@ func (w *Worker) getContractDescriptorInfo(cd bchain.AddressDescriptor, typeFrom glog.Errorf("StoreContractInfo error %v, contract %v", err, cd) } } - } else if (len(contractInfo.Name) > 0 && contractInfo.Name[0] == 0) || (len(contractInfo.Symbol) > 0 && contractInfo.Symbol[0] == 0) { + } else if (contractInfo.Type == 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 { @@ -667,6 +676,10 @@ func (w *Worker) getContractDescriptorInfo(cd bchain.AddressDescriptor, typeFrom if blockchainContractInfo != nil { contractInfo.Decimals = blockchainContractInfo.Decimals } + if contractInfo.Type == bchain.UnhandledTokenType { + glog.Infof("Contract %v %v [%s] handled", cd, typeFromContext, contractInfo.Name) + contractInfo.Type = typeFromContext + } if err = w.db.StoreContractInfo(contractInfo); err != nil { glog.Errorf("StoreContractInfo error %v, contract %v", err, cd) } @@ -687,7 +700,7 @@ func (w *Worker) getEthereumTokensTransfers(transfers bchain.TokenTransfers, add if info, ok := contractCache[t.Contract]; ok { contractInfo = info } else { - info, _, err := w.getContractInfo(t.Contract, typeName) + info, _, err := w.GetContractInfo(t.Contract, typeName) if err != nil { glog.Errorf("getContractInfo error %v, contract %v", err, t.Contract) continue @@ -1124,10 +1137,16 @@ func (w *Worker) getEthereumTypeAddressBalances(addrDesc bchain.AddressDescripto d.tokens = d.tokens[:j] sort.Sort(d.tokens) } - d.contractInfo, err = w.db.GetContractInfo(addrDesc, "") + d.contractInfo, err = w.db.GetContractInfo(addrDesc, bchain.UnknownTokenType) if err != nil { return nil, nil, err } + if d.contractInfo != nil && d.contractInfo.Type == bchain.UnhandledTokenType { + d.contractInfo, _, err = w.getContractDescriptorInfo(addrDesc, bchain.UnknownTokenType) + if err != nil { + return nil, nil, err + } + } if filter.FromHeight == 0 && filter.ToHeight == 0 { // compute total results for paging if filter.Vout == AddressFilterVoutOff { diff --git a/bchain/coins/eth/ethrpc.go b/bchain/coins/eth/ethrpc.go index b9eb843e..c2c5b111 100644 --- a/bchain/coins/eth/ethrpc.go +++ b/bchain/coins/eth/ethrpc.go @@ -610,13 +610,15 @@ type rpcTraceResult struct { } func (b *EthereumRPC) getCreationContractInfo(contract string, height uint32) *bchain.ContractInfo { - ci, err := b.fetchContractInfo(contract) - if ci == nil || err != nil { - ci = &bchain.ContractInfo{ - Contract: contract, - } + // do not fetch fetchContractInfo in sync, it slows it down + // the contract will be fetched only when asked by a client + // ci, err := b.fetchContractInfo(contract) + // if ci == nil || err != nil { + ci := &bchain.ContractInfo{ + Contract: contract, } - ci.Type = bchain.UnknownTokenType + // } + ci.Type = bchain.UnhandledTokenType ci.CreatedInBlock = height return ci } diff --git a/bchain/types.go b/bchain/types.go index 3fd2dcfe..b33a5834 100644 --- a/bchain/types.go +++ b/bchain/types.go @@ -131,7 +131,8 @@ type TokenTypeName string // Token types const ( - UnknownTokenType TokenTypeName = "" + UnknownTokenType TokenTypeName = "" + UnhandledTokenType TokenTypeName = "-" // XPUBAddressTokenType is address derived from xpub XPUBAddressTokenType TokenTypeName = "XPUBAddress"