use of com.hedera.hashgraph.sdk.CustomFractionalFee in project hedera-sdk-java by hashgraph.
the class TokenFeeScheduleUpdateIntegrationTest method cannotUpdateWithAnyOtherKey.
@Test
@DisplayName("Cannot update fee schedule with any key other than fee schedule key")
void cannotUpdateWithAnyOtherKey() throws Exception {
var testEnv = new IntegrationTestEnv(1).useThrowawayAccount();
var response = new TokenCreateTransaction().setTokenName("ffff").setTokenSymbol("F").setTreasuryAccountId(testEnv.operatorId).setAdminKey(testEnv.operatorKey).setFeeScheduleKey(PrivateKey.generate()).setFreezeDefault(false).execute(testEnv.client);
var tokenId = Objects.requireNonNull(response.getReceipt(testEnv.client).tokenId);
var customFees = new ArrayList<CustomFee>();
customFees.add(new CustomFixedFee().setAmount(10).setFeeCollectorAccountId(testEnv.operatorId));
customFees.add(new CustomFractionalFee().setNumerator(1).setDenominator(20).setMin(1).setMax(10).setFeeCollectorAccountId(testEnv.operatorId));
assertThatExceptionOfType(ReceiptStatusException.class).isThrownBy(() -> {
new TokenFeeScheduleUpdateTransaction().setTokenId(tokenId).setCustomFees(customFees).execute(testEnv.client).getReceipt(testEnv.client);
}).withMessageContaining(Status.INVALID_SIGNATURE.toString());
testEnv.close(tokenId);
}
use of com.hedera.hashgraph.sdk.CustomFractionalFee in project hedera-sdk-java by hashgraph.
the class TokenFeeScheduleUpdateIntegrationTest method canUpdateToken.
@Test
@DisplayName("Can update token fees")
void canUpdateToken() throws Exception {
var testEnv = new IntegrationTestEnv(1).useThrowawayAccount();
var response = new TokenCreateTransaction().setTokenName("ffff").setTokenSymbol("F").setDecimals(3).setInitialSupply(1000000).setTreasuryAccountId(testEnv.operatorId).setAdminKey(testEnv.operatorKey).setFreezeKey(testEnv.operatorKey).setWipeKey(testEnv.operatorKey).setKycKey(testEnv.operatorKey).setSupplyKey(testEnv.operatorKey).setFeeScheduleKey(testEnv.operatorKey).setFreezeDefault(false).execute(testEnv.client);
var tokenId = Objects.requireNonNull(response.getReceipt(testEnv.client).tokenId);
@Var var info = new TokenInfoQuery().setTokenId(tokenId).execute(testEnv.client);
assertThat(info.tokenId).isEqualTo(tokenId);
assertThat(info.name).isEqualTo("ffff");
assertThat(info.symbol).isEqualTo("F");
assertThat(info.decimals).isEqualTo(3);
assertThat(testEnv.operatorId).isEqualTo(info.treasuryAccountId);
assertThat(info.adminKey).isNotNull();
assertThat(info.freezeKey).isNotNull();
assertThat(info.wipeKey).isNotNull();
assertThat(info.kycKey).isNotNull();
assertThat(info.supplyKey).isNotNull();
assertThat(info.adminKey.toString()).isEqualTo(testEnv.operatorKey.toString());
assertThat(info.freezeKey.toString()).isEqualTo(testEnv.operatorKey.toString());
assertThat(info.wipeKey.toString()).isEqualTo(testEnv.operatorKey.toString());
assertThat(info.kycKey.toString()).isEqualTo(testEnv.operatorKey.toString());
assertThat(info.supplyKey.toString()).isEqualTo(testEnv.operatorKey.toString());
assertThat(info.feeScheduleKey.toString()).isEqualTo(testEnv.operatorKey.toString());
assertThat(info.defaultFreezeStatus).isNotNull();
assertThat(info.defaultFreezeStatus).isFalse();
assertThat(info.defaultKycStatus).isNotNull();
assertThat(info.defaultKycStatus).isFalse();
assertThat(info.customFees.size()).isEqualTo(0);
var customFees = new ArrayList<CustomFee>();
customFees.add(new CustomFixedFee().setAmount(10).setFeeCollectorAccountId(testEnv.operatorId));
customFees.add(new CustomFractionalFee().setNumerator(1).setDenominator(20).setMin(1).setMax(10).setFeeCollectorAccountId(testEnv.operatorId));
new TokenFeeScheduleUpdateTransaction().setTokenId(tokenId).setCustomFees(customFees).execute(testEnv.client).getReceipt(testEnv.client);
info = new TokenInfoQuery().setTokenId(tokenId).execute(testEnv.client);
assertThat(info.tokenId).isEqualTo(tokenId);
assertThat(info.name).isEqualTo("ffff");
assertThat(info.symbol).isEqualTo("F");
assertThat(info.decimals).isEqualTo(3);
assertThat(info.treasuryAccountId).isEqualTo(testEnv.operatorId);
assertThat(info.adminKey).isNotNull();
assertThat(info.freezeKey).isNotNull();
assertThat(info.wipeKey).isNotNull();
assertThat(info.kycKey).isNotNull();
assertThat(info.supplyKey).isNotNull();
assertThat(info.adminKey.toString()).isEqualTo(testEnv.operatorKey.toString());
assertThat(info.freezeKey.toString()).isEqualTo(testEnv.operatorKey.toString());
assertThat(info.wipeKey.toString()).isEqualTo(testEnv.operatorKey.toString());
assertThat(info.kycKey.toString()).isEqualTo(testEnv.operatorKey.toString());
assertThat(info.supplyKey.toString()).isEqualTo(testEnv.operatorKey.toString());
assertThat(info.feeScheduleKey.toString()).isEqualTo(testEnv.operatorKey.toString());
assertThat(info.defaultFreezeStatus).isNotNull();
assertThat(info.defaultFreezeStatus).isFalse();
assertThat(info.defaultKycStatus).isNotNull();
assertThat(info.defaultKycStatus).isFalse();
var fees = info.customFees;
assertThat(fees.size()).isEqualTo(2);
@Var int fixedCount = 0;
@Var int fractionalCount = 0;
for (var fee : fees) {
if (fee instanceof CustomFixedFee) {
fixedCount++;
var fixed = (CustomFixedFee) fee;
assertThat(fixed.getAmount()).isEqualTo(10);
assertThat(fixed.getFeeCollectorAccountId()).isEqualTo(testEnv.operatorId);
assertThat(fixed.getDenominatingTokenId()).isNull();
} else if (fee instanceof CustomFractionalFee) {
fractionalCount++;
var fractional = (CustomFractionalFee) fee;
assertThat(fractional.getNumerator()).isEqualTo(1);
assertThat(fractional.getDenominator()).isEqualTo(20);
assertThat(fractional.getMin()).isEqualTo(1);
assertThat(fractional.getMax()).isEqualTo(10);
assertThat(fractional.getFeeCollectorAccountId()).isEqualTo(testEnv.operatorId);
}
}
assertThat(fixedCount).isEqualTo(1);
assertThat(fractionalCount).isEqualTo(1);
testEnv.close(tokenId);
}
use of com.hedera.hashgraph.sdk.CustomFractionalFee in project hedera-sdk-java by hashgraph.
the class CustomFeesExample method main.
public static void main(String[] args) throws TimeoutException, PrecheckStatusException, ReceiptStatusException {
Client client = Client.forName(HEDERA_NETWORK);
// Defaults the operator account ID and key such that all generated transactions will be paid for
// by this account and be signed by this key
client.setOperator(OPERATOR_ID, OPERATOR_KEY);
// Create three accounts, Alice, Bob, and Charlie. Alice will be the treasury for our example token.
// Fees only apply in transactions not involving the treasury, so we need two other accounts.
PrivateKey aliceKey = PrivateKey.generateED25519();
AccountId aliceId = new AccountCreateTransaction().setInitialBalance(new Hbar(10)).setKey(aliceKey).freezeWith(client).sign(aliceKey).execute(client).getReceipt(client).accountId;
PrivateKey bobKey = PrivateKey.generateED25519();
AccountId bobId = new AccountCreateTransaction().setInitialBalance(new Hbar(10)).setKey(bobKey).freezeWith(client).sign(bobKey).execute(client).getReceipt(client).accountId;
PrivateKey charlieKey = PrivateKey.generateED25519();
AccountId charlieId = new AccountCreateTransaction().setInitialBalance(new Hbar(10)).setKey(charlieKey).freezeWith(client).sign(charlieKey).execute(client).getReceipt(client).accountId;
System.out.println("Alice: " + aliceId);
System.out.println("Bob: " + bobId);
System.out.println("Charlie: " + charlieId);
// Let's start with a custom fee list of 1 fixed fee. A custom fee list can be a list of up to
// 10 custom fees, where each fee is a fixed fee or a fractional fee.
// This fixed fee will mean that every time Bob transfers any number of tokens to Charlie,
// Alice will collect 1 Hbar from each account involved in the transaction who is SENDING
// the Token (in this case, Bob).
CustomFixedFee customHbarFee = new CustomFixedFee().setHbarAmount(new Hbar(1)).setFeeCollectorAccountId(aliceId);
List<CustomFee> hbarFeeList = Collections.singletonList(customHbarFee);
// In this example the fee is in Hbar, but you can charge a fixed fee in a token if you'd like.
// EG, you can make it so that each time an account transfers Foo tokens,
// they must pay a fee in Bar tokens to the fee collecting account.
// To charge a fixed fee in tokens, instead of calling setHbarAmount(), call
// setDenominatingTokenId(tokenForFee) and setAmount(tokenFeeAmount).
// Setting the feeScheduleKey to Alice's key will enable Alice to change the custom
// fees list on this token later using the TokenFeeScheduleUpdateTransaction.
// We will create an initial supply of 100 of these tokens.
TokenId tokenId = new TokenCreateTransaction().setTokenName("Example Token").setTokenSymbol("EX").setAdminKey(aliceKey).setSupplyKey(aliceKey).setFeeScheduleKey(aliceKey).setTreasuryAccountId(aliceId).setCustomFees(hbarFeeList).setInitialSupply(100).freezeWith(client).sign(aliceKey).execute(client).getReceipt(client).tokenId;
System.out.println("Token: " + tokenId);
TokenInfo tokenInfo1 = new TokenInfoQuery().setTokenId(tokenId).execute(client);
System.out.println("Custom Fees according to TokenInfoQuery:");
System.out.println(tokenInfo1.customFees);
// We must associate the token with Bob and Charlie before they can trade in it.
new TokenAssociateTransaction().setAccountId(bobId).setTokenIds(Collections.singletonList(tokenId)).freezeWith(client).sign(bobKey).execute(client).getReceipt(client);
new TokenAssociateTransaction().setAccountId(charlieId).setTokenIds(Collections.singletonList(tokenId)).freezeWith(client).sign(charlieKey).execute(client).getReceipt(client);
// give all 100 tokens to Bob
new TransferTransaction().addTokenTransfer(tokenId, bobId, 100).addTokenTransfer(tokenId, aliceId, -100).freezeWith(client).sign(aliceKey).execute(client).getReceipt(client);
Hbar aliceHbar1 = new AccountBalanceQuery().setAccountId(aliceId).execute(client).hbars;
System.out.println("Alice's Hbar balance before Bob transfers 20 tokens to Charlie: " + aliceHbar1);
TransactionRecord record1 = new TransferTransaction().addTokenTransfer(tokenId, bobId, -20).addTokenTransfer(tokenId, charlieId, 20).freezeWith(client).sign(bobKey).execute(client).getRecord(client);
Hbar aliceHbar2 = new AccountBalanceQuery().setAccountId(aliceId).execute(client).hbars;
System.out.println("Alices's Hbar balance after Bob transfers 20 tokens to Charlie: " + aliceHbar2);
System.out.println("Assessed fees according to transaction record:");
System.out.println(record1.assessedCustomFees);
// Let's use the TokenUpdateFeeScheduleTransaction with Alice's key to change the custom fees on our token.
// TokenUpdateFeeScheduleTransaction will replace the list of fees that apply to the token with
// an entirely new list. Let's charge a 10% fractional fee. This means that when Bob attempts to transfer
// 20 tokens to Charlie, 10% of the tokens he attempts to transfer (2 in this case) will be transferred to
// Alice instead.
// Fractional fees default to FeeAssessmentMethod.INCLUSIVE, which is the behavior described above.
// If you set the assessment method to EXCLUSIVE, then when Bob attempts to transfer 20 tokens to Charlie,
// Charlie will receive all 20 tokens, and Bob will be charged an _additional_ 10% fee which
// will be transferred to Alice.
CustomFractionalFee customFractionalFee = new CustomFractionalFee().setNumerator(1).setDenominator(10).setMin(1).setMax(10).setFeeCollectorAccountId(aliceId);
List<CustomFee> fractionalFeeList = Collections.singletonList(customFractionalFee);
new TokenFeeScheduleUpdateTransaction().setTokenId(tokenId).setCustomFees(fractionalFeeList).freezeWith(client).sign(aliceKey).execute(client).getReceipt(client);
TokenInfo tokenInfo2 = new TokenInfoQuery().setTokenId(tokenId).execute(client);
System.out.println("Custom Fees according to TokenInfoQuery:");
System.out.println(tokenInfo2.customFees);
Map<TokenId, Long> aliceTokens3 = new AccountBalanceQuery().setAccountId(aliceId).execute(client).tokens;
System.out.println("Alice's token balance before Bob transfers 20 tokens to Charlie: " + aliceTokens3);
TransactionRecord record2 = new TransferTransaction().addTokenTransfer(tokenId, bobId, -20).addTokenTransfer(tokenId, charlieId, 20).freezeWith(client).sign(bobKey).execute(client).getRecord(client);
Map<TokenId, Long> aliceTokens4 = new AccountBalanceQuery().setAccountId(aliceId).execute(client).tokens;
System.out.println("Alices's token balance after Bob transfers 20 tokens to Charlie: " + aliceTokens4);
System.out.println("Token transfers according to transaction record:");
System.out.println(record2.tokenTransfers);
System.out.println("Assessed fees according to transaction record:");
System.out.println(record2.assessedCustomFees);
// clean up
new TokenDeleteTransaction().setTokenId(tokenId).freezeWith(client).sign(aliceKey).execute(client).getReceipt(client);
new AccountDeleteTransaction().setAccountId(charlieId).setTransferAccountId(client.getOperatorAccountId()).freezeWith(client).sign(charlieKey).execute(client).getReceipt(client);
new AccountDeleteTransaction().setAccountId(bobId).setTransferAccountId(client.getOperatorAccountId()).freezeWith(client).sign(bobKey).execute(client).getReceipt(client);
new AccountDeleteTransaction().setAccountId(aliceId).setTransferAccountId(client.getOperatorAccountId()).freezeWith(client).sign(aliceKey).execute(client).getReceipt(client);
client.close();
}
use of com.hedera.hashgraph.sdk.CustomFractionalFee in project hedera-mirror-node by hashgraph.
the class CustomFeesConverter method customFee.
@DataTableType
public CustomFee customFee(Map<String, String> entry) {
String amount = entry.get("amount");
AccountId collector = tokenFeature.getRecipientAccountId(Integer.parseInt(entry.get("collector")));
if (Strings.isNotEmpty(amount)) {
// fixed fee
CustomFixedFee fixedFee = new CustomFixedFee();
fixedFee.setAmount(Long.parseLong(amount));
fixedFee.setFeeCollectorAccountId(collector);
fixedFee.setDenominatingTokenId(getTokenId(entry.get("token")));
return fixedFee;
} else {
CustomFractionalFee fractionalFee = new CustomFractionalFee();
fractionalFee.setNumerator(Long.parseLong(entry.get("numerator")));
fractionalFee.setDenominator(Long.parseLong(entry.get("denominator")));
fractionalFee.setFeeCollectorAccountId(collector);
fractionalFee.setMax(getValueOrDefault(entry.get("maximum")));
fractionalFee.setMin(getValueOrDefault(entry.get("minimum")));
return fractionalFee;
}
}
use of com.hedera.hashgraph.sdk.CustomFractionalFee in project hedera-mirror-node by hashgraph.
the class TokenFeature method verifyTokenWithCustomFeesSchedule.
private void verifyTokenWithCustomFeesSchedule(TokenId tokenId, String createdTimestamp) {
MirrorTokenResponse response = verifyToken(tokenId);
MirrorCustomFees expected = new MirrorCustomFees();
expected.setCreatedTimestamp(createdTimestamp);
for (CustomFee customFee : tokenCustomFees.get(tokenId)) {
if (customFee instanceof CustomFixedFee) {
CustomFixedFee sdkFixedFee = (CustomFixedFee) customFee;
MirrorFixedFee fixedFee = new MirrorFixedFee();
fixedFee.setAmount(sdkFixedFee.getAmount());
fixedFee.setCollectorAccountId(sdkFixedFee.getFeeCollectorAccountId().toString());
if (sdkFixedFee.getDenominatingTokenId() != null) {
fixedFee.setDenominatingTokenId(sdkFixedFee.getDenominatingTokenId().toString());
}
expected.getFixedFees().add(fixedFee);
} else {
CustomFractionalFee sdkFractionalFee = (CustomFractionalFee) customFee;
MirrorFractionalFee fractionalFee = new MirrorFractionalFee();
MirrorFraction fraction = new MirrorFraction();
fraction.setNumerator(sdkFractionalFee.getNumerator());
fraction.setDenominator(sdkFractionalFee.getDenominator());
fractionalFee.setAmount(fraction);
fractionalFee.setCollectorAccountId(sdkFractionalFee.getFeeCollectorAccountId().toString());
fractionalFee.setDenominatingTokenId(tokenId.toString());
if (sdkFractionalFee.getMax() != 0) {
fractionalFee.setMaximum(sdkFractionalFee.getMax());
}
fractionalFee.setMinimum(sdkFractionalFee.getMin());
expected.getFractionalFees().add(fractionalFee);
}
}
MirrorCustomFees actual = response.getCustomFees();
assertThat(actual).usingRecursiveComparison(RecursiveComparisonConfiguration.builder().withIgnoreCollectionOrder(true).build()).isEqualTo(expected);
}
Aggregations