From 642c69e9309c6c6fbba8ce36b5694b827bad2a76 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Viterbo=20Rodr=C3=ADguez?= Date: Sun, 3 Nov 2024 12:28:24 -0300 Subject: [PATCH 1/3] remove null cache, getContract() only retrieves from cache, getContractForced() added --- src/lib/contract/ContractManager.js | 104 ++++++++-------------------- 1 file changed, 29 insertions(+), 75 deletions(-) diff --git a/src/lib/contract/ContractManager.js b/src/lib/contract/ContractManager.js index 79f70c8b..86c5bca3 100644 --- a/src/lib/contract/ContractManager.js +++ b/src/lib/contract/ContractManager.js @@ -9,11 +9,10 @@ const tokenList = 'https://raw.githubusercontent.com/telosnetwork/token-list/mai const systemContractList = 'https://raw.githubusercontent.com/telosnetwork/token-list/main/telosevm.systemcontractlist.json'; + class AddressCacheManager { constructor() { - this.addressesByNetwork = {}; this.contractInfoByNetwork = {}; - this.maxAddresses = 1000; this.loadFromLocalStorage(); } @@ -22,37 +21,16 @@ class AddressCacheManager { } loadFromLocalStorage() { - const storedAddresses = localStorage.getItem('noContractAddressesByNetwork'); const storedContractInfo = localStorage.getItem('contractInfoByNetwork'); - if (storedAddresses) { - this.addressesByNetwork = JSON.parse(storedAddresses); - } if (storedContractInfo) { this.contractInfoByNetwork = JSON.parse(storedContractInfo); } } saveToLocalStorage() { - localStorage.setItem('noContractAddressesByNetwork', JSON.stringify(this.addressesByNetwork)); localStorage.setItem('contractInfoByNetwork', JSON.stringify(this.contractInfoByNetwork)); } - addNullAddress(address) { - const addressLower = typeof address === 'string' ? address.toLowerCase() : ''; - const network = this.getCurrentNetwork(); - if (!this.addressesByNetwork[network]) { - this.addressesByNetwork[network] = []; - } - - if (!this.exists(addressLower)) { - this.addressesByNetwork[network].push(addressLower); - if (this.addressesByNetwork[network].length > this.maxAddresses) { - this.addressesByNetwork[network] = this.addressesByNetwork[network].slice(-this.maxAddresses); - } - this.saveToLocalStorage(); - } - } - addContractInfo(address, name, symbol = null) { const addressLower = typeof address === 'string' ? address.toLowerCase() : ''; const network = this.getCurrentNetwork(); @@ -66,14 +44,6 @@ class AddressCacheManager { this.contractInfoByNetwork[network][addressLower] = info; this.saveToLocalStorage(); } - - this.removeNullAddress(address); - } - - exists(address) { - const addressLower = typeof address === 'string' ? address.toLowerCase() : ''; - const network = this.getCurrentNetwork(); - return this.addressesByNetwork[network] && this.addressesByNetwork[network].includes(addressLower); } existsContract(address) { @@ -82,37 +52,12 @@ class AddressCacheManager { return this.contractInfoByNetwork[network] && this.contractInfoByNetwork[network][addressLower]; } - removeNullAddress(address) { - const addressLower = typeof address === 'string' ? address.toLowerCase() : ''; - const network = this.getCurrentNetwork(); - if (this.addressesByNetwork[network]) { - const index = this.addressesByNetwork[network].indexOf(addressLower); - if (index !== -1) { - this.addressesByNetwork[network].splice(index, 1); - this.saveToLocalStorage(); - } - } - } - - getAddresses() { - const network = this.getCurrentNetwork(); - return this.addressesByNetwork[network] ? [...this.addressesByNetwork[network]] : []; - } - getContractInfo(address) { const addressLower = typeof address === 'string' ? address.toLowerCase() : ''; const network = this.getCurrentNetwork(); return this.contractInfoByNetwork[network] ? this.contractInfoByNetwork[network][addressLower] : null; } - clearAddresses() { - const network = this.getCurrentNetwork(); - if (this.addressesByNetwork[network]) { - this.addressesByNetwork[network] = []; - this.saveToLocalStorage(); - } - } - clearContractInfo() { const network = this.getCurrentNetwork(); if (this.contractInfoByNetwork[network]) { @@ -141,10 +86,7 @@ export default class ContractManager { if (!this.contracts[network]) { this.contracts[network] = {}; } - if (this.nullContractsManager.exists(address)) { - return null; - } - return this.contracts[network][address.toLowerCase()]; + return this.contracts[network][address.toLowerCase()] || null; } setNetworkContract(address, contract) { @@ -153,9 +95,7 @@ export default class ContractManager { this.contracts[network] = {}; } this.contracts[network][address.toLowerCase()] = contract; - if (contract === null) { - this.nullContractsManager.addNullAddress(address); - } else { + if (contract) { this.nullContractsManager.addContractInfo(address, contract.name, contract.properties?.symbol || null); } } @@ -381,27 +321,21 @@ export default class ContractManager { } async getContractDisplayInfo(address) { const addressLower = typeof address === 'string' ? address.toLowerCase() : ''; - let result; - if (this.nullContractsManager.exists(addressLower)) { - result = null; - } else if (this.nullContractsManager.existsContract(addressLower)) { - result = this.nullContractsManager.getContractInfo(addressLower); + if (this.nullContractsManager.existsContract(addressLower)) { + return this.nullContractsManager.getContractInfo(addressLower); } else { // We are going to always assume that if the address is a contract, it is already in the cache // Because the indexer API should always return all involved contracts in a query response + return null; } - return result; } - async getContract(address, force) { + async getContractForced(address) { if (address === null || typeof address !== 'string') { return; } const addressLower = address.toLowerCase(); - if (!force && typeof this.getNetworkContract(addressLower) !== 'undefined') { - return this.getNetworkContract(addressLower); - } - + // if this function is repeatedly called for the same address, wait for the first call to finish if (this.processing.includes(addressLower)) { await new Promise(resolve => setTimeout(resolve, 300)); return await this.getNetworkContract(addressLower); @@ -430,8 +364,28 @@ export default class ContractManager { if(index > -1){ this.processing.splice(index, 1); } - return this.factory.buildContract(contract); + return this.getContract(address); + } + + async getContract(address, force) { + if (address === null || typeof address !== 'string') { + return null; + } + + if (force) { + return await this.getContractForced(address); + } + + const addressLower = address.toLowerCase(); + + const cashedContract = this.getNetworkContract(addressLower); + if (!force && cashedContract) { + return cashedContract; + } + + return null; } + async getContractFromAbi(address, abi){ return new ethers.Contract(address, abi, this.getEthersProvider()); } From 5c3346fdcb27b3cca7b76144220d10efe987f09c Mon Sep 17 00:00:00 2001 From: Viterbo Date: Sun, 3 Nov 2024 21:29:42 -0300 Subject: [PATCH 2/3] address page has blockie icon (based on address) --- src/components/AppSearch.vue | 2 +- src/components/AppSearchResultEntry.vue | 12 ++---------- src/components/MethodField.vue | 2 +- src/lib/blockies/blockies.ts | 11 +++++++++++ src/pages/AccountPage.vue | 16 ++++++++++++++++ 5 files changed, 31 insertions(+), 12 deletions(-) create mode 100644 src/lib/blockies/blockies.ts diff --git a/src/components/AppSearch.vue b/src/components/AppSearch.vue index 5ed2cdd7..878a1542 100644 --- a/src/components/AppSearch.vue +++ b/src/components/AppSearch.vue @@ -591,7 +591,7 @@ const handleResultClick = (item: SearchResult): void => {