Skip to content

Commit

Permalink
Fix erc20 tokenscript (#3361)
Browse files Browse the repository at this point in the history
* Rework gas fetch methods

* Display ERC20 correctly
  • Loading branch information
JamesSmartCell authored Feb 19, 2024
1 parent cc137ba commit b36cc41
Show file tree
Hide file tree
Showing 14 changed files with 200 additions and 191 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -58,7 +58,6 @@
import com.alphawallet.token.entity.SigReturnType;
import com.alphawallet.token.entity.TSAction;
import com.alphawallet.token.entity.TSSelection;
import com.alphawallet.token.entity.TSTokenView;
import com.alphawallet.token.entity.TokenScriptResult;
import com.alphawallet.token.entity.TokenscriptContext;
import com.alphawallet.token.entity.TokenscriptElement;
Expand Down Expand Up @@ -210,7 +209,7 @@ private void loadAssetScripts()
Timber.e(e);
}

List<String> handledHashes = checkRealmScriptsForChanges();
//List<String> handledHashes = checkRealmScriptsForChanges();
//loadNewFiles(handledHashes);

//executes after observable completes due to blockingForEach
Expand Down Expand Up @@ -1203,6 +1202,7 @@ private TokenDefinition parseFile(InputStream xmlInputStream) throws Exception

private Single<TokenDefinition> handleNewTSFile(File newFile)
{
//if unchanged return existing definition
if (!newFile.exists())
{
return signalUnchangedScript(newFile.getName());
Expand Down Expand Up @@ -2176,12 +2176,6 @@ public boolean hasDefinition(Token token)
return hasDefinition;
}

//when user reloads the tokens we should also check XML for any files
public void clearCheckTimes()
{
assetChecked.clear();
}

public boolean hasTokenView(Token token, String type)
{
try (Realm realm = realmManager.getRealmInstance(ASSET_DEFINITION_DB))
Expand All @@ -2194,43 +2188,6 @@ public boolean hasTokenView(Token token, String type)
}
}

public TSTokenView getTSTokenView(Token token, String type)
{
TokenDefinition td = getAssetDefinition(token);
if (td != null)
{
return td.getTSTokenView(type);
}
else
{
return null;
}
}

public String getTokenView(Token token, String type)
{
String viewHTML = "";
TokenDefinition td = getAssetDefinition(token);
if (td != null)
{
viewHTML = td.getTokenView(type);
}

return viewHTML;
}

public String getTokenViewStyle(Token token, String type)
{
String styleData = "";
TokenDefinition td = getAssetDefinition(token);
if (td != null)
{
styleData = td.getTokenViewStyle(type);
}

return styleData;
}

public List<Attribute> getTokenViewLocalAttributes(Token token)
{
TokenDefinition td = getAssetDefinition(token);
Expand Down Expand Up @@ -2312,6 +2269,9 @@ public Single<Map<BigInteger, List<String>>> fetchFunctionMap(Token token, @NotN
Map<BigInteger, Map<String, TokenScriptResult.Attribute>> attrResults // Map of attribute results vs tokenId
= getRequiredAttributeResults(requiredAttrNames, tokenIds, td, token, update); // Map of all required attribute values vs all the tokenIds

//What about for ERC20?
//ERC20!

for (BigInteger tokenId : tokenIds)
{
for (String actionName : actions.keySet())
Expand Down Expand Up @@ -2741,7 +2701,7 @@ private void addOpenSeaAttributes(StringBuilder attrs, Token token, BigInteger t
}
}

public StringBuilder getTokenAttrs(Token token, BigInteger tokenId, int count)
public StringBuilder getTokenAttrs(Token token, BigInteger tokenId, BigInteger count)
{
StringBuilder attrs = new StringBuilder();

Expand All @@ -2754,7 +2714,7 @@ public StringBuilder getTokenAttrs(Token token, BigInteger tokenId, int count)
TokenScriptResult.addPair(attrs, "name", token.tokenInfo.name);
TokenScriptResult.addPair(attrs, "label", label);
TokenScriptResult.addPair(attrs, "symbol", token.getSymbol());
TokenScriptResult.addPair(attrs, "_count", BigInteger.valueOf(count));
TokenScriptResult.addPair(attrs, "_count", count.toString());
TokenScriptResult.addPair(attrs, "contractAddress", token.tokenInfo.address);
TokenScriptResult.addPair(attrs, "chainId", BigInteger.valueOf(token.tokenInfo.chainId));
TokenScriptResult.addPair(attrs, "tokenId", tokenId);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -213,7 +213,7 @@ private void initWebViewCheck(TokenDefinition td)
BigInteger tokenId = token.getArrayBalance().get(0);
TicketRange data = new TicketRange(tokenId, token.getAddress());
testView.setChainId(token.tokenInfo.chainId);
testView.renderTokenScriptView(token, data, viewModel.getAssetDefinitionService(), ViewType.ITEM_VIEW, td);
testView.renderTokenScriptInfoView(token, data, viewModel.getAssetDefinitionService(), ViewType.ITEM_VIEW, td);
testView.setOnReadyCallback(this);
}
else
Expand Down
35 changes: 25 additions & 10 deletions app/src/main/java/com/alphawallet/app/ui/Erc20DetailActivity.java
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
import static com.alphawallet.app.repository.TokensRealmSource.databaseKey;
import static com.alphawallet.app.ui.MyAddressActivity.KEY_MODE;
import static com.alphawallet.ethereum.EthereumNetworkBase.MAINNET_ID;
import static com.alphawallet.token.tools.TokenDefinition.UNCHANGED_SCRIPT;

import android.content.Context;
import android.content.Intent;
Expand Down Expand Up @@ -49,13 +50,15 @@
import com.alphawallet.app.widget.CertifiedToolbarView;
import com.alphawallet.app.widget.FunctionButtonBar;
import com.alphawallet.token.entity.XMLDsigDescriptor;
import com.alphawallet.token.tools.TokenDefinition;
import com.google.android.material.tabs.TabLayout;
import com.google.android.material.tabs.TabLayoutMediator;

import org.jetbrains.annotations.NotNull;

import java.math.BigInteger;
import java.util.ArrayList;
import java.util.Collections;
import java.util.List;

import dagger.hilt.android.AndroidEntryPoint;
Expand All @@ -79,6 +82,8 @@ public class Erc20DetailActivity extends BaseActivity implements StandardFunctio
private ActivityHistoryList activityHistoryList = null;
private Realm realm = null;
private RealmResults<RealmToken> realmTokenUpdates;
private CertifiedToolbarView certHeader;
private final TokenInfoFragment tokenInfoFragment = new TokenInfoFragment();

private ViewPager2 viewPager;

Expand All @@ -100,7 +105,7 @@ public Pair<String, Fragment> init(Context context, Bundle bundle)
}

private final DetailPage[] detailPages = new DetailPage[] {
new DetailPage(R.string.tab_info, new TokenInfoFragment()),
new DetailPage(R.string.tab_info, tokenInfoFragment),
new DetailPage(R.string.tab_activity, new TokenActivityFragment()),
new DetailPage(R.string.tab_alert, new TokenAlertsFragment())
};
Expand All @@ -126,6 +131,8 @@ protected void onCreate(@Nullable Bundle savedInstanceState)
getIntentData();
}

certHeader = findViewById(R.id.certified_toolbar);
certHeader.startDownload();
viewModel.checkForNewScript(token);

setTitle(token.tokenInfo.name + " (" + token.tokenInfo.symbol + ")");
Expand Down Expand Up @@ -190,8 +197,8 @@ private void setupButtons()
if (BuildConfig.DEBUG || wallet.type != WalletType.WATCH)
{
FunctionButtonBar functionBar = findViewById(R.id.layoutButtons);
functionBar.setupFunctions(this, viewModel.getAssetDefinitionService(), token, null, Collections.singletonList(BigInteger.ZERO));
functionBar.setupBuyFunction(this, viewModel.getOnRampRepository());
functionBar.setupFunctions(this, viewModel.getAssetDefinitionService(), token, null, null);
functionBar.revealButtons();
functionBar.setWalletType(wallet.type);
}
Expand All @@ -203,10 +210,9 @@ private void setupViewModel()
{
viewModel = new ViewModelProvider(this)
.get(Erc20DetailViewModel.class);
viewModel.newScriptFound().observe(this, this::onNewScript);
viewModel.newScriptFound().observe(this, this::newScriptFound);
viewModel.sig().observe(this, this::onSignature);
viewModel.scriptUpdateInProgress().observe(this, this::startScriptDownload);
// findViewById(R.id.certificate_spinner).setVisibility(View.VISIBLE); //Samoa TODO: restore certificate toolbar
}
}

Expand All @@ -225,16 +231,27 @@ private void startScriptDownload(Boolean status)
}
}

private void onNewScript(Boolean hasNewScript)
private void newScriptFound(TokenDefinition td)
{
//determinate signature
certHeader.stopDownload();
certHeader.setVisibility(View.VISIBLE);

final TokenGroup group = viewModel.getTokensService().getTokenGroup(token);
//found a new tokenscript for this token, create a new meta with balance set to trigger view update; view will update the token name
tokenViewAdapter.updateToken(new TokenCardMeta(token.tokenInfo.chainId, token.getAddress(), "force_update",
token.updateBlancaTime, token.lastTxCheck, token.getInterfaceSpec(), group));
viewModel.checkTokenScriptValidity(token); //check script signature
CertifiedToolbarView certificateToolbar = findViewById(R.id.certified_toolbar);
certificateToolbar.stopDownload();

setupButtons();

if (td.nameSpace.equals(UNCHANGED_SCRIPT))
{
td = viewModel.getAssetDefinitionService().getAssetDefinition(token);
}

//new script, need to check validity
viewModel.checkTokenScriptValidity(token); //check script signature
tokenInfoFragment.initTokenScript(td);
}

private void onSignature(XMLDsigDescriptor descriptor)
Expand Down Expand Up @@ -274,7 +291,6 @@ public boolean canScrollVertically()
tokenView.setAdapter(tokenViewAdapter);
setTokenListener();
setupButtons();
viewModel.checkTokenScriptValidity(token);
}

private void getIntentData()
Expand All @@ -288,7 +304,6 @@ private void getIntentData()
token = viewModel.getTokensService().getTokenOrBase(chainId, getIntent().getStringExtra(C.EXTRA_ADDRESS));
token.group = viewModel.getTokensService().getTokenGroup(token);
tokenMeta = new TokenCardMeta(token, token.getName());
viewModel.checkForNewScript(token);
}

@Override
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -197,7 +197,7 @@ private void getAttrs()
{
try
{
attrs = viewModel.getAssetDefinitionService().getTokenAttrs(token, tokenId, 1);
attrs = viewModel.getAssetDefinitionService().getTokenAttrs(token, tokenId, BigInteger.ONE);
//add extra tokenIds if required
addMultipleTokenIds(attrs);
}
Expand Down
25 changes: 0 additions & 25 deletions app/src/main/java/com/alphawallet/app/ui/NFTActivity.java
Original file line number Diff line number Diff line change
Expand Up @@ -51,7 +51,6 @@ public class NFTActivity extends BaseActivity implements StandardFunctionInterfa
private NFTViewModel viewModel;
private Wallet wallet;
private Token token;
private FunctionButtonBar functionBar;
private boolean isGridView;
private MenuItem sendMultipleTokensMenuItem;
private MenuItem switchToGridViewMenuItem;
Expand Down Expand Up @@ -241,11 +240,9 @@ public void onTabSelected(TabLayout.Tab tab)
switch (tab.getPosition())
{
case 0:
// showFunctionBar(true);
showMenu();
break;
default:
// showFunctionBar(false);
hideMenu();
break;
}
Expand All @@ -265,16 +262,6 @@ public void onTabReselected(TabLayout.Tab tab)
});
}

private void showFunctionBar(boolean show)
{
if (functionBar == null && !show) return;
if (BuildConfig.DEBUG || wallet.type != WalletType.WATCH)
{
if (functionBar == null) setupFunctionBar();
functionBar.setVisibility(show ? View.VISIBLE : View.GONE);
}
}

@Override
public boolean onPrepareOptionsMenu(Menu menu)
{
Expand Down Expand Up @@ -346,18 +333,6 @@ public void onDestroy()
viewModel.onDestroy();
}

private void setupFunctionBar()
{
if (BuildConfig.DEBUG || wallet.type != WalletType.WATCH)
{
functionBar = findViewById(R.id.layoutButtons);
functionBar.setupFunctions(this, viewModel.getAssetDefinitionService(), token, null, null);
functionBar.revealButtons();
functionBar.setWalletType(wallet.type);
functionBar.setVisibility(View.GONE);
}
}

private void hideMenu()
{
if (sendMultipleTokensMenuItem != null)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,6 @@
import com.alphawallet.app.entity.GasEstimate;
import com.alphawallet.app.entity.SignAuthenticationCallback;
import com.alphawallet.app.entity.StandardFunctionInterface;
import com.alphawallet.app.entity.TSAttrCallback;
import com.alphawallet.app.entity.TransactionReturn;
import com.alphawallet.app.entity.Wallet;
import com.alphawallet.app.entity.WalletType;
Expand Down Expand Up @@ -75,6 +74,7 @@

import dagger.hilt.android.AndroidEntryPoint;
import io.reactivex.functions.Consumer;
import timber.log.Timber;

@AndroidEntryPoint
public class NFTAssetDetailActivity extends BaseActivity implements StandardFunctionInterface, ActionSheetCallback
Expand Down Expand Up @@ -851,11 +851,19 @@ private boolean displayTokenView(final TokenDefinition td)
try
{
LinearLayout webWrapper = findViewById(R.id.layout_webwrapper);
//restart if required
if (tokenScriptView != null)
{
webWrapper.removeView(tokenScriptView);
tokenScriptView.destroy();
tokenScriptView = null;
}

tokenScriptView = new Web3TokenView(this);
tokenScriptView.setLayoutParams(new ViewGroup.LayoutParams(ViewGroup.LayoutParams.MATCH_PARENT, ViewGroup.LayoutParams.WRAP_CONTENT));
tokenScriptView.clearCache(true);

if (tokenScriptView.renderTokenScriptView(token, new TicketRange(tokenId, token.getAddress()), viewModel.getAssetDefinitionService(), ViewType.VIEW, td))
if (tokenScriptView.renderTokenScriptInfoView(token, new TicketRange(tokenId, token.getAddress()), viewModel.getAssetDefinitionService(), ViewType.VIEW, td))
{
webWrapper.setVisibility(View.VISIBLE);
tokenScriptView.setChainId(token.tokenInfo.chainId);
Expand All @@ -866,6 +874,7 @@ private boolean displayTokenView(final TokenDefinition td)
}
catch (Exception e)
{
Timber.e(e);
//fillEmpty();
}

Expand Down
Loading

0 comments on commit b36cc41

Please sign in to comment.