use of com.hedera.mirror.common.domain.balance.TokenBalance in project hedera-mirror-node by hashgraph.
the class TokenBalanceRepositoryTest method save.
@Test
void save() {
TokenBalance tokenBalance1 = create(1L, 1, 1, 100);
TokenBalance tokenBalance2 = create(2L, 2, 2, 200);
TokenBalance tokenBalance3 = create(3L, 3, 2, 300);
assertThat(tokenBalanceRepository.findById(tokenBalance1.getId())).get().isEqualTo(tokenBalance1);
assertThat(tokenBalanceRepository.findAll()).containsExactlyInAnyOrder(tokenBalance1, tokenBalance2, tokenBalance3);
}
use of com.hedera.mirror.common.domain.balance.TokenBalance in project hedera-mirror-node by hashgraph.
the class ProtoBalanceFileReader method toAccountBalance.
private AccountBalance toAccountBalance(long consensusTimestamp, SingleAccountBalances balances) {
EntityId accountId = EntityId.of(balances.getAccountID());
List<TokenBalance> tokenBalances = balances.getTokenUnitBalancesList().stream().map(tokenBalance -> {
EntityId tokenId = EntityId.of(tokenBalance.getTokenId());
TokenBalance.Id id = new TokenBalance.Id(consensusTimestamp, accountId, tokenId);
return new TokenBalance(tokenBalance.getBalance(), id);
}).collect(Collectors.toList());
return new AccountBalance(balances.getHbarBalance(), tokenBalances, new AccountBalance.Id(consensusTimestamp, accountId));
}
use of com.hedera.mirror.common.domain.balance.TokenBalance in project hedera-mirror-node by hashgraph.
the class AccountBalanceLineParserV2 method parseTokenBalanceList.
private List<TokenBalance> parseTokenBalanceList(String tokenBalancesProtoString, long consensusTimestamp, EntityId accountId) throws InvalidProtocolBufferException {
List<com.hederahashgraph.api.proto.java.TokenBalance> tokenBalanceProtoList = TokenBalances.parseFrom(Base64.decodeBase64(tokenBalancesProtoString)).getTokenBalancesList();
List<TokenBalance> tokenBalances = new ArrayList<>();
for (com.hederahashgraph.api.proto.java.TokenBalance tokenBalanceProto : tokenBalanceProtoList) {
TokenID tokenId = tokenBalanceProto.getTokenId();
TokenBalance tokenBalance = new TokenBalance(tokenBalanceProto.getBalance(), new TokenBalance.Id(consensusTimestamp, accountId, EntityId.of(tokenId)));
tokenBalances.add(tokenBalance);
}
return tokenBalances;
}
use of com.hedera.mirror.common.domain.balance.TokenBalance in project hedera-mirror-node by hashgraph.
the class AccountBalanceLineParserV2 method parse.
/**
* Parses an account balance line to extract shard, realm, account, balance, and token balances. If the shard
* matches systemShardNum, creates and returns an {@code AccountBalance} entity object. The account balance line
* should be in the format of "shard,realm,account,balance"
*
* @param line The account balance line
* @param consensusTimestamp The consensus timestamp of the account balance line
* @return {@code AccountBalance} entity object
* @throws InvalidDatasetException if the line is malformed or the shard does not match {@code systemShardNum}
*/
@Override
public AccountBalance parse(String line, long consensusTimestamp) {
try {
if (line == null) {
throw new InvalidDatasetException("Null line cannot be parsed");
}
List<String> parts = SPLITTER.splitToList(line);
boolean hasTokenBalance;
if (parts.size() == 5) {
hasTokenBalance = true;
} else if (parts.size() == 4) {
hasTokenBalance = false;
} else {
throw new InvalidDatasetException("Invalid account balance line: " + line);
}
long shardNum = Long.parseLong(parts.get(0));
int realmNum = Integer.parseInt(parts.get(1));
int accountNum = Integer.parseInt(parts.get(2));
long balance = Long.parseLong(parts.get(3));
if (shardNum < 0 || realmNum < 0 || accountNum < 0 || balance < 0) {
throw new InvalidDatasetException("Invalid account balance line: " + line);
}
if (shardNum != mirrorProperties.getShard()) {
throw new InvalidDatasetException(String.format("Invalid account balance line: %s. Expect " + "shard (%d), got shard (%d)", line, mirrorProperties.getShard(), shardNum));
}
EntityId accountId = EntityId.of(shardNum, realmNum, accountNum, EntityType.ACCOUNT);
List<TokenBalance> tokenBalances = hasTokenBalance ? parseTokenBalanceList(parts.get(4), consensusTimestamp, accountId) : Collections.emptyList();
return new AccountBalance(balance, tokenBalances, new AccountBalance.Id(consensusTimestamp, accountId));
} catch (NumberFormatException | InvalidProtocolBufferException ex) {
throw new InvalidDatasetException("Invalid account balance line: " + line, ex);
}
}
use of com.hedera.mirror.common.domain.balance.TokenBalance in project hedera-mirror-node by hashgraph.
the class AccountBalanceFileParser method doParse.
@Override
protected void doParse(AccountBalanceFile accountBalanceFile) {
log.info("Starting processing account balances file {}", accountBalanceFile.getName());
DateRangeFilter filter = mirrorDateRangePropertiesProcessor.getDateRangeFilter(StreamType.BALANCE);
int batchSize = ((BalanceParserProperties) parserProperties).getBatchSize();
long count = 0L;
if (filter.filter(accountBalanceFile.getConsensusTimestamp())) {
List<AccountBalance> accountBalances = new ArrayList<>(batchSize);
Map<TokenBalance.Id, TokenBalance> tokenBalances = new HashMap<>(batchSize);
count = accountBalanceFile.getItems().doOnNext(accountBalance -> {
accountBalances.add(accountBalance);
for (var tokenBalance : accountBalance.getTokenBalances()) {
if (tokenBalances.putIfAbsent(tokenBalance.getId(), tokenBalance) != null) {
log.warn("Skipping duplicate token balance: {}", tokenBalance);
}
}
if (accountBalances.size() >= batchSize) {
batchPersister.persist(accountBalances);
accountBalances.clear();
}
if (tokenBalances.size() >= batchSize) {
batchPersister.persist(tokenBalances.values());
tokenBalances.clear();
}
}).count().block();
batchPersister.persist(accountBalances);
batchPersister.persist(tokenBalances.values());
}
Instant loadEnd = Instant.now();
accountBalanceFile.setCount(count);
accountBalanceFile.setLoadEnd(loadEnd.getEpochSecond());
streamFileListener.onEnd(accountBalanceFile);
streamFileRepository.save(accountBalanceFile);
}
Aggregations