Search in sources :

Example 11 with Token

use of com.hedera.mirror.common.domain.token.Token in project hedera-mirror-node by hashgraph.

the class EntityRecordItemListener method updateTokenSupply.

private void updateTokenSupply(EntityId tokenId, long newTotalSupply, long modifiedTimestamp) {
    Token token = Token.of(tokenId);
    token.setTotalSupply(newTotalSupply);
    updateToken(token, modifiedTimestamp);
}
Also used : Token(com.hedera.mirror.common.domain.token.Token)

Example 12 with Token

use of com.hedera.mirror.common.domain.token.Token in project hedera-mirror-node by hashgraph.

the class EntityRecordItemListener method insertFungibleTokenTransfers.

private void insertFungibleTokenTransfers(long consensusTimestamp, TransactionBody body, boolean isTokenDissociate, TokenID tokenId, EntityId entityTokenId, EntityId payerAccountId, List<AccountAmount> tokenTransfers) {
    for (AccountAmount accountAmount : tokenTransfers) {
        EntityId accountId = EntityId.of(accountAmount.getAccountID());
        long amount = accountAmount.getAmount();
        TokenTransfer tokenTransfer = new TokenTransfer();
        tokenTransfer.setAmount(amount);
        tokenTransfer.setId(new TokenTransfer.Id(consensusTimestamp, entityTokenId, accountId));
        tokenTransfer.setIsApproval(false);
        tokenTransfer.setPayerAccountId(payerAccountId);
        tokenTransfer.setTokenDissociate(isTokenDissociate);
        // then again set is_approval=true
        if (amount < 0) {
            // Is the accountAmount from the record also inside a body's transfer list for the given tokenId?
            AccountAmount accountAmountInsideTransferList = findAccountAmount(accountAmount::equals, tokenId, body);
            if (accountAmountInsideTransferList == null) {
                // Is there any account amount inside the body's transfer list for the given tokenId
                // with the same accountId as the accountAmount from the record?
                AccountAmount accountAmountWithSameIdInsideBody = findAccountAmount(aa -> aa.getAccountID().equals(accountAmount.getAccountID()) && aa.getIsApproval(), tokenId, body);
                if (accountAmountWithSameIdInsideBody != null) {
                    tokenTransfer.setIsApproval(true);
                }
            } else {
                tokenTransfer.setIsApproval(accountAmountInsideTransferList.getIsApproval());
            }
        }
        entityListener.onTokenTransfer(tokenTransfer);
        if (isTokenDissociate) {
            // token transfers in token dissociate are for deleted tokens and the amount is negative to
            // bring the account's balance of the token to 0. Set the totalSupply of the token object to the
            // negative amount, later in the pipeline the token total supply will be reduced accordingly
            Token token = Token.of(entityTokenId);
            token.setModifiedTimestamp(consensusTimestamp);
            token.setTotalSupply(accountAmount.getAmount());
            entityListener.onToken(token);
        }
    }
}
Also used : EntityId(com.hedera.mirror.common.domain.entity.EntityId) Token(com.hedera.mirror.common.domain.token.Token) TokenTransfer(com.hedera.mirror.common.domain.token.TokenTransfer) AccountAmount(com.hederahashgraph.api.proto.java.AccountAmount)

Example 13 with Token

use of com.hedera.mirror.common.domain.token.Token in project hedera-mirror-node by hashgraph.

the class EntityRecordItemListener method insertTokenCreate.

private void insertTokenCreate(RecordItem recordItem) {
    if (!entityProperties.getPersist().isTokens()) {
        return;
    }
    // pull token details from TokenCreation body and TokenId from receipt
    TokenCreateTransactionBody tokenCreateTransactionBody = recordItem.getTransactionBody().getTokenCreation();
    long consensusTimestamp = recordItem.getConsensusTimestamp();
    EntityId tokenId = EntityId.of(recordItem.getRecord().getReceipt().getTokenID());
    EntityId treasury = EntityId.of(tokenCreateTransactionBody.getTreasury());
    Token token = new Token();
    token.setCreatedTimestamp(consensusTimestamp);
    token.setDecimals(tokenCreateTransactionBody.getDecimals());
    token.setFreezeDefault(tokenCreateTransactionBody.getFreezeDefault());
    token.setInitialSupply(tokenCreateTransactionBody.getInitialSupply());
    token.setMaxSupply(tokenCreateTransactionBody.getMaxSupply());
    token.setModifiedTimestamp(consensusTimestamp);
    token.setName(tokenCreateTransactionBody.getName());
    token.setSupplyType(TokenSupplyTypeEnum.fromId(tokenCreateTransactionBody.getSupplyTypeValue()));
    token.setSymbol(tokenCreateTransactionBody.getSymbol());
    token.setTokenId(new TokenId(tokenId));
    token.setTotalSupply(tokenCreateTransactionBody.getInitialSupply());
    token.setTreasuryAccountId(treasury);
    token.setType(TokenTypeEnum.fromId(tokenCreateTransactionBody.getTokenTypeValue()));
    if (tokenCreateTransactionBody.hasFeeScheduleKey()) {
        token.setFeeScheduleKey(tokenCreateTransactionBody.getFeeScheduleKey().toByteArray());
    }
    if (tokenCreateTransactionBody.hasFreezeKey()) {
        token.setFreezeKey(tokenCreateTransactionBody.getFreezeKey().toByteArray());
    }
    if (tokenCreateTransactionBody.hasKycKey()) {
        token.setKycKey(tokenCreateTransactionBody.getKycKey().toByteArray());
    }
    if (tokenCreateTransactionBody.hasPauseKey()) {
        token.setPauseKey(tokenCreateTransactionBody.getPauseKey().toByteArray());
        token.setPauseStatus(TokenPauseStatusEnum.UNPAUSED);
    } else {
        token.setPauseStatus(TokenPauseStatusEnum.NOT_APPLICABLE);
    }
    if (tokenCreateTransactionBody.hasSupplyKey()) {
        token.setSupplyKey(tokenCreateTransactionBody.getSupplyKey().toByteArray());
    }
    if (tokenCreateTransactionBody.hasWipeKey()) {
        token.setWipeKey(tokenCreateTransactionBody.getWipeKey().toByteArray());
    }
    Set<EntityId> autoAssociatedAccounts = insertCustomFees(tokenCreateTransactionBody.getCustomFeesList(), consensusTimestamp, true, tokenId);
    autoAssociatedAccounts.add(treasury);
    if (recordItem.getRecord().getAutomaticTokenAssociationsCount() > 0) {
        // automatic_token_associations does not exist prior to services 0.18.0
        autoAssociatedAccounts.clear();
        recordItem.getRecord().getAutomaticTokenAssociationsList().stream().map(TokenAssociation::getAccountId).map(EntityId::of).forEach(autoAssociatedAccounts::add);
    }
    TokenFreezeStatusEnum freezeStatus = token.getFreezeKey() != null ? TokenFreezeStatusEnum.UNFROZEN : TokenFreezeStatusEnum.NOT_APPLICABLE;
    TokenKycStatusEnum kycStatus = token.getKycKey() != null ? TokenKycStatusEnum.GRANTED : TokenKycStatusEnum.NOT_APPLICABLE;
    autoAssociatedAccounts.forEach(account -> {
        TokenAccount tokenAccount = getAssociatedTokenAccount(account, false, consensusTimestamp, freezeStatus, kycStatus, tokenId);
        entityListener.onTokenAccount(tokenAccount);
    });
    entityListener.onToken(token);
}
Also used : EntityId(com.hedera.mirror.common.domain.entity.EntityId) TokenCreateTransactionBody(com.hederahashgraph.api.proto.java.TokenCreateTransactionBody) TokenAssociation(com.hederahashgraph.api.proto.java.TokenAssociation) TokenKycStatusEnum(com.hedera.mirror.common.domain.token.TokenKycStatusEnum) TokenAccount(com.hedera.mirror.common.domain.token.TokenAccount) Token(com.hedera.mirror.common.domain.token.Token) TokenId(com.hedera.mirror.common.domain.token.TokenId) TokenFreezeStatusEnum(com.hedera.mirror.common.domain.token.TokenFreezeStatusEnum)

Example 14 with Token

use of com.hedera.mirror.common.domain.token.Token in project hedera-mirror-node by hashgraph.

the class BatchUpserterTest method tokenUpdateNegativeTotalSupply.

@Test
void tokenUpdateNegativeTotalSupply() {
    // given
    Token token = getToken("0.0.2000", "0.0.1001", 3L);
    tokenRepository.save(token);
    // when
    Token update = new Token();
    update.setModifiedTimestamp(8L);
    update.setTokenId(token.getTokenId());
    update.setTotalSupply(-50L);
    persist(batchPersister, List.of(update));
    // then
    token.setTotalSupply(token.getTotalSupply() - 50L);
    token.setModifiedTimestamp(8L);
    assertThat(tokenRepository.findAll()).containsOnly(token);
}
Also used : Token(com.hedera.mirror.common.domain.token.Token) Test(org.junit.jupiter.api.Test) IntegrationTest(com.hedera.mirror.importer.IntegrationTest)

Example 15 with Token

use of com.hedera.mirror.common.domain.token.Token in project hedera-mirror-node by hashgraph.

the class BatchUpserterTest method getDeletedNftClass.

private Token getDeletedNftClass(long createdTimestamp, long deletedTimestamp, EntityId tokenId) {
    Token token = Token.of(tokenId);
    token.setCreatedTimestamp(createdTimestamp);
    token.setDecimals(0);
    token.setFreezeDefault(false);
    token.setInitialSupply(0L);
    token.setModifiedTimestamp(deletedTimestamp);
    token.setName("foo");
    token.setPauseStatus(TokenPauseStatusEnum.NOT_APPLICABLE);
    token.setSupplyType(TokenSupplyTypeEnum.FINITE);
    token.setSymbol("bar");
    token.setTotalSupply(200L);
    token.setTreasuryAccountId(EntityId.of("0.0.200", EntityType.ACCOUNT));
    token.setType(TokenTypeEnum.NON_FUNGIBLE_UNIQUE);
    return token;
}
Also used : Token(com.hedera.mirror.common.domain.token.Token)

Aggregations

Token (com.hedera.mirror.common.domain.token.Token)33 Test (org.junit.jupiter.api.Test)20 EntityId (com.hedera.mirror.common.domain.entity.EntityId)18 IntegrationTest (com.hedera.mirror.importer.IntegrationTest)18 ParameterizedTest (org.junit.jupiter.params.ParameterizedTest)15 TokenAccount (com.hedera.mirror.common.domain.token.TokenAccount)9 ByteString (com.google.protobuf.ByteString)6 Nft (com.hedera.mirror.common.domain.token.Nft)6 TokenId (com.hedera.mirror.common.domain.token.TokenId)6 TokenTransfer (com.hedera.mirror.common.domain.token.TokenTransfer)3 SneakyThrows (lombok.SneakyThrows)3 TokenFreezeStatusEnum (com.hedera.mirror.common.domain.token.TokenFreezeStatusEnum)2 TokenKycStatusEnum (com.hedera.mirror.common.domain.token.TokenKycStatusEnum)2 ArrayList (java.util.ArrayList)2 DomainBuilder (com.hedera.mirror.common.domain.DomainBuilder)1 Contract (com.hedera.mirror.common.domain.contract.Contract)1 CryptoAllowance (com.hedera.mirror.common.domain.entity.CryptoAllowance)1 Entity (com.hedera.mirror.common.domain.entity.Entity)1 EntityType (com.hedera.mirror.common.domain.entity.EntityType)1 ACCOUNT (com.hedera.mirror.common.domain.entity.EntityType.ACCOUNT)1