Skip to content

Commit

Permalink
feat: add royalty fee support
Browse files Browse the repository at this point in the history
Signed-off-by: Daniel Akhterov <[email protected]>
  • Loading branch information
janaakhterov committed Aug 25, 2021
1 parent 812cca5 commit 57e8427
Show file tree
Hide file tree
Showing 103 changed files with 581 additions and 535 deletions.
2 changes: 2 additions & 0 deletions src/main/java/com/hedera/hashgraph/sdk/FreezeTransaction.java
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
import com.hedera.hashgraph.proto.Transaction;
import com.hedera.hashgraph.proto.TransactionResponse;

import java.time.Instant;
import java.time.OffsetTime;
import java.time.ZoneOffset;

Expand Down Expand Up @@ -54,6 +55,7 @@ private static boolean checkMinute(int minute) {
public FreezeTransaction setStartTime(OffsetTime startTime) {
OffsetTime actual = startTime.withOffsetSameInstant(ZoneOffset.UTC);

builder.setStartTime(TimestampHelper.timestampFrom(Instant.from(actual)));
builder.setStartHour(actual.getHour());
builder.setStartMin(actual.getMinute());
return this;
Expand Down
16 changes: 13 additions & 3 deletions src/main/java/com/hedera/hashgraph/sdk/Status.java
Original file line number Diff line number Diff line change
Expand Up @@ -205,7 +205,7 @@ public enum Status {
CustomFeeMustBePositive(ResponseCodeEnum.CUSTOM_FEE_MUST_BE_POSITIVE),
TokenHasNoFeeScheduleKey(ResponseCodeEnum.TOKEN_HAS_NO_FEE_SCHEDULE_KEY),
CustomFeeOutsideNumericRange(ResponseCodeEnum.CUSTOM_FEE_OUTSIDE_NUMERIC_RANGE),
InvalidCustomFractionalFeesSum(ResponseCodeEnum.INVALID_CUSTOM_FRACTIONAL_FEES_SUM),
RoyaltyFractionCannotExceedOne(ResponseCodeEnum.ROYALTY_FRACTION_CANNOT_EXCEED_ONE),
FractionalFeeMaxAmountLessThanMinAmount(ResponseCodeEnum.FRACTIONAL_FEE_MAX_AMOUNT_LESS_THAN_MIN_AMOUNT),
CustomScheduleAlreadyHasNoFees(ResponseCodeEnum.CUSTOM_SCHEDULE_ALREADY_HAS_NO_FEES),
CustomFeeDenominationMustBeFungibleCommon(ResponseCodeEnum.CUSTOM_FEE_DENOMINATION_MUST_BE_FUNGIBLE_COMMON),
Expand All @@ -221,7 +221,12 @@ public enum Status {
MaxNftsInPriceRegimeHaveBeenMinted(ResponseCodeEnum.MAX_NFTS_IN_PRICE_REGIME_HAVE_BEEN_MINTED),
PayerAccountDeleted(ResponseCodeEnum.PAYER_ACCOUNT_DELETED),
CustomFeeChargingExceededMaxRecursionDepth(ResponseCodeEnum.CUSTOM_FEE_CHARGING_EXCEEDED_MAX_RECURSION_DEPTH),
CustomFeeChargingExceededMaxAccountAmounts(ResponseCodeEnum.CUSTOM_FEE_CHARGING_EXCEEDED_MAX_ACCOUNT_AMOUNTS);
CustomFeeChargingExceededMaxAccountAmounts(ResponseCodeEnum.CUSTOM_FEE_CHARGING_EXCEEDED_MAX_ACCOUNT_AMOUNTS),
InsufficientSenderAccountBalanceForCustomFee(ResponseCodeEnum.INSUFFICIENT_SENDER_ACCOUNT_BALANCE_FOR_CUSTOM_FEE),
SerialNumberLimitReached(ResponseCodeEnum.SERIAL_NUMBER_LIMIT_REACHED),
CustomRoyaltyFeeOnlyAllowedForNonFungibleUnique(ResponseCodeEnum.CUSTOM_ROYALTY_FEE_ONLY_ALLOWED_FOR_NON_FUNGIBLE_UNIQUE),
NoRemainingAutoAssociations(ResponseCodeEnum.NO_REMAINING_AUTO_ASSOCIATIONS),
ExistingAutomaticAssociationsExceedGivenLimit(ResponseCodeEnum.EXISTING_AUTOMATIC_ASSOCIATIONS_EXCEED_GIVEN_LIMIT);

private final ResponseCodeEnum responseCode;

Expand Down Expand Up @@ -442,7 +447,7 @@ static Status valueOf(ResponseCodeEnum responseCode) {
case CUSTOM_FEE_MUST_BE_POSITIVE: return CustomFeeMustBePositive;
case TOKEN_HAS_NO_FEE_SCHEDULE_KEY: return TokenHasNoFeeScheduleKey;
case CUSTOM_FEE_OUTSIDE_NUMERIC_RANGE: return CustomFeeOutsideNumericRange;
case INVALID_CUSTOM_FRACTIONAL_FEES_SUM: return InvalidCustomFractionalFeesSum;
case ROYALTY_FRACTION_CANNOT_EXCEED_ONE: return RoyaltyFractionCannotExceedOne;
case FRACTIONAL_FEE_MAX_AMOUNT_LESS_THAN_MIN_AMOUNT: return FractionalFeeMaxAmountLessThanMinAmount;
case CUSTOM_SCHEDULE_ALREADY_HAS_NO_FEES: return CustomScheduleAlreadyHasNoFees;
case CUSTOM_FEE_DENOMINATION_MUST_BE_FUNGIBLE_COMMON: return CustomFeeDenominationMustBeFungibleCommon;
Expand All @@ -459,6 +464,11 @@ static Status valueOf(ResponseCodeEnum responseCode) {
case PAYER_ACCOUNT_DELETED: return PayerAccountDeleted;
case CUSTOM_FEE_CHARGING_EXCEEDED_MAX_RECURSION_DEPTH: return CustomFeeChargingExceededMaxRecursionDepth;
case CUSTOM_FEE_CHARGING_EXCEEDED_MAX_ACCOUNT_AMOUNTS: return CustomFeeChargingExceededMaxAccountAmounts;
case INSUFFICIENT_SENDER_ACCOUNT_BALANCE_FOR_CUSTOM_FEE: return InsufficientSenderAccountBalanceForCustomFee;
case SERIAL_NUMBER_LIMIT_REACHED: return SerialNumberLimitReached;
case CUSTOM_ROYALTY_FEE_ONLY_ALLOWED_FOR_NON_FUNGIBLE_UNIQUE: return CustomRoyaltyFeeOnlyAllowedForNonFungibleUnique;
case NO_REMAINING_AUTO_ASSOCIATIONS: return NoRemainingAutoAssociations;
case EXISTING_AUTOMATIC_ASSOCIATIONS_EXCEED_GIVEN_LIMIT: return ExistingAutomaticAssociationsExceedGivenLimit;
case UNRECOGNIZED:
// protobufs won't give us the actual value that was unexpected, unfortunately
throw new IllegalArgumentException(
Expand Down
Original file line number Diff line number Diff line change
@@ -1,10 +1,14 @@
package com.hedera.hashgraph.sdk.token;

import com.google.common.base.MoreObjects;
import com.hedera.hashgraph.proto.AccountID;
import com.hedera.hashgraph.sdk.Internal;
import com.hedera.hashgraph.sdk.account.AccountId;

import javax.annotation.Nullable;
import java.util.ArrayList;
import java.util.Collections;
import java.util.List;

public class AssessedCustomFee {
public final long amount;
Expand All @@ -13,17 +17,25 @@ public class AssessedCustomFee {
@Nullable
public final AccountId feeCollectorAccountId;

public final List<AccountId> payerAccountIds;

AssessedCustomFee() {
this.amount = 0;
this.tokenId = null;
this.feeCollectorAccountId = null;
this.payerAccountIds = Collections.emptyList();
}

@Internal
public AssessedCustomFee(com.hedera.hashgraph.proto.AssessedCustomFee assessedCustomFee) {
this.amount = assessedCustomFee.getAmount();
this.tokenId = assessedCustomFee.hasTokenId() ? new TokenId(assessedCustomFee.getTokenId()) : null;
this.feeCollectorAccountId = assessedCustomFee.hasFeeCollectorAccountId() ? new AccountId(assessedCustomFee.getFeeCollectorAccountId()) : null;
this.payerAccountIds = new ArrayList<>(assessedCustomFee.getEffectivePayerAccountIdCount());

for (AccountID accountID : assessedCustomFee.getEffectivePayerAccountIdList()) {
payerAccountIds.add(new AccountId(accountID));
}
}

@Override
Expand All @@ -32,6 +44,7 @@ public String toString() {
.add("amount", amount)
.add("tokenId", tokenId)
.add("feeCollectorAccountId", feeCollectorAccountId)
.add("payerAccountIds", payerAccountIds)
.toString();
}

Expand All @@ -40,9 +53,15 @@ com.hedera.hashgraph.proto.AssessedCustomFee toProto() {
if (tokenId != null) {
builder.setTokenId(tokenId.toProto());
}

if (feeCollectorAccountId != null) {
builder.setFeeCollectorAccountId(feeCollectorAccountId.toProto());
}

for (AccountId accountId : payerAccountIds) {
builder.addEffectivePayerAccountId(accountId.toProto());
}

return builder.build();
}

Expand Down
3 changes: 3 additions & 0 deletions src/main/java/com/hedera/hashgraph/sdk/token/CustomFee.java
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,9 @@ static CustomFee fromProto(com.hedera.hashgraph.proto.CustomFee customFee) throw
case FRACTIONAL_FEE:
return new CustomFractionalFee(customFee);

case ROYALTY_FEE:
return new CustomRoyaltyFee(customFee);

default:
throw new IllegalStateException("CustomFee#fromProtobuf: unhandled fee case: " + customFee.getFeeCase());
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,11 @@
import com.hedera.hashgraph.sdk.account.AccountId;

public class CustomFractionalFee extends CustomFee {
private long numerator = 0, denominator = 1, min = 0, max = 0;
private long numerator = 0;
private long denominator = 1;
private long min = 0;
private long max = 0;
private FeeAssessmentMethod assessmentMethod = FeeAssessmentMethod.INCLUSIVE;

public CustomFractionalFee() {
}
Expand All @@ -22,6 +26,7 @@ public CustomFractionalFee() {
this.denominator = fraction.getDenominator();
this.min = fractionalFee.getMinimumAmount();
this.max = fractionalFee.getMaximumAmount();
this.assessmentMethod = FeeAssessmentMethod.valueOf(fractionalFee.getNetOfTransfers());
}

public CustomFractionalFee setFeeCollectorAccountId(AccountId feeCollectorAccountId) {
Expand Down Expand Up @@ -65,6 +70,15 @@ public CustomFractionalFee setMax(long max) {
return this;
}

public FeeAssessmentMethod getAssessmentMethod() {
return assessmentMethod;
}

public CustomFractionalFee setAssessmentMethod(FeeAssessmentMethod assessmentMethod) {
this.assessmentMethod = assessmentMethod;
return this;
}

@Override
public String toString() {
return MoreObjects.toStringHelper(this)
Expand All @@ -88,10 +102,13 @@ com.hedera.hashgraph.proto.CustomFee toProto() {
.setNumerator(getNumerator())
.setDenominator(getDenominator())
)
.setNetOfTransfers(assessmentMethod.code)
);

if (getFeeCollectorAccountId() != null) {
customFeeBuilder.setFeeCollectorAccountId(getFeeCollectorAccountId().toProto());
}

return customFeeBuilder.build();
}
}
91 changes: 91 additions & 0 deletions src/main/java/com/hedera/hashgraph/sdk/token/CustomRoyaltyFee.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,91 @@
package com.hedera.hashgraph.sdk.token;

import com.google.common.base.MoreObjects;
import com.hedera.hashgraph.proto.FixedFee;
import com.hedera.hashgraph.proto.Fraction;
import com.hedera.hashgraph.proto.RoyaltyFee;
import com.hedera.hashgraph.sdk.account.AccountId;

import javax.annotation.Nullable;
import java.util.Objects;

public class CustomRoyaltyFee extends CustomFee {
private long numerator = 0;
private long denominator = 1;
@Nullable
private CustomFixedFee fallbackFee = null;

public CustomRoyaltyFee() {
}

CustomRoyaltyFee(com.hedera.hashgraph.proto.CustomFee customFee) {
RoyaltyFee royaltyFee = customFee.getRoyaltyFee();

this.feeCollectorAccountId = customFee.hasFeeCollectorAccountId() ?
new AccountId(customFee.getFeeCollectorAccountId()) : null;
this.denominator = royaltyFee.hasExchangeValueFraction() ? royaltyFee.getExchangeValueFraction().getDenominator() : 0;
this.numerator = royaltyFee.hasExchangeValueFraction() ? royaltyFee.getExchangeValueFraction().getNumerator() : 0;
this.fallbackFee = royaltyFee.hasFallbackFee() ?
new CustomFixedFee(com.hedera.hashgraph.proto.CustomFee.newBuilder()
.setFixedFee(royaltyFee.getFallbackFee())
.build()
) : null;
}

public CustomRoyaltyFee setFeeCollectorAccountId(AccountId feeCollectorAccountId) {
doSetFeeCollectorAccountId(feeCollectorAccountId);
return this;
}

public long getNumerator() {
return numerator;
}

public CustomRoyaltyFee setNumerator(long numerator) {
this.numerator = numerator;
return this;
}

public long getDenominator() {
return denominator;
}

public CustomRoyaltyFee setDenominator(long denominator) {
this.denominator = denominator;
return this;
}

public CustomRoyaltyFee setFallbackFee(CustomFixedFee fallbackFee) {
Objects.requireNonNull(fallbackFee);
this.fallbackFee = fallbackFee;
return this;
}

@Override
com.hedera.hashgraph.proto.CustomFee toProto() {
RoyaltyFee.Builder builder = RoyaltyFee.newBuilder()
.setExchangeValueFraction(
Fraction.newBuilder()
.setNumerator(numerator)
.setDenominator(denominator)
);

if (fallbackFee != null) {
builder.setFallbackFee(fallbackFee.toProto().getFixedFee());
}

return com.hedera.hashgraph.proto.CustomFee.newBuilder()
.setRoyaltyFee(builder)
.build();
}

@Override
public String toString() {
return MoreObjects.toStringHelper(this)
.add("feeCollectorAccountId", getFeeCollectorAccountId())
.add("numerator", getNumerator())
.add("denominator", getDenominator())
.add("fallbackFee", fallbackFee)
.toString();
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
package com.hedera.hashgraph.sdk.token;

public enum FeeAssessmentMethod {
INCLUSIVE(false),
EXCLUSIVE(true);

final boolean code;

FeeAssessmentMethod(boolean code) {
this.code = code;
}

static FeeAssessmentMethod valueOf(boolean code) {
return code ? EXCLUSIVE : INCLUSIVE;
}

@Override
public String toString() {
return code ? "EXCLUSIVE" : "INCLUSIVE";
}
}
20 changes: 0 additions & 20 deletions src/main/proto/ConsensusTopicDefinition.proto

This file was deleted.

15 changes: 0 additions & 15 deletions src/main/proto/ConsensusTopicState.proto

This file was deleted.

27 changes: 0 additions & 27 deletions src/main/proto/CryptoAddClaim.proto

This file was deleted.

14 changes: 0 additions & 14 deletions src/main/proto/CryptoDeleteClaim.proto

This file was deleted.

Loading

0 comments on commit 57e8427

Please sign in to comment.