Skip to content

Commit

Permalink
Update ticker call
Browse files Browse the repository at this point in the history
  • Loading branch information
JamesSmartCell committed Apr 19, 2024
1 parent 1a33f65 commit 02f36bf
Show file tree
Hide file tree
Showing 6 changed files with 99 additions and 14 deletions.
22 changes: 22 additions & 0 deletions app/src/main/cpp/keys.c
Original file line number Diff line number Diff line change
Expand Up @@ -310,6 +310,28 @@ Java_com_alphawallet_app_repository_KeyProviderJNIImpl_getSmartPassKey( JNIEnv*
#endif
}

JNIEXPORT jstring JNICALL
Java_com_alphawallet_app_repository_KeyProviderJNIImpl_getCoinGeckoKey( JNIEnv* env, jclass thiz )
{
#if (HAS_KEYS == 1)
return getDecryptedKey(env, coinGeckoKey);
#else
const jstring key = "";
return (*env)->NewStringUTF(env, key);
#endif
}

JNIEXPORT jstring JNICALL
Java_com_alphawallet_app_repository_KeyProviderJNIImpl_getBackupKey( JNIEnv* env, jclass thiz )
{
#if (HAS_KEYS == 1)
return getDecryptedKey(env, backupKey1);
#else
const jstring key = "";
return (*env)->NewStringUTF(env, key);
#endif
}

JNIEXPORT jstring JNICALL
Java_com_alphawallet_app_repository_KeyProviderJNIImpl_getSmartPassDevKey( JNIEnv* env, jclass thiz )
{
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -49,4 +49,8 @@ public interface KeyProvider
String getSmartPassKey();

String getSmartPassDevKey();

String getCoinGeckoKey();

String getBackupKey();
}
Original file line number Diff line number Diff line change
Expand Up @@ -54,4 +54,8 @@ public KeyProviderJNIImpl()
public native String getSmartPassKey();

public native String getSmartPassDevKey();

public native String getCoinGeckoKey();

public native String getBackupKey();
}
59 changes: 45 additions & 14 deletions app/src/main/java/com/alphawallet/app/service/TickerService.java
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,8 @@
import com.alphawallet.app.entity.tokens.Token;
import com.alphawallet.app.entity.tokens.TokenCardMeta;
import com.alphawallet.app.repository.EthereumNetworkRepository;
import com.alphawallet.app.repository.KeyProvider;
import com.alphawallet.app.repository.KeyProviderFactory;
import com.alphawallet.app.repository.PreferenceRepositoryType;
import com.alphawallet.app.repository.TokenLocalSource;
import com.alphawallet.app.repository.TokenRepository;
Expand Down Expand Up @@ -96,7 +98,7 @@ public class TickerService
private static final boolean ALLOW_UNVERIFIED_TICKERS = false; //allows verified:false tickers from DEX.GURU. Not recommended
public static final long TICKER_TIMEOUT = DateUtils.WEEK_IN_MILLIS; //remove ticker if not seen in one week
public static final long TICKER_STALE_TIMEOUT = 30 * DateUtils.MINUTE_IN_MILLIS; //Use market API if AlphaWallet market oracle not updating

private final KeyProvider keyProvider = KeyProviderFactory.get();
private final OkHttpClient httpClient;
private final PreferenceRepositoryType sharedPrefs;
private final TokenLocalSource localSource;
Expand All @@ -107,6 +109,7 @@ public class TickerService
private static final ConcurrentLinkedDeque<TokenCardMeta> tokenCheckQueue = new ConcurrentLinkedDeque<>();
private static final Map<String, TokenCardMeta> dexGuruQuery = new ConcurrentHashMap<>();
private static long lastTickerUpdate;
private static int keyCycle = 0;

@Nullable
private Disposable tickerUpdateTimer;
Expand Down Expand Up @@ -290,7 +293,7 @@ private List<TokenCardMeta> nextTickerSet(int count)
{
List<TokenCardMeta> tickerList = new ArrayList<>(count);
long chainId = 0;
if (tokenCheckQueue.size() > 0)
if (!tokenCheckQueue.isEmpty())
{
List<TokenCardMeta> addBack = new ArrayList<>();
TokenCardMeta firstTcm = tokenCheckQueue.removeFirst();
Expand Down Expand Up @@ -325,7 +328,7 @@ private List<TokenCardMeta> nextTickerSet(int count)
public Single<Integer> syncERC20Tickers(long chainId, List<TokenCardMeta> erc20Tokens)
{
//add to queue here
long staleTime = System.currentTimeMillis() - 5 * DateUtils.MINUTE_IN_MILLIS;
long staleTime = System.currentTimeMillis() - 10 * DateUtils.MINUTE_IN_MILLIS;
//only check networks with value and if there's actually tokens to check
if (!EthereumNetworkRepository.hasRealValue(chainId) || erc20Tokens.isEmpty())
{
Expand All @@ -345,7 +348,7 @@ public Single<Integer> syncERC20Tickers(long chainId, List<TokenCardMeta> erc20T
}
}

if (tokenCheckQueue.size() == 0)
if (tokenCheckQueue.isEmpty())
{
return Single.fromCallable(() -> 0);
}
Expand Down Expand Up @@ -425,14 +428,16 @@ private Map<String, TokenTicker> fetchERC20TokenTickers(List<TokenCardMeta> erc2
isFirst = false;
}

Request request = new Request.Builder()
Request.Builder buildRequest = new Request.Builder()
.url(COINGECKO_API.replace(CHAIN_IDS, apiChainName).replace(CONTRACT_ADDR, sb.toString()).replace(CURRENCY_TOKEN, currentCurrencySymbolTxt))
.get()
.build();
.get();

addAPIHeader(buildRequest);

try (okhttp3.Response response = httpClient.newCall(request)
try (okhttp3.Response response = httpClient.newCall(buildRequest.build())
.execute())
{
int code = response.code();
String responseStr = response.body().string();
List<CoinGeckoTicker> tickers = CoinGeckoTicker.buildTickerList(responseStr, currentCurrencySymbolTxt, currentConversionRate);
for (CoinGeckoTicker t : tickers)
Expand All @@ -448,22 +453,48 @@ private Map<String, TokenTicker> fetchERC20TokenTickers(List<TokenCardMeta> erc2
}
else
{
final Map<String, TokenTicker> blankTickers = new HashMap<>(); //These tokens have no ticker, don't check them again today
for (String address : lookupMap.keySet())
{
blankTickers.put(address, new TokenTicker(System.currentTimeMillis() + DateUtils.DAY_IN_MILLIS));
}
localSource.updateERC20Tickers(chainId, blankTickers);
addDelayCheckOnTickers(code, chainId, lookupMap);
}
}
catch (Exception e)
{
Timber.e(e);
addDelayCheckOnTickers(0, chainId, lookupMap);
}

return erc20Tickers;
}

private void addAPIHeader(Request.Builder buildRequest)
{
String coinGeckoKey = keyProvider.getCoinGeckoKey();
String backupKey = keyProvider.getBackupKey();

if (!TextUtils.isEmpty(coinGeckoKey))
{
if (keyCycle%3 == 0)
{
buildRequest.addHeader("x-cg-demo-api-key", coinGeckoKey);
}
else if (keyCycle%3 == 1)
{
buildRequest.addHeader("x-cg-demo-api-key", backupKey);
}
keyCycle++;
}
}

private void addDelayCheckOnTickers(int code, long chainId, Map<String, TokenCardMeta> lookupMap)
{
long delayTime = (code/100 == 2) ? DateUtils.DAY_IN_MILLIS * 5 : DateUtils.MINUTE_IN_MILLIS * 30; // if API call was successful, and no ticker was found, don't check for 5 days
final Map<String, TokenTicker> blankTickers = new HashMap<>(); //These tokens have no ticker, don't check them again for another hour
for (String address : lookupMap.keySet())
{
blankTickers.put(address, new TokenTicker(System.currentTimeMillis() + delayTime));
}
localSource.updateERC20Tickers(chainId, blankTickers);
}

private void addDexGuruTickers(Collection<TokenCardMeta> tokens)
{
for (TokenCardMeta tcm : tokens)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -149,4 +149,16 @@ public String getSmartPassDevKey()
{
return FAKE_KEY_FOR_TESTING;
}

@Override
public String getCoinGeckoKey()
{
return FAKE_KEY_FOR_TESTING;
}

@Override
public String getBackupKey()
{
return FAKE_KEY_FOR_TESTING;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -145,4 +145,16 @@ public String getSmartPassDevKey()
{
return null;
}

@Override
public String getCoinGeckoKey()
{
return null;
}

@Override
public String getBackupKey()
{
return null;
}
}

0 comments on commit 02f36bf

Please sign in to comment.