Search in sources :

Example 16 with AccountAmount

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

the class UsageBasedFeeCalculator method estimatedNonFeePayerAdjustments.

@Override
public long estimatedNonFeePayerAdjustments(TxnAccessor accessor, Timestamp at) {
    switch(accessor.getFunction()) {
        case CryptoCreate:
            var cryptoCreateOp = accessor.getTxn().getCryptoCreateAccount();
            return -cryptoCreateOp.getInitialBalance();
        case CryptoTransfer:
            var payer = accessor.getPayer();
            var cryptoTransferOp = accessor.getTxn().getCryptoTransfer();
            var adjustments = cryptoTransferOp.getTransfers().getAccountAmountsList();
            long cryptoTransferNet = 0L;
            for (AccountAmount adjustment : adjustments) {
                if (payer.equals(adjustment.getAccountID())) {
                    cryptoTransferNet += adjustment.getAmount();
                }
            }
            return cryptoTransferNet;
        case ContractCreate:
            var contractCreateOp = accessor.getTxn().getContractCreateInstance();
            return -contractCreateOp.getInitialBalance() - contractCreateOp.getGas() * estimatedGasPriceInTinybars(ContractCreate, at);
        case ContractCall:
            var contractCallOp = accessor.getTxn().getContractCall();
            return -contractCallOp.getAmount() - contractCallOp.getGas() * estimatedGasPriceInTinybars(ContractCall, at);
        default:
            return 0L;
    }
}
Also used : AccountAmount(com.hederahashgraph.api.proto.java.AccountAmount)

Example 17 with AccountAmount

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

the class UtilVerbs method validateTransferListForBalances.

public static HapiSpecOperation validateTransferListForBalances(List<String> txns, List<String> accounts, Set<String> wereDeleted) {
    return assertionsHold((spec, assertLog) -> {
        Map<String, Long> actualBalances = accounts.stream().collect(Collectors.toMap((String account) -> asAccountString(spec.registry().getAccountID(account)), (String account) -> {
            if (wereDeleted.contains(account)) {
                return 0L;
            }
            long balance = -1L;
            try {
                BalanceSnapshot preOp = balanceSnapshot("x", account);
                allRunFor(spec, preOp);
                balance = spec.registry().getBalanceSnapshot("x");
            } catch (Throwable ignore) {
            }
            return balance;
        }));
        List<AccountAmount> transfers = new ArrayList<>();
        for (String txn : txns) {
            HapiGetTxnRecord subOp = getTxnRecord(txn).logged().payingWith(EXCHANGE_RATE_CONTROL);
            allRunFor(spec, subOp);
            TransactionRecord record = subOp.getResponse().getTransactionGetRecord().getTransactionRecord();
            transfers.addAll(record.getTransferList().getAccountAmountsList());
        }
        Map<String, Long> changes = changesAccordingTo(transfers);
        assertLog.info("Balance changes according to transfer list: " + changes);
        changes.entrySet().forEach(change -> {
            String account = change.getKey();
            long oldBalance = -1L;
            /* The account/contract may have just been created, no snapshot was taken. */
            try {
                oldBalance = spec.registry().getBalanceSnapshot(account + "Snapshot");
            } catch (Throwable ignore) {
            }
            long expectedBalance = change.getValue() + Math.max(0L, oldBalance);
            long actualBalance = actualBalances.getOrDefault(account, -1L);
            assertLog.info("Balance of " + account + " was expected to be " + expectedBalance + ", is actually " + actualBalance + "...");
            Assertions.assertEquals(expectedBalance, actualBalance, "New balance for " + account + " should be " + expectedBalance + " tinyBars.");
        });
    });
}
Also used : HapiGetTxnRecord(com.hedera.services.bdd.spec.queries.meta.HapiGetTxnRecord) OptionalLong(java.util.OptionalLong) AtomicLong(java.util.concurrent.atomic.AtomicLong) ArrayList(java.util.ArrayList) HapiPropertySource.asAccountString(com.hedera.services.bdd.spec.HapiPropertySource.asAccountString) ByteString(com.google.protobuf.ByteString) TransactionRecord(com.hederahashgraph.api.proto.java.TransactionRecord) AccountAmount(com.hederahashgraph.api.proto.java.AccountAmount)

Example 18 with AccountAmount

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

the class PubSubMessageTest method testSerializationAllFieldsSet.

@Test
void testSerializationAllFieldsSet() throws Exception {
    Iterable<AccountAmount> nonFeeTransfers = Lists.newArrayList(AccountAmount.newBuilder().setAccountID(ACCOUNT_ID).setAmount(INT64_VALUE).build(), AccountAmount.newBuilder().setAccountID(ACCOUNT_ID).setAmount(INT64_VALUE).build());
    PubSubMessage pubSubMessage = new PubSubMessage(DEFAULT_TIMESTAMP_LONG, EntityId.of(TOPIC_ID), 10, new PubSubMessage.Transaction(getTransactionBody(), getSignatureMap()), getTransactionRecord(), nonFeeTransfers);
    ObjectMapper objectMapper = new ObjectMapper();
    String actual = objectMapper.writeValueAsString(pubSubMessage);
    String expected = "{" + "  \"consensusTimestamp\" : 123456789," + "  \"entity\" : {" + "    \"shardNum\" : 0," + "    \"realmNum\" : 0," + "    \"entityNum\" : 20," + "    \"type\" : 4" + "  }," + "  \"transactionType\" : 10," + getExpectedTransactionJson() + "," + getExpectedTransactionRecord() + "," + "  \"nonFeeTransfers\" : [ {" + "    \"accountID\": {" + "      \"shardNum\": \"0\"," + "      \"realmNum\": \"0\"," + "      \"accountNum\": \"10\"" + "      }," + "    \"amount\": \"100000000\"," + "    \"isApproval\": false" + "  }, {" + "    \"accountID\": {" + "      \"shardNum\": \"0\"," + "      \"realmNum\": \"0\"," + "      \"accountNum\": \"10\"" + "      }," + "    \"amount\": \"100000000\"," + "    \"isApproval\": false" + "  } ]" + "}";
    JSONAssert.assertEquals(expected, actual, true);
}
Also used : ByteString(com.google.protobuf.ByteString) ObjectMapper(com.fasterxml.jackson.databind.ObjectMapper) AccountAmount(com.hederahashgraph.api.proto.java.AccountAmount) Test(org.junit.jupiter.api.Test)

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