Added retrieve of asset id, for send transaction

Added retrieve of to Account id for send transaction
This commit is contained in:
henry 2018-01-21 23:55:40 -04:00
parent b23ad68c27
commit 159743b54e

View file

@ -268,13 +268,18 @@ public class BitsharesAccountManager implements CryptoAccountManager, CryptoNetI
* Broadcast a transaction request * Broadcast a transaction request
*/ */
private void validateSendRequest(final ValidateBitsharesSendRequest sendRequest){ 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 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() TransferOperationBuilder builder = new TransferOperationBuilder()
.setSource(fromUserAccount) .setSource(fromUserAccount)
.setDestination(toUserAccount) .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)); .setFee(new AssetAmount(UnsignedLong.valueOf(0), feeAsset));
if(sendRequest.getMemo() != null) { if(sendRequest.getMemo() != null) {
//builder.setMemo(new Memo(fromUserAccount,toUserAccount,0,sendRequest.getMemo().getBytes())); //builder.setMemo(new Memo(fromUserAccount,toUserAccount,0,sendRequest.getMemo().getBytes()));
@ -284,6 +289,7 @@ public class BitsharesAccountManager implements CryptoAccountManager, CryptoNetI
operationList.add(builder.build()); operationList.add(builder.build());
ECKey privateKey = sendRequest.getSourceAccount().getActiveKey(sendRequest.getContext()); ECKey privateKey = sendRequest.getSourceAccount().getActiveKey(sendRequest.getContext());
Transaction transaction = new Transaction(privateKey, null, operationList); Transaction transaction = new Transaction(privateKey, null, operationList);
ApiRequest transactionRequest = new ApiRequest(0, new ApiRequestListener() { ApiRequest transactionRequest = new ApiRequest(0, new ApiRequestListener() {
@ -353,6 +359,31 @@ public class BitsharesAccountManager implements CryptoAccountManager, CryptoNetI
return listener.account; 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<String> 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 * 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 * @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 * Class to retrieve the transaction date
*/ */