/*- * * Hedera Wallet Snap * * Copyright (C) 2024 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. * */ import type { Client, PrivateKey } from '@hashgraph/sdk'; import { CustomFixedFee, Hbar, TokenFeeScheduleUpdateTransaction, } from '@hashgraph/sdk'; import type { TxRecord } from '../../types/hedera'; import type { TokenCustomFee } from '../../types/params'; import { Utils } from '../../utils/Utils'; export class UpdateTokenFeeScheduleCommand { readonly #tokenId: string; readonly #feeScheduleKey: PrivateKey; readonly #decimals: number; readonly #customFees: TokenCustomFee[]; constructor( tokenId: string, feeScheduleKey: PrivateKey, decimals: number, customFees: TokenCustomFee[], ) { this.#tokenId = tokenId; this.#feeScheduleKey = feeScheduleKey; this.#decimals = decimals; this.#customFees = customFees; } public async execute(client: Client): Promise { const scheduleTransaction = new TokenFeeScheduleUpdateTransaction() .setTokenId(this.#tokenId) .setCustomFees(this.#convertCustomFees(this.#customFees, this.#decimals)) .freezeWith(client); const transaction = await scheduleTransaction.sign(this.#feeScheduleKey); return await Utils.executeTransaction(client, transaction); } #convertCustomFees( customTokenFees: TokenCustomFee[], decimals: number, ): CustomFixedFee[] { const customFees: CustomFixedFee[] = customTokenFees.map( (tokenCustomFee: TokenCustomFee) => { const customFee = new CustomFixedFee({ feeCollectorAccountId: tokenCustomFee.feeCollectorAccountId, }); if (tokenCustomFee.hbarAmount) { customFee.setHbarAmount(new Hbar(tokenCustomFee.hbarAmount)); } if (tokenCustomFee.tokenAmount) { customFee.setAmount( tokenCustomFee.tokenAmount * Math.pow(10, decimals), ); } if (tokenCustomFee.denominatingTokenId) { customFee.setDenominatingTokenId(tokenCustomFee.denominatingTokenId); } if (tokenCustomFee.allCollectorsAreExempt) { customFee.setAllCollectorsAreExempt( tokenCustomFee.allCollectorsAreExempt, ); } return customFee; }, ); return customFees; } }