From a0797edf6e8f26c2e534e5592f78499c71ce04b5 Mon Sep 17 00:00:00 2001 From: henry Date: Wed, 25 Oct 2017 22:46:42 -0400 Subject: [PATCH] save transactions in db Validate send request only one asset and amount --- .../apigenerator/GrapheneApiGenerator.java | 2 +- .../ValidateBitsharesSendRequest.java | 37 ++++++------------- .../manager/BitsharesAccountManager.java | 35 ++++++++++-------- .../models/BitsharesAssetInfo.java | 1 + .../models/CryptoCoinTransaction.java | 2 + 5 files changed, 35 insertions(+), 42 deletions(-) diff --git a/app/src/main/java/cy/agorise/crystalwallet/apigenerator/GrapheneApiGenerator.java b/app/src/main/java/cy/agorise/crystalwallet/apigenerator/GrapheneApiGenerator.java index 1706e93..399fca9 100644 --- a/app/src/main/java/cy/agorise/crystalwallet/apigenerator/GrapheneApiGenerator.java +++ b/app/src/main/java/cy/agorise/crystalwallet/apigenerator/GrapheneApiGenerator.java @@ -136,7 +136,7 @@ public abstract class GrapheneApiGenerator { * @param request The Api request object, to answer this petition */ public static void getAccountTransaction(String accountGrapheneId, int start, int stop, int limit, final ApiRequest request){ - WebSocketThread thread = new WebSocketThread(new GetRelativeAccountHistory(new UserAccount(accountGrapheneId), stop, limit, start, new WitnessResponseListener() { + WebSocketThread thread = new WebSocketThread(new GetRelativeAccountHistory(new UserAccount(accountGrapheneId), start, limit, stop, new WitnessResponseListener() { @Override public void onSuccess(WitnessResponse response) { WitnessResponse> resp = response; diff --git a/app/src/main/java/cy/agorise/crystalwallet/cryptonetinforequests/ValidateBitsharesSendRequest.java b/app/src/main/java/cy/agorise/crystalwallet/cryptonetinforequests/ValidateBitsharesSendRequest.java index 3b6c136..b1f0336 100644 --- a/app/src/main/java/cy/agorise/crystalwallet/cryptonetinforequests/ValidateBitsharesSendRequest.java +++ b/app/src/main/java/cy/agorise/crystalwallet/cryptonetinforequests/ValidateBitsharesSendRequest.java @@ -19,36 +19,28 @@ public class ValidateBitsharesSendRequest extends CryptoNetInfoRequest { // The destination account id private String mToAccount; // The amount of the transaction - private long mBaseAmount; + private long mAmount; // The asset id of the transaction - private String mBaseAsset; - // The fee amount - private long mFeeAmount; - // The fee asset id - private String mFeeAsset; + private String mAsset; // The memo, can be null private String mMemo; // The state of this request private Boolean isSend; public ValidateBitsharesSendRequest(Context context, GrapheneAccount sourceAccount, - String toAccount, long baseAmount, String baseAsset, - long feeAmount,String feeAsset, String memo) { + String toAccount, long amount, String asset, String memo) { super(CryptoCoin.BITSHARES); this.mContext = context; this.mSourceAccount = sourceAccount; this.mToAccount = toAccount; - this.mBaseAmount = baseAmount; - this.mBaseAsset = baseAsset; - this.mFeeAmount = feeAmount; - this.mFeeAsset = feeAsset; + this.mAmount = amount; + this.mAsset = asset; this.mMemo = memo; } public ValidateBitsharesSendRequest(Context context, GrapheneAccount sourceAccount, - String toAccount, long baseAmount, String baseAsset, - long feeAmount,String feeAsset) { - this(context, sourceAccount,toAccount,baseAmount,baseAsset,feeAmount,feeAsset,null); + String toAccount, long amount, String asset) { + this(context, sourceAccount,toAccount,amount,asset,null); } public Context getContext() { @@ -63,21 +55,14 @@ public class ValidateBitsharesSendRequest extends CryptoNetInfoRequest { return mToAccount; } - public long getBaseAmount() { - return mBaseAmount; + public long getAmount() { + return mAmount; } - public String getBaseAsset() { - return mBaseAsset; + public String getAsset() { + return mAsset; } - public long getFeeAmount() { - return mFeeAmount; - } - - public String getFeeAsset() { - return mFeeAsset; - } public String getMemo() { return mMemo; diff --git a/app/src/main/java/cy/agorise/crystalwallet/manager/BitsharesAccountManager.java b/app/src/main/java/cy/agorise/crystalwallet/manager/BitsharesAccountManager.java index 37498da..74f2559 100644 --- a/app/src/main/java/cy/agorise/crystalwallet/manager/BitsharesAccountManager.java +++ b/app/src/main/java/cy/agorise/crystalwallet/manager/BitsharesAccountManager.java @@ -223,14 +223,14 @@ public class BitsharesAccountManager implements CryptoAccountManager, CryptoNetI } private void validateSendRequest(final ValidateBitsharesSendRequest sendRequest){ - Asset feeAsset = new Asset(sendRequest.getFeeAsset()); + Asset feeAsset = new Asset(sendRequest.getAsset()); UserAccount fromUserAccount =new UserAccount(sendRequest.getSourceAccount().getAccountId()); UserAccount toUserAccount = new UserAccount(sendRequest.getToAccount()); TransferOperationBuilder builder = new TransferOperationBuilder() .setSource(fromUserAccount) .setDestination(toUserAccount) - .setTransferAmount(new AssetAmount(UnsignedLong.valueOf(sendRequest.getBaseAmount()), new Asset(sendRequest.getBaseAsset()))) - .setFee(new AssetAmount(UnsignedLong.valueOf(sendRequest.getFeeAmount()), feeAsset)); + .setTransferAmount(new AssetAmount(UnsignedLong.valueOf(sendRequest.getAmount()), new Asset(sendRequest.getAsset()))) + .setFee(new AssetAmount(UnsignedLong.valueOf(0), feeAsset)); if(sendRequest.getMemo() != null) { //builder.setMemo(new Memo(fromUserAccount,toUserAccount,0,sendRequest.getMemo().getBytes())); //TODO memo @@ -315,7 +315,7 @@ public class BitsharesAccountManager implements CryptoAccountManager, CryptoNetI int stop = start + limit; ApiRequest transactionRequest = new ApiRequest(0, new TransactionRequestListener(start, stop, limit, grapheneAccount, db)); - GrapheneApiGenerator.getAccountTransaction(grapheneAccount.getName(), start, stop, limit, transactionRequest); + GrapheneApiGenerator.getAccountTransaction(grapheneAccount.getAccountId(), start, stop, limit, transactionRequest); } } @@ -338,12 +338,13 @@ public class BitsharesAccountManager implements CryptoAccountManager, CryptoNetI @Override public void success(Object answer, int idPetition) { List transfers = (List) answer ; - for(HistoricalTransfer transfer : transfers){ - CryptoCoinTransaction transaction = new CryptoCoinTransaction(); + for(HistoricalTransfer transfer : transfers) { + if (transfer.getOperation() != null){ + CryptoCoinTransaction transaction = new CryptoCoinTransaction(); transaction.setAccountId(account.getId()); transaction.setAmount(transfer.getOperation().getAssetAmount().getAmount().longValue()); CryptoCurrency currency = db.cryptoCurrencyDao().getByName(transfer.getOperation().getAssetAmount().getAsset().getSymbol()); - if(currency == null){ + if (currency == null) { System.out.println("CryptoCurrency not in database"); //CryptoCurrency not in database Asset asset = transfer.getOperation().getAssetAmount().getAsset(); @@ -357,24 +358,28 @@ public class BitsharesAccountManager implements CryptoAccountManager, CryptoNetI }else if(asset.getAssetType() == Asset.AssetType.UIA){ assetType = BitsharesAsset.Type.UIA; }*/ - currency = new BitsharesAsset(asset.getSymbol(),asset.getPrecision(),asset.getObjectId(), assetType); - db.cryptoCurrencyDao().insertCryptoCurrency(currency); - db.bitsharesAssetDao().insertBitsharesAssetInfo(new BitsharesAssetInfo((BitsharesAsset)currency)); + currency = new BitsharesAsset(asset.getSymbol(), asset.getPrecision(), asset.getObjectId(), assetType); + long idCryptoCurrency = db.cryptoCurrencyDao().insertCryptoCurrency(currency)[0]; + BitsharesAssetInfo info = new BitsharesAssetInfo((BitsharesAsset) currency); + info.setCryptoCurrencyId(idCryptoCurrency); + currency.setId((int)idCryptoCurrency); + db.bitsharesAssetDao().insertBitsharesAssetInfo(info); } transaction.setIdCurrency(currency.getId()); transaction.setConfirmed(true); //graphene transaction are always confirmed - transaction.setFrom(transfer.getOperation().getFrom().getName()); - transaction.setInput(!transfer.getOperation().getFrom().getName().equals(account.getName())); - transaction.setTo(transfer.getOperation().getTo().getName()); + transaction.setFrom(transfer.getOperation().getFrom().getObjectId()); + transaction.setInput(!transfer.getOperation().getFrom().getObjectId().equals(account.getAccountId())); + transaction.setTo(transfer.getOperation().getTo().getObjectId()); - GrapheneApiGenerator.getBlockHeaderTime(transfer.getBlockNum(),new ApiRequest(0,new GetTransactionDate(transaction,db.transactionDao()))); + GrapheneApiGenerator.getBlockHeaderTime(transfer.getBlockNum(), new ApiRequest(0, new GetTransactionDate(transaction, db.transactionDao()))); + } } if(transfers.size()>= limit){ int newStart= start + limit; int newStop= stop + limit; ApiRequest transactionRequest = new ApiRequest(newStart/limit, new TransactionRequestListener(newStart,newStop,limit,account,db)); - GrapheneApiGenerator.getAccountTransaction(account.getName(),newStart,newStop,limit,transactionRequest); + GrapheneApiGenerator.getAccountTransaction(account.getAccountId(),newStart,newStop,limit,transactionRequest); } } diff --git a/app/src/main/java/cy/agorise/crystalwallet/models/BitsharesAssetInfo.java b/app/src/main/java/cy/agorise/crystalwallet/models/BitsharesAssetInfo.java index 2acc933..1318ef2 100644 --- a/app/src/main/java/cy/agorise/crystalwallet/models/BitsharesAssetInfo.java +++ b/app/src/main/java/cy/agorise/crystalwallet/models/BitsharesAssetInfo.java @@ -37,6 +37,7 @@ public class BitsharesAssetInfo { } public BitsharesAssetInfo(BitsharesAsset asset){ + this.cryptoCurrencyId = asset.getId(); this.bitsharesId = asset.getBitsharesId(); this.assetType = asset.getAssetType(); } diff --git a/app/src/main/java/cy/agorise/crystalwallet/models/CryptoCoinTransaction.java b/app/src/main/java/cy/agorise/crystalwallet/models/CryptoCoinTransaction.java index e6dff18..e62f455 100644 --- a/app/src/main/java/cy/agorise/crystalwallet/models/CryptoCoinTransaction.java +++ b/app/src/main/java/cy/agorise/crystalwallet/models/CryptoCoinTransaction.java @@ -3,6 +3,7 @@ package cy.agorise.crystalwallet.models; import android.arch.persistence.room.ColumnInfo; import android.arch.persistence.room.Entity; import android.arch.persistence.room.ForeignKey; +import android.arch.persistence.room.Ignore; import android.arch.persistence.room.PrimaryKey; import android.support.annotation.NonNull; import android.support.v7.recyclerview.extensions.DiffCallback; @@ -32,6 +33,7 @@ public class CryptoCoinTransaction { /** * The account associated with this transaction */ + @Ignore protected CryptoNetAccount account; /**