Search in sources :

Example 1 with RunIotaAPI

use of jota.RunIotaAPI in project run-wallet-android by runplay.

the class AutoNudgeHandler method doNudge.

public static ApiResponse doNudge(RunIotaAPI apiProxy, Context context, ApiRequest inrequest) {
    ApiResponse response = new ApiResponse();
    Store.init(context, false);
    Store.loadNudgeTransfers(context);
    List<NudgeTransfer> nudgeTransfers = Store.getNudgeTransfers();
    SharedPreferences prefs = PreferenceManager.getDefaultSharedPreferences(context);
    int nudgeAttempts = Sf.toInt(prefs.getString(Constants.PREF_TRANSFER_NUDGE_ATTEMPTS, "" + Constants.PREF_TRANSFER_NUDGE_ATTEMPTS_VALUE));
    if (nudgeAttempts > 0) {
        Nodes.Node node = Store.getNode();
        if (node != null) {
            RunIotaAPI api = new RunIotaAPI.Builder().protocol(node.protocol).host(node.ip).port(((Integer) node.port).toString()).build();
            jota.dto.response.GetNodeInfoResponse nir = null;
            try {
                nir = api.getNodeInfo();
            } catch (Exception e) {
                node = Store.getNode();
                try {
                    nir = api.getNodeInfo();
                } catch (Exception e2) {
                }
            }
            if (nir != null) {
                if (nir.getLatestMilestoneIndex() == nir.getLatestSolidSubtangleMilestoneIndex()) {
                    Map<String, NudgeTransfer> refreshSeedShorts = new HashMap<>();
                    List<NudgeTransfer> removeFromNudges = new ArrayList<>();
                    int len = nudgeTransfers.size();
                    // strictly this way incase one gets added from another method
                    for (int i = 0; i < len; i++) {
                        NudgeTransfer ntran = nudgeTransfers.get(i);
                        if (ntran.transfer.getMilestone() < nir.getLatestMilestoneIndex() && ntran.transfer.getNudgeCount() < nudgeAttempts) {
                            try {
                                Seeds.Seed seed = null;
                                for (Seeds.Seed tseed : Store.getSeedList()) {
                                    if (tseed.getSystemShortValue().equals(ntran.seedShort)) {
                                        seed = tseed;
                                    }
                                }
                                if (seed != null) {
                                    boolean hascompleted = false;
                                    List<Transfer> allTransfers = Store.getTransfers(context, seed);
                                    List<String> hashes = new ArrayList<>();
                                    for (Transfer transfer : allTransfers) {
                                        if (transfer.getValue() == ntran.transfer.getValue() && transfer.getAddress().equals(ntran.transfer.getAddress())) {
                                            if (transfer.isCompleted()) {
                                                hascompleted = true;
                                            }
                                            hashes.add(transfer.getHash());
                                        }
                                    }
                                    List<jota.model.Transaction> transactions = new ArrayList<>();
                                    if (!hascompleted) {
                                        transactions = api.findTransactionsObjectsByHashes(hashes.toArray(new String[hashes.size()]));
                                        if (!transactions.isEmpty()) {
                                            for (jota.model.Transaction transaction : transactions) {
                                                if (transaction.getPersistence() != null && transaction.getPersistence().booleanValue()) {
                                                    hascompleted = true;
                                                }
                                            }
                                        }
                                    }
                                    if (hascompleted) {
                                        refreshSeedShorts.put(ntran.seedShort, ntran);
                                    } else if (hascompleted || hashes.isEmpty() || transactions.isEmpty()) {
                                    // do nothing
                                    } else {
                                        ApiResponse resp = NudgeRequestHandler.doNudge(api, context, new NudgeRequest(seed, ntran.transfer));
                                        if (resp instanceof NudgeResponse) {
                                            NudgeResponse nresponse = (NudgeResponse) resp;
                                            if (nresponse.getSuccessfully()) {
                                                refreshSeedShorts.put(ntran.seedShort, ntran);
                                            }
                                        }
                                    }
                                }
                            } catch (Exception e) {
                            // removeFromNudges.add(ntran);
                            }
                        }
                        removeFromNudges.add(ntran);
                    }
                    if (!refreshSeedShorts.isEmpty()) {
                        List<Seeds.Seed> seeds = Store.getSeedList();
                        for (String seedShort : refreshSeedShorts.keySet()) {
                            for (Seeds.Seed seed : seeds) {
                                if (String.valueOf(Store.getSeedRaw(context, seed)).startsWith(seedShort)) {
                                    AppService.getAccountData(context, seed);
                                    break;
                                }
                            }
                        }
                        Store.saveNudgeTransfers(context);
                    }
                    if (!removeFromNudges.isEmpty()) {
                        Store.removeNudgeTransfer(context, removeFromNudges);
                    }
                }
            }
        }
    } else {
        Store.removeNudgeTransfer(context, nudgeTransfers);
    }
    return response;
}
Also used : Seeds(run.wallet.iota.model.Seeds) HashMap(java.util.HashMap) ArrayList(java.util.ArrayList) NudgeResponse(run.wallet.iota.api.responses.NudgeResponse) ApiResponse(run.wallet.iota.api.responses.ApiResponse) SharedPreferences(android.content.SharedPreferences) NudgeRequest(run.wallet.iota.api.requests.NudgeRequest) AutoNudgeRequest(run.wallet.iota.api.requests.AutoNudgeRequest) Nodes(run.wallet.iota.model.Nodes) RunIotaAPI(jota.RunIotaAPI) NudgeTransfer(run.wallet.iota.model.NudgeTransfer) Transfer(run.wallet.iota.model.Transfer) NudgeTransfer(run.wallet.iota.model.NudgeTransfer)

Aggregations

SharedPreferences (android.content.SharedPreferences)1 ArrayList (java.util.ArrayList)1 HashMap (java.util.HashMap)1 RunIotaAPI (jota.RunIotaAPI)1 AutoNudgeRequest (run.wallet.iota.api.requests.AutoNudgeRequest)1 NudgeRequest (run.wallet.iota.api.requests.NudgeRequest)1 ApiResponse (run.wallet.iota.api.responses.ApiResponse)1 NudgeResponse (run.wallet.iota.api.responses.NudgeResponse)1 Nodes (run.wallet.iota.model.Nodes)1 NudgeTransfer (run.wallet.iota.model.NudgeTransfer)1 Seeds (run.wallet.iota.model.Seeds)1 Transfer (run.wallet.iota.model.Transfer)1