Skip to content

Commit 6068cf5

Browse files
Further update fixes to reduce overhead with balance updates
Add TODO
1 parent cda91d3 commit 6068cf5

File tree

13 files changed

+117
-110
lines changed

13 files changed

+117
-110
lines changed

app/src/main/java/com/alphawallet/app/entity/tokens/ERC1155Token.java

Lines changed: 3 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -320,35 +320,25 @@ private List<Uint256> fetchBalances(Set<BigInteger> tokenIds)
320320
@Override
321321
public Map<BigInteger, NFTAsset> queryAssets(Map<BigInteger, NFTAsset> assetMap)
322322
{
323-
//first see if there's no change; if this is the case we can skip
324-
if (assetsUnchanged(assetMap)) return assets;
325-
326-
//add all known tokens in
327-
Map<BigInteger, NFTAsset> sum = new HashMap<>(assetMap);
328-
sum.putAll(assets);
329-
Set<BigInteger> tokenIds = sum.keySet();
323+
Set<BigInteger> tokenIds = assetMap.keySet();
330324
Function balanceOfBatch = balanceOfBatch(getWallet(), tokenIds);
331325
List<Uint256> balances = callSmartContractFunctionArray(tokenInfo.chainId, balanceOfBatch, getAddress(), getWallet());
332-
Map<BigInteger, NFTAsset> updatedAssetMap;
326+
Map<BigInteger, NFTAsset> updatedAssetMap = new HashMap<>();
333327

334328
if (balances != null && balances.size() > 0)
335329
{
336330
updatedAssetMap = new HashMap<>();
337331
int index = 0;
338332
for (BigInteger tokenId : tokenIds)
339333
{
340-
NFTAsset thisAsset = new NFTAsset(sum.get(tokenId));
334+
NFTAsset thisAsset = new NFTAsset(assetMap.get(tokenId));
341335
BigInteger balance = balances.get(index).getValue();
342336
thisAsset.setBalance(new BigDecimal(balance));
343337
updatedAssetMap.put(tokenId, thisAsset);
344338

345339
index++;
346340
}
347341
}
348-
else
349-
{
350-
updatedAssetMap = assets;
351-
}
352342

353343
return updatedAssetMap;
354344
}

app/src/main/java/com/alphawallet/app/entity/tokens/ERC721Ticket.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -171,7 +171,7 @@ public boolean hasArrayBalance()
171171
public List<BigInteger> getNonZeroArrayBalance()
172172
{
173173
List<BigInteger> nonZeroValues = new ArrayList<>();
174-
for (BigInteger value : balanceArray) if (value.compareTo(BigInteger.ZERO) != 0 && !nonZeroValues.contains(value)) nonZeroValues.add(value);
174+
for (BigInteger value : balanceArray) if (value.compareTo(BigInteger.ZERO) != 0) nonZeroValues.add(value);
175175
return nonZeroValues;
176176
}
177177

app/src/main/java/com/alphawallet/app/entity/tokens/ERC721Token.java

Lines changed: 2 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -681,22 +681,8 @@ public BigDecimal getBalanceRaw()
681681
@Override
682682
public Map<BigInteger, NFTAsset> queryAssets(Map<BigInteger, NFTAsset> assetMap)
683683
{
684-
//check all tokens in this contract
685-
assetMap.putAll(tokenBalanceAssets);
686-
687-
HashSet<BigInteger> currentAssets = new HashSet<>(assetMap.keySet());
688-
689684
final Web3j web3j = TokenRepository.getWeb3jService(tokenInfo.chainId);
690685

691-
try
692-
{
693-
currentAssets = checkBalances(web3j, currentAssets);
694-
}
695-
catch (Exception e)
696-
{
697-
//
698-
}
699-
700686
for (Map.Entry<BigInteger, NFTAsset> entry : assetMap.entrySet())
701687
{
702688
BigInteger checkId = entry.getKey();
@@ -717,10 +703,10 @@ else if (owner.equalsIgnoreCase(getWallet()))
717703
checkAsset.setBalance(BigDecimal.ZERO);
718704
}
719705

720-
tokenBalanceAssets.put(checkId, checkAsset);
706+
assetMap.put(checkId, checkAsset);
721707
}
722708

723-
return tokenBalanceAssets;
709+
return assetMap;
724710
}
725711

726712
// Check for new/missing tokenBalanceAssets

app/src/main/java/com/alphawallet/app/entity/tokens/Ticket.java

Lines changed: 19 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,6 @@
1010
import com.alphawallet.app.entity.tokendata.TokenGroup;
1111
import com.alphawallet.app.repository.EventResult;
1212
import com.alphawallet.app.repository.entity.RealmToken;
13-
import com.alphawallet.app.service.AssetDefinitionService;
1413
import com.alphawallet.app.util.Utils;
1514
import com.alphawallet.app.viewmodel.BaseViewModel;
1615
import com.alphawallet.token.entity.TicketRange;
@@ -27,31 +26,34 @@
2726
import java.util.List;
2827

2928
/**
30-
* Created by James on 27/01/2018. It might seem counter intuitive
31-
* but here Ticket refers to a container of an asset class here, not
32-
* the right to seat somewhere in the venue. Therefore, there
33-
* shouldn't be List<Ticket> To understand this, imagine that one says
34-
* "I have two cryptocurrencies: Ether and Bitcoin, each amounts to a
35-
* hundred", and he pauses and said, "I also have two indices: FIFA
36-
* and Formuler-one, which, too, amounts to a hundred each".
29+
* Created by James on 27/01/2018.
3730
*/
3831

3932
public class Ticket extends Token
4033
{
4134
private final List<BigInteger> balanceArray;
42-
private boolean isMatchedInXML = false;
4335

44-
public Ticket(TokenInfo tokenInfo, List<BigInteger> balances, long blancaTime, String networkName, ContractType type) {
36+
public Ticket(TokenInfo tokenInfo, List<BigInteger> balances, long blancaTime, String networkName, ContractType type)
37+
{
4538
super(tokenInfo, BigDecimal.ZERO, blancaTime, networkName, type);
4639
this.balanceArray = balances;
47-
balance = balanceArray != null ? BigDecimal.valueOf(balanceArray.size()) : BigDecimal.ZERO;
40+
balance = balanceArray != null ? BigDecimal.valueOf(getNonZeroArrayBalance().size()) : BigDecimal.ZERO;
4841
group = TokenGroup.NFT;
4942
}
5043

51-
public Ticket(TokenInfo tokenInfo, String balances, long blancaTime, String networkName, ContractType type) {
44+
public Ticket(TokenInfo tokenInfo, String balances, long blancaTime, String networkName, ContractType type)
45+
{
5246
super(tokenInfo, BigDecimal.ZERO, blancaTime, networkName, type);
5347
this.balanceArray = stringHexToBigIntegerList(balances);
54-
balance = BigDecimal.valueOf(balanceArray.size());
48+
balance = BigDecimal.valueOf(getNonZeroArrayBalance().size());
49+
group = TokenGroup.NFT;
50+
}
51+
52+
public Ticket(Token oldTicket, List<BigInteger> balances)
53+
{
54+
super(oldTicket.tokenInfo, BigDecimal.ZERO, oldTicket.updateBlancaTime, oldTicket.getNetworkName(), oldTicket.contractType);
55+
this.balanceArray = balances;
56+
balance = BigDecimal.valueOf(getNonZeroArrayBalance().size());
5557
group = TokenGroup.NFT;
5658
}
5759

@@ -235,11 +237,6 @@ private List<BigInteger> tokenIdsToTokenIndices(List<BigInteger> tokenIds)
235237
return indexList;
236238
}
237239

238-
public void checkIsMatchedInXML(AssetDefinitionService assetService)
239-
{
240-
isMatchedInXML = assetService.hasDefinition(tokenInfo.chainId, tokenInfo.address);
241-
}
242-
243240
@Override
244241
public Function getTransferFunction(String to, List<BigInteger> tokenIndices) throws NumberFormatException
245242
{
@@ -305,7 +302,10 @@ public boolean hasArrayBalance()
305302
public List<BigInteger> getNonZeroArrayBalance()
306303
{
307304
List<BigInteger> nonZeroValues = new ArrayList<>();
308-
for (BigInteger value : balanceArray) if (value.compareTo(BigInteger.ZERO) != 0 && !nonZeroValues.contains(value)) nonZeroValues.add(value);
305+
for (BigInteger value : balanceArray)
306+
{
307+
if (value.compareTo(BigInteger.ZERO) != 0) nonZeroValues.add(value);
308+
}
309309
return nonZeroValues;
310310
}
311311

app/src/main/java/com/alphawallet/app/entity/tokens/Token.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -383,7 +383,7 @@ public void setIsEthereum()
383383

384384
public boolean isBad()
385385
{
386-
return tokenInfo == null || (tokenInfo.symbol == null && tokenInfo.name == null);
386+
return tokenInfo == null || tokenInfo.chainId == 0 || (tokenInfo.symbol == null && tokenInfo.name == null);
387387
}
388388

389389
public void setTokenWallet(String address)

app/src/main/java/com/alphawallet/app/repository/TokenRepository.java

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,7 @@
2121
import com.alphawallet.app.entity.tokendata.TokenTicker;
2222
import com.alphawallet.app.entity.tokens.ERC721Ticket;
2323
import com.alphawallet.app.entity.tokens.ERC721Token;
24+
import com.alphawallet.app.entity.tokens.Ticket;
2425
import com.alphawallet.app.entity.tokens.Token;
2526
import com.alphawallet.app.entity.tokens.TokenCardMeta;
2627
import com.alphawallet.app.entity.tokens.TokenInfo;
@@ -326,6 +327,11 @@ public Single<Integer> fixFullNames(Wallet wallet, AssetDefinitionService svs)
326327
@Override
327328
public Single<BigDecimal> updateTokenBalance(String walletAddress, Token token)
328329
{
330+
if (token.isBad())
331+
{
332+
return Single.fromCallable(() -> BigDecimal.ZERO);
333+
}
334+
329335
Wallet wallet = new Wallet(walletAddress);
330336
return updateBalance(wallet, token)
331337
.subscribeOn(Schedulers.io())
@@ -411,6 +417,8 @@ public String getTokenImageUrl(long networkId, String address)
411417
return localSource.getTokenImageUrl(networkId, address);
412418
}
413419

420+
//TODO: Refactor this so the balance update is abstracted into the Token itself
421+
// Once the token is updated we can store it. May need to make the token internal balance non-final
414422
private Single<BigDecimal> updateBalance(final Wallet wallet, final Token token)
415423
{
416424
return Single.fromCallable(() -> {
@@ -429,6 +437,7 @@ private Single<BigDecimal> updateBalance(final Wallet wallet, final Token token)
429437
case ERC875:
430438
case ERC875_LEGACY:
431439
balanceArray = getBalanceArray875(wallet, token.tokenInfo.chainId, token.getAddress());
440+
thisToken = new Ticket(thisToken, balanceArray);
432441
balance = BigDecimal.valueOf(balanceArray.size());
433442
break;
434443
case ERC721_LEGACY:

app/src/main/java/com/alphawallet/app/repository/TokensRealmSource.java

Lines changed: 49 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -288,11 +288,9 @@ public void updateNFTAssets(String wallet, Token token, List<BigInteger> additio
288288
realm.executeTransaction(r -> {
289289
createTokenIfRequired(r, token);
290290
deleteAssets(r, token, removals);
291-
int assetCount = updateNFTAssets(r, token, additions);
292-
//now re-do the balance
293-
assetCount = token.getBalanceRaw().intValue();
291+
updateNFTAssets(r, token, additions);
294292

295-
setTokenUpdateTime(r, token, assetCount);
293+
setTokenUpdateTime(r, token);
296294
});
297295
}
298296
catch (Exception e)
@@ -313,20 +311,22 @@ private void createTokenIfRequired(Realm realm, Token token)
313311
}
314312
}
315313

316-
private void setTokenUpdateTime(Realm realm, Token token, int assetCount)
314+
private void setTokenUpdateTime(Realm realm, Token token)
317315
{
318316
RealmToken realmToken = realm.where(RealmToken.class)
319317
.equalTo("address", databaseKey(token))
320318
.findFirst();
321319

320+
long tokenBalance = token.balance.longValue();
321+
322322
if (realmToken != null)
323323
{
324-
if (!realmToken.isEnabled() && !realmToken.isVisibilityChanged() && assetCount > 0)
324+
if (!realmToken.isEnabled() && !realmToken.isVisibilityChanged() && tokenBalance > 0)
325325
{
326326
token.tokenInfo.isEnabled = true;
327327
realmToken.setEnabled(true);
328328
}
329-
else if (!realmToken.isVisibilityChanged() && assetCount == 0)
329+
else if (!realmToken.isVisibilityChanged() && tokenBalance == 0)
330330
{
331331
token.tokenInfo.isEnabled = false;
332332
realmToken.setEnabled(false);
@@ -335,32 +335,51 @@ else if (!realmToken.isVisibilityChanged() && assetCount == 0)
335335
realmToken.setLastTxTime(System.currentTimeMillis());
336336
realmToken.setAssetUpdateTime(System.currentTimeMillis());
337337

338-
if (realmToken.getBalance() == null || !realmToken.getBalance().equals(String.valueOf(assetCount)))
338+
if (realmToken.getBalance() == null || !realmToken.getBalance().equals(String.valueOf(tokenBalance)))
339339
{
340-
realmToken.setBalance(String.valueOf(assetCount));
340+
realmToken.setBalance(String.valueOf(tokenBalance));
341341
}
342342
}
343343
}
344344

345-
private int updateNFTAssets(Realm realm, Token token, List<BigInteger> additions) throws RealmException
345+
private void updateNFTAssets(Realm realm, Token token, List<BigInteger> additions) throws RealmException
346346
{
347-
if (!token.isNonFungible()) return 0;
347+
if (!token.isNonFungible()) return;
348348

349349
//load all the old assets
350350
Map<BigInteger, NFTAsset> assetMap = getNFTAssets(realm, token);
351-
int assetCount = assetMap.size();
352351

353-
for (BigInteger updatedTokenId : additions)
352+
//create addition asset map
353+
Map<BigInteger, NFTAsset> additionMap = new HashMap<>();
354+
355+
for (BigInteger tokenId : additions)
356+
{
357+
NFTAsset asset = assetMap.get(tokenId);
358+
if (asset == null) asset = new NFTAsset(tokenId);
359+
additionMap.put(tokenId, asset);
360+
}
361+
362+
Map<BigInteger, NFTAsset> balanceMap = token.queryAssets(additionMap);
363+
364+
List<BigInteger> deleteList = new ArrayList<>();
365+
366+
//update token assets
367+
for (Map.Entry<BigInteger, NFTAsset> entry : balanceMap.entrySet())
354368
{
355-
NFTAsset asset = assetMap.get(updatedTokenId);
356-
if (asset == null || asset.requiresReplacement())
369+
if (entry.getValue().getBalance().longValue() == 0)
370+
{
371+
deleteList.add(entry.getKey());
372+
}
373+
else
357374
{
358-
writeAsset(realm, token, updatedTokenId, new NFTAsset());
359-
if (asset == null) assetCount++;
375+
writeAsset(realm, token, entry.getKey(), entry.getValue());
360376
}
361377
}
362378

363-
return assetCount;
379+
if (deleteList.size() > 0)
380+
{
381+
deleteAssets(realm, token, deleteList);
382+
}
364383
}
365384

366385
@Override
@@ -668,7 +687,7 @@ private void writeAssetContract(final Realm realm, Token token)
668687
realm.insertOrUpdate(realmNFT);
669688
}
670689

671-
// NFT Assets From Opensea
690+
// NFT Assets From Opensea - assume this list is trustworthy - events will catch up with it
672691
@Override
673692
public Token[] initNFTAssets(Wallet wallet, Token[] tokens)
674693
{
@@ -682,14 +701,18 @@ public Token[] initNFTAssets(Wallet wallet, Token[] tokens)
682701
//load all the assets from the database
683702
Map<BigInteger, NFTAsset> assetMap = getNFTAssets(r, token);
684703

685-
//Query the changes
686-
Map<BigInteger, NFTAsset> updatedMap = token.getAssetChange(assetMap); // feed in old assets
687-
688-
//is there any difference?
689-
if (updatedMap.hashCode() != assetMap.hashCode())
704+
//run through the new assets and patch
705+
for (Map.Entry<BigInteger, NFTAsset> entry : token.getTokenAssets().entrySet())
690706
{
691-
token.getTokenAssets().clear();
692-
token.getTokenAssets().putAll(updatedMap);
707+
NFTAsset fromOpenSea = entry.getValue();
708+
NFTAsset fromDataBase = assetMap.get(entry.getKey());
709+
710+
fromOpenSea.updateAsset(fromDataBase);
711+
712+
token.getTokenAssets().put(entry.getKey(), fromOpenSea);
713+
714+
//write to realm
715+
writeAsset(realm, token, entry.getKey(), fromOpenSea);
693716
}
694717
}
695718
});

app/src/main/java/com/alphawallet/app/router/TokenDetailRouter.java

Lines changed: 12 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -4,11 +4,11 @@
44
import android.app.Activity;
55
import android.content.Context;
66
import android.content.Intent;
7-
import android.util.Log;
87

98
import com.alphawallet.app.C;
10-
import com.alphawallet.app.entity.tokens.Token;
119
import com.alphawallet.app.entity.Wallet;
10+
import com.alphawallet.app.entity.tokens.Token;
11+
import com.alphawallet.app.ui.AssetDisplayActivity;
1212
import com.alphawallet.app.ui.Erc20DetailActivity;
1313
import com.alphawallet.app.ui.NFTActivity;
1414

@@ -55,4 +55,14 @@ public void open(Activity activity, Token token, Wallet wallet)
5555
intent.setFlags(Intent.FLAG_ACTIVITY_MULTIPLE_TASK);
5656
activity.startActivityForResult(intent, C.TERMINATE_ACTIVITY);
5757
}
58+
59+
public void openLegacyToken(Activity context, Token token, Wallet wallet)
60+
{
61+
Intent intent = new Intent(context, AssetDisplayActivity.class);
62+
intent.putExtra(C.EXTRA_CHAIN_ID, token.tokenInfo.chainId);
63+
intent.putExtra(C.EXTRA_ADDRESS, token.getAddress());
64+
intent.putExtra(C.Key.WALLET, wallet);
65+
intent.setFlags(Intent.FLAG_ACTIVITY_MULTIPLE_TASK);
66+
context.startActivityForResult(intent, C.TERMINATE_ACTIVITY);
67+
}
5868
}

0 commit comments

Comments
 (0)