Bitshares petition fix

This commit is contained in:
henry 2017-10-23 23:25:09 -04:00
parent efb6820b9e
commit e3629f5c5b
3 changed files with 133 additions and 65 deletions

View file

@ -1,6 +1,5 @@
package cy.agorise.crystalwallet.apigenerator;
import android.app.Application;
import android.arch.lifecycle.LiveData;
import android.content.Context;
@ -17,7 +16,7 @@ import cy.agorise.crystalwallet.manager.BitsharesAccountManager;
import cy.agorise.crystalwallet.models.BitsharesAsset;
import cy.agorise.crystalwallet.models.BitsharesAssetInfo;
import cy.agorise.crystalwallet.models.CryptoCoinBalance;
import cy.agorise.crystalwallet.models.CryptoNetBalance;
import cy.agorise.crystalwallet.models.CryptoCurrency;
import cy.agorise.crystalwallet.network.WebSocketThread;
import cy.agorise.graphenej.Address;
import cy.agorise.graphenej.Asset;
@ -28,6 +27,7 @@ import cy.agorise.graphenej.UserAccount;
import cy.agorise.graphenej.api.GetAccountBalances;
import cy.agorise.graphenej.api.GetAccountByName;
import cy.agorise.graphenej.api.GetAccounts;
import cy.agorise.graphenej.api.GetBlockHeader;
import cy.agorise.graphenej.api.GetKeyReferences;
import cy.agorise.graphenej.api.GetRelativeAccountHistory;
import cy.agorise.graphenej.api.ListAssets;
@ -40,7 +40,6 @@ import cy.agorise.graphenej.interfaces.WitnessResponseListener;
import cy.agorise.graphenej.models.AccountBalanceUpdate;
import cy.agorise.graphenej.models.AccountProperties;
import cy.agorise.graphenej.models.BaseResponse;
import cy.agorise.graphenej.models.BroadcastedTransaction;
import cy.agorise.graphenej.models.HistoricalTransfer;
import cy.agorise.graphenej.models.SubscriptionResponse;
import cy.agorise.graphenej.models.WitnessResponse;
@ -237,8 +236,8 @@ public abstract class GrapheneApiGenerator {
}else{
ArrayList<BitsharesAsset> responseAssets = new ArrayList<>();
for(Asset asset: assets){
BitsharesAsset.Type assetType = null;
if(asset.getAssetType().equals(Asset.AssetType.CORE_ASSET)){
BitsharesAsset.Type assetType = BitsharesAsset.Type.UIA;
/*if(asset.getAssetType().equals(Asset.AssetType.CORE_ASSET)){
assetType = BitsharesAsset.Type.CORE;
}else if(asset.getAssetType().equals(Asset.AssetType.SMART_COIN)){
assetType = BitsharesAsset.Type.SMART_COIN;
@ -246,7 +245,7 @@ public abstract class GrapheneApiGenerator {
assetType = BitsharesAsset.Type.UIA;
}else if(asset.getAssetType().equals(Asset.AssetType.PREDICTION_MARKET)){
assetType = BitsharesAsset.Type.PREDICTION_MARKET;
}
}*/
BitsharesAsset responseAsset = new BitsharesAsset(asset.getSymbol(),asset.getPrecision(),asset.getObjectId(),assetType);
responseAssets.add(responseAsset);
}
@ -278,8 +277,8 @@ public abstract class GrapheneApiGenerator {
}else{
ArrayList<BitsharesAsset> responseAssets = new ArrayList<>();
for(Asset asset: assets){
BitsharesAsset.Type assetType = null;
if(asset.getAssetType().equals(Asset.AssetType.CORE_ASSET)){
BitsharesAsset.Type assetType = BitsharesAsset.Type.UIA;
/*if(asset.getAssetType().equals(Asset.AssetType.CORE_ASSET)){
assetType = BitsharesAsset.Type.CORE;
}else if(asset.getAssetType().equals(Asset.AssetType.SMART_COIN)){
assetType = BitsharesAsset.Type.SMART_COIN;
@ -287,7 +286,7 @@ public abstract class GrapheneApiGenerator {
assetType = BitsharesAsset.Type.UIA;
}else if(asset.getAssetType().equals(Asset.AssetType.PREDICTION_MARKET)){
assetType = BitsharesAsset.Type.PREDICTION_MARKET;
}
}*/
BitsharesAsset responseAsset = new BitsharesAsset(asset.getSymbol(),asset.getPrecision(),asset.getObjectId(),assetType);
responseAssets.add(responseAsset);
}
@ -323,35 +322,40 @@ public abstract class GrapheneApiGenerator {
if(update instanceof AccountBalanceUpdate){
AccountBalanceUpdate balanceUpdate = (AccountBalanceUpdate) update;
if(balanceUpdate.owner.equals(accountBitsharesId)){
CryptoCoinBalance balance = new CryptoCoinBalance();
final CryptoCoinBalance balance = new CryptoCoinBalance();
balance.setAccountId(accountId);
balance.setBalance(((AccountBalanceUpdate) update).balance);
/*LiveData<BitsharesAssetInfo> assetInfo = bitsharesAssetDao.getBitsharesAssetInfoById(((AccountBalanceUpdate) update).asset_type);
LiveData<BitsharesAssetInfo> assetInfo = bitsharesAssetDao.getBitsharesAssetInfoById(((AccountBalanceUpdate) update).asset_type);
if(assetInfo == null || assetInfo.getValue() == null){
BitsharesAsset.Type assetType;
Asset asset = balance.getAsset();
if(asset.getAssetType().equals(Asset.AssetType.CORE_ASSET)){
assetType = BitsharesAsset.Type.CORE;
}else if(asset.getAssetType().equals(Asset.AssetType.SMART_COIN)){
assetType = BitsharesAsset.Type.SMART_COIN;
}else if(asset.getAssetType().equals(Asset.AssetType.UIA)){
assetType = BitsharesAsset.Type.UIA;
}else if(asset.getAssetType().equals(Asset.AssetType.PREDICTION_MARKET)){
assetType = BitsharesAsset.Type.PREDICTION_MARKET;
}
final String assetType = ((AccountBalanceUpdate) update).asset_type;
ArrayList<String> idAssets = new ArrayList<>();
idAssets.add(assetType);
ApiRequest getAssetRequest = new ApiRequest(1, new ApiRequestListener() {
@Override
public void success(Object answer, int idPetition) {
if(answer instanceof BitsharesAsset){
BitsharesAssetInfo info = new BitsharesAssetInfo((BitsharesAsset) answer);
cryptoCurrencyDao.insertCryptoCurrency((CryptoCurrency)answer );
bitsharesAssetDao.insertBitsharesAssetInfo(info);
balance.setCryptoCurrencyId(bitsharesAssetDao.getBitsharesAssetInfoById(assetType).getValue().getCryptoCurrencyId());
balanceDao.insertCryptoCoinBalance(balance);
}
}
BitsharesAsset newAsset = new BitsharesAsset(asset.getSymbol(),asset.getPrecision(),asset.getObjectId(),assetType);
BitsharesAssetInfo info = new BitsharesAssetInfo(newAsset);
cryptoCurrencyDao.insertCryptoCurrency(newAsset);
bitsharesAssetDao.insertBitsharesAssetInfo(info);
assetInfo = bitsharesAssetDao.getBitsharesAssetInfoById(balance.getAsset().getObjectId());
@Override
public void fail(int idPetition) {
}
});
getAssetById(idAssets,getAssetRequest);
}else {
balance.setCryptoCurrencyId(assetInfo.getValue().getCryptoCurrencyId());
balanceDao.insertCryptoCoinBalance(balance);
//TODO balance function
}
balance.setCryptoCurrency(assetInfo.getValue().getCryptoCurrencyId());
balanceDao.insertCryptoCoinBalance(balance);*/
//TODO balance function
BitsharesAccountManager.refreshAccountTransactions(accountId,context);
}
}
}
@ -381,37 +385,42 @@ public abstract class GrapheneApiGenerator {
final CryptoCoinBalanceDao balanceDao = db.cryptoCoinBalanceDao();
final BitsharesAssetDao bitsharesAssetDao = db.bitsharesAssetDao();
final CryptoCurrencyDao cryptoCurrencyDao = db.cryptoCurrencyDao();
WebSocketThread thread = new WebSocketThread(new GetAccountBalances(new UserAccount(accountGrapheneId), null, new WitnessResponseListener() {
WebSocketThread thread = new WebSocketThread(new GetAccountBalances(new UserAccount(accountGrapheneId), new ArrayList<Asset>(), new WitnessResponseListener() {
@Override
public void onSuccess(WitnessResponse response) {
List<AssetAmount> balances = (List<AssetAmount>) response.result;
for(AssetAmount balance : balances){
CryptoCoinBalance ccBalance = new CryptoCoinBalance();
for(final AssetAmount balance : balances){
final CryptoCoinBalance ccBalance = new CryptoCoinBalance();
ccBalance.setAccountId(accountId);
ccBalance.setBalance(balance.getAmount().longValue());
LiveData<BitsharesAssetInfo> assetInfo = bitsharesAssetDao.getBitsharesAssetInfoById(balance.getAsset().getObjectId());
if(assetInfo == null || assetInfo.getValue() == null){
BitsharesAsset.Type assetType = null;
Asset asset = balance.getAsset();
if(asset.getAssetType().equals(Asset.AssetType.CORE_ASSET)){
assetType = BitsharesAsset.Type.CORE;
}else if(asset.getAssetType().equals(Asset.AssetType.SMART_COIN)){
assetType = BitsharesAsset.Type.SMART_COIN;
}else if(asset.getAssetType().equals(Asset.AssetType.UIA)){
assetType = BitsharesAsset.Type.UIA;
}else if(asset.getAssetType().equals(Asset.AssetType.PREDICTION_MARKET)){
assetType = BitsharesAsset.Type.PREDICTION_MARKET;
}
ArrayList<String> idAssets = new ArrayList<>();
idAssets.add(balance.getAsset().getObjectId());
ApiRequest getAssetRequest = new ApiRequest(1, new ApiRequestListener() {
@Override
public void success(Object answer, int idPetition) {
if(answer instanceof BitsharesAsset){
BitsharesAssetInfo info = new BitsharesAssetInfo((BitsharesAsset) answer);
cryptoCurrencyDao.insertCryptoCurrency((CryptoCurrency)answer );
bitsharesAssetDao.insertBitsharesAssetInfo(info);
ccBalance.setCryptoCurrencyId(bitsharesAssetDao.getBitsharesAssetInfoById(balance.getAsset().getObjectId()).getValue().getCryptoCurrencyId());
balanceDao.insertCryptoCoinBalance(ccBalance);
}
}
BitsharesAsset newAsset = new BitsharesAsset(asset.getSymbol(),asset.getPrecision(),asset.getObjectId(),assetType);
BitsharesAssetInfo info = new BitsharesAssetInfo(newAsset);
cryptoCurrencyDao.insertCryptoCurrency(newAsset);
bitsharesAssetDao.insertBitsharesAssetInfo(info);
assetInfo = bitsharesAssetDao.getBitsharesAssetInfoById(balance.getAsset().getObjectId());
@Override
public void fail(int idPetition) {
}
});
getAssetById(idAssets,getAssetRequest);
}else {
ccBalance.setCryptoCurrencyId(assetInfo.getValue().getCryptoCurrencyId());
balanceDao.insertCryptoCoinBalance(ccBalance);
}
ccBalance.setCryptoCurrencyId(assetInfo.getValue().getCryptoCurrencyId());
balanceDao.insertCryptoCoinBalance(ccBalance);
}
}
@ -425,4 +434,24 @@ public abstract class GrapheneApiGenerator {
}
public static void getBlockHeaderTime(long blockHeader, final ApiRequest request){
WebSocketThread thread = new WebSocketThread(new GetBlockHeader(blockHeader, new WitnessResponseListener() {
@Override
public void onSuccess(WitnessResponse response) {
if(response == null){
request.getListener().fail(request.getId());
}else {
request.getListener().success(response.result, request.getId());
}
}
@Override
public void onError(BaseResponse.Error error) {
request.getListener().fail(request.getId());
}
}),url);
thread.start();
}
}

View file

@ -22,6 +22,9 @@ public interface CryptoCurrencyDao {
@Query("SELECT * FROM crypto_currency WHERE id = :id")
CryptoCurrency getById(int id);
@Query("SELECT * FROM crypto_currency WHERE name = :name")
CryptoCurrency getByName(String name);
@Insert(onConflict = OnConflictStrategy.REPLACE)
public long[] insertCryptoCurrency(CryptoCurrency... currencies);

View file

@ -5,11 +5,15 @@ import android.content.Context;
import com.google.common.primitives.UnsignedLong;
import org.bitcoinj.core.Block;
import org.bitcoinj.core.ECKey;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.List;
import java.util.Objects;
import java.util.TimeZone;
import cy.agorise.crystalwallet.apigenerator.ApiRequest;
import cy.agorise.crystalwallet.apigenerator.ApiRequestListener;
@ -21,9 +25,11 @@ import cy.agorise.crystalwallet.cryptonetinforequests.ValidateBitsharesSendReque
import cy.agorise.crystalwallet.cryptonetinforequests.ValidateExistBitsharesAccountRequest;
import cy.agorise.crystalwallet.cryptonetinforequests.ValidateImportBitsharesAccountRequest;
import cy.agorise.crystalwallet.dao.CrystalDatabase;
import cy.agorise.crystalwallet.dao.TransactionDao;
import cy.agorise.crystalwallet.enums.CryptoNet;
import cy.agorise.crystalwallet.models.AccountSeed;
import cy.agorise.crystalwallet.models.BitsharesAsset;
import cy.agorise.crystalwallet.models.BitsharesAssetInfo;
import cy.agorise.crystalwallet.models.CryptoCoinTransaction;
import cy.agorise.crystalwallet.models.CryptoCurrency;
import cy.agorise.crystalwallet.models.CryptoNetAccount;
@ -38,6 +44,7 @@ import cy.agorise.graphenej.PublicKey;
import cy.agorise.graphenej.Transaction;
import cy.agorise.graphenej.UserAccount;
import cy.agorise.graphenej.models.AccountProperties;
import cy.agorise.graphenej.models.BlockHeader;
import cy.agorise.graphenej.models.HistoricalTransfer;
import cy.agorise.graphenej.objects.Memo;
import cy.agorise.graphenej.operations.TransferOperationBuilder;
@ -77,14 +84,17 @@ public class BitsharesAccountManager implements CryptoAccountManager, CryptoNetI
GrapheneAccount grapheneAccount = (GrapheneAccount) account;
if(grapheneAccount.getAccountId() == null){
System.out.println("Importing without accountid");
grapheneAccount = this.getAccountInfoByName(grapheneAccount.getName());
}else if(grapheneAccount.getName() == null){
System.out.println("Importing without accountname");
grapheneAccount = this.getAccountInfoById(grapheneAccount.getAccountId());
}
if(grapheneAccount == null) {
//TODO grapaheneAccount null, error fetching
return null;
}
System.out.println("Importing not null " + ((GrapheneAccount) account).getName() + " " + ((GrapheneAccount) account).getAccountId());
CrystalDatabase db = CrystalDatabase.getAppDatabase(context);
db.cryptoNetAccountDao().insertCryptoNetAccount(grapheneAccount);
@ -268,23 +278,20 @@ public class BitsharesAccountManager implements CryptoAccountManager, CryptoNetI
}
public static void refreshAccountTransactions(long idAccount, Context context){
System.out.println("Call refresh Account transactions from " + idAccount);
CrystalDatabase db = CrystalDatabase.getAppDatabase(context);
List<CryptoCoinTransaction> transactions = db.transactionDao().getByIdAccount(idAccount);
CryptoNetAccount account = db.cryptoNetAccountDao().getById(idAccount);
if(account.getCryptoNet() == CryptoNet.BITSHARES) {
GrapheneAccount grapheneAccount = new GrapheneAccount(account);
grapheneAccount.loadInfo(db.grapheneAccountInfoDao().getByAccountId(idAccount));
int start = transactions.size();
int limit = 50;
int stop = start + limit;
ApiRequest transactionRequest = new ApiRequest(0, new TransactionRequestListener(start, stop, limit, grapheneAccount, db));
GrapheneApiGenerator.getAccountTransaction(grapheneAccount.getName(), start, stop, limit, transactionRequest);
}
}
@ -312,13 +319,12 @@ public class BitsharesAccountManager implements CryptoAccountManager, CryptoNetI
CryptoCoinTransaction transaction = new CryptoCoinTransaction();
transaction.setAccountId(account.getId());
transaction.setAmount(transfer.getOperation().getAssetAmount().getAmount().longValue());
BitsharesAsset currency = null;
//TODO find currency by symbol db.cryptoCurrencyDao().getBySymbol(transfer.getOperation().getAssetAmount().getAsset().getSymbol())
CryptoCurrency currency = db.cryptoCurrencyDao().getByName(transfer.getOperation().getAssetAmount().getAsset().getSymbol());
if(currency == null){
//CryptoCurrency not in database
Asset asset = transfer.getOperation().getAssetAmount().getAsset();
BitsharesAsset.Type assetType = null;
if(asset.getAssetType() == Asset.AssetType.CORE_ASSET){
BitsharesAsset.Type assetType = BitsharesAsset.Type.UIA;
/*if(asset.getAssetType() == Asset.AssetType.CORE_ASSET){
assetType = BitsharesAsset.Type.CORE;
}else if(asset.getAssetType() == Asset.AssetType.SMART_COIN){
assetType = BitsharesAsset.Type.SMART_COIN;
@ -326,18 +332,19 @@ public class BitsharesAccountManager implements CryptoAccountManager, CryptoNetI
assetType = BitsharesAsset.Type.PREDICTION_MARKET;
}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));
}
transaction.setIdCurrency(currency.getId());
transaction.setConfirmed(true); //graphene transaction are always confirmed
//TODO date of the transaction transaction.setDate(transfer.);
transaction.setFrom(transfer.getOperation().getFrom().getName());
transaction.setInput(!transfer.getOperation().getFrom().getName().equals(account.getName()));
transaction.setTo(transfer.getOperation().getTo().getName());
db.transactionDao().insertTransaction(transaction);
GrapheneApiGenerator.getBlockHeaderTime(transfer.getBlockNum(),new ApiRequest(0,new GetTransactionDate(transaction,db.transactionDao())));
}
if(transfers.size()>= limit){
int newStart= start + limit;
@ -387,4 +394,33 @@ public class BitsharesAccountManager implements CryptoAccountManager, CryptoNetI
}
}
private static class GetTransactionDate implements ApiRequestListener{
private CryptoCoinTransaction transaction;
TransactionDao transactionDao;
public GetTransactionDate(CryptoCoinTransaction transaction, TransactionDao transactionDao) {
this.transaction = transaction;
this.transactionDao = transactionDao;
}
@Override
public void success(Object answer, int idPetition) {
if(answer instanceof BlockHeader){
SimpleDateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd'T'HH:mm:ss");
dateFormat.setTimeZone(TimeZone.getTimeZone("GMT"));
try {
transaction.setDate(dateFormat.parse(((BlockHeader) answer).timestamp));
transactionDao.insertTransaction(transaction);
} catch (ParseException e) {
e.printStackTrace();
}
}
}
@Override
public void fail(int idPetition) {
}
}
}