Skip to content

Commit

Permalink
Update Ticker fetch system (#3410)
Browse files Browse the repository at this point in the history
  • Loading branch information
JamesSmartCell authored Nov 28, 2024
1 parent 23a043a commit 964102c
Show file tree
Hide file tree
Showing 9 changed files with 175 additions and 209 deletions.
Original file line number Diff line number Diff line change
@@ -0,0 +1,48 @@
package com.alphawallet.app.entity.ticker;

import android.text.TextUtils;

import com.alphawallet.app.entity.tokendata.TokenTicker;

import java.math.BigDecimal;
import java.math.RoundingMode;

public abstract class BaseTicker
{
public final String address;
public final double fiatPrice;
public final BigDecimal fiatChange;

public BaseTicker(String address, double fiatPrice, BigDecimal fiatChange)
{
this.address = address;
this.fiatPrice = fiatPrice;
this.fiatChange = fiatChange;
}

protected static BigDecimal getFiatChange(String fiatChangeStr)
{
if (TextUtils.isEmpty(fiatChangeStr)) return BigDecimal.ZERO;

try
{
return new BigDecimal(fiatChangeStr);
}
catch (Exception e)
{
return BigDecimal.ZERO;
}
}

public TokenTicker toTokenTicker(String currentCurrencySymbolTxt)
{
return new TokenTicker(String.valueOf(fiatPrice),
fiatChange.setScale(3, RoundingMode.DOWN).toString(), currentCurrencySymbolTxt, "", System.currentTimeMillis());
}

public TokenTicker toTokenTicker(String currentCurrencySymbolTxt, double conversionRate)
{
return new TokenTicker(String.valueOf(fiatPrice * conversionRate),
fiatChange.setScale(3, RoundingMode.DOWN).toString(), currentCurrencySymbolTxt, "", System.currentTimeMillis());
}
}
Original file line number Diff line number Diff line change
@@ -1,31 +1,20 @@
package com.alphawallet.app.entity;

import android.text.TextUtils;

import com.alphawallet.app.entity.tokendata.TokenTicker;
package com.alphawallet.app.entity.ticker;

import org.json.JSONException;
import org.json.JSONObject;

import java.math.BigDecimal;
import java.math.RoundingMode;
import java.util.ArrayList;
import java.util.List;

/**
* Created by JB on 21/04/2021.
*/
public class CoinGeckoTicker
public class CoinGeckoTicker extends BaseTicker
{
public final String address;
public final double fiatPrice;
public final BigDecimal fiatChange;

public CoinGeckoTicker(String address, double fiatPrice, BigDecimal fiatChange)
{
this.address = address;
this.fiatChange = fiatChange;
this.fiatPrice = fiatPrice;
super(address, fiatPrice, fiatChange);
}

public static List<CoinGeckoTicker> buildTickerList(String jsonData, String currencyIsoSymbol, double currentConversionRate) throws JSONException
Expand Down Expand Up @@ -60,24 +49,4 @@ else if (obj.has("usd"))

return res;
}

private static BigDecimal getFiatChange(String fiatChangeStr)
{
if (TextUtils.isEmpty(fiatChangeStr)) return BigDecimal.ZERO;

try
{
return new BigDecimal(fiatChangeStr);
}
catch (Exception e)
{
return BigDecimal.ZERO;
}
}

public TokenTicker toTokenTicker(String currentCurrencySymbolTxt)
{
return new TokenTicker(String.valueOf(fiatPrice),
fiatChange.setScale(3, RoundingMode.DOWN).toString(), currentCurrencySymbolTxt, "", System.currentTimeMillis());
}
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
package com.alphawallet.app.entity.ticker;


import com.alphawallet.app.entity.tokendata.TokenTicker;

import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;

import java.math.BigDecimal;
import java.util.Map;

public class TNDiscoveryTicker extends BaseTicker
{
public TNDiscoveryTicker(String address, double fiatPrice, BigDecimal fiatChange)
{
super(address, fiatPrice, fiatChange);
}

public TNDiscoveryTicker(JSONObject result, String address) throws JSONException
{
super(address, result.getDouble("usdPrice"), getFiatChange(result.getString("24hrPercentChange")));
}

public static void toTokenTickers(Map<String, TokenTicker> tickers, JSONArray result, String currentCurrencySymbolTxt, double currentConversionRate) throws JSONException
{
for (int i = 0; i < result.length(); i++)
{
JSONObject thisTickerObject = result.getJSONObject(i);
TNDiscoveryTicker thisTicker = new TNDiscoveryTicker(thisTickerObject, thisTickerObject.getString("contract"));
tickers.put(thisTickerObject.getString("contract"), thisTicker.toTokenTicker(currentCurrencySymbolTxt, currentConversionRate));
}
}
}
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
package com.alphawallet.app.repository;

import static com.alphawallet.app.service.TickerService.TICKER_STALE_TIMEOUT;
import static com.alphawallet.app.service.TickerService.TICKER_TIMEOUT;
import static com.alphawallet.app.service.TokensService.EXPIRED_CONTRACT;

Expand Down Expand Up @@ -1409,7 +1410,13 @@ public Map<String, Long> getTickerTimeMap(long chainId, List<TokenCardMeta> erc2

if (realmItem != null)
{
updateMap.put(meta.getAddress(), realmItem.getUpdatedTime());
// how old is the update time?
long updateTime = realmItem.getUpdatedTime();
long age = System.currentTimeMillis() - updateTime;
if (age < TICKER_STALE_TIMEOUT) //ticker old, don't fetch
{
updateMap.put(meta.getAddress(), realmItem.getUpdatedTime());
}
}
}
}
Expand Down Expand Up @@ -1454,10 +1461,7 @@ public void deleteTickers()
//.lessThan("updatedTime", System.currentTimeMillis() - TICKER_TIMEOUT)
.findAll();

for (RealmTokenTicker data : realmItems)
{
data.deleteFromRealm();
}
realmItems.deleteAllFromRealm();
});
}
catch (Exception e)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -83,7 +83,7 @@ private QueryResponse getFromIPFS(String url) throws IOException
if (isTestCode(url)) return loadTestCode();

//try Infura first
String tryIPFS = Utils.resolveIPFS(url, Utils.IPFS_IO_RESOLVER);
String tryIPFS = Utils.resolveIPFS(url, Utils.IPFS_INFURA_RESOLVER);
//attempt to load content
QueryResponse r;
try
Expand Down
Loading

0 comments on commit 964102c

Please sign in to comment.