use of com.hedera.mirror.common.domain.entity.EntityId in project hedera-mirror-node by hashgraph.
the class ProtoBalanceFileReader method toAccountBalance.
private AccountBalance toAccountBalance(long consensusTimestamp, SingleAccountBalances balances) {
EntityId accountId = EntityId.of(balances.getAccountID());
List<TokenBalance> tokenBalances = balances.getTokenUnitBalancesList().stream().map(tokenBalance -> {
EntityId tokenId = EntityId.of(tokenBalance.getTokenId());
TokenBalance.Id id = new TokenBalance.Id(consensusTimestamp, accountId, tokenId);
return new TokenBalance(tokenBalance.getBalance(), id);
}).collect(Collectors.toList());
return new AccountBalance(balances.getHbarBalance(), tokenBalances, new AccountBalance.Id(consensusTimestamp, accountId));
}
use of com.hedera.mirror.common.domain.entity.EntityId in project hedera-mirror-node by hashgraph.
the class AccountBalanceLineParserV2 method parse.
/**
* Parses an account balance line to extract shard, realm, account, balance, and token balances. If the shard
* matches systemShardNum, creates and returns an {@code AccountBalance} entity object. The account balance line
* should be in the format of "shard,realm,account,balance"
*
* @param line The account balance line
* @param consensusTimestamp The consensus timestamp of the account balance line
* @return {@code AccountBalance} entity object
* @throws InvalidDatasetException if the line is malformed or the shard does not match {@code systemShardNum}
*/
@Override
public AccountBalance parse(String line, long consensusTimestamp) {
try {
if (line == null) {
throw new InvalidDatasetException("Null line cannot be parsed");
}
List<String> parts = SPLITTER.splitToList(line);
boolean hasTokenBalance;
if (parts.size() == 5) {
hasTokenBalance = true;
} else if (parts.size() == 4) {
hasTokenBalance = false;
} else {
throw new InvalidDatasetException("Invalid account balance line: " + line);
}
long shardNum = Long.parseLong(parts.get(0));
int realmNum = Integer.parseInt(parts.get(1));
int accountNum = Integer.parseInt(parts.get(2));
long balance = Long.parseLong(parts.get(3));
if (shardNum < 0 || realmNum < 0 || accountNum < 0 || balance < 0) {
throw new InvalidDatasetException("Invalid account balance line: " + line);
}
if (shardNum != mirrorProperties.getShard()) {
throw new InvalidDatasetException(String.format("Invalid account balance line: %s. Expect " + "shard (%d), got shard (%d)", line, mirrorProperties.getShard(), shardNum));
}
EntityId accountId = EntityId.of(shardNum, realmNum, accountNum, EntityType.ACCOUNT);
List<TokenBalance> tokenBalances = hasTokenBalance ? parseTokenBalanceList(parts.get(4), consensusTimestamp, accountId) : Collections.emptyList();
return new AccountBalance(balance, tokenBalances, new AccountBalance.Id(consensusTimestamp, accountId));
} catch (NumberFormatException | InvalidProtocolBufferException ex) {
throw new InvalidDatasetException("Invalid account balance line: " + line, ex);
}
}
use of com.hedera.mirror.common.domain.entity.EntityId in project hedera-mirror-node by hashgraph.
the class ContractUpdateTransactionHandler method updateStakingInfo.
private void updateStakingInfo(RecordItem recordItem, Contract contract) {
var transactionBody = recordItem.getTransactionBody().getContractUpdateInstance();
if (transactionBody.hasDeclineReward()) {
contract.setDeclineReward(transactionBody.getDeclineReward().getValue());
}
switch(transactionBody.getStakedIdCase()) {
case STAKEDID_NOT_SET:
break;
case STAKED_NODE_ID:
contract.setStakedNodeId(transactionBody.getStakedNodeId());
contract.setStakedAccountId(-1L);
break;
case STAKED_ACCOUNT_ID:
EntityId accountId = EntityId.of(transactionBody.getStakedAccountId());
contract.setStakedAccountId(AccountIdConverter.INSTANCE.convertToDatabaseColumn(accountId));
contract.setStakedNodeId(-1L);
break;
}
// If the stake node id or the decline reward value has changed, we start a new stake period.
if (transactionBody.getStakedIdCase() != STAKEDID_NOT_SET || transactionBody.hasDeclineReward()) {
contract.setStakePeriodStart(Utility.getEpochDay(recordItem.getConsensusTimestamp()));
}
}
use of com.hedera.mirror.common.domain.entity.EntityId in project hedera-mirror-node by hashgraph.
the class CryptoApproveAllowanceTransactionHandler method parseCryptoAllowances.
private void parseCryptoAllowances(List<com.hederahashgraph.api.proto.java.CryptoAllowance> cryptoAllowances, RecordItem recordItem) {
var consensusTimestamp = recordItem.getConsensusTimestamp();
var cryptoAllowanceState = new HashMap<CryptoAllowance.Id, CryptoAllowance>();
var payerAccountId = recordItem.getPayerAccountId();
// iterate the crypto allowance list in reverse order and honor the last allowance for the same owner and spender
var iterator = cryptoAllowances.listIterator(cryptoAllowances.size());
while (iterator.hasPrevious()) {
var cryptoApproval = iterator.previous();
EntityId ownerAccountId = getOwnerAccountId(cryptoApproval.getOwner(), payerAccountId);
if (ownerAccountId == EntityId.EMPTY) {
// and the partialDataAction is SKIP
continue;
}
CryptoAllowance cryptoAllowance = new CryptoAllowance();
cryptoAllowance.setAmount(cryptoApproval.getAmount());
cryptoAllowance.setOwner(ownerAccountId.getId());
cryptoAllowance.setPayerAccountId(payerAccountId);
cryptoAllowance.setSpender(EntityId.of(cryptoApproval.getSpender()).getId());
cryptoAllowance.setTimestampLower(consensusTimestamp);
if (cryptoAllowanceState.putIfAbsent(cryptoAllowance.getId(), cryptoAllowance) == null) {
entityListener.onCryptoAllowance(cryptoAllowance);
}
}
}
use of com.hedera.mirror.common.domain.entity.EntityId in project hedera-mirror-node by hashgraph.
the class CryptoApproveAllowanceTransactionHandler method parseTokenAllowances.
private void parseTokenAllowances(List<com.hederahashgraph.api.proto.java.TokenAllowance> tokenAllowances, RecordItem recordItem) {
var consensusTimestamp = recordItem.getConsensusTimestamp();
var payerAccountId = recordItem.getPayerAccountId();
var tokenAllowanceState = new HashMap<TokenAllowance.Id, TokenAllowance>();
// iterate the token allowance list in reverse order and honor the last allowance for the same owner, spender,
// and token
var iterator = tokenAllowances.listIterator(tokenAllowances.size());
while (iterator.hasPrevious()) {
var tokenApproval = iterator.previous();
EntityId ownerAccountId = getOwnerAccountId(tokenApproval.getOwner(), payerAccountId);
if (ownerAccountId == EntityId.EMPTY) {
// and the partialDataAction is SKIP
continue;
}
TokenAllowance tokenAllowance = new TokenAllowance();
tokenAllowance.setAmount(tokenApproval.getAmount());
tokenAllowance.setOwner(ownerAccountId.getId());
tokenAllowance.setPayerAccountId(payerAccountId);
tokenAllowance.setSpender(EntityId.of(tokenApproval.getSpender()).getId());
tokenAllowance.setTokenId(EntityId.of(tokenApproval.getTokenId()).getId());
tokenAllowance.setTimestampLower(consensusTimestamp);
if (tokenAllowanceState.putIfAbsent(tokenAllowance.getId(), tokenAllowance) == null) {
entityListener.onTokenAllowance(tokenAllowance);
}
}
}
Aggregations