3 import com.google.gson.annotations.SerializedName;
4 import io.bytom.common.Utils;
5 import io.bytom.exception.BytomException;
6 import io.bytom.http.Client;
7 import org.apache.log4j.Logger;
9 import java.lang.reflect.Type;
10 import java.util.ArrayList;
11 import java.util.List;
14 public class Balance {
19 @SerializedName("account_id")
20 public String accountId;
25 @SerializedName("account_alias")
26 public String accountAlias;
29 * sum of the unspent outputs.
30 * specified asset balance of account.
37 @SerializedName("asset_id")
38 public String assetId;
43 @SerializedName("asset_alias")
44 public String assetAlias;
46 @SerializedName("asset_definition")
47 public Map<String, Object> definition;
49 private static Logger logger = Logger.getLogger(Balance.class);
52 * Serializes the Balance into a form that is safe to transfer over the wire.
54 * @return the JSON-serialized representation of the Receiver object
56 public String toJson() {
57 return Utils.serializer.toJson(this);
61 public static class QueryBuilder {
64 * Call list-Balances api
68 * @throws BytomException
70 public List<Balance> list(Client client) throws BytomException {
72 Type listType = new ParameterizedTypeImpl(List.class, new Class[]{Balance.class});
73 List<Balance> balanceList = client.request("list-balances", null, listType);
74 logger.info("list-balances:");
75 logger.info("size of :" + balanceList.size());
76 for (Balance result : balanceList) {
77 logger.info(result.toJson());
84 * sum of all Asset alias amount
89 * @throws BytomException
91 public Balance listByAssetAlias(Client client, String assetAlias) throws BytomException {
92 List<Balance> balanceList = list(client);
93 Balance assetBalance = new Balance();
94 assetBalance.assetAlias = assetAlias;
96 for (Balance result : balanceList) {
97 if (result.assetAlias.equals(assetAlias)) {
98 amount += result.amount;
99 assetBalance.assetId = result.assetId;
102 assetBalance.amount = amount;
104 logger.info(assetBalance.toJson());
110 * sum of all Account alias amount
113 * @param accountAlias
115 * @throws BytomException
117 public List<Balance> listByAccountAlias(Client client, String accountAlias) throws BytomException {
118 List<Balance> balanceList = list(client);
119 List<Balance> accountBalance = new ArrayList<>();
120 for (Balance result : balanceList) {
121 if (result.accountAlias.equals(accountAlias)) {
122 accountBalance.add(result);
123 logger.info(result.toJson());
126 return accountBalance;