use of com.hederahashgraph.api.proto.java.CryptoGetAccountBalanceQuery in project hedera-services by hashgraph.
the class GetAccountBalanceAnswer method checkValidity.
@Override
public ResponseCodeEnum checkValidity(Query query, StateView view) {
MerkleMap<EntityNum, MerkleAccount> accounts = view.accounts();
CryptoGetAccountBalanceQuery op = query.getCryptogetAccountBalance();
return validityOf(op, accounts);
}
use of com.hederahashgraph.api.proto.java.CryptoGetAccountBalanceQuery in project hedera-services by hashgraph.
the class GetAccountBalanceAnswer method responseGiven.
@Override
public Response responseGiven(Query query, StateView view, ResponseCodeEnum validity, long cost) {
MerkleMap<EntityNum, MerkleAccount> accounts = view.accounts();
CryptoGetAccountBalanceQuery op = query.getCryptogetAccountBalance();
final var id = targetOf(op);
CryptoGetAccountBalanceResponse.Builder opAnswer = CryptoGetAccountBalanceResponse.newBuilder().setHeader(answerOnlyHeader(validity)).setAccountID(id);
if (validity == OK) {
var key = EntityNum.fromAccountId(id);
var account = accounts.get(key);
opAnswer.setBalance(account.getBalance());
for (TokenID tId : account.tokens().asTokenIds()) {
var relKey = fromAccountTokenRel(id, tId);
var relationship = view.tokenAssociations().get(relKey);
var decimals = view.tokenWith(tId).map(MerkleToken::decimals).orElse(0);
opAnswer.addTokenBalances(TokenBalance.newBuilder().setTokenId(tId).setBalance(relationship.getBalance()).setDecimals(decimals).build());
}
}
return Response.newBuilder().setCryptogetAccountBalance(opAnswer).build();
}
use of com.hederahashgraph.api.proto.java.CryptoGetAccountBalanceQuery in project hedera-services by hashgraph.
the class GetAccountBalanceAnswerTest method requiresNothing.
@Test
void requiresNothing() {
// setup:
CryptoGetAccountBalanceQuery costAnswerOp = CryptoGetAccountBalanceQuery.newBuilder().setHeader(QueryHeader.newBuilder().setResponseType(ResponseType.COST_ANSWER)).build();
Query costAnswerQuery = Query.newBuilder().setCryptogetAccountBalance(costAnswerOp).build();
CryptoGetAccountBalanceQuery answerOnlyOp = CryptoGetAccountBalanceQuery.newBuilder().setHeader(QueryHeader.newBuilder().setResponseType(ResponseType.ANSWER_ONLY)).build();
Query answerOnlyQuery = Query.newBuilder().setCryptogetAccountBalance(answerOnlyOp).build();
// expect:
assertFalse(subject.requiresNodePayment(costAnswerQuery));
assertFalse(subject.requiresNodePayment(answerOnlyQuery));
assertFalse(subject.needsAnswerOnlyCost(answerOnlyQuery));
assertFalse(subject.needsAnswerOnlyCost(costAnswerQuery));
}
use of com.hederahashgraph.api.proto.java.CryptoGetAccountBalanceQuery in project hedera-services by hashgraph.
the class GetAccountBalanceAnswerTest method requiresOkMetaValidity.
@Test
void requiresOkMetaValidity() {
// setup:
AccountID id = asAccount(accountIdLit);
// given:
CryptoGetAccountBalanceQuery op = CryptoGetAccountBalanceQuery.newBuilder().setAccountID(id).build();
Query query = Query.newBuilder().setCryptogetAccountBalance(op).build();
// when:
Response response = subject.responseGiven(query, view, PLATFORM_NOT_ACTIVE);
ResponseCodeEnum status = response.getCryptogetAccountBalance().getHeader().getNodeTransactionPrecheckCode();
// expect:
assertEquals(PLATFORM_NOT_ACTIVE, status);
assertEquals(id, response.getCryptogetAccountBalance().getAccountID());
}
use of com.hederahashgraph.api.proto.java.CryptoGetAccountBalanceQuery in project hedera-services by hashgraph.
the class GetAccountBalanceAnswerTest method answersWithAccountBalanceWhenTheAccountIDIsContractID.
@Test
void answersWithAccountBalanceWhenTheAccountIDIsContractID() {
// setup:
ContractID id = asContract(accountIdLit);
// given:
CryptoGetAccountBalanceQuery op = CryptoGetAccountBalanceQuery.newBuilder().setContractID(id).build();
Query query = Query.newBuilder().setCryptogetAccountBalance(op).build();
// when:
Response response = subject.responseGiven(query, view, OK);
ResponseCodeEnum status = response.getCryptogetAccountBalance().getHeader().getNodeTransactionPrecheckCode();
long answer = response.getCryptogetAccountBalance().getBalance();
// expect:
assertTrue(response.getCryptogetAccountBalance().hasHeader(), "Missing response header!");
assertEquals(List.of(tokenBalanceWith(aToken, aBalance, 1), tokenBalanceWith(bToken, bBalance, 2), tokenBalanceWith(cToken, cBalance, 123), tokenBalanceWith(dToken, dBalance, 0)), response.getCryptogetAccountBalance().getTokenBalancesList());
assertEquals(OK, status);
assertEquals(balance, answer);
assertEquals(asAccount(accountIdLit), response.getCryptogetAccountBalance().getAccountID());
}
Aggregations