Updated on 2026-08-14

This commit is contained in:
Tangem 2020-02-14 10:16:35 +00:00
commit 7dbbd67849
6 changed files with 149 additions and 55 deletions

View file

@ -4,11 +4,16 @@ import org.stellar.sdk.KeyPair;
import org.stellar.sdk.Server;
import org.stellar.sdk.Transaction;
import org.stellar.sdk.requests.ErrorResponse;
import org.stellar.sdk.requests.RequestBuilder;
import org.stellar.sdk.responses.AccountResponse;
import org.stellar.sdk.responses.LedgerResponse;
import org.stellar.sdk.responses.Page;
import org.stellar.sdk.responses.SubmitTransactionResponse;
import org.stellar.sdk.responses.operations.OperationResponse;
import java.io.IOException;
import java.net.URISyntaxException;
import java.util.List;
/**
* Created by dvol on 7.01.2019.
@ -72,13 +77,37 @@ public class StellarRequest {
public static class Ledgers extends Base {
public LedgerResponse ledgerResponse;
public Ledgers() {};
public Ledgers() {
}
@Override
public void process(Server server) throws IOException {
int latestLedger = server.root().getCoreLatestLedger();
int latestLedger = server.root().getHistoryLatestLedger();
ledgerResponse = server.ledgers().ledger(latestLedger);
}
}
public static class Operations extends Base {
KeyPair accountKeyPair;
public List<OperationResponse> operationsList;
int limit = 200;
public Operations(String walletAddress) {
accountKeyPair = KeyPair.fromAccountId(walletAddress);
}
@Override
public void process(Server server) throws IOException {
Page<OperationResponse> operationsResponse = server.operations().forAccount(accountKeyPair).limit(limit).order(RequestBuilder.Order.DESC).execute();
operationsList = operationsResponse.getRecords();
while (operationsResponse.getRecords().size() == limit) {
try {
operationsResponse = operationsResponse.getNextPage(server.getHttpClient());
operationsList.addAll(operationsResponse.getRecords());
} catch (URISyntaxException e) {
break;
}
}
}
}
}

File diff suppressed because one or more lines are too long

View file

@ -30,7 +30,7 @@ public class XlmAssetData extends CoinData {
private Long sequenceNumber = 0L;
private CoinEngine.Amount baseReserve = new CoinEngine.Amount("0.5", "XLM");
private CoinEngine.Amount baseFee = new CoinEngine.Amount("0.00001", "XLM");
private boolean error404 = false;
private boolean error404, targetAccountCreated = false;
@Override
public void clearInfo() {
@ -38,6 +38,7 @@ public class XlmAssetData extends CoinData {
xlmBalance = null;
assetBalance = null;
error404 = false;
targetAccountCreated = false;
}
CoinEngine.Amount getXlmBalance() {
@ -101,6 +102,14 @@ public class XlmAssetData extends CoinData {
return true;
}
public boolean isTargetAccountCreated() {
return targetAccountCreated;
}
public void setTargetAccountCreated(boolean targetAccountCreated) {
this.targetAccountCreated = targetAccountCreated;
}
@Override
public void loadFromBundle(Bundle B) {
super.loadFromBundle(B);
@ -137,6 +146,10 @@ public class XlmAssetData extends CoinData {
if (B.containsKey("Error404")) error404 = B.getBoolean("Error404");
else error404 = false;
if (B.containsKey("TargetAccountCreated"))
targetAccountCreated = B.getBoolean("TargetAccountCreated");
else targetAccountCreated = false;
}
@Override
@ -169,6 +182,8 @@ public class XlmAssetData extends CoinData {
if (error404) B.putBoolean("Error404", true);
if (targetAccountCreated) B.putBoolean("TargetAccountCreated", true);
} catch (Exception e) {
Log.e("Can't save to bundle ", e.getMessage());
}

View file

@ -1,12 +1,10 @@
package com.tangem.wallet.xlm;
import android.net.Uri;
import android.os.StrictMode;
import android.text.InputFilter;
import android.util.Log;
import com.tangem.App;
import com.tangem.data.Blockchain;
import com.tangem.data.network.ServerApiStellar;
import com.tangem.data.network.StellarRequest;
import com.tangem.tangem_card.data.TangemCard;
@ -28,6 +26,7 @@ import org.stellar.sdk.Operation;
import org.stellar.sdk.PaymentOperation;
import org.stellar.sdk.Transaction;
import org.stellar.sdk.TransactionEx;
import org.stellar.sdk.responses.operations.OperationResponse;
import java.io.IOException;
import java.math.BigDecimal;
@ -90,7 +89,7 @@ public class XlmAssetEngine extends CoinEngine {
if (balance != null) {
if (!coinData.isAssetBalanceZero()) {
return " " + assetBalance.toDescriptionString(getDecimals()) + "<br><small><small>"+ balance.toDescriptionString(getDecimals()) + " for fee + " + coinData.getReserve().toDescriptionString(getDecimals()) + " reserve</small></small>";
return " " + assetBalance.toDescriptionString(getDecimals()) + "<br><small><small>" + balance.toDescriptionString(getDecimals()) + " for fee + " + coinData.getReserve().toDescriptionString(getDecimals()) + " reserve</small></small>";
}
return " " + balance.toDescriptionString(getDecimals()) + "<br><small><small>+ " + coinData.getReserve().toDescriptionString(getDecimals()) + " reserve</small></small>";
} else {
@ -171,7 +170,7 @@ public class XlmAssetEngine extends CoinEngine {
// if (ctx.getCard().getDenomination() != null) {
// return Uri.parse(ctx.getCoinData().getWallet() + "?amount=" + convertToAmount(convertToInternalAmount(ctx.getCard().getDenomination())).toValueString());
// } else {
return Uri.parse(ctx.getCoinData().getWallet());
return Uri.parse(ctx.getCoinData().getWallet());
// }
}
@ -186,7 +185,7 @@ public class XlmAssetEngine extends CoinEngine {
Amount balance;
if (!coinData.isAssetBalanceZero()) {
balance = coinData.getAssetBalance();
balance = coinData.getAssetBalance();
} else {
balance = coinData.getXlmBalance();
}
@ -377,9 +376,6 @@ public class XlmAssetEngine extends CoinEngine {
@Override
public SignTask.TransactionToSign constructTransaction(Amount amountValue, Amount feeValue, boolean IncFee, String targetAddress) throws Exception {
checkBlockchainDataExists();
StrictMode.ThreadPolicy policy = new StrictMode.ThreadPolicy.Builder().permitAll().build();
StrictMode.setThreadPolicy(policy);
if (coinData.isAssetBalanceZero() && IncFee) {
amountValue = new Amount(amountValue.subtract(feeValue), amountValue.getCurrency());
@ -392,13 +388,13 @@ public class XlmAssetEngine extends CoinEngine {
if (!coinData.isAssetBalanceZero()) {
operation = new PaymentOperation.Builder(KeyPair.fromAccountId(targetAddress), Asset.createNonNativeAsset(ctx.getCard().getTokenSymbol(), KeyPair.fromAccountId(ctx.getCard().getContractAddress())), amountValue.toValueString()).build();
} else {
if (isAccountCreated(targetAddress))
if (coinData.isTargetAccountCreated())
operation = new PaymentOperation.Builder(KeyPair.fromAccountId(targetAddress), new AssetTypeNative(), amountValue.toValueString()).build();
else
operation = new CreateAccountOperation.Builder(KeyPair.fromAccountId(targetAddress), amountValue.toValueString()).build();
}
}
TransactionEx transaction = TransactionEx.buildEx(60, coinData.getAccountResponse(), operation);
TransactionEx transaction = TransactionEx.buildEx(120, coinData.getAccountResponse(), operation);
if (transaction.getFee() != convertToInternalAmount(feeValue).intValueExact()) {
@ -447,23 +443,40 @@ public class XlmAssetEngine extends CoinEngine {
}
// network call inside, don't use on main thread
private boolean isAccountCreated(String address) {
private void checkTargetAccountCreated(BlockchainRequestsCallbacks blockchainRequestsCallbacks, String targetAddress, Amount amount) {
final ServerApiStellar serverApi = new ServerApiStellar(ctx.getBlockchain());
StellarRequest.Balance request = new StellarRequest.Balance(address);
ServerApiStellar.Listener listener = new ServerApiStellar.Listener() {
@Override
public void onSuccess(StellarRequest.Base request) {
coinData.setTargetAccountCreated(true);
blockchainRequestsCallbacks.onComplete(true);
}
try {
serverApi.doStellarRequest(ctx, request);
} catch (IOException e) {
Log.e(TAG, e.getMessage());
return true; // suppose account is created if anything goes wrong TODO:check
}
if (request.errorResponse != null && request.errorResponse.getCode() == 404)
return false;
else
return true;
@Override
public void onFail(StellarRequest.Base request) {
Log.i(TAG, "onFail: " + request.getClass().getSimpleName() + " " + request.getError());
if (request.errorResponse != null && request.errorResponse.getCode() == 404) {
coinData.setTargetAccountCreated(false);
if (amount.compareTo(coinData.getReserve()) >= 0) { //TODO: take fee inclusion in account, now 1 XLM with fee included will fail after transaction is sent
blockchainRequestsCallbacks.onComplete(true);
} else {
ctx.setError(R.string.confirm_transaction_error_not_enough_xlm_for_create);
blockchainRequestsCallbacks.onComplete(false);
}
} else { // suppose account is created if anything goes wrong
coinData.setTargetAccountCreated(true);
blockchainRequestsCallbacks.onComplete(true);
}
}
};
serverApi.setListener(listener);
serverApi.requestData(ctx, new StellarRequest.Balance(targetAddress));
}
@Override
@ -496,6 +509,21 @@ public class XlmAssetEngine extends CoinEngine {
} else {
blockchainRequestsCallbacks.onProgress();
}
} else if (request instanceof StellarRequest.Operations) {
StellarRequest.Operations operationsRequest = (StellarRequest.Operations) request;
for (OperationResponse operationResponse : operationsRequest.operationsList) {
if (operationResponse.getSourceAccount().getAccountId().equals(coinData.getWallet())) {
coinData.incSentTransactionsCount();
}
}
if (serverApi.isRequestsSequenceCompleted()) {
blockchainRequestsCallbacks.onComplete(!ctx.hasError());
} else {
blockchainRequestsCallbacks.onProgress();
}
} else {
ctx.setError("Invalid request logic");
blockchainRequestsCallbacks.onComplete(false);
@ -529,13 +557,14 @@ public class XlmAssetEngine extends CoinEngine {
serverApi.requestData(ctx, new StellarRequest.Balance(coinData.getWallet()));
serverApi.requestData(ctx, new StellarRequest.Ledgers());
serverApi.requestData(ctx, new StellarRequest.Operations(coinData.getWallet()));
}
@Override
public void requestFee(BlockchainRequestsCallbacks blockchainRequestsCallbacks, String targetAddress, Amount amount) throws Exception {
// TODO: get fee stats?
coinData.minFee = coinData.normalFee = coinData.maxFee = coinData.getBaseFee();
blockchainRequestsCallbacks.onComplete(true);
checkTargetAccountCreated(blockchainRequestsCallbacks, targetAddress, amount);
}
@Override

View file

@ -26,6 +26,7 @@ import org.stellar.sdk.Operation;
import org.stellar.sdk.PaymentOperation;
import org.stellar.sdk.Transaction;
import org.stellar.sdk.TransactionEx;
import org.stellar.sdk.responses.operations.OperationResponse;
import java.io.IOException;
import java.math.BigDecimal;
@ -97,7 +98,7 @@ public class XlmEngine extends CoinEngine {
@Override
public boolean hasBalanceInfo() {
if (coinData == null) return false;
return (coinData.getBalance() != null) || (coinData.isError404()) ;
return (coinData.getBalance() != null) || (coinData.isError404());
}
@ -365,7 +366,7 @@ public class XlmEngine extends CoinEngine {
else
operation = new CreateAccountOperation.Builder(KeyPair.fromAccountId(targetAddress), amountValue.toValueString()).build();
TransactionEx transaction = TransactionEx.buildEx(60, coinData.getAccountResponse(), operation);
TransactionEx transaction = TransactionEx.buildEx(120, coinData.getAccountResponse(), operation);
if (transaction.getFee() != convertToInternalAmount(feeValue).intValueExact()) {
@ -470,6 +471,7 @@ public class XlmEngine extends CoinEngine {
} else {
blockchainRequestsCallbacks.onProgress();
}
} else if (request instanceof StellarRequest.Ledgers) {
StellarRequest.Ledgers ledgersRequest = (StellarRequest.Ledgers) request;
@ -480,6 +482,22 @@ public class XlmEngine extends CoinEngine {
} else {
blockchainRequestsCallbacks.onProgress();
}
} else if (request instanceof StellarRequest.Operations) {
StellarRequest.Operations operationsRequest = (StellarRequest.Operations) request;
for (OperationResponse operationResponse : operationsRequest.operationsList) {
if (operationResponse.getSourceAccount().getAccountId().equals(coinData.getWallet())) {
coinData.incSentTransactionsCount();
}
}
if (serverApi.isRequestsSequenceCompleted()) {
blockchainRequestsCallbacks.onComplete(!ctx.hasError());
} else {
blockchainRequestsCallbacks.onProgress();
}
} else {
ctx.setError("Invalid request logic");
blockchainRequestsCallbacks.onComplete(false);
@ -513,6 +531,7 @@ public class XlmEngine extends CoinEngine {
serverApi.requestData(ctx, new StellarRequest.Balance(coinData.getWallet()));
serverApi.requestData(ctx, new StellarRequest.Ledgers());
serverApi.requestData(ctx, new StellarRequest.Operations(coinData.getWallet()));
}
@Override

View file

@ -61,7 +61,7 @@ class StellarTransactionBuilder(private val newtorkManager: StellarNetworkManage
accountID.accountID = sourceKeyPair.xdrPublicKey
val currentTime = Calendar.getInstance().timeInMillis / 1000
val minTime = 0L
val maxTime = currentTime + 60
val maxTime = currentTime + 120
transaction = Transaction.Builder(
Account(sourceKeyPair.accountId, sequence), newtorkManager.network)