Search in sources :

Example 1 with AccountAmount

use of com.hederahashgraph.api.proto.java.AccountAmount in project hedera-services by hashgraph.

the class RequestBuilder method getTokenTransferRequestToAlias.

public static Transaction getTokenTransferRequestToAlias(Long payerAccountNum, Long payerRealmNum, Long payerShardNum, Long nodeAccountNum, Long nodeRealmNum, Long nodeShardNum, long transactionFee, Timestamp timestamp, Duration transactionDuration, boolean generateRecord, String memo, Long senderActNum, Long tokenNum, Long amountSend, ByteString receivingAlias, Long amountReceived) {
    AccountAmount a1 = AccountAmount.newBuilder().setAccountID(getAccountIdBuild(senderActNum, 0l, 0l)).setAmount(amountSend).build();
    AccountAmount a2 = AccountAmount.newBuilder().setAccountID(getAccountIdBuild(receivingAlias, 0l, 0l)).setAmount(amountReceived).build();
    NftTransfer a3 = NftTransfer.newBuilder().setReceiverAccountID(AccountID.newBuilder().setAlias(receivingAlias).build()).setSenderAccountID(getAccountIdBuild(senderActNum, 0l, 0l)).setSerialNumber(1).build();
    TokenTransferList tokenTransferList = TokenTransferList.newBuilder().setToken(TokenID.newBuilder().setTokenNum(tokenNum).build()).addTransfers(a1).addTransfers(a2).addNftTransfers(a3).build();
    return getTokenTransferRequest(payerAccountNum, payerRealmNum, payerShardNum, nodeAccountNum, nodeRealmNum, nodeShardNum, transactionFee, timestamp, transactionDuration, generateRecord, memo, tokenTransferList);
}
Also used : NftTransfer(com.hederahashgraph.api.proto.java.NftTransfer) TokenTransferList(com.hederahashgraph.api.proto.java.TokenTransferList) AccountAmount(com.hederahashgraph.api.proto.java.AccountAmount)

Example 2 with AccountAmount

use of com.hederahashgraph.api.proto.java.AccountAmount in project hedera-services by hashgraph.

the class QueryFeeCheck method validateQueryPaymentTransfers.

/**
 * Validates query payment transfer transaction before reaching consensus.
 * Validate each payer has enough balance that is needed for transfer.
 * If one of the payer for query is also paying transactionFee validate the payer has balance to pay both
 *
 * @param txn the transaction body to validate
 * @return the corresponding {@link ResponseCodeEnum} after the validation
 */
public ResponseCodeEnum validateQueryPaymentTransfers(TransactionBody txn) {
    AccountID transactionPayer = txn.getTransactionID().getAccountID();
    TransferList transferList = txn.getCryptoTransfer().getTransfers();
    List<AccountAmount> transfers = transferList.getAccountAmountsList();
    long transactionFee = txn.getTransactionFee();
    final var currentAccounts = accounts.get();
    ResponseCodeEnum status;
    for (AccountAmount accountAmount : transfers) {
        var id = accountAmount.getAccountID();
        long amount = accountAmount.getAmount();
        if (amount < 0) {
            amount = -1 * amount;
            if (id.equals(transactionPayer)) {
                try {
                    amount = Math.addExact(amount, transactionFee);
                } catch (ArithmeticException e) {
                    return INSUFFICIENT_PAYER_BALANCE;
                }
            }
            if ((status = balanceCheck(currentAccounts.get(fromAccountId(id)), amount)) != OK) {
                return status;
            }
        }
    }
    return OK;
}
Also used : ResponseCodeEnum(com.hederahashgraph.api.proto.java.ResponseCodeEnum) AccountID(com.hederahashgraph.api.proto.java.AccountID) TransferList(com.hederahashgraph.api.proto.java.TransferList) AccountAmount(com.hederahashgraph.api.proto.java.AccountAmount)

Example 3 with AccountAmount

use of com.hederahashgraph.api.proto.java.AccountAmount in project hedera-services by hashgraph.

the class ScheduleExecutionSpecs method transferListCheck.

private boolean transferListCheck(HapiGetTxnRecord triggered, AccountID givingAccountID, AccountID receivingAccountID, AccountID payingAccountID, Long amount) {
    AccountAmount givingAmount = AccountAmount.newBuilder().setAccountID(givingAccountID).setAmount(-amount).build();
    AccountAmount receivingAmount = AccountAmount.newBuilder().setAccountID(receivingAccountID).setAmount(amount).build();
    var accountAmountList = triggered.getResponseRecord().getTransferList().getAccountAmountsList();
    boolean payerHasPaid = accountAmountList.stream().anyMatch(a -> a.getAccountID().equals(payingAccountID) && a.getAmount() < 0);
    boolean amountHasBeenTransferred = accountAmountList.contains(givingAmount) && accountAmountList.contains(receivingAmount);
    return amountHasBeenTransferred && payerHasPaid;
}
Also used : AccountAmount(com.hederahashgraph.api.proto.java.AccountAmount)

Example 4 with AccountAmount

use of com.hederahashgraph.api.proto.java.AccountAmount in project hedera-services by hashgraph.

the class HapiCryptoTransfer method fullyAggregateTokenTransfersList.

private Map<TokenID, Pair<Integer, List<AccountAmount>>> fullyAggregateTokenTransfersList(final HapiApiSpec spec) {
    Map<TokenID, Pair<Integer, List<AccountAmount>>> map = new HashMap<>();
    for (TokenMovement xfer : tokenAwareProviders) {
        if (xfer.isFungibleToken()) {
            var list = xfer.specializedFor(spec);
            if (map.containsKey(list.getToken())) {
                var existingVal = map.get(list.getToken());
                List<AccountAmount> newList = Stream.of(existingVal.getRight(), list.getTransfersList()).flatMap(Collection::stream).collect(Collectors.toList());
                map.put(list.getToken(), Pair.of(existingVal.getLeft(), aggregateTransfers(newList)));
            } else {
                map.put(list.getToken(), Pair.of(list.getExpectedDecimals().getValue(), aggregateTransfers(list.getTransfersList())));
            }
        }
    }
    return map;
}
Also used : TokenMovement(com.hedera.services.bdd.spec.transactions.token.TokenMovement) HashMap(java.util.HashMap) LinkedHashMap(java.util.LinkedHashMap) TokenID(com.hederahashgraph.api.proto.java.TokenID) Pair(org.apache.commons.lang3.tuple.Pair) AccountAmount(com.hederahashgraph.api.proto.java.AccountAmount)

Example 5 with AccountAmount

use of com.hederahashgraph.api.proto.java.AccountAmount in project hedera-mirror-node by hashgraph.

the class EntityRecordItemListenerCryptoTest method groupCryptoTransfersByAccountId.

private void groupCryptoTransfersByAccountId(TransactionRecord.Builder recordBuilder, List<AccountAmount.Builder> amountsToBeAdded) {
    var accountAmounts = recordBuilder.getTransferListBuilder().getAccountAmountsBuilderList();
    var transfers = new HashMap<AccountID, Long>();
    Stream.concat(accountAmounts.stream(), amountsToBeAdded.stream()).forEach(accountAmount -> transfers.compute(accountAmount.getAccountID(), (k, v) -> {
        long currentValue = (v == null) ? 0 : v;
        return currentValue + accountAmount.getAmount();
    }));
    TransferList.Builder transferListBuilder = TransferList.newBuilder();
    transfers.entrySet().forEach(entry -> {
        AccountAmount accountAmount = AccountAmount.newBuilder().setAccountID(entry.getKey()).setAmount(entry.getValue()).build();
        transferListBuilder.addAccountAmounts(accountAmount);
    });
    recordBuilder.setTransferList(transferListBuilder);
}
Also used : BeforeEach(org.junit.jupiter.api.BeforeEach) Arrays(java.util.Arrays) CryptoAllowanceRepository(com.hedera.mirror.importer.repository.CryptoAllowanceRepository) EntityId(com.hedera.mirror.common.domain.entity.EntityId) NonFeeTransfer(com.hedera.mirror.common.domain.transaction.NonFeeTransfer) ContractRepository(com.hedera.mirror.importer.repository.ContractRepository) Assertions.assertThat(org.assertj.core.api.Assertions.assertThat) StringValue(com.google.protobuf.StringValue) Duration(com.hederahashgraph.api.proto.java.Duration) RecordParserProperties(com.hedera.mirror.importer.parser.record.RecordParserProperties) UtilityTest(com.hedera.mirror.importer.util.UtilityTest) CryptoDeleteLiveHashTransactionBody(com.hederahashgraph.api.proto.java.CryptoDeleteLiveHashTransactionBody) KeyList(com.hederahashgraph.api.proto.java.KeyList) NftAllowanceRepository(com.hedera.mirror.importer.repository.NftAllowanceRepository) TestUtils(com.hedera.mirror.importer.TestUtils) Utility(com.hedera.mirror.importer.util.Utility) RecordItemBuilder(com.hedera.mirror.importer.parser.domain.RecordItemBuilder) CryptoCreateTransactionBody(com.hederahashgraph.api.proto.java.CryptoCreateTransactionBody) CryptoUpdateTransactionBody(com.hederahashgraph.api.proto.java.CryptoUpdateTransactionBody) Collection(java.util.Collection) Resource(javax.annotation.Resource) CryptoTransfer(com.hedera.mirror.common.domain.transaction.CryptoTransfer) Instant(java.time.Instant) Collectors(java.util.stream.Collectors) String.format(java.lang.String.format) ByteString(com.google.protobuf.ByteString) Test(org.junit.jupiter.api.Test) AccountAmount(com.hederahashgraph.api.proto.java.AccountAmount) List(java.util.List) Nft(com.hedera.mirror.common.domain.token.Nft) RealmID(com.hederahashgraph.api.proto.java.RealmID) Stream(java.util.stream.Stream) ShardID(com.hederahashgraph.api.proto.java.ShardID) NftRemoveAllowance(com.hederahashgraph.api.proto.java.NftRemoveAllowance) StakingRewardTransfer(com.hedera.mirror.common.domain.transaction.StakingRewardTransfer) Optional(java.util.Optional) AccountIdConverter(com.hedera.mirror.common.converter.AccountIdConverter) RecordItem(com.hedera.mirror.common.domain.transaction.RecordItem) ErrataType(com.hedera.mirror.common.domain.transaction.ErrataType) Transaction(com.hederahashgraph.api.proto.java.Transaction) TransactionRecord(com.hederahashgraph.api.proto.java.TransactionRecord) NftId(com.hedera.mirror.common.domain.token.NftId) CsvSource(org.junit.jupiter.params.provider.CsvSource) Contract(com.hedera.mirror.common.domain.contract.Contract) PartialDataAction(com.hedera.mirror.importer.parser.PartialDataAction) BoolValue(com.google.protobuf.BoolValue) HashMap(java.util.HashMap) EnumSource(org.junit.jupiter.params.provider.EnumSource) NftAllowance(com.hederahashgraph.api.proto.java.NftAllowance) TransactionBody(com.hederahashgraph.api.proto.java.TransactionBody) SignedTransaction(com.hederahashgraph.api.proto.java.SignedTransaction) AliasNotFoundException(com.hedera.mirror.importer.exception.AliasNotFoundException) LinkedList(java.util.LinkedList) AccountID(com.hederahashgraph.api.proto.java.AccountID) LiveHash(com.hedera.mirror.common.domain.transaction.LiveHash) Timestamp(com.hederahashgraph.api.proto.java.Timestamp) TransferList(com.hederahashgraph.api.proto.java.TransferList) NftRepository(com.hedera.mirror.importer.repository.NftRepository) TokenAllowanceRepository(com.hedera.mirror.importer.repository.TokenAllowanceRepository) Int32Value(com.google.protobuf.Int32Value) IterableAssert(org.assertj.core.api.IterableAssert) DomainUtils(com.hedera.mirror.common.util.DomainUtils) ResponseCodeEnum(com.hederahashgraph.api.proto.java.ResponseCodeEnum) TransactionID(com.hederahashgraph.api.proto.java.TransactionID) DisplayName(org.junit.jupiter.api.DisplayName) Entity(com.hedera.mirror.common.domain.entity.Entity) Consumer(java.util.function.Consumer) ParameterizedTest(org.junit.jupiter.params.ParameterizedTest) TokenID(com.hederahashgraph.api.proto.java.TokenID) Assertions(org.junit.jupiter.api.Assertions) Condition(org.assertj.core.api.Condition) CryptoAddLiveHashTransactionBody(com.hederahashgraph.api.proto.java.CryptoAddLiveHashTransactionBody) HashMap(java.util.HashMap) TransferList(com.hederahashgraph.api.proto.java.TransferList) AccountAmount(com.hederahashgraph.api.proto.java.AccountAmount)

Aggregations

AccountAmount (com.hederahashgraph.api.proto.java.AccountAmount)18 TransferList (com.hederahashgraph.api.proto.java.TransferList)5 ByteString (com.google.protobuf.ByteString)4 EntityId (com.hedera.mirror.common.domain.entity.EntityId)4 TokenTransferList (com.hederahashgraph.api.proto.java.TokenTransferList)4 CryptoTransfer (com.hedera.mirror.common.domain.transaction.CryptoTransfer)3 RecordItem (com.hedera.mirror.common.domain.transaction.RecordItem)2 TokenMovement (com.hedera.services.bdd.spec.transactions.token.TokenMovement)2 AccountID (com.hederahashgraph.api.proto.java.AccountID)2 ResponseCodeEnum (com.hederahashgraph.api.proto.java.ResponseCodeEnum)2 SignedTransaction (com.hederahashgraph.api.proto.java.SignedTransaction)2 TokenID (com.hederahashgraph.api.proto.java.TokenID)2 Transaction (com.hederahashgraph.api.proto.java.Transaction)2 ArrayList (java.util.ArrayList)2 HashMap (java.util.HashMap)2 AtomicLong (java.util.concurrent.atomic.AtomicLong)2 Test (org.junit.jupiter.api.Test)2 ObjectMapper (com.fasterxml.jackson.databind.ObjectMapper)1 BoolValue (com.google.protobuf.BoolValue)1 Int32Value (com.google.protobuf.Int32Value)1