Bitshares manager use the new bitshares account cache

This commit is contained in:
hvarona 2018-06-17 09:47:32 -04:00
parent 39a8a1e446
commit d0df53d8e2
2 changed files with 69 additions and 36 deletions

View file

@ -24,6 +24,9 @@ public interface BitsharesAccountNameCacheDao {
@Query("SELECT * FROM bitshares_account_name_cache WHERE account_id = :account_id") @Query("SELECT * FROM bitshares_account_name_cache WHERE account_id = :account_id")
BitsharesAccountNameCache getByAccountId(String 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) @Insert(onConflict = OnConflictStrategy.REPLACE)
public long[] insertBitsharesAccountNameCache(BitsharesAccountNameCache... accountsNames); public long[] insertBitsharesAccountNameCache(BitsharesAccountNameCache... accountsNames);

View file

@ -1,6 +1,5 @@
package cy.agorise.crystalwallet.manager; package cy.agorise.crystalwallet.manager;
import android.accounts.Account;
import android.annotation.SuppressLint; import android.annotation.SuppressLint;
import android.content.Context; 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.ApiRequestListener;
import cy.agorise.crystalwallet.apigenerator.BitsharesFaucetApiGenerator; import cy.agorise.crystalwallet.apigenerator.BitsharesFaucetApiGenerator;
import cy.agorise.crystalwallet.apigenerator.GrapheneApiGenerator; import cy.agorise.crystalwallet.apigenerator.GrapheneApiGenerator;
import cy.agorise.crystalwallet.apigenerator.grapheneoperation.AccountUpgradeOperation;
import cy.agorise.crystalwallet.apigenerator.grapheneoperation.AccountUpgradeOperationBuilder; import cy.agorise.crystalwallet.apigenerator.grapheneoperation.AccountUpgradeOperationBuilder;
import cy.agorise.crystalwallet.application.constant.BitsharesConstant; import cy.agorise.crystalwallet.application.constant.BitsharesConstant;
import cy.agorise.crystalwallet.models.BitsharesAccountNameCache;
import cy.agorise.crystalwallet.models.seed.BIP39; import cy.agorise.crystalwallet.models.seed.BIP39;
import cy.agorise.crystalwallet.requestmanagers.CryptoNetEquivalentRequest; import cy.agorise.crystalwallet.requestmanagers.CryptoNetEquivalentRequest;
import cy.agorise.crystalwallet.requestmanagers.CryptoNetInfoRequest; import cy.agorise.crystalwallet.requestmanagers.CryptoNetInfoRequest;
@ -389,23 +388,62 @@ 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){ private void validateSendRequest(final ValidateBitsharesSendRequest sendRequest , final String idAsset){
final Asset feeAsset = new Asset(idAsset); final Asset feeAsset = new Asset(idAsset);
final UserAccount fromUserAccount =new UserAccount(sendRequest.getSourceAccount().getAccountId()); final UserAccount fromUserAccount =new UserAccount(sendRequest.getSourceAccount().getAccountId());
//TODO cached to accounts final CrystalDatabase db = CrystalDatabase.getAppDatabase(sendRequest.getContext());
BitsharesAccountNameCache cacheAccount = db.bitsharesAccountNameCacheDao().getByAccountName(sendRequest.getToAccount());
if(cacheAccount == null) {
this.getAccountInfoByName(sendRequest.getToAccount(), new ManagerRequest() { this.getAccountInfoByName(sendRequest.getToAccount(), new ManagerRequest() {
@Override @Override
public void success(Object answer) { public void success(Object answer) {
GrapheneAccount toUserGrapheneAccount = (GrapheneAccount) answer; GrapheneAccount toUserGrapheneAccount = (GrapheneAccount) answer;
UserAccount toUserAccount = new UserAccount(toUserGrapheneAccount.getAccountId()); 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);
}
@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);
}
}
/**
* 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() TransferOperationBuilder builder = new TransferOperationBuilder()
.setSource(fromUserAccount) .setSource(fromUserAccount)
.setDestination(toUserAccount) .setDestination(toUserAccount)
.setTransferAmount(new AssetAmount(UnsignedLong.valueOf(sendRequest.getAmount()), new Asset(idAsset))) .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()));
//TODO memo //TODO memo
System.out.println("transaction has memo"); System.out.println("transaction has memo");
@ -430,15 +468,7 @@ public class BitsharesAccountManager implements CryptoAccountManager, CryptoNetI
} }
}); });
GrapheneApiGenerator.broadcastTransaction(transaction,feeAsset, transactionRequest); GrapheneApiGenerator.broadcastTransaction(transaction, feeAsset, transactionRequest);
}
@Override
public void fail() {
sendRequest.setStatus(ValidateBitsharesSendRequest.StatusCode.NO_TO_USER_INFO);
}
});
} }
/** /**
@ -667,7 +697,7 @@ public class BitsharesAccountManager implements CryptoAccountManager, CryptoNetI
GrapheneApiGenerator.getEquivalentValue(fromAsset.getBitsharesId(),toAsset.getBitsharesId(), getEquivalentRequest); GrapheneApiGenerator.getEquivalentValue(fromAsset.getBitsharesId(),toAsset.getBitsharesId(), getEquivalentRequest);
}else{ }else{
//TODO error //TODO error
System.out.println("Equivalen Value error "); System.out.println("Equivalent Value error ");
} }
} }