2017-09-27 02:48:34 +00:00
|
|
|
package cy.agorise.crystalwallet.apigenerator;
|
|
|
|
|
2017-10-16 00:23:44 +00:00
|
|
|
import android.arch.lifecycle.LiveData;
|
|
|
|
import android.content.Context;
|
|
|
|
|
2017-10-12 03:37:05 +00:00
|
|
|
import java.io.Serializable;
|
2017-09-30 19:38:04 +00:00
|
|
|
import java.util.ArrayList;
|
2017-10-23 00:36:41 +00:00
|
|
|
import java.util.HashMap;
|
2017-09-30 19:38:04 +00:00
|
|
|
import java.util.List;
|
|
|
|
|
2017-10-19 02:46:09 +00:00
|
|
|
import cy.agorise.crystalwallet.dao.BitsharesAssetDao;
|
2017-10-18 02:55:42 +00:00
|
|
|
import cy.agorise.crystalwallet.dao.CryptoCoinBalanceDao;
|
2017-10-19 02:46:09 +00:00
|
|
|
import cy.agorise.crystalwallet.dao.CryptoCurrencyDao;
|
2017-10-16 00:23:44 +00:00
|
|
|
import cy.agorise.crystalwallet.dao.CrystalDatabase;
|
2017-10-18 02:55:42 +00:00
|
|
|
import cy.agorise.crystalwallet.manager.BitsharesAccountManager;
|
2017-10-09 00:10:46 +00:00
|
|
|
import cy.agorise.crystalwallet.models.BitsharesAsset;
|
2017-10-19 02:46:09 +00:00
|
|
|
import cy.agorise.crystalwallet.models.BitsharesAssetInfo;
|
2017-10-16 00:23:44 +00:00
|
|
|
import cy.agorise.crystalwallet.models.CryptoCoinBalance;
|
2017-10-24 03:25:09 +00:00
|
|
|
import cy.agorise.crystalwallet.models.CryptoCurrency;
|
2017-10-08 16:02:11 +00:00
|
|
|
import cy.agorise.crystalwallet.network.WebSocketThread;
|
2017-09-28 03:04:16 +00:00
|
|
|
import cy.agorise.graphenej.Address;
|
2017-10-09 00:10:46 +00:00
|
|
|
import cy.agorise.graphenej.Asset;
|
2017-10-16 00:23:44 +00:00
|
|
|
import cy.agorise.graphenej.AssetAmount;
|
2017-10-11 02:48:39 +00:00
|
|
|
import cy.agorise.graphenej.ObjectType;
|
2017-10-09 00:10:46 +00:00
|
|
|
import cy.agorise.graphenej.Transaction;
|
2017-09-28 03:04:16 +00:00
|
|
|
import cy.agorise.graphenej.UserAccount;
|
2017-10-16 00:23:44 +00:00
|
|
|
import cy.agorise.graphenej.api.GetAccountBalances;
|
2017-10-02 01:39:51 +00:00
|
|
|
import cy.agorise.graphenej.api.GetAccountByName;
|
2017-09-28 03:04:16 +00:00
|
|
|
import cy.agorise.graphenej.api.GetAccounts;
|
2017-10-24 03:25:09 +00:00
|
|
|
import cy.agorise.graphenej.api.GetBlockHeader;
|
2017-09-28 03:04:16 +00:00
|
|
|
import cy.agorise.graphenej.api.GetKeyReferences;
|
|
|
|
import cy.agorise.graphenej.api.GetRelativeAccountHistory;
|
2017-10-09 00:10:46 +00:00
|
|
|
import cy.agorise.graphenej.api.ListAssets;
|
|
|
|
import cy.agorise.graphenej.api.LookupAssetSymbols;
|
2017-10-11 02:48:39 +00:00
|
|
|
import cy.agorise.graphenej.api.SubscriptionMessagesHub;
|
2017-10-09 00:10:46 +00:00
|
|
|
import cy.agorise.graphenej.api.TransactionBroadcastSequence;
|
2017-10-11 02:48:39 +00:00
|
|
|
import cy.agorise.graphenej.interfaces.NodeErrorListener;
|
|
|
|
import cy.agorise.graphenej.interfaces.SubscriptionListener;
|
2017-09-28 03:04:16 +00:00
|
|
|
import cy.agorise.graphenej.interfaces.WitnessResponseListener;
|
2017-10-12 03:37:05 +00:00
|
|
|
import cy.agorise.graphenej.models.AccountBalanceUpdate;
|
2017-09-28 03:04:16 +00:00
|
|
|
import cy.agorise.graphenej.models.AccountProperties;
|
|
|
|
import cy.agorise.graphenej.models.BaseResponse;
|
2017-09-28 00:56:05 +00:00
|
|
|
import cy.agorise.graphenej.models.HistoricalTransfer;
|
2017-10-11 02:48:39 +00:00
|
|
|
import cy.agorise.graphenej.models.SubscriptionResponse;
|
2017-09-28 03:04:16 +00:00
|
|
|
import cy.agorise.graphenej.models.WitnessResponse;
|
|
|
|
|
2017-09-28 00:56:05 +00:00
|
|
|
|
2017-09-27 02:48:34 +00:00
|
|
|
/**
|
2017-10-08 16:02:11 +00:00
|
|
|
* This class manage all the api request directly to the Graphene Servers
|
2017-09-27 02:48:34 +00:00
|
|
|
* Created by henry on 26/9/2017.
|
|
|
|
*/
|
|
|
|
|
2017-10-16 00:23:44 +00:00
|
|
|
public abstract class GrapheneApiGenerator {
|
2017-09-28 00:56:05 +00:00
|
|
|
|
2017-09-28 03:04:16 +00:00
|
|
|
//TODO network connections
|
2017-10-08 16:02:11 +00:00
|
|
|
//TODO make to work with all Graphene stype, not only bitshares
|
2017-09-30 19:38:04 +00:00
|
|
|
private static String url = "http://128.0.69.157:8090";
|
2017-09-28 03:04:16 +00:00
|
|
|
|
2017-10-11 02:48:39 +00:00
|
|
|
// The meesage broker for bitshares
|
|
|
|
private static SubscriptionMessagesHub bitsharesSubscriptionHub = new SubscriptionMessagesHub("", "", true, new NodeErrorListener() {
|
|
|
|
@Override
|
|
|
|
public void onError(BaseResponse.Error error) {
|
|
|
|
//TODO subcription hub error
|
|
|
|
}
|
|
|
|
});
|
|
|
|
|
|
|
|
private static WebSocketThread subscriptionThread = new WebSocketThread(bitsharesSubscriptionHub,url);
|
2017-10-23 00:36:41 +00:00
|
|
|
private static HashMap<Long,SubscriptionListener> currentBitsharesListener = new HashMap<>();
|
2017-10-11 02:48:39 +00:00
|
|
|
|
2017-09-28 00:56:05 +00:00
|
|
|
/**
|
|
|
|
* Retrieves the data of an account searching by it's id
|
|
|
|
*
|
|
|
|
* @param accountId The accountId to retrieve
|
2017-09-28 03:04:16 +00:00
|
|
|
* @param request The Api request object, to answer this petition
|
2017-09-28 00:56:05 +00:00
|
|
|
*/
|
2017-09-28 03:04:16 +00:00
|
|
|
public static void getAccountById(String accountId, final ApiRequest request){
|
2017-10-08 16:02:11 +00:00
|
|
|
WebSocketThread thread = new WebSocketThread(new GetAccounts(accountId, new WitnessResponseListener() {
|
|
|
|
@Override
|
|
|
|
public void onSuccess(WitnessResponse response) {
|
|
|
|
if (response.result.getClass() == ArrayList.class) {
|
|
|
|
List list = (List) response.result;
|
|
|
|
if (list.size() > 0) {
|
|
|
|
if (list.get(0).getClass() == AccountProperties.class) {
|
|
|
|
request.getListener().success(list.get(0),request.getId());
|
|
|
|
return;
|
|
|
|
//TODO answer a crystal model
|
2017-09-28 03:04:16 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2017-10-08 16:02:11 +00:00
|
|
|
request.getListener().fail(request.getId());
|
|
|
|
}
|
2017-09-28 03:04:16 +00:00
|
|
|
|
2017-10-08 16:02:11 +00:00
|
|
|
@Override
|
|
|
|
public void onError(BaseResponse.Error error) {
|
|
|
|
request.getListener().fail(request.getId());
|
|
|
|
}
|
|
|
|
}),url);
|
|
|
|
thread.start();
|
2017-09-28 00:56:05 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Gets the account ID from an owner or active key
|
|
|
|
*
|
|
|
|
* @param address The address to retrieve
|
2017-09-28 03:04:16 +00:00
|
|
|
* @param request The Api request object, to answer this petition
|
2017-09-28 00:56:05 +00:00
|
|
|
*/
|
2017-09-28 03:04:16 +00:00
|
|
|
public static void getAccountByOwnerOrActiveAddress(Address address, final ApiRequest request){
|
2017-10-08 16:02:11 +00:00
|
|
|
WebSocketThread thread = new WebSocketThread(new GetKeyReferences(address, true, new WitnessResponseListener() {
|
2017-09-28 03:04:16 +00:00
|
|
|
@Override
|
|
|
|
public void onSuccess(WitnessResponse response) {
|
|
|
|
final List<List<UserAccount>> resp = (List<List<UserAccount>>) response.result;
|
|
|
|
if(resp.size() > 0){
|
|
|
|
List<UserAccount> accounts = resp.get(0);
|
|
|
|
if(accounts.size() > 0){
|
|
|
|
for(UserAccount account : accounts) {
|
|
|
|
request.getListener().success(account,request.getId());}}}
|
|
|
|
request.getListener().fail(request.getId());
|
|
|
|
}
|
|
|
|
|
|
|
|
@Override
|
|
|
|
public void onError(BaseResponse.Error error) {
|
|
|
|
request.getListener().fail(request.getId());
|
|
|
|
}
|
2017-10-08 16:02:11 +00:00
|
|
|
}),url);
|
|
|
|
|
2017-09-28 03:04:16 +00:00
|
|
|
thread.start();
|
2017-09-28 00:56:05 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
2017-09-28 03:04:16 +00:00
|
|
|
* Gets the Transaction for an Account
|
2017-09-28 00:56:05 +00:00
|
|
|
*
|
2017-10-16 00:23:44 +00:00
|
|
|
* @param accountGrapheneId The account id to search
|
2017-09-28 00:56:05 +00:00
|
|
|
* @param start The start index of the transaction list
|
|
|
|
* @param stop The stop index of the transaction list
|
|
|
|
* @param limit the maximun transactions to retrieve
|
2017-09-28 03:04:16 +00:00
|
|
|
* @param request The Api request object, to answer this petition
|
2017-09-28 00:56:05 +00:00
|
|
|
*/
|
2017-10-16 00:23:44 +00:00
|
|
|
public static void getAccountTransaction(String accountGrapheneId, int start, int stop, int limit, final ApiRequest request){
|
|
|
|
WebSocketThread thread = new WebSocketThread(new GetRelativeAccountHistory(new UserAccount(accountGrapheneId), stop, limit, start, new WitnessResponseListener() {
|
2017-09-28 03:04:16 +00:00
|
|
|
@Override
|
|
|
|
public void onSuccess(WitnessResponse response) {
|
|
|
|
WitnessResponse<List<HistoricalTransfer>> resp = response;
|
2017-10-16 00:23:44 +00:00
|
|
|
request.getListener().success(resp.result,request.getId());
|
2017-09-28 03:04:16 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
@Override
|
|
|
|
public void onError(BaseResponse.Error error) {
|
|
|
|
request.getListener().fail(request.getId());
|
|
|
|
}
|
2017-10-08 16:02:11 +00:00
|
|
|
}),url);
|
2017-09-28 03:04:16 +00:00
|
|
|
thread.start();
|
2017-09-28 00:56:05 +00:00
|
|
|
}
|
|
|
|
|
2017-10-16 00:23:44 +00:00
|
|
|
/**
|
|
|
|
* Retrieves the account id by the name of the account
|
|
|
|
*
|
|
|
|
* @param accountName The account Name to find
|
|
|
|
* @param request The Api request object, to answer this petition
|
|
|
|
*/
|
|
|
|
public static void getAccountByName(String accountName, final ApiRequest request){
|
|
|
|
WebSocketThread thread = new WebSocketThread(new GetAccountByName(accountName, new WitnessResponseListener() {
|
|
|
|
@Override
|
|
|
|
public void onSuccess(WitnessResponse response) {
|
|
|
|
AccountProperties accountProperties = ((WitnessResponse<AccountProperties>) response).result;
|
|
|
|
if(accountProperties == null){
|
|
|
|
request.getListener().fail(request.getId());
|
|
|
|
}else{
|
|
|
|
request.getListener().success(accountProperties,request.getId());
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
@Override
|
|
|
|
public void onError(BaseResponse.Error error) {
|
|
|
|
request.getListener().fail(request.getId());
|
|
|
|
}
|
|
|
|
}),url);
|
|
|
|
thread.start();
|
|
|
|
}
|
|
|
|
|
2017-09-28 00:56:05 +00:00
|
|
|
/**
|
2017-10-02 01:42:21 +00:00
|
|
|
* Retrieves the account id by the name of the account
|
2017-09-28 00:56:05 +00:00
|
|
|
*
|
|
|
|
* @param accountName The account Name to find
|
2017-09-28 03:04:16 +00:00
|
|
|
* @param request The Api request object, to answer this petition
|
2017-09-28 00:56:05 +00:00
|
|
|
*/
|
2017-10-02 01:42:21 +00:00
|
|
|
public static void getAccountIdByName(String accountName, final ApiRequest request){
|
2017-10-08 16:02:11 +00:00
|
|
|
WebSocketThread thread = new WebSocketThread(new GetAccountByName(accountName, new WitnessResponseListener() {
|
2017-10-02 01:39:51 +00:00
|
|
|
@Override
|
|
|
|
public void onSuccess(WitnessResponse response) {
|
|
|
|
AccountProperties accountProperties = ((WitnessResponse<AccountProperties>) response).result;
|
2017-10-05 02:04:42 +00:00
|
|
|
if(accountProperties == null){
|
2017-10-02 01:42:21 +00:00
|
|
|
request.getListener().success(null,request.getId());
|
2017-10-02 01:39:51 +00:00
|
|
|
}else{
|
2017-10-02 01:42:21 +00:00
|
|
|
request.getListener().success(accountProperties.id,request.getId());
|
2017-10-02 01:39:51 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
@Override
|
|
|
|
public void onError(BaseResponse.Error error) {
|
|
|
|
request.getListener().fail(request.getId());
|
|
|
|
}
|
2017-10-08 16:02:11 +00:00
|
|
|
}),url);
|
|
|
|
thread.start();
|
2017-09-28 00:56:05 +00:00
|
|
|
}
|
2017-10-09 00:10:46 +00:00
|
|
|
|
|
|
|
public static void broadcastTransaction(Transaction transaction, Asset feeAsset, final ApiRequest request){
|
|
|
|
WebSocketThread thread = new WebSocketThread(new TransactionBroadcastSequence(transaction, feeAsset, new WitnessResponseListener() {
|
|
|
|
@Override
|
|
|
|
public void onSuccess(WitnessResponse response) {
|
|
|
|
request.getListener().success(true,request.getId());
|
|
|
|
}
|
|
|
|
|
|
|
|
@Override
|
|
|
|
public void onError(BaseResponse.Error error) {
|
|
|
|
request.getListener().fail(request.getId());
|
|
|
|
}
|
|
|
|
}),url);
|
|
|
|
thread.start();
|
|
|
|
}
|
|
|
|
|
|
|
|
public static void getAssetByName(ArrayList<String> assetNames, final ApiRequest request){
|
|
|
|
//TODO the graphenej library needs a way to creates the Asset object only with the symbol
|
|
|
|
ArrayList<Asset> assets = new ArrayList<>();
|
|
|
|
for(String assetName : assetNames){
|
|
|
|
Asset asset = new Asset("",assetName,-1);
|
|
|
|
assets.add(asset);
|
|
|
|
}
|
|
|
|
//TODO the graphenj library needs to add the lookupAssetSymbols to be able to search the asset by symbol
|
|
|
|
// this can be done with the same lookupassetsymbol, but passing only the symbol not the objetcid
|
|
|
|
WebSocketThread thread = new WebSocketThread(new LookupAssetSymbols(assets, new WitnessResponseListener() {
|
|
|
|
@Override
|
|
|
|
public void onSuccess(WitnessResponse response) {
|
|
|
|
List<Asset> assets = (List<Asset>) response.result;
|
|
|
|
if(assets.size() <= 0){
|
|
|
|
request.getListener().fail(request.getId());
|
|
|
|
}else{
|
|
|
|
ArrayList<BitsharesAsset> responseAssets = new ArrayList<>();
|
|
|
|
for(Asset asset: assets){
|
2017-10-24 03:25:09 +00:00
|
|
|
BitsharesAsset.Type assetType = BitsharesAsset.Type.UIA;
|
|
|
|
/*if(asset.getAssetType().equals(Asset.AssetType.CORE_ASSET)){
|
2017-10-09 00:10:46 +00:00
|
|
|
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;
|
2017-10-24 03:25:09 +00:00
|
|
|
}*/
|
2017-10-09 00:10:46 +00:00
|
|
|
BitsharesAsset responseAsset = new BitsharesAsset(asset.getSymbol(),asset.getPrecision(),asset.getObjectId(),assetType);
|
|
|
|
responseAssets.add(responseAsset);
|
|
|
|
}
|
|
|
|
request.getListener().success(responseAssets,request.getId());
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
@Override
|
|
|
|
public void onError(BaseResponse.Error error) {
|
|
|
|
request.getListener().fail(request.getId());
|
|
|
|
}
|
|
|
|
}),url);
|
|
|
|
thread.start();
|
|
|
|
}
|
|
|
|
|
|
|
|
public static void getAssetById(ArrayList<String> assetIds, final ApiRequest request){
|
|
|
|
ArrayList<Asset> assets = new ArrayList<>();
|
|
|
|
for(String assetId : assetIds){
|
|
|
|
Asset asset = new Asset(assetId);
|
|
|
|
assets.add(asset);
|
|
|
|
}
|
2017-10-16 00:23:44 +00:00
|
|
|
|
2017-10-09 00:10:46 +00:00
|
|
|
WebSocketThread thread = new WebSocketThread(new LookupAssetSymbols(assets, new WitnessResponseListener() {
|
|
|
|
@Override
|
|
|
|
public void onSuccess(WitnessResponse response) {
|
|
|
|
List<Asset> assets = (List<Asset>) response.result;
|
|
|
|
if(assets.size() <= 0){
|
|
|
|
request.getListener().fail(request.getId());
|
|
|
|
}else{
|
|
|
|
ArrayList<BitsharesAsset> responseAssets = new ArrayList<>();
|
|
|
|
for(Asset asset: assets){
|
2017-10-24 03:25:09 +00:00
|
|
|
BitsharesAsset.Type assetType = BitsharesAsset.Type.UIA;
|
|
|
|
/*if(asset.getAssetType().equals(Asset.AssetType.CORE_ASSET)){
|
2017-10-09 00:10:46 +00:00
|
|
|
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;
|
2017-10-24 03:25:09 +00:00
|
|
|
}*/
|
2017-10-09 00:10:46 +00:00
|
|
|
BitsharesAsset responseAsset = new BitsharesAsset(asset.getSymbol(),asset.getPrecision(),asset.getObjectId(),assetType);
|
|
|
|
responseAssets.add(responseAsset);
|
|
|
|
}
|
|
|
|
request.getListener().success(responseAssets,request.getId());
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
@Override
|
|
|
|
public void onError(BaseResponse.Error error) {
|
|
|
|
request.getListener().fail(request.getId());
|
|
|
|
}
|
|
|
|
}),url);
|
|
|
|
thread.start();
|
|
|
|
}
|
2017-10-11 02:48:39 +00:00
|
|
|
|
2017-10-18 02:55:42 +00:00
|
|
|
public static void subscribeBitsharesAccount(final long accountId, final String accountBitsharesId, final Context context){
|
2017-10-23 00:36:41 +00:00
|
|
|
if(!currentBitsharesListener.containsKey(accountId)){
|
2017-10-18 02:55:42 +00:00
|
|
|
CrystalDatabase db = CrystalDatabase.getAppDatabase(context);
|
|
|
|
final CryptoCoinBalanceDao balanceDao = db.cryptoCoinBalanceDao();
|
2017-10-19 02:46:09 +00:00
|
|
|
final BitsharesAssetDao bitsharesAssetDao = db.bitsharesAssetDao();
|
|
|
|
final CryptoCurrencyDao cryptoCurrencyDao = db.cryptoCurrencyDao();
|
2017-10-11 02:48:39 +00:00
|
|
|
SubscriptionListener balanceListener = new SubscriptionListener() {
|
|
|
|
@Override
|
|
|
|
public ObjectType getInterestObjectType() {
|
|
|
|
return ObjectType.BALANCE_OBJECT;
|
|
|
|
}
|
|
|
|
|
|
|
|
@Override
|
|
|
|
public void onSubscriptionUpdate(SubscriptionResponse response) {
|
2017-10-12 03:37:05 +00:00
|
|
|
List<Serializable> updatedObjects = (List<Serializable>) response.params.get(1);
|
|
|
|
if(updatedObjects.size() > 0){
|
|
|
|
for(Serializable update : updatedObjects){
|
|
|
|
if(update instanceof AccountBalanceUpdate){
|
|
|
|
AccountBalanceUpdate balanceUpdate = (AccountBalanceUpdate) update;
|
2017-10-16 00:23:44 +00:00
|
|
|
if(balanceUpdate.owner.equals(accountBitsharesId)){
|
2017-10-24 03:25:09 +00:00
|
|
|
final CryptoCoinBalance balance = new CryptoCoinBalance();
|
2017-10-19 02:46:09 +00:00
|
|
|
balance.setAccountId(accountId);
|
|
|
|
balance.setBalance(((AccountBalanceUpdate) update).balance);
|
2017-10-24 03:25:09 +00:00
|
|
|
LiveData<BitsharesAssetInfo> assetInfo = bitsharesAssetDao.getBitsharesAssetInfoById(((AccountBalanceUpdate) update).asset_type);
|
2017-10-19 02:46:09 +00:00
|
|
|
if(assetInfo == null || assetInfo.getValue() == null){
|
2017-10-24 03:25:09 +00:00
|
|
|
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);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
@Override
|
|
|
|
public void fail(int idPetition) {
|
|
|
|
|
|
|
|
}
|
|
|
|
});
|
|
|
|
getAssetById(idAssets,getAssetRequest);
|
|
|
|
}else {
|
|
|
|
|
|
|
|
balance.setCryptoCurrencyId(assetInfo.getValue().getCryptoCurrencyId());
|
|
|
|
balanceDao.insertCryptoCoinBalance(balance);
|
|
|
|
//TODO balance function
|
2017-10-18 02:55:42 +00:00
|
|
|
}
|
|
|
|
BitsharesAccountManager.refreshAccountTransactions(accountId,context);
|
2017-10-24 03:25:09 +00:00
|
|
|
|
2017-10-12 03:37:05 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2017-10-18 02:55:42 +00:00
|
|
|
|
2017-10-12 03:37:05 +00:00
|
|
|
}
|
2017-10-11 02:48:39 +00:00
|
|
|
}
|
|
|
|
};
|
|
|
|
|
2017-10-23 00:36:41 +00:00
|
|
|
currentBitsharesListener.put(accountId,balanceListener);
|
2017-10-11 02:48:39 +00:00
|
|
|
bitsharesSubscriptionHub.addSubscriptionListener(balanceListener);
|
|
|
|
|
|
|
|
if(!subscriptionThread.isConnected()){
|
|
|
|
subscriptionThread.start();
|
|
|
|
}else if(!bitsharesSubscriptionHub.isSubscribed()){
|
|
|
|
bitsharesSubscriptionHub.resubscribe();
|
|
|
|
}
|
2017-10-23 00:36:41 +00:00
|
|
|
}
|
2017-10-11 02:48:39 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
public static void cancelBitsharesAccountSubcriptions(){
|
|
|
|
bitsharesSubscriptionHub.cancelSubscriptions();
|
|
|
|
}
|
|
|
|
|
2017-10-16 00:23:44 +00:00
|
|
|
public static void getAccountBalance(final long accountId, final String accountGrapheneId, final Context context){
|
2017-10-23 00:36:41 +00:00
|
|
|
|
2017-10-18 02:55:42 +00:00
|
|
|
CrystalDatabase db = CrystalDatabase.getAppDatabase(context);
|
|
|
|
final CryptoCoinBalanceDao balanceDao = db.cryptoCoinBalanceDao();
|
2017-10-19 02:46:09 +00:00
|
|
|
final BitsharesAssetDao bitsharesAssetDao = db.bitsharesAssetDao();
|
|
|
|
final CryptoCurrencyDao cryptoCurrencyDao = db.cryptoCurrencyDao();
|
2017-10-24 03:25:09 +00:00
|
|
|
WebSocketThread thread = new WebSocketThread(new GetAccountBalances(new UserAccount(accountGrapheneId), new ArrayList<Asset>(), new WitnessResponseListener() {
|
2017-10-16 00:23:44 +00:00
|
|
|
@Override
|
|
|
|
public void onSuccess(WitnessResponse response) {
|
|
|
|
List<AssetAmount> balances = (List<AssetAmount>) response.result;
|
2017-10-24 03:25:09 +00:00
|
|
|
for(final AssetAmount balance : balances){
|
|
|
|
final CryptoCoinBalance ccBalance = new CryptoCoinBalance();
|
2017-10-16 00:23:44 +00:00
|
|
|
ccBalance.setAccountId(accountId);
|
|
|
|
ccBalance.setBalance(balance.getAmount().longValue());
|
2017-10-19 02:46:09 +00:00
|
|
|
LiveData<BitsharesAssetInfo> assetInfo = bitsharesAssetDao.getBitsharesAssetInfoById(balance.getAsset().getObjectId());
|
|
|
|
if(assetInfo == null || assetInfo.getValue() == null){
|
2017-10-24 03:25:09 +00:00
|
|
|
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);
|
|
|
|
}
|
|
|
|
}
|
2017-10-18 02:55:42 +00:00
|
|
|
|
2017-10-24 03:25:09 +00:00
|
|
|
@Override
|
|
|
|
public void fail(int idPetition) {
|
2017-10-16 00:23:44 +00:00
|
|
|
|
2017-10-24 03:25:09 +00:00
|
|
|
}
|
|
|
|
});
|
|
|
|
getAssetById(idAssets,getAssetRequest);
|
|
|
|
|
|
|
|
}else {
|
|
|
|
|
|
|
|
ccBalance.setCryptoCurrencyId(assetInfo.getValue().getCryptoCurrencyId());
|
|
|
|
balanceDao.insertCryptoCoinBalance(ccBalance);
|
|
|
|
}
|
2017-10-16 00:23:44 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
@Override
|
|
|
|
public void onError(BaseResponse.Error error) {
|
|
|
|
|
|
|
|
}
|
|
|
|
}),url);
|
|
|
|
|
|
|
|
thread.start();
|
|
|
|
|
|
|
|
}
|
|
|
|
|
2017-10-24 03:25:09 +00:00
|
|
|
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();
|
|
|
|
|
|
|
|
}
|
|
|
|
|
2017-09-27 02:48:34 +00:00
|
|
|
}
|