Repository: alphawallet/alpha-wallet-android
Issue: Value to tx is not added from TokenScript

Normally the value param allows you to add eth to your transaction, this does not work in android when the amount to add is not user-entry

Changed files:
app/src/main/java/com/alphawallet/app/ui/ConfirmationActivity.java, app/src/main/java/com/alphawallet/app/service/AssetDefinitionService.java, app/src/main/java/com/alphawallet/app/viewmodel/TokenFunctionViewModel.java

diff --git a/app/src/main/java/com/alphawallet/app/service/AssetDefinitionService.java b/app/src/main/java/com/alphawallet/app/service/AssetDefinitionService.java
index 5367c9196..0a540f145 100644
--- a/app/src/main/java/com/alphawallet/app/service/AssetDefinitionService.java
+++ b/app/src/main/java/com/alphawallet/app/service/AssetDefinitionService.java
@@ -12,6 +12,7 @@ import android.os.FileObserver;
 import android.support.annotation.Nullable;
 import android.support.v4.app.NotificationCompat;
 import android.support.v4.content.ContextCompat;
+import android.text.TextUtils;
 import android.util.SparseArray;
 
 import com.alphawallet.app.C;
@@ -41,7 +42,6 @@ import com.alphawallet.token.entity.ContractInfo;
 import com.alphawallet.token.entity.EventDefinition;
 import com.alphawallet.token.entity.FunctionDefinition;
 import com.alphawallet.token.entity.MethodArg;
-import com.alphawallet.token.entity.NonFungibleToken;
 import com.alphawallet.token.entity.ParseResult;
 import com.alphawallet.token.entity.SigReturnType;
 import com.alphawallet.token.entity.TSAction;
@@ -53,6 +53,7 @@ import com.alphawallet.token.entity.XMLDsigDescriptor;
 import com.alphawallet.token.tools.Numeric;
 import com.alphawallet.token.tools.TokenDefinition;
 
+import org.jetbrains.annotations.NotNull;
 import org.web3j.abi.FunctionEncoder;
 import org.web3j.abi.datatypes.Function;
 import org.web3j.crypto.WalletUtils;
@@ -60,7 +61,6 @@ import org.web3j.protocol.Web3j;
 import org.web3j.protocol.core.methods.request.EthFilter;
 import org.web3j.protocol.core.methods.response.EthBlock;
 import org.web3j.protocol.core.methods.response.EthLog;
-import org.web3j.protocol.core.methods.response.EthTransaction;
 import org.web3j.protocol.core.methods.response.Log;
 import org.xml.sax.SAXException;
 
@@ -76,7 +76,6 @@ import java.math.BigInteger;
 import java.net.HttpURLConnection;
 import java.text.SimpleDateFormat;
 import java.util.ArrayList;
-import java.util.Arrays;
 import java.util.Date;
 import java.util.List;
 import java.util.Locale;
@@ -85,7 +84,6 @@ import java.util.TimeZone;
 import java.util.concurrent.ConcurrentHashMap;
 import java.util.concurrent.Semaphore;
 import java.util.concurrent.TimeUnit;
-import java.util.stream.Collectors;
 
 import io.reactivex.Completable;
 import io.reactivex.Observable;
@@ -1835,4 +1833,16 @@ public class AssetDefinitionService implements ParseResult, AttributeInterface
             generateAndSendEvents();
         }
     }
+
+    public void resolveReference(@NotNull Token token, TSAction action, TokenscriptElement arg, BigInteger tokenId)
+    {
+        TokenDefinition td = getAssetDefinition(token.tokenInfo.chainId, token.getAddress());
+        if (td == null || TextUtils.isEmpty(arg.ref)) return;
+        AttributeType attributeType = (action != null && action.attributeTypes != null) ? action.attributeTypes.get(arg.ref) : null; // try locals first
+        if (attributeType == null) attributeType = td.attributeTypes.get(arg.ref); // now try globals
+        if (attributeType != null)
+        {
+            arg.value = tokenscriptUtility.fetchAttrResult(tokensService.getCurrentAddress(), attributeType, tokenId, null, td, this, 0).blockingSingle().text;
+        }
+    }
 }
diff --git a/app/src/main/java/com/alphawallet/app/ui/ConfirmationActivity.java b/app/src/main/java/com/alphawallet/app/ui/ConfirmationActivity.java
index d038a007b..4a427e35a 100644
--- a/app/src/main/java/com/alphawallet/app/ui/ConfirmationActivity.java
+++ b/app/src/main/java/com/alphawallet/app/ui/ConfirmationActivity.java
@@ -7,6 +7,7 @@ import android.content.Context;
 import android.content.Intent;
 import android.os.Bundle;
 import android.support.annotation.Nullable;
+import android.text.TextUtils;
 import android.view.Menu;
 import android.view.MenuItem;
 import android.view.View;
@@ -135,6 +136,7 @@ public class ConfirmationActivity extends BaseActivity implements SignAuthentica
         String tokenList = getIntent().getStringExtra(C.EXTRA_TOKENID_LIST);
         token = getIntent().getParcelableExtra(C.EXTRA_TOKEN_ID);
         chainId = token != null ? token.tokenInfo.chainId : getIntent().getIntExtra(C.EXTRA_NETWORKID, 1);
+        String functionDetails = getIntent().getStringExtra(C.EXTRA_FUNCTION_NAME);
 
         String amountString;
 
@@ -191,6 +193,7 @@ public class ConfirmationActivity extends BaseActivity implements SignAuthentica
                 contractAddrLabel.setVisibility(View.VISIBLE);
                 contractAddrText.setText(contractAddress);
                 amountString = getIntent().getStringExtra(C.EXTRA_ACTION_NAME);
+                if (!TextUtils.isEmpty(functionDetails)) amountString = functionDetails;
                 symbolText.setVisibility(View.GONE);
 
                 transactionHex = getIntent().getStringExtra(C.EXTRA_TRANSACTION_DATA);
diff --git a/app/src/main/java/com/alphawallet/app/viewmodel/TokenFunctionViewModel.java b/app/src/main/java/com/alphawallet/app/viewmodel/TokenFunctionViewModel.java
index 5c6b354d4..485af7cf9 100644
--- a/app/src/main/java/com/alphawallet/app/viewmodel/TokenFunctionViewModel.java
+++ b/app/src/main/java/com/alphawallet/app/viewmodel/TokenFunctionViewModel.java
@@ -6,6 +6,7 @@ import android.arch.lifecycle.MutableLiveData;
 import android.content.Context;
 import android.content.Intent;
 import android.support.annotation.Nullable;
+import android.text.TextUtils;
 
 import com.alphawallet.app.C;
 import com.alphawallet.app.R;
@@ -39,6 +40,7 @@ import com.alphawallet.token.entity.SigReturnType;
 import com.alphawallet.token.entity.TSAction;
 import com.alphawallet.token.entity.TicketRange;
 import com.alphawallet.token.entity.TokenScriptResult;
+import com.alphawallet.token.entity.TokenscriptElement;
 import com.alphawallet.token.entity.XMLDsigDescriptor;
 
 import java.math.BigDecimal;
@@ -371,11 +373,14 @@ public class TokenFunctionViewModel extends BaseViewModel
             String value = "0";
             if (action.function.tx != null && action.function.tx.args.containsKey("value"))
             {
+                TokenscriptElement arg = action.function.tx.args.get("value");
+                //resolve reference
+                assetDefinitionService.resolveReference(token, action, arg, tokenId);
                 //this is very specific but 'value' is a specifically handled param
-                value = action.function.tx.args.get("value").value;
-                BigDecimal valCorrected = getCorrectedBalance(value, 18);
+                value = arg.value;
+
                 Token currency = getCurrency(token.tokenInfo.chainId);
-                functionEffect = valCorrected.toString() + " " + currency.getSymbol() + " to " + action.function.method;
+                functionEffect = getCorrectedBalance(value, 18) + " " + currency.getSymbol() + " to " + action.function.method;
             }
 
             //finished resolving attributes, blank definition cache so definition is re-loaded when next needed
@@ -431,13 +436,13 @@ public class TokenFunctionViewModel extends BaseViewModel
         }
     }
 
-    private BigDecimal getCorrectedBalance(String value, int scale)
+    private BigDecimal getCorrectedBalance(String value, int divisor)
     {
         BigDecimal val = BigDecimal.ZERO;
         try
         {
             val = new BigDecimal(value);
-            BigDecimal decimalDivisor = new BigDecimal(Math.pow(10, scale));
+            BigDecimal decimalDivisor = new BigDecimal(Math.pow(10, divisor));
             val = val.divide(decimalDivisor);
         }
         catch (Exception e)
@@ -445,7 +450,7 @@ public class TokenFunctionViewModel extends BaseViewModel
             e.printStackTrace();
         }
 
-        return val.setScale(scale, RoundingMode.HALF_DOWN).stripTrailingZeros();
+        return val.setScale(4, RoundingMode.HALF_DOWN).stripTrailingZeros();
     }
 
     public OpenseaService getOpenseaService()