Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Refactor Covalent Handling #2801

Merged
merged 4 commits into from
Sep 2, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions app/build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -309,6 +309,7 @@ dependencies {
testImplementation group: 'org.powermock', name: 'powermock-module-junit4-rule-agent', version: '2.0.9'
testImplementation group: 'org.powermock', name: 'powermock-module-junit4', version: '2.0.9'
testImplementation group: 'org.powermock', name: 'powermock-api-mockito2', version: '2.0.9'
testImplementation group: 'org.json', name: 'json', version: '20220320'

// Component tests
testImplementation 'org.robolectric:robolectric:4.8.2'
Expand Down
Original file line number Diff line number Diff line change
@@ -1,25 +1,17 @@
package com.alphawallet.app.entity;

import com.alphawallet.app.entity.tokenscript.EventUtils;
import com.alphawallet.app.util.Utils;
import com.alphawallet.token.tools.Numeric;
import android.text.TextUtils;

import org.web3j.protocol.Web3j;
import com.alphawallet.app.util.Utils;

import java.math.BigInteger;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Locale;
import java.util.Map;

import static com.alphawallet.app.repository.TokenRepository.getWeb3jService;

import android.text.TextUtils;

/**
* Created by JB on 17/05/2021.
*/
Expand Down Expand Up @@ -65,14 +57,10 @@ public Map<String, Param> getParams() throws Exception
Param param = new Param();
param.type = lp.type;
String rawValue = TextUtils.isEmpty(lp.value) || lp.value.equals("null") ? rawLogValue : lp.value;
param.value = rawValue;
if (lp.type.startsWith("uint") || lp.type.startsWith("int"))
{
param.valueBI = rawValue.startsWith("0x") ? Numeric.toBigInt(rawValue) : new BigInteger(rawValue);
param.value = "";
}
else
{
param.value = rawValue;
param.valueBI = Utils.stringToBigInteger(rawValue);// rawValue.startsWith("0x") ? Numeric.toBigInt(rawValue) : new BigInteger(rawValue);
}

params.put(lp.name, param);
Expand Down Expand Up @@ -153,21 +141,10 @@ private EtherscanEvent getEtherscanTransferEvent(LogEvent logEvent) throws Excep

Map<String, Param> logParams = logEvent.getParams();

ev.from = logParams.get("from").value;
ev.to = logParams.get("to").value;

logParams.remove("from");
logParams.remove("to");

if (logEvent.sender_contract_decimals == 0)
{
//get TokenId
ev.tokenID = logParams.values().iterator().next().valueBI.toString();
}
else
{
ev.value = logParams.values().iterator().next().valueBI.toString();
}
ev.from = logParams.containsKey("from") ? logParams.get("from").value : "";
ev.to = logParams.containsKey("to") ? logParams.get("to").value : "";
ev.tokenID = logParams.containsKey("tokenId") ? logParams.get("tokenId").valueBI.toString() : "";
ev.value = logParams.containsKey("value") ? logParams.get("value").valueBI.toString() : "";

ev.gasUsed = gas_spent;
ev.gasPrice = gas_price;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -167,7 +167,7 @@ public Single<Transaction[]> storeNewTransactions(TokensService svs, NetworkInfo
{
lastTransaction = syncDownwards(updates, instance, svs, networkInfo, tokenAddress, 9999999999L);
}
else // try to sydenc upwards from the last read
else // try to sync upwards from the last read
{
lastTransaction = syncUpwards(updates, instance, svs, networkInfo, tokenAddress, lastBlockNumber);
}
Expand Down Expand Up @@ -699,7 +699,6 @@ else if (networkInfo.chainId == BINANCE_TEST_ID || networkInfo.chainId == BINANC
}
else if (networkInfo.chainId == POLYGON_ID || networkInfo.chainId == POLYGON_TEST_ID)
{

return POLYGONSCAN_API_KEY;
}
else if (networkInfo.chainId == AURORA_MAINNET_ID || networkInfo.chainId == AURORA_TESTNET_ID)
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,76 @@
package com.alphawallet.app;

import static com.alphawallet.ethereum.EthereumNetworkBase.KLAYTN_ID;
import static org.junit.Assert.assertEquals;

import com.alphawallet.app.entity.CovalentTransaction;
import com.alphawallet.app.entity.EtherscanEvent;
import com.alphawallet.app.entity.EtherscanTransaction;
import com.alphawallet.app.entity.NetworkInfo;
import com.google.common.io.Resources;
import com.google.gson.Gson;

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

import java.io.IOException;
import java.net.URL;
import java.nio.charset.StandardCharsets;

/**
* Created by JB on 2/09/2022.
*/
public class CovalentProcessingTest
{
private String APIReturn;

public CovalentProcessingTest() throws IOException
{
URL url = Resources.getResource("covalenttxs.json");
APIReturn = Resources.toString(url, StandardCharsets.UTF_8);
}

@Test
public void testCovalentTx() throws JSONException
{
CovalentTransaction[] covalentTransactions = getCovalentTransactions(APIReturn);

NetworkInfo info = new NetworkInfo("Klaytn", "Klaytn", "", "", KLAYTN_ID, "", "");
EtherscanEvent[] events = CovalentTransaction.toEtherscanEvents(covalentTransactions);
EtherscanTransaction[] txs = CovalentTransaction.toRawEtherscanTransactions(covalentTransactions, info);

assertEquals(events.length, 517);
assertEquals(txs.length, 139);

EtherscanEvent ev = events[0];
assertEquals(ev.value, "2700000000");
assertEquals(ev.tokenDecimal, "6");

ev = events[516];

assertEquals(ev.value, "");
assertEquals(ev.tokenID, "20007");
assertEquals(ev.tokenDecimal, "0");
assertEquals(ev.from, "0xc067a53c91258ba513059919e03b81cf93f57ac7");
assertEquals(ev.to, "0xf9c883c8dca140ebbdc87a225fe6e330be5d25ef");
}

private CovalentTransaction[] getCovalentTransactions(String response) throws JSONException
{
if (response == null || response.length() < 80)
{
return new CovalentTransaction[0];
}
JSONObject stateData = new JSONObject(response);

JSONObject data = stateData.getJSONObject("data");
JSONArray orders = data.getJSONArray("items");
CovalentTransaction[] ctxs = new Gson().fromJson(orders.toString(), CovalentTransaction[].class);

return ctxs;
}
}


1 change: 1 addition & 0 deletions app/src/test/resources/covalenttxs.json

Large diffs are not rendered by default.