import org.apache.log4j.Logger;
import java.lang.reflect.Type;
+import java.util.ArrayList;
import java.util.List;
import java.util.Map;
*/
public List<Balance> list(Client client) throws BytomException {
- // TODO: 2018/5/23 need tx and test
Type listType = new ParameterizedTypeImpl(List.class, new Class[]{Balance.class});
List<Balance> balanceList = client.request("list-balances", null, listType);
logger.info("list-balances:");
logger.info("size of :" + balanceList.size());
- logger.info(balanceList.get(0).toJson());
+ for (Balance result : balanceList) {
+ logger.info(result.toJson());
+ }
return balanceList;
}
+
+ /**
+ * sum of all Asset alias amount
+ *
+ * @param client
+ * @param assetAlias
+ * @return
+ * @throws BytomException
+ */
+ public long listByAssetAlias(Client client, String assetAlias) throws BytomException {
+ List<Balance> balanceList = list(client);
+ Balance assetBalance = new Balance();
+ long amount = 0;
+ for (Balance result : balanceList) {
+ if (result.assetAlias.equals(assetAlias)) {
+ amount += result.amount;
+ }
+ }
+ logger.info("amount:"+amount);
+ return amount;
+ }
+
+ /**
+ * sum of all Account alias amount
+ *
+ * @param client
+ * @param accountAlias
+ * @return
+ * @throws BytomException
+ */
+ public List<Balance> listByAccountAlias(Client client, String accountAlias) throws BytomException {
+ List<Balance> balanceList = list(client);
+ List<Balance> accountBalance = new ArrayList<>();
+ for (Balance result : balanceList) {
+ if (result.accountAlias.equals(accountAlias)) {
+ accountBalance.add(result);
+ logger.info(result.toJson());
+ }
+ }
+ return accountBalance;
+ }
}
}
--- /dev/null
+package io.bytom.integration;
+
+import io.bytom.TestUtils;
+import io.bytom.api.Balance;
+import io.bytom.api.Wallet;
+import io.bytom.exception.BytomException;
+import io.bytom.http.Client;
+import org.apache.log4j.Logger;
+import org.junit.Assert;
+import org.junit.Test;
+
+import java.util.List;
+
+public class BalanceTest {
+
+ static Client client;
+
+ static {
+ try {
+ client = TestUtils.generateClient();
+ } catch (BytomException e) {
+ e.printStackTrace();
+ }
+ }
+
+ static Balance balance;
+
+ @Test
+ public void testBalanceList() throws Exception {
+ List<Balance> balanceList = new Balance.QueryBuilder().list(client);
+ Assert.assertNotNull(balanceList);
+ }
+
+ @Test
+ public void testBalanceByAssetAlias() throws Exception {
+ long amount = new Balance.QueryBuilder().listByAssetAlias(client, "BTM");
+ Assert.assertNotNull(amount);
+ }
+
+ @Test
+ public void testBalanceByAccountAlias() throws Exception {
+ List<Balance> balanceList = new Balance.QueryBuilder().listByAccountAlias(client, "test");
+ Assert.assertNotNull(balanceList);
+ }
+
+}
static Account receiverAccount;
static Receiver senderReceiver;
static Receiver receiverReceiver;
- static Address senderAddress;
- static Address receiverAddress;
+ static Account.Address senderAddress;
+ static Account.Address receiverAddress;
static Asset senderAsset;
static Asset receiverAsset;