Search in sources :

Example 1 with TokenBalance

use of com.hedera.mirror.common.domain.balance.TokenBalance in project hedera-mirror-node by hashgraph.

the class AccountBalanceFileParserTest method accountBalance.

private AccountBalance accountBalance(long timestamp, int offset) {
    EntityId accountId = EntityId.of(0, 0, offset + 1000, EntityType.ACCOUNT);
    EntityId tokenId = EntityId.of(0, 0, offset + 2000, EntityType.ACCOUNT);
    TokenBalance tokenBalance = new TokenBalance();
    tokenBalance.setBalance(offset);
    tokenBalance.setId(new TokenBalance.Id(timestamp, accountId, tokenId));
    AccountBalance accountBalance = new AccountBalance();
    accountBalance.setBalance(offset);
    accountBalance.setId(new AccountBalance.Id(timestamp, accountId));
    accountBalance.setTokenBalances(List.of(tokenBalance, tokenBalance));
    return accountBalance;
}
Also used : EntityId(com.hedera.mirror.common.domain.entity.EntityId) AccountBalance(com.hedera.mirror.common.domain.balance.AccountBalance) TokenBalance(com.hedera.mirror.common.domain.balance.TokenBalance)

Example 2 with TokenBalance

use of com.hedera.mirror.common.domain.balance.TokenBalance in project hedera-mirror-node by hashgraph.

the class ProtoBalanceFileReaderTest method getExpectedAccountBalanceFile.

private AccountBalanceFile getExpectedAccountBalanceFile(StreamFileData streamFileData) {
    Instant instant = Instant.parse(TIMESTAMP.replace("_", ":"));
    long consensusTimestamp = DomainUtils.convertToNanosMax(instant);
    long accountNum = 2000;
    long hbarBalance = 3000;
    long tokenNum = 5000;
    long tokenBalance = 6000;
    List<AccountBalance> accountBalances = IntStream.range(0, 10).mapToObj(i -> {
        EntityId accountId = EntityId.of(0, 0, accountNum + i, EntityType.ACCOUNT);
        List<TokenBalance> tokenBalances = IntStream.range(0, 5).mapToObj(j -> {
            EntityId tokenId = EntityId.of(0, 0, tokenNum + i * 5 + j, EntityType.TOKEN);
            return new TokenBalance(tokenBalance + i * 5 + j, new TokenBalance.Id(consensusTimestamp, accountId, tokenId));
        }).collect(Collectors.toList());
        return new AccountBalance(hbarBalance + i, tokenBalances, new AccountBalance.Id(consensusTimestamp, accountId));
    }).collect(Collectors.toList());
    return AccountBalanceFile.builder().bytes(streamFileData.getBytes()).consensusTimestamp(consensusTimestamp).fileHash("67c2fd054621366dd5a37b6ee36a51bc590361379d539fdac2265af08cb8097729218c7d9ff1f1e354c85b820c5b8cf8").items(Flux.fromIterable(accountBalances)).name(streamFileData.getFilename()).build();
}
Also used : IntStream(java.util.stream.IntStream) BeforeEach(org.junit.jupiter.api.BeforeEach) EntityId(com.hedera.mirror.common.domain.entity.EntityId) Assertions.assertThat(org.assertj.core.api.Assertions.assertThat) AccountBalanceFile(com.hedera.mirror.common.domain.balance.AccountBalanceFile) EntityType(com.hedera.mirror.common.domain.entity.EntityType) AccountBalance(com.hedera.mirror.common.domain.balance.AccountBalance) TestUtils(com.hedera.mirror.importer.TestUtils) SingleAccountBalances(com.hedera.services.stream.proto.SingleAccountBalances) Timestamp(com.hederahashgraph.api.proto.java.Timestamp) UnknownFieldSet(com.google.protobuf.UnknownFieldSet) ValueSource(org.junit.jupiter.params.provider.ValueSource) DomainUtils(com.hedera.mirror.common.util.DomainUtils) Instant(java.time.Instant) TokenBalance(com.hedera.mirror.common.domain.balance.TokenBalance) Collectors(java.util.stream.Collectors) AllAccountBalances(com.hedera.services.stream.proto.AllAccountBalances) File(java.io.File) Test(org.junit.jupiter.api.Test) Flux(reactor.core.publisher.Flux) InvalidStreamFileException(com.hedera.mirror.importer.exception.InvalidStreamFileException) List(java.util.List) ParameterizedTest(org.junit.jupiter.params.ParameterizedTest) Paths(java.nio.file.Paths) Assertions(org.junit.jupiter.api.Assertions) StreamFileData(com.hedera.mirror.importer.domain.StreamFileData) EntityId(com.hedera.mirror.common.domain.entity.EntityId) AccountBalance(com.hedera.mirror.common.domain.balance.AccountBalance) Instant(java.time.Instant) List(java.util.List) TokenBalance(com.hedera.mirror.common.domain.balance.TokenBalance) EntityId(com.hedera.mirror.common.domain.entity.EntityId)

Example 3 with TokenBalance

use of com.hedera.mirror.common.domain.balance.TokenBalance in project hedera-mirror-node by hashgraph.

the class AccountBalanceLineParserV2Test method parse.

@DisplayName("Parse account balance line")
@ParameterizedTest(name = "from \"{0}\"")
@CsvSource(value = { "'0,0,123,700,CggKAxjsBxCEBwoHCgMY7QcQGQoKCgMY7gcQwKilBAoICgMY8gcQhAcKBwoDGPMHEBkKCgoDGPQHEMCopQQ';false;" + "0;123;700;1004=900,1005=25,1006=9000000,1010=900,1011=25,1012=9000000", "'0,0,123,700,';false;" + "0;123;700;;", "'0,0,123,700,CggKAxjsBxCEBwoHCgMY7QcQGQoKCgMY7gcQwKilBAoICgMY8gcQhAcKBwoDGPMHEBkKCgo';true;;;;", "' 0,0,123,700,CggKAxjsBxCEBwoHCgMY7QcQGQoKCgMY7gcQwKilBAoICgMY8gcQhAcKBwoDGPMHEBkKCgoDGPQHEMCopQQ';" + "false;0;123;700;1004=900,1005=25,1006=9000000,1010=900,1011=25,1012=9000000", "'0, 0,123,700,CggKAxjsBxCEBwoHCgMY7QcQGQoKCgMY7gcQwKilBAoICgMY8gcQhAcKBwoDGPMHEBkKCgoDGPQHEMCopQQ';" + "false;0;123;700;1004=900,1005=25,1006=9000000,1010=900,1011=25,1012=9000000", "'0,0, 123,700,CggKAxjsBxCEBwoHCgMY7QcQGQoKCgMY7gcQwKilBAoICgMY8gcQhAcKBwoDGPMHEBkKCgoDGPQHEMCopQQ';" + "false;0;123;700;1004=900,1005=25,1006=9000000,1010=900,1011=25,1012=9000000", "'0,0,123, 700,CggKAxjsBxCEBwoHCgMY7QcQGQoKCgMY7gcQwKilBAoICgMY8gcQhAcKBwoDGPMHEBkKCgoDGPQHEMCopQQ';" + "false;0;123;700;1004=900,1005=25,1006=9000000,1010=900,1011=25,1012=9000000", "'0,0,123,700, CggKAxjsBxCEBwoHCgMY7QcQGQoKCgMY7gcQwKilBAoICgMY8gcQhAcKBwoDGPMHEBkKCgoDGPQHEMCopQQ';" + "false;0;123;700;1004=900,1005=25,1006=9000000,1010=900,1011=25,1012=9000000", "'0,0,123,700,CggKAxjsBxCEBwoHCgMY7QcQGQoKCgMY7gcQwKilBAoICgMY8gcQhAcKBwoDGPMHEBkKCgoDGPQHEMCopQQ ';" + "false;0;123;700;1004=900,1005=25,1006=9000000,1010=900,1011=25,1012=9000000", "'1,0,123,700,';true;;;;", "'x,0,123,700,';true;;;;", "'0,x,123,700,';true;;;;", "'0,0,x,700,';true;;;;", "'0,0,123,a00,';true;;;;", "'1000000000000000000000000000,0,123,700,';true;;;;", "'0,1000000000000000000000000000,123,700,';true;;;;", "'0,0,1000000000000000000000000000,700,';true;;;;", "'0,0,123,1000000000000000000000000000,';true;;;;", "'-1,0,123,700,';true;;;;", "'0,-1,123,700,';true;;;;", "'0,0,-1,700,';true;;;;", "'0,0,123,-1,';true;;;;", "'foobar';true;;;;", "'';true;;;;", ";true;;;;" }, delimiter = ';')
void parse(String line, boolean expectThrow, Long expectedRealm, Long expectedAccount, Long expectedBalance, String tokenBalances) throws IOException {
    if (!expectThrow) {
        AccountBalance accountBalance = parser.parse(line, timestamp);
        var id = accountBalance.getId();
        assertThat(accountBalance.getBalance()).isEqualTo(expectedBalance);
        assertThat(id).isNotNull();
        assertThat(id.getAccountId().getRealmNum()).isEqualTo(expectedRealm);
        assertThat(id.getAccountId().getEntityNum()).isEqualTo(expectedAccount);
        assertThat(id.getConsensusTimestamp()).isEqualTo(timestamp);
        List<TokenBalance> actualTokenBalanceList = accountBalance.getTokenBalances();
        if (StringUtils.isNotBlank(tokenBalances)) {
            Map<Long, Long> expectedTokenBalances = Splitter.on(',').withKeyValueSeparator('=').split(tokenBalances).entrySet().stream().collect(Collectors.toMap(entry -> Long.parseLong(entry.getKey()), entry -> Long.parseLong(entry.getValue())));
            assertThat(actualTokenBalanceList.size()).isEqualTo(expectedTokenBalances.size());
            for (int i = 0; i < actualTokenBalanceList.size(); i++) {
                TokenBalance actualTokenBalance = actualTokenBalanceList.get(i);
                TokenBalance.Id actualId = actualTokenBalance.getId();
                assertThat(expectedTokenBalances).containsKey(actualId.getTokenId().getEntityNum());
                assertThat(actualTokenBalance.getBalance()).isEqualTo(expectedTokenBalances.get(actualId.getTokenId().getEntityNum()));
                assertThat(actualId).isNotNull();
                assertThat(actualId.getConsensusTimestamp()).isEqualTo(timestamp);
                assertThat(actualId.getAccountId().getShardNum()).isEqualTo(mirrorProperties.getShard());
                assertThat(actualId.getAccountId().getRealmNum()).isEqualTo(expectedRealm);
                assertThat(actualId.getAccountId().getEntityNum()).isEqualTo(expectedAccount);
                assertThat(actualId.getTokenId().getShardNum()).isEqualTo(mirrorProperties.getShard());
                assertThat(actualId.getTokenId().getRealmNum()).isEqualTo(expectedRealm);
                assertThat(actualId.getTokenId().getType()).isEqualTo(EntityType.TOKEN);
            }
        } else {
            assertThat(actualTokenBalanceList.size()).isEqualTo(0);
        }
    } else {
        assertThrows(InvalidDatasetException.class, () -> {
            parser.parse(line, timestamp);
        });
    }
}
Also used : BeforeEach(org.junit.jupiter.api.BeforeEach) CsvSource(org.junit.jupiter.params.provider.CsvSource) Assertions.assertThat(org.assertj.core.api.Assertions.assertThat) IOException(java.io.IOException) TokenBalance(com.hedera.mirror.common.domain.balance.TokenBalance) Collectors(java.util.stream.Collectors) EntityType(com.hedera.mirror.common.domain.entity.EntityType) StringUtils(org.apache.commons.lang3.StringUtils) DisplayName(org.junit.jupiter.api.DisplayName) Test(org.junit.jupiter.api.Test) List(java.util.List) ParameterizedTest(org.junit.jupiter.params.ParameterizedTest) AccountBalance(com.hedera.mirror.common.domain.balance.AccountBalance) MirrorProperties(com.hedera.mirror.importer.MirrorProperties) Map(java.util.Map) Assertions(org.junit.jupiter.api.Assertions) Splitter(com.google.common.base.Splitter) InvalidDatasetException(com.hedera.mirror.importer.exception.InvalidDatasetException) AccountBalance(com.hedera.mirror.common.domain.balance.AccountBalance) TokenBalance(com.hedera.mirror.common.domain.balance.TokenBalance) CsvSource(org.junit.jupiter.params.provider.CsvSource) DisplayName(org.junit.jupiter.api.DisplayName) ParameterizedTest(org.junit.jupiter.params.ParameterizedTest)

Example 4 with TokenBalance

use of com.hedera.mirror.common.domain.balance.TokenBalance in project hedera-mirror-node by hashgraph.

the class AccountBalanceRepositoryTest method createTokenBalances.

private List<TokenBalance> createTokenBalances(long consensusTimestamp, int accountNum, long balance, int numberOfBalances) {
    List<TokenBalance> tokenBalanceList = new ArrayList<>();
    for (int i = 1; i <= numberOfBalances; i++) {
        TokenBalance tokenBalance = new TokenBalance();
        TokenBalance.Id id = new TokenBalance.Id();
        id.setAccountId(EntityId.of(0, 0, accountNum, EntityType.ACCOUNT));
        id.setConsensusTimestamp(consensusTimestamp);
        id.setTokenId(EntityId.of(0, 1, i, EntityType.TOKEN));
        tokenBalance.setBalance(balance);
        tokenBalance.setId(id);
        tokenBalanceList.add(tokenBalance);
    }
    return tokenBalanceList;
}
Also used : ArrayList(java.util.ArrayList) TokenBalance(com.hedera.mirror.common.domain.balance.TokenBalance) EntityId(com.hedera.mirror.common.domain.entity.EntityId)

Example 5 with TokenBalance

use of com.hedera.mirror.common.domain.balance.TokenBalance in project hedera-mirror-node by hashgraph.

the class TokenBalanceRepositoryTest method create.

private TokenBalance create(long consensusTimestamp, int accountNum, int tokenNum, long balance) {
    TokenBalance tokenBalance = new TokenBalance();
    TokenBalance.Id id = new TokenBalance.Id();
    id.setAccountId(EntityId.of(0, 0, accountNum, EntityType.ACCOUNT));
    id.setConsensusTimestamp(consensusTimestamp);
    id.setTokenId(EntityId.of(0, 0, tokenNum, EntityType.TOKEN));
    tokenBalance.setBalance(balance);
    tokenBalance.setId(id);
    return tokenBalanceRepository.save(tokenBalance);
}
Also used : TokenBalance(com.hedera.mirror.common.domain.balance.TokenBalance) EntityId(com.hedera.mirror.common.domain.entity.EntityId)

Aggregations

TokenBalance (com.hedera.mirror.common.domain.balance.TokenBalance)10 AccountBalance (com.hedera.mirror.common.domain.balance.AccountBalance)6 EntityId (com.hedera.mirror.common.domain.entity.EntityId)6 List (java.util.List)4 AccountBalanceFile (com.hedera.mirror.common.domain.balance.AccountBalanceFile)3 Instant (java.time.Instant)3 ArrayList (java.util.ArrayList)3 Collectors (java.util.stream.Collectors)3 Test (org.junit.jupiter.api.Test)3 UnknownFieldSet (com.google.protobuf.UnknownFieldSet)2 EntityType (com.hedera.mirror.common.domain.entity.EntityType)2 DomainUtils (com.hedera.mirror.common.util.DomainUtils)2 StreamFileData (com.hedera.mirror.importer.domain.StreamFileData)2 InvalidDatasetException (com.hedera.mirror.importer.exception.InvalidDatasetException)2 InvalidStreamFileException (com.hedera.mirror.importer.exception.InvalidStreamFileException)2 SingleAccountBalances (com.hedera.services.stream.proto.SingleAccountBalances)2 Timestamp (com.hederahashgraph.api.proto.java.Timestamp)2 IOException (java.io.IOException)2 Map (java.util.Map)2 Named (javax.inject.Named)2