Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Acceptance tests for token allowance amounts #6592

Merged
merged 15 commits into from
Aug 11, 2023
Merged
Show file tree
Hide file tree
Changes from 14 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
7 changes: 7 additions & 0 deletions hedera-mirror-rest/api/v1/openapi.yml
Original file line number Diff line number Diff line change
Expand Up @@ -1308,6 +1308,11 @@ components:
Allowance:
type: object
properties:
amount:
description: The amount remaining of the original amount granted.
format: int64
type: integer
example: 75
amount_granted:
description: The granted amount of the spender's allowance.
format: int64
Expand Down Expand Up @@ -2356,6 +2361,8 @@ components:
allOf:
- $ref: "#/components/schemas/Allowance"
- properties:
amount:
description: The amount remaining of the original amount granted in tinybars.
amount_granted:
description: The granted amount of the spender's allowance in tinybars.
CryptoAllowances:
Expand Down
2 changes: 1 addition & 1 deletion hedera-mirror-test/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -54,7 +54,7 @@ include:
| `hedera.mirror.test.acceptance.nodes` | [] | A map of custom consensus node with the key being the account ID and the value its endpoint. |
| `hedera.mirror.test.acceptance.operatorBalance` | 70000000000 | The amount of tinybars to fund the operator. Applicable only when `createOperatorAccount` is `true`. |
| `hedera.mirror.test.acceptance.operatorId` | 0.0.2 | Operator account ID used to pay for transactions. |
| `hedera.mirror.test.acceptance.operatorKey` | Genesis key | Operator ED25519 private key used to sign transactions in hex encoded DER format. |
| `hedera.mirror.test.acceptance.operatorKey` | Genesis key | Operator ED25519 or ECDSA private key used to sign transactions in hex encoded DER format. |
| `hedera.mirror.test.acceptance.rest.baseUrl` | https://testnet.mirrornode.hedera.com/api/v1 | The URL to the mirror node REST API. |
| `hedera.mirror.test.acceptance.rest.maxAttempts` | 20 | The maximum number of attempts when calling a REST API endpoint and receiving a 404. |
| `hedera.mirror.test.acceptance.rest.maxBackoff` | 4s | The maximum amount of time to wait between REST API attempts. |
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -93,7 +93,7 @@ public ExpandedAccountId getAccount(AccountNameEnum accountNameEnum) {
// retrieve account, setting if it doesn't exist
ExpandedAccountId accountId = accountMap.computeIfAbsent(accountNameEnum, x -> {
try {
return createNewAccount(SMALL_INITIAL_BALANCE, accountNameEnum);
return createNewAccount(DEFAULT_INITIAL_BALANCE, accountNameEnum);
jascks marked this conversation as resolved.
Show resolved Hide resolved
} catch (Exception e) {
log.warn("Issue creating additional account: {}, ex: {}", accountNameEnum, e);
return null;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,7 @@
import com.hedera.mirror.test.e2e.acceptance.response.MirrorNftResponse;
import com.hedera.mirror.test.e2e.acceptance.response.MirrorNftTransactionsResponse;
import com.hedera.mirror.test.e2e.acceptance.response.MirrorScheduleResponse;
import com.hedera.mirror.test.e2e.acceptance.response.MirrorTokenAllowanceResponse;
import com.hedera.mirror.test.e2e.acceptance.response.MirrorTokenRelationshipResponse;
import com.hedera.mirror.test.e2e.acceptance.response.MirrorTokenResponse;
import com.hedera.mirror.test.e2e.acceptance.response.MirrorTransactionsResponse;
Expand Down Expand Up @@ -173,6 +174,21 @@ public MirrorCryptoAllowanceResponse getAccountCryptoAllowanceBySpender(String a
spenderId);
}

public MirrorTokenAllowanceResponse getAccountTokenAllowanceBySpender(
String accountId, String tokenId, String spenderId) {
log.debug(
"Verify account '{}''s token allowance for token {} and spender {} is returned by Mirror Node",
accountId,
tokenId,
spenderId);
return callRestEndpoint(
"/accounts/{accountId}/allowances/tokens?token.id={tokenId}&spender.id={spenderId}",
MirrorTokenAllowanceResponse.class,
accountId,
tokenId,
spenderId);
}

public MirrorContractResponse getContractInfo(String contractId) {
log.debug("Verify contract '{}' is returned by Mirror Node", contractId);
return callRestEndpoint("/contracts/{contractId}", MirrorContractResponse.class, contractId);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -41,13 +41,19 @@
import com.hedera.hashgraph.sdk.TokenWipeTransaction;
import com.hedera.hashgraph.sdk.TransferTransaction;
import com.hedera.hashgraph.sdk.proto.TokenFreezeStatus;
import com.hedera.hashgraph.sdk.proto.TokenKycStatus;
import com.hedera.mirror.test.e2e.acceptance.props.ExpandedAccountId;
import com.hedera.mirror.test.e2e.acceptance.response.NetworkTransactionResponse;
import jakarta.inject.Named;
import java.time.Duration;
import java.util.Collection;
import java.util.Collections;
import java.util.List;
import java.util.Map;
import java.util.concurrent.ConcurrentHashMap;
import java.util.concurrent.CopyOnWriteArrayList;
import lombok.Getter;
import lombok.RequiredArgsConstructor;
import org.apache.commons.lang3.RandomStringUtils;
import org.springframework.retry.support.RetryTemplate;

Expand All @@ -56,6 +62,7 @@ public class TokenClient extends AbstractNetworkClient {

private final Collection<TokenAccount> tokenAccounts = new CopyOnWriteArrayList<>();
private final Collection<TokenId> tokenIds = new CopyOnWriteArrayList<>();
private final Map<TokenNameEnum, TokenId> tokenMap = new ConcurrentHashMap<>();

public TokenClient(SDKClient sdkClient, RetryTemplate retryTemplate) {
super(sdkClient, retryTemplate);
Expand Down Expand Up @@ -83,6 +90,41 @@ public int getOrder() {
return -1;
}

public TokenId getToken(TokenNameEnum tokenNameEnum) {
// Create token only if it does not currently exist
var tokenId = tokenMap.computeIfAbsent(tokenNameEnum, x -> {
var operator = sdkClient.getExpandedOperatorAccountId();
try {
var response = createToken(tokenNameEnum, operator);
return response.getReceipt().tokenId;
} catch (Exception e) {
log.warn("Issue creating additional token: {}, operator: {}, ex: {}", tokenNameEnum, operator, e);
return null;
}
});

if (tokenId == null) {
throw new NetworkException("Null tokenId retrieved from receipt");
}

log.debug("Retrieved token: {} with ID: {}", tokenNameEnum, tokenId);
return tokenId;
}

private NetworkTransactionResponse createToken(TokenNameEnum tokenNameEnum, ExpandedAccountId expandedAccountId) {
return createToken(
expandedAccountId,
tokenNameEnum.symbol,
TokenFreezeStatus.FreezeNotApplicable_VALUE,
TokenKycStatus.KycNotApplicable_VALUE,
expandedAccountId,
1_000_000,
TokenSupplyType.INFINITE,
1,
tokenNameEnum.tokenType,
Collections.emptyList());
}

public NetworkTransactionResponse createToken(
ExpandedAccountId expandedAccountId,
String symbol,
Expand Down Expand Up @@ -330,11 +372,17 @@ public NetworkTransactionResponse unpause(TokenId tokenId) {
return response;
}

public TransferTransaction getFungibleTokenTransferTransaction(
TokenId tokenId, AccountId sender, AccountId recipient, long amount) {
return getTransferTransaction()
.addTokenTransfer(tokenId, sender, Math.negateExact(amount))
.addTokenTransfer(tokenId, recipient, amount);
private TransferTransaction getFungibleTokenTransferTransaction(
TokenId tokenId, AccountId owner, AccountId sender, AccountId recipient, long amount, boolean isApproval) {
var transferTransaction = getTransferTransaction();

if (isApproval) {
transferTransaction.addApprovedTokenTransfer(tokenId, owner, Math.negateExact(amount));
} else {
transferTransaction.addTokenTransfer(tokenId, sender, Math.negateExact(amount));
}
transferTransaction.addTokenTransfer(tokenId, recipient, amount);
return transferTransaction;
}

public TransferTransaction getNonFungibleTokenTransferTransaction(
Expand All @@ -352,15 +400,27 @@ private TransferTransaction getTransferTransaction() {

public NetworkTransactionResponse transferFungibleToken(
TokenId tokenId, ExpandedAccountId sender, AccountId recipient, long amount) {
var transaction = getFungibleTokenTransferTransaction(tokenId, sender.getAccountId(), recipient, amount);
return transferFungibleToken(tokenId, sender.getAccountId(), sender, recipient, amount, false);
}

public NetworkTransactionResponse transferFungibleToken(
TokenId tokenId,
AccountId owner,
ExpandedAccountId sender,
AccountId recipient,
long amount,
boolean isApproval) {
var transaction = getFungibleTokenTransferTransaction(
tokenId, owner, sender.getAccountId(), recipient, amount, isApproval);
var response = executeTransactionAndRetrieveReceipt(transaction, sender);
log.info(
"Transferred {} tokens of {} from {} to {} via {}",
"Transferred {} tokens of {} from {} to {} via {}, isApproval {}",
amount,
tokenId,
sender,
recipient,
response.getTransactionId());
response.getTransactionId(),
isApproval);
return response;
}

Expand Down Expand Up @@ -506,5 +566,20 @@ public NetworkTransactionResponse updateTokenFeeSchedule(
return response;
}

@RequiredArgsConstructor
@Getter
public enum TokenNameEnum {
FUNGIBLE("fungible", TokenType.FUNGIBLE_COMMON),
NFT("non_fungible", TokenType.NON_FUNGIBLE_UNIQUE);

private final String symbol;
private final TokenType tokenType;
steven-sheehy marked this conversation as resolved.
Show resolved Hide resolved

@Override
public String toString() {
return String.format("%s (%s)", symbol, tokenType);
}
}

private record TokenAccount(TokenId tokenId, ExpandedAccountId accountId) {}
}
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,10 @@
import lombok.Data;

@Data
public class MirrorCryptoAllowance extends MirrorTransferAllowance {
public class MirrorCryptoAllowance {
private long amount;
private long amountGranted;
private String owner;
private String spender;
private MirrorTimestampRange timestamp;
}
Original file line number Diff line number Diff line change
Expand Up @@ -19,11 +19,6 @@
import lombok.Data;

@Data
public class MirrorTransferAllowance {

private String owner;

private String spender;

private MirrorTimestampRange timestamp;
public class MirrorTokenAllowance extends MirrorCryptoAllowance {
private String tokenId;
}
Original file line number Diff line number Diff line change
Expand Up @@ -17,8 +17,12 @@
package com.hedera.mirror.test.e2e.acceptance.props;

import lombok.Data;
import lombok.NoArgsConstructor;
import lombok.experimental.SuperBuilder;

@SuperBuilder
@Data
@NoArgsConstructor
public class MirrorTokenTransfer extends MirrorTransfer {
private String tokenId;
}
Original file line number Diff line number Diff line change
Expand Up @@ -16,18 +16,21 @@

package com.hedera.mirror.test.e2e.acceptance.props;

import com.fasterxml.jackson.annotation.JsonProperty;
import lombok.AccessLevel;
import lombok.AllArgsConstructor;
import lombok.Builder;
import lombok.Data;
import lombok.NoArgsConstructor;
import lombok.experimental.SuperBuilder;

@AllArgsConstructor(access = AccessLevel.PRIVATE) // for builder
@Builder
@SuperBuilder
@Data
@NoArgsConstructor
public class MirrorTransfer {
private String account;
private long amount;

@JsonProperty("is_approval")
jascks marked this conversation as resolved.
Show resolved Hide resolved
private boolean isApproval;
}
Original file line number Diff line number Diff line change
Expand Up @@ -17,11 +17,10 @@
package com.hedera.mirror.test.e2e.acceptance.response;

import com.hedera.mirror.test.e2e.acceptance.props.MirrorCryptoAllowance;
import com.hedera.mirror.test.e2e.acceptance.props.MirrorTransferAllowance;
import java.util.List;
import lombok.Data;

@Data
public class MirrorCryptoAllowanceResponse extends MirrorTransferAllowance {
public class MirrorCryptoAllowanceResponse {
List<MirrorCryptoAllowance> allowances;
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
/*
* Copyright (C) 2019-2023 Hedera Hashgraph, LLC
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/

package com.hedera.mirror.test.e2e.acceptance.response;

import com.hedera.mirror.test.e2e.acceptance.props.MirrorTokenAllowance;
import java.util.List;
import lombok.Data;

@Data
public class MirrorTokenAllowanceResponse {
List<MirrorTokenAllowance> allowances;
}
Loading
Loading