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);
}
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;
}
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;
}
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;
}
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);
}
Aggregations