Search in sources :

Example 1 with CryptoGetAccountBalanceQuery

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);
}
Also used : CryptoGetAccountBalanceQuery(com.hederahashgraph.api.proto.java.CryptoGetAccountBalanceQuery) MerkleAccount(com.hedera.services.state.merkle.MerkleAccount) EntityNum(com.hedera.services.utils.EntityNum)

Example 2 with CryptoGetAccountBalanceQuery

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();
}
Also used : CryptoGetAccountBalanceQuery(com.hederahashgraph.api.proto.java.CryptoGetAccountBalanceQuery) MerkleAccount(com.hedera.services.state.merkle.MerkleAccount) TokenID(com.hederahashgraph.api.proto.java.TokenID) EntityNum(com.hedera.services.utils.EntityNum) CryptoGetAccountBalanceResponse(com.hederahashgraph.api.proto.java.CryptoGetAccountBalanceResponse)

Example 3 with CryptoGetAccountBalanceQuery

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));
}
Also used : CryptoGetAccountBalanceQuery(com.hederahashgraph.api.proto.java.CryptoGetAccountBalanceQuery) Query(com.hederahashgraph.api.proto.java.Query) CryptoGetAccountBalanceQuery(com.hederahashgraph.api.proto.java.CryptoGetAccountBalanceQuery) Test(org.junit.jupiter.api.Test)

Example 4 with CryptoGetAccountBalanceQuery

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());
}
Also used : Response(com.hederahashgraph.api.proto.java.Response) CryptoGetAccountBalanceResponse(com.hederahashgraph.api.proto.java.CryptoGetAccountBalanceResponse) ResponseCodeEnum(com.hederahashgraph.api.proto.java.ResponseCodeEnum) AccountID(com.hederahashgraph.api.proto.java.AccountID) CryptoGetAccountBalanceQuery(com.hederahashgraph.api.proto.java.CryptoGetAccountBalanceQuery) Query(com.hederahashgraph.api.proto.java.Query) CryptoGetAccountBalanceQuery(com.hederahashgraph.api.proto.java.CryptoGetAccountBalanceQuery) Test(org.junit.jupiter.api.Test)

Example 5 with CryptoGetAccountBalanceQuery

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());
}
Also used : Response(com.hederahashgraph.api.proto.java.Response) CryptoGetAccountBalanceResponse(com.hederahashgraph.api.proto.java.CryptoGetAccountBalanceResponse) ResponseCodeEnum(com.hederahashgraph.api.proto.java.ResponseCodeEnum) CryptoGetAccountBalanceQuery(com.hederahashgraph.api.proto.java.CryptoGetAccountBalanceQuery) Query(com.hederahashgraph.api.proto.java.Query) CryptoGetAccountBalanceQuery(com.hederahashgraph.api.proto.java.CryptoGetAccountBalanceQuery) ContractID(com.hederahashgraph.api.proto.java.ContractID) Test(org.junit.jupiter.api.Test)

Aggregations

CryptoGetAccountBalanceQuery (com.hederahashgraph.api.proto.java.CryptoGetAccountBalanceQuery)10 Query (com.hederahashgraph.api.proto.java.Query)8 Test (org.junit.jupiter.api.Test)8 ResponseCodeEnum (com.hederahashgraph.api.proto.java.ResponseCodeEnum)7 CryptoGetAccountBalanceResponse (com.hederahashgraph.api.proto.java.CryptoGetAccountBalanceResponse)5 Response (com.hederahashgraph.api.proto.java.Response)4 AccountID (com.hederahashgraph.api.proto.java.AccountID)3 MerkleAccount (com.hedera.services.state.merkle.MerkleAccount)2 EntityNum (com.hedera.services.utils.EntityNum)2 ContractID (com.hederahashgraph.api.proto.java.ContractID)2 TokenID (com.hederahashgraph.api.proto.java.TokenID)1