From 50602bcaae3fb3df7e6e6f8c2cc9c1d95717b6ed Mon Sep 17 00:00:00 2001 From: Martin Boehm Date: Sun, 16 Oct 2022 11:00:38 +0200 Subject: [PATCH] Update display of contracts in explorer --- .gitignore | 1 + api/worker.go | 46 +++++++++++++++++++++------- db/rocksdb_ethereumtype.go | 4 +++ server/public_ethereumtype_test.go | 4 +-- static/templates/address.html | 48 +++++++++++++++++++++++++----- 5 files changed, 82 insertions(+), 21 deletions(-) diff --git a/.gitignore b/.gitignore index 98fe00b3..5cc7d1f1 100644 --- a/.gitignore +++ b/.gitignore @@ -15,3 +15,4 @@ build/*.deb .bin-image .deb-image \.idea/ +__debug* \ No newline at end of file diff --git a/api/worker.go b/api/worker.go index bab95444..864c6314 100644 --- a/api/worker.go +++ b/api/worker.go @@ -569,6 +569,27 @@ 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) { + // fix contract name/symbol that was parsed as a string consisting of zeroes + blockchainContractInfo, err := w.chain.GetContractInfo(cd) + if err != nil { + glog.Errorf("GetContractInfo from chain error %v, contract %v", err, cd) + } else { + if len(blockchainContractInfo.Name) > 0 && blockchainContractInfo.Name[0] != 0 { + contractInfo.Name = blockchainContractInfo.Name + } else { + contractInfo.Name = "" + } + if len(blockchainContractInfo.Symbol) > 0 && blockchainContractInfo.Symbol[0] != 0 { + contractInfo.Symbol = blockchainContractInfo.Symbol + } else { + contractInfo.Symbol = "" + } + contractInfo.Decimals = blockchainContractInfo.Decimals + if err = w.db.StoreContractInfo(contractInfo); err != nil { + glog.Errorf("StoreContractInfo error %v, contract %v", err, cd) + } + } } return contractInfo, validContract, nil } @@ -957,8 +978,10 @@ func (w *Worker) getEthereumTypeAddressBalances(addrDesc bchain.AddressDescripto totalResults = int(ca.TotalTxs) } else if filter.Vout == 0 { totalResults = int(ca.NonContractTxs) - } else if filter.Vout > 0 && filter.Vout-1 < len(ca.Contracts) { - totalResults = int(ca.Contracts[filter.Vout-1].Txs) + } else if filter.Vout == db.InternalTxIndexOffset { + totalResults = int(ca.InternalTxs) + } else if filter.Vout >= db.ContractIndexOffset && filter.Vout-db.ContractIndexOffset < len(ca.Contracts) { + totalResults = int(ca.Contracts[filter.Vout-db.ContractIndexOffset].Txs) } else if filter.Vout == AddressFilterVoutQueryNotNecessary { totalResults = 0 } @@ -972,16 +995,17 @@ func (w *Worker) getEthereumTypeAddressBalances(addrDesc bchain.AddressDescripto BalanceSat: *b, } } - // special handling if filtering for a contract, check the ballance of it in the blockchain - if len(filterDesc) > 0 && details >= AccountDetailsTokens { - t, err := w.getEthereumContractBalanceFromBlockchain(addrDesc, filterDesc, details) - if err != nil { - return nil, nil, nil, 0, 0, 0, 0, err - } - tokens = []Token{*t} - // switch off query for transactions, there are no transactions - filter.Vout = AddressFilterVoutQueryNotNecessary + } + // special handling if filtering for a contract, return the contract details even though the address had no transactions with it + if len(tokens) == 0 && len(filterDesc) > 0 && details >= AccountDetailsTokens { + t, err := w.getEthereumContractBalanceFromBlockchain(addrDesc, filterDesc, details) + if err != nil { + return nil, nil, nil, 0, 0, 0, 0, err } + tokens = []Token{*t} + // switch off query for transactions, there are no transactions + filter.Vout = AddressFilterVoutQueryNotNecessary + totalResults = -1 } return ba, tokens, ci, n, nonContractTxs, internalTxs, totalResults, nil } diff --git a/db/rocksdb_ethereumtype.go b/db/rocksdb_ethereumtype.go index b29a8fe9..b16429b3 100644 --- a/db/rocksdb_ethereumtype.go +++ b/db/rocksdb_ethereumtype.go @@ -812,6 +812,10 @@ func (d *RocksDB) storeContractInfo(wb *grocksdb.WriteBatch, contractInfo *bchai contractInfo = storedCI } wb.PutCF(d.cfh[cfContracts], key, packContractInfo(contractInfo)) + cacheKey := string(key) + cachedContractsMux.Lock() + delete(cachedContracts, cacheKey) + cachedContractsMux.Unlock() } return nil } diff --git a/server/public_ethereumtype_test.go b/server/public_ethereumtype_test.go index 221ef57f..41d31643 100644 --- a/server/public_ethereumtype_test.go +++ b/server/public_ethereumtype_test.go @@ -24,7 +24,7 @@ func httpTestsEthereumType(t *testing.T, ts *httptest.Server) { status: http.StatusOK, contentType: "text/html; charset=utf-8", body: []string{ - `Trezor Fake Coin Explorer

Address 0.000000000123450123 FAKE

0x7B62EB7fe80350DC7EC945C0B73242cb9877FB1b

Confirmed

Balance0.000000000123450123 FAKE
Transactions2
Non-contract Transactions0
Internal Transactions0
Nonce123
ERC20 Tokens
ContractTokensTransfers
Contract 740.001000123074 S741
Contract 130.000000001000123013 S131
ERC721 Tokens
ContractTokensTransfers
Contract 20511

Transactions

ERC721 Token Transfers
0x7B62EB7fe80350DC7EC945C0B73242cb9877FB1b
ID 1 S205
Fee: 0.00008794500041041 FAKE
Unconfirmed Transaction!0 FAKE
ERC20 Token Transfers
0x7B62EB7fe80350DC7EC945C0B73242cb9877FB1b
871.180000950184 S74
0x7B62EB7fe80350DC7EC945C0B73242cb9877FB1b
7.674999999999991915 S13
Fee: 0.000216368 FAKE
Unconfirmed Transaction!0 FAKE
`, + `Trezor Fake Coin Explorer

Address 0.000000000123450123 FAKE

0x7B62EB7fe80350DC7EC945C0B73242cb9877FB1b

Confirmed

Balance0.000000000123450123 FAKE
Transactions2
Non-contract Transactions0
Internal Transactions0
Nonce123
ERC20 Tokens
ContractTokensTransfers
Contract 740.001000123074 S741
Contract 130.000000001000123013 S131
ERC721 Tokens
ContractTokensTransfers
Contract 20511

Transactions

ERC721 Token Transfers
0x7B62EB7fe80350DC7EC945C0B73242cb9877FB1b
ID 1 S205
Fee: 0.00008794500041041 FAKE
Unconfirmed Transaction!0 FAKE
ERC20 Token Transfers
0x7B62EB7fe80350DC7EC945C0B73242cb9877FB1b
871.180000950184 S74
0x7B62EB7fe80350DC7EC945C0B73242cb9877FB1b
7.674999999999991915 S13
Fee: 0.000216368 FAKE
Unconfirmed Transaction!0 FAKE
`, }, }, { @@ -33,7 +33,7 @@ func httpTestsEthereumType(t *testing.T, ts *httptest.Server) { status: http.StatusOK, contentType: "text/html; charset=utf-8", body: []string{ - `Trezor Fake Coin Explorer

Address 0.000000000123450093 FAKE

0x5Dc6288b35E0807A3d6fEB89b3a2Ff4aB773168e

Confirmed

Balance0.000000000123450093 FAKE
Transactions1
Non-contract Transactions1
Internal Transactions0
Nonce93
ERC1155 Tokens
ContractTokensTransfers
Contract 1111 of ID 1776, 10 of ID 18981

Transactions

0x5Dc6288b35E0807A3d6fEB89b3a2Ff4aB773168e
0 FAKE
ERC1155 Token Transfers
0x5Dc6288b35E0807A3d6fEB89b3a2Ff4aB773168e
1 S111 of ID 1776, 10 S111 of ID 1898
Fee: 0.000081891755740665 FAKE
Unconfirmed Transaction!0 FAKE
`, + `Trezor Fake Coin Explorer

Address 0.000000000123450093 FAKE

0x5Dc6288b35E0807A3d6fEB89b3a2Ff4aB773168e

Confirmed

Balance0.000000000123450093 FAKE
Transactions1
Non-contract Transactions1
Internal Transactions0
Nonce93
ERC1155 Tokens
ContractTokensTransfers
Contract 1111 of ID 1776, 10 of ID 18981

Transactions

0x5Dc6288b35E0807A3d6fEB89b3a2Ff4aB773168e
0 FAKE
ERC1155 Token Transfers
0x5Dc6288b35E0807A3d6fEB89b3a2Ff4aB773168e
1 S111 of ID 1776, 10 S111 of ID 1898
Fee: 0.000081891755740665 FAKE
Unconfirmed Transaction!0 FAKE
`, }, }, { diff --git a/static/templates/address.html b/static/templates/address.html index 590da4a7..c3bc0c9e 100644 --- a/static/templates/address.html +++ b/static/templates/address.html @@ -1,5 +1,5 @@ {{define "specific"}}{{$cs := .CoinShortcut}}{{$addr := .Address}}{{$data := .}} -

{{if $addr.ContractInfo}}Contract {{$addr.ContractInfo.Name}} ({{$addr.ContractInfo.Symbol}}){{else}}Address{{end}} {{formatAmount $addr.BalanceSat}} {{$cs}} +

{{if $addr.ContractInfo}}Contract {{$addr.ContractInfo.Name}}{{if $addr.ContractInfo.Symbol}} ({{$addr.ContractInfo.Symbol}}){{end}}{{else}}Address{{end}} {{formatAmount $addr.BalanceSat}} {{$cs}}

{{$addr.AddrStr}} @@ -10,6 +10,26 @@ {{- if eq .ChainType 1 -}} + {{if $addr.ContractInfo}} + {{if $addr.ContractInfo.Type}} + + + + + {{end}} + {{if $addr.ContractInfo.CreatedInBlock}} + + + + + {{end}} + {{if $addr.ContractInfo.DestructedInBlock}} + + + + + {{end}} + {{end}} @@ -158,19 +178,31 @@ {{- end}}{{if or $addr.Transactions $addr.Filter -}}

Transactions

- - - + + {{- if $addr.Tokens -}} - - + + {{- range $t := $addr.Tokens -}} - + {{if eq $t.Type "ERC20"}} + + {{- end -}} + {{- end -}} + {{- range $t := $addr.Tokens -}} + {{if eq $t.Type "ERC721"}} + + {{- end -}} + {{- end -}} + {{- range $t := $addr.Tokens -}} + {{if eq $t.Type "ERC1155"}} + + {{- end -}} {{- end -}} {{- end -}} -
+
Contract type{{$addr.ContractInfo.Type}}
Created in Block{{$addr.ContractInfo.CreatedInBlock}}
Destructed in Block{{$addr.ContractInfo.DestructedInBlock}}
Balance {{formatAmount $addr.BalanceSat}} {{$cs}}