Skip to content

Commit

Permalink
refactor web page rendering
Browse files Browse the repository at this point in the history
  • Loading branch information
bitcoinwarrior1 committed Nov 30, 2019
1 parent 3f6d026 commit 0ff0727
Show file tree
Hide file tree
Showing 3 changed files with 45 additions and 69 deletions.
104 changes: 41 additions & 63 deletions dmz/src/main/java/com/alphawallet/token/web/AppSiteController.java
Original file line number Diff line number Diff line change
Expand Up @@ -168,7 +168,6 @@ private String handleTokenLink(

String tokenName = txHandler.getNameOnly(data.contractAddress);
String symbol = txHandler.getSymbolOnly(data.contractAddress);
String nameWithSymbol = tokenName + "(" + symbol + ")";

try
{
Expand All @@ -186,93 +185,65 @@ private String handleTokenLink(
.isDisposed();

String available = "available";
if (Calendar.getInstance().getTime().after(new Date(data.expiry*1000))) available = "expired";

String price = getEthString(data.price) + " " + MagicLinkInfo.getNetworkNameById(data.chainId);

String title = data.ticketCount + " " + definition.getTokenName(data.ticketCount) + " " + available;
if (Calendar.getInstance().getTime().after(new Date(data.expiry*1000)))
{
available = "expired";
}

String view = definition.getCardData("view");
String style = definition.getCardData("style");
String initHTML = loadFile("templates/tokenscriptTemplate.html");

String scriptData = loadFile("templates/token_inject.js.tokenscript");
String tokenView = String.format(scriptData,
tokenData.toString(), view);

String expiry = new java.util.Date(data.expiry*1000).toString();
String tokenView = String.format(scriptData, tokenData.toString(), view);

String availableUntil = "<span title=\"Unix Time is " + data.expiry + "\">" + expiry + "</span>";
String action = "\"" + universalLink + "\"";
String originalLink = "\"https://" + MagicLinkInfo.getMagicLinkDomainFromNetworkId(data.chainId) + "/" + universalLink + "\"";

String etherscanAccountLink = MagicLinkInfo.getEtherscanURLbyNetwork(data.chainId) + "address/" + data.ownerAddress;
String etherscanTokenLink = MagicLinkInfo.getEtherscanURLbyNetwork(data.chainId) + "token/" + data.contractAddress;

return String.format(
initHTML,
title, style, String.valueOf(data.ticketCount), nameWithSymbol, definition.getTokenName(data.ticketCount),
price, available,
data.ticketCount, definition.getTokenName(data.ticketCount),
tokenView, availableUntil,
action, originalLink,
etherscanAccountLink, data.ownerAddress,
etherscanTokenLink, data.contractAddress
);
return formWebPage(txHandler, data, universalLink, available, style, tokenView);
}



//TODO check spawnable link is actual valid by checking balance or checking link has not been used (if coming from issuer)
private String renderTokenWithoutTokenScript(MagicLinkData data, String universalLink)
{
TransactionHandler txHandler = new TransactionHandler(data.chainId);

List<BigInteger> balanceArray;
BigInteger firstTokenId = BigInteger.ZERO;
String available = "available";

if (Calendar.getInstance().getTime().after(new Date(data.expiry*1000)))
{
available = "expired";
}

if (data.contractType == spawnable)
{
firstTokenId = data.tokenIds.get(0);
}
else
try
{
try
balanceArray = txHandler.getBalanceArray(data.ownerAddress, data.contractAddress);
//check indices
for (int index : data.indices)
{
balanceArray = txHandler.getBalanceArray(data.ownerAddress, data.contractAddress);
//check indices
for (int index : data.indices)
if (index >= balanceArray.size() || balanceArray.get(index).equals(BigInteger.ZERO))
{
if (index >= balanceArray.size() || balanceArray.get(index).equals(BigInteger.ZERO))
{
available = "unavailable";
}
available = "unavailable";
}
firstTokenId = balanceArray.get(0);
}
catch (Exception e)
{
e.printStackTrace();
}
}
return formWebPage(firstTokenId, txHandler, data, universalLink, available);
catch (Exception e)
{
e.printStackTrace();
}

return formWebPage(txHandler, data, universalLink, available, "", "");
}

private String formWebPage(
BigInteger firstTokenId,
TransactionHandler txHandler,
MagicLinkData data,
String universalLink,
String available
String available,
String style,
String tokenView
)
{
System.out.println(firstTokenId.toString(16));
String tokenName = txHandler.getNameOnly(data.contractAddress);
String tokenName = txHandler.getName(data.contractAddress);
String symbol = txHandler.getSymbolOnly(data.contractAddress);
String nameWithSymbol = tokenName + "(" + symbol + ")";

Expand All @@ -282,7 +253,7 @@ private String formWebPage(

String initHTML = loadFile("templates/tokenscriptTemplate.html");

String expiry = new java.util.Date(data.expiry*1000).toString();
String expiry = new java.util.Date(data.expiry * 1000).toString();

String availableUntil = "<span title=\"Unix Time is " + data.expiry + "\">" + expiry + "</span>";
String action = "\"" + universalLink + "\"";
Expand All @@ -294,24 +265,32 @@ private String formWebPage(
return String.format(
initHTML,
title,
style,
String.valueOf(data.ticketCount),
nameWithSymbol,
"",
String.valueOf(data.ticketCount), nameWithSymbol, "Tokens",
price, available,
data.ticketCount, "Tokens",
"", availableUntil,
action, originalLink,
etherscanAccountLink, data.ownerAddress,
etherscanTokenLink, data.contractAddress
price,
available,
data.ticketCount,
tokenName,
tokenView,
availableUntil,
action,
originalLink,
etherscanAccountLink,
data.ownerAddress,
etherscanTokenLink,
data.contractAddress
);
}

private TokenDefinition getTokenDefinition(int chainId, String contractAddress) throws IOException, SAXException, NoHandlerFoundException
{
File xml = null;
TokenDefinition definition = null;
if (addresses.containsKey(100) && addresses.get(100).containsKey(contractAddress))
if (addresses.containsKey(chainId) && addresses.get(chainId).containsKey(contractAddress))
{
xml = addresses.get(100).get(contractAddress);
xml = addresses.get(chainId).get(contractAddress);
if (xml == null) {
/* this is impossible to happen, because at least 1 xml should present or main() bails out */
throw new NoHandlerFoundException("GET", "/" + contractAddress, new HttpHeaders());
Expand All @@ -322,7 +301,6 @@ private TokenDefinition getTokenDefinition(int chainId, String contractAddress)
definition = new TokenDefinition(in, new Locale("en"), null);
}
}

return definition;
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -96,18 +96,16 @@ public String getSymbolOnly(String address)
public String getName(String address)
{
String name = "";
String symbol = "";
try
{
name = getContractData(address, stringParam("name"));
symbol = getContractData(address, stringParam("symbol"));
}
catch (Exception e)
{
e.printStackTrace();
}

return name + " (" + symbol + ")";
return name;
}

private <T> T getContractData(String address, org.web3j.abi.datatypes.Function function) throws Exception
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -13,11 +13,11 @@
class SpawnableLinkGenerator {

private static List<BigInteger> tokens = new ArrayList<>();
private static final String contractAddress = "0xC1c14278Cc455d9dFA0b0257aF40862bD8677EFC";
private static final String contractAddress = "0xc2f901070AdcfA07Bc5c46cc2390063248eb9E81";
private static ParseMagicLink parseMagicLink = new ParseMagicLink(new CryptoFunctions(), null);
//TODO set private key & chain id
private static final BigInteger privateKey = BigInteger.TEN;
private static final int chainId = 4;
private static final int chainId = 3;

// Time todo put in right format & set each time
private static final String date = "20240706210000+0300";
Expand Down Expand Up @@ -71,7 +71,7 @@ class SpawnableLinkGenerator {

public static void main(String[] args) throws SalesOrderMalformed {
//TODO set token ids here
new SpawnableLinkGenerator(date, LONDON, PARKEN_STADIUM, TEAM_A, TEAM_B, 5);
new SpawnableLinkGenerator(date, LONDON, PARKEN_STADIUM, TEAM_A, TEAM_B, 1);
}

private SpawnableLinkGenerator(
Expand Down

0 comments on commit 0ff0727

Please sign in to comment.