From 159743b54e00a4d8eef7a27687fce754b56aaae1 Mon Sep 17 00:00:00 2001 From: henry Date: Sun, 21 Jan 2018 23:55:40 -0400 Subject: [PATCH] Added retrieve of asset id, for send transaction Added retrieve of to Account id for send transaction --- .../manager/BitsharesAccountManager.java | 74 ++++++++++++++++++- 1 file changed, 71 insertions(+), 3 deletions(-) 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 f374de8..7390cdc 100644 --- a/app/src/main/java/cy/agorise/crystalwallet/manager/BitsharesAccountManager.java +++ b/app/src/main/java/cy/agorise/crystalwallet/manager/BitsharesAccountManager.java @@ -268,13 +268,18 @@ public class BitsharesAccountManager implements CryptoAccountManager, CryptoNetI * Broadcast a transaction request */ private void validateSendRequest(final ValidateBitsharesSendRequest sendRequest){ - Asset feeAsset = new Asset(sendRequest.getAsset()); + //TODO feeAsset + String idAsset = getAssetInfoByName(sendRequest.getAsset()); + Asset feeAsset = new Asset(idAsset); UserAccount fromUserAccount =new UserAccount(sendRequest.getSourceAccount().getAccountId()); - UserAccount toUserAccount = new UserAccount(sendRequest.getToAccount()); + + GrapheneAccount toUserGrapheneAccount = this.getAccountInfoByName(sendRequest.getToAccount()); + //TODO bad user to user account + UserAccount toUserAccount = new UserAccount(toUserGrapheneAccount.getAccountId()); TransferOperationBuilder builder = new TransferOperationBuilder() .setSource(fromUserAccount) .setDestination(toUserAccount) - .setTransferAmount(new AssetAmount(UnsignedLong.valueOf(sendRequest.getAmount()), new Asset(sendRequest.getAsset()))) + .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())); @@ -284,6 +289,7 @@ public class BitsharesAccountManager implements CryptoAccountManager, CryptoNetI operationList.add(builder.build()); ECKey privateKey = sendRequest.getSourceAccount().getActiveKey(sendRequest.getContext()); + Transaction transaction = new Transaction(privateKey, null, operationList); ApiRequest transactionRequest = new ApiRequest(0, new ApiRequestListener() { @@ -353,6 +359,31 @@ public class BitsharesAccountManager implements CryptoAccountManager, CryptoNetI return listener.account; } + //TODO expand function to be more generic + private String getAssetInfoByName(String assetName){ + final Object SYNC = new Object(); + long timeout = 60000; + + AssetIdOrNameListener nameListener = new AssetIdOrNameListener(SYNC); + ApiRequest request = new ApiRequest(0, nameListener); + ArrayList assetNames = new ArrayList<>(); + assetNames.add(assetName); + GrapheneApiGenerator.getAssetByName(assetNames, request); + + long cTime = System.currentTimeMillis(); + + while(!nameListener.ready && (System.currentTimeMillis()-cTime) < timeout){ + synchronized (SYNC){ + try { + SYNC.wait(100); + } catch (InterruptedException ignore) {} + } + } + + return nameListener.asset.getBitsharesId(); + + } + /** * Refresh the transactions of an account, important to notice, it return nothing, to get the changes tuse the LiveData * @param idAccount database id of the account @@ -553,6 +584,43 @@ public class BitsharesAccountManager implements CryptoAccountManager, CryptoNetI } } + /** + * Class to retrieve the asset id or the asset name, if one of those is missing + */ + private class AssetIdOrNameListener implements ApiRequestListener{ + final Object SYNC; + boolean ready = false; + + BitsharesAsset asset; + + AssetIdOrNameListener(Object SYNC) { + this.SYNC = SYNC; + } + + @Override + public void success(Object answer, int idPetition) { + if(answer instanceof ArrayList) { + + if (((ArrayList) answer).get(0) instanceof BitsharesAsset) { + asset = (BitsharesAsset) ((ArrayList) answer).get(0); + } + } + + synchronized (SYNC){ + ready = true; + SYNC.notifyAll(); + } + } + + @Override + public void fail(int idPetition) { + synchronized (SYNC){ + ready = true; + SYNC.notifyAll(); + } + } + } + /** * Class to retrieve the transaction date */