From d0df53d8e24f9609246e290763da6594fcbf718f Mon Sep 17 00:00:00 2001 From: hvarona Date: Sun, 17 Jun 2018 09:47:32 -0400 Subject: [PATCH] Bitshares manager use the new bitshares account cache --- .../dao/BitsharesAccountNameCacheDao.java | 3 + .../manager/BitsharesAccountManager.java | 102 +++++++++++------- 2 files changed, 69 insertions(+), 36 deletions(-) diff --git a/app/src/main/java/cy/agorise/crystalwallet/dao/BitsharesAccountNameCacheDao.java b/app/src/main/java/cy/agorise/crystalwallet/dao/BitsharesAccountNameCacheDao.java index 5d6e413..3389db1 100644 --- a/app/src/main/java/cy/agorise/crystalwallet/dao/BitsharesAccountNameCacheDao.java +++ b/app/src/main/java/cy/agorise/crystalwallet/dao/BitsharesAccountNameCacheDao.java @@ -24,6 +24,9 @@ public interface BitsharesAccountNameCacheDao { @Query("SELECT * FROM bitshares_account_name_cache WHERE account_id = :account_id") BitsharesAccountNameCache getByAccountId(String account_id); + @Query("SELECT * FROM bitshares_account_name_cache WHERE account_name = :name") + BitsharesAccountNameCache getByAccountName(String account_name); + @Insert(onConflict = OnConflictStrategy.REPLACE) public long[] insertBitsharesAccountNameCache(BitsharesAccountNameCache... accountsNames); 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 7b4c6a8..46b89d3 100644 --- a/app/src/main/java/cy/agorise/crystalwallet/manager/BitsharesAccountManager.java +++ b/app/src/main/java/cy/agorise/crystalwallet/manager/BitsharesAccountManager.java @@ -1,6 +1,5 @@ package cy.agorise.crystalwallet.manager; -import android.accounts.Account; import android.annotation.SuppressLint; import android.content.Context; @@ -18,9 +17,9 @@ import cy.agorise.crystalwallet.apigenerator.ApiRequest; import cy.agorise.crystalwallet.apigenerator.ApiRequestListener; import cy.agorise.crystalwallet.apigenerator.BitsharesFaucetApiGenerator; import cy.agorise.crystalwallet.apigenerator.GrapheneApiGenerator; -import cy.agorise.crystalwallet.apigenerator.grapheneoperation.AccountUpgradeOperation; import cy.agorise.crystalwallet.apigenerator.grapheneoperation.AccountUpgradeOperationBuilder; import cy.agorise.crystalwallet.application.constant.BitsharesConstant; +import cy.agorise.crystalwallet.models.BitsharesAccountNameCache; import cy.agorise.crystalwallet.models.seed.BIP39; import cy.agorise.crystalwallet.requestmanagers.CryptoNetEquivalentRequest; import cy.agorise.crystalwallet.requestmanagers.CryptoNetInfoRequest; @@ -389,56 +388,87 @@ public class BitsharesAccountManager implements CryptoAccountManager, CryptoNetI } } + /** + * Broadcast a send asset request, the idAsset is already fetched + * @param sendRequest The petition for transfer + * @param idAsset The Bitshares Asset's id + */ private void validateSendRequest(final ValidateBitsharesSendRequest sendRequest , final String idAsset){ final Asset feeAsset = new Asset(idAsset); final UserAccount fromUserAccount =new UserAccount(sendRequest.getSourceAccount().getAccountId()); - //TODO cached to accounts - this.getAccountInfoByName(sendRequest.getToAccount(), new ManagerRequest() { + final CrystalDatabase db = CrystalDatabase.getAppDatabase(sendRequest.getContext()); + BitsharesAccountNameCache cacheAccount = db.bitsharesAccountNameCacheDao().getByAccountName(sendRequest.getToAccount()); + if(cacheAccount == null) { + this.getAccountInfoByName(sendRequest.getToAccount(), new ManagerRequest() { - @Override - public void success(Object answer) { - GrapheneAccount toUserGrapheneAccount = (GrapheneAccount) answer; - UserAccount toUserAccount = new UserAccount(toUserGrapheneAccount.getAccountId()); - TransferOperationBuilder builder = new TransferOperationBuilder() - .setSource(fromUserAccount) - .setDestination(toUserAccount) - .setTransferAmount(new AssetAmount(UnsignedLong.valueOf(sendRequest.getAmount()), new Asset(idAsset))) - .setFee(new AssetAmount(UnsignedLong.valueOf(0), feeAsset)); - if(sendRequest.getMemo() != null) { - //builder.setMemo(new Memo(fromUserAccount,toUserAccount,0,sendRequest.getMemo().getBytes())); - //TODO memo - System.out.println("transaction has memo"); + @Override + public void success(Object answer) { + GrapheneAccount toUserGrapheneAccount = (GrapheneAccount) answer; + UserAccount toUserAccount = new UserAccount(toUserGrapheneAccount.getAccountId()); + try { + BitsharesAccountNameCache cacheAccount = new BitsharesAccountNameCache(); + cacheAccount.setName(sendRequest.getToAccount()); + cacheAccount.setAccountId(toUserAccount.getObjectId()); + db.bitsharesAccountNameCacheDao().insertBitsharesAccountNameCache(cacheAccount); + }catch(Exception e){ + e.printStackTrace(); + } + validateSendRequest(sendRequest,fromUserAccount,toUserAccount,feeAsset,idAsset); } - ArrayList operationList = new ArrayList<>(); - operationList.add(builder.build()); - ECKey privateKey = sendRequest.getSourceAccount().getActiveKey(sendRequest.getContext()); + @Override + public void fail() { + sendRequest.setStatus(ValidateBitsharesSendRequest.StatusCode.NO_TO_USER_INFO); + } + }); + }else { + UserAccount toUserAccount = new UserAccount(cacheAccount.getAccountId()); + this.validateSendRequest(sendRequest,fromUserAccount,toUserAccount,feeAsset,idAsset); + } + } - Transaction transaction = new Transaction(privateKey, null, operationList); - transaction.setChainId(CryptoNetManager.getChaindId(CryptoNet.BITSHARES)); + /** + * Broadcast a transaction request + * @param sendRequest The petition for transfer operation + * @param fromUserAccount The source account + * @param toUserAccount The receiver account + * @param feeAsset The Fee Asset + * @param idAsset The id of the asset to be used on the operation + */ + private void validateSendRequest(final ValidateBitsharesSendRequest sendRequest, UserAccount fromUserAccount, + UserAccount toUserAccount, Asset feeAsset, String idAsset){ + TransferOperationBuilder builder = new TransferOperationBuilder() + .setSource(fromUserAccount) + .setDestination(toUserAccount) + .setTransferAmount(new AssetAmount(UnsignedLong.valueOf(sendRequest.getAmount()), new Asset(idAsset))) + .setFee(new AssetAmount(UnsignedLong.valueOf(0), feeAsset)); + if (sendRequest.getMemo() != null) { + //builder.setMemo(new Memo(fromUserAccount,toUserAccount,0,sendRequest.getMemo().getBytes())); + //TODO memo + System.out.println("transaction has memo"); + } + ArrayList operationList = new ArrayList<>(); + operationList.add(builder.build()); - ApiRequest transactionRequest = new ApiRequest(0, new ApiRequestListener() { - @Override - public void success(Object answer, int idPetition) { - sendRequest.setStatus(ValidateBitsharesSendRequest.StatusCode.SUCCEEDED); - } + ECKey privateKey = sendRequest.getSourceAccount().getActiveKey(sendRequest.getContext()); - @Override - public void fail(int idPetition) { - sendRequest.setStatus(ValidateBitsharesSendRequest.StatusCode.PETITION_FAILED); - } - }); + Transaction transaction = new Transaction(privateKey, null, operationList); + transaction.setChainId(CryptoNetManager.getChaindId(CryptoNet.BITSHARES)); - GrapheneApiGenerator.broadcastTransaction(transaction,feeAsset, transactionRequest); + ApiRequest transactionRequest = new ApiRequest(0, new ApiRequestListener() { + @Override + public void success(Object answer, int idPetition) { + sendRequest.setStatus(ValidateBitsharesSendRequest.StatusCode.SUCCEEDED); } @Override - public void fail() { - sendRequest.setStatus(ValidateBitsharesSendRequest.StatusCode.NO_TO_USER_INFO); + public void fail(int idPetition) { + sendRequest.setStatus(ValidateBitsharesSendRequest.StatusCode.PETITION_FAILED); } }); + GrapheneApiGenerator.broadcastTransaction(transaction, feeAsset, transactionRequest); } /** @@ -667,7 +697,7 @@ public class BitsharesAccountManager implements CryptoAccountManager, CryptoNetI GrapheneApiGenerator.getEquivalentValue(fromAsset.getBitsharesId(),toAsset.getBitsharesId(), getEquivalentRequest); }else{ //TODO error - System.out.println("Equivalen Value error "); + System.out.println("Equivalent Value error "); } }