Skip to content
This repository has been archived by the owner on Apr 4, 2024. It is now read-only.

Commit

Permalink
Feat: Add feeFactory (#27)
Browse files Browse the repository at this point in the history
* Feat: Add fixFactory

Signed-off-by: Stanislaw Mazowiecki <[email protected]>

---------

Signed-off-by: Stanislaw Mazowiecki <[email protected]>
  • Loading branch information
StanislawMazowieckiAriane authored Feb 8, 2024
1 parent 8f9a4ad commit 07353d2
Show file tree
Hide file tree
Showing 15 changed files with 437 additions and 15 deletions.
38 changes: 38 additions & 0 deletions src/FeeFactory.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
import { FixedFeeType, RoyaltyFeeType } from './types/fees';
import { createFixedFeeFunction } from './functions/createFixedFeeFunction';
import { CustomFixedFee, CustomRoyaltyFee } from '@hashgraph/sdk';
import { createRoyaltyFeeFunction } from './functions/createRoyaltyFeeFunction';

export class FeeFactory {
fixedFee({
collectorAccountId,
hbarAmount,
amount,
denominatingTokenId,
allCollectorsAreExempt,
}: FixedFeeType): CustomFixedFee {
return createFixedFeeFunction({
collectorAccountId,
hbarAmount,
amount,
denominatingTokenId,
allCollectorsAreExempt,
});
}

royaltyFee({
collectorAccountId,
numerator,
denominator,
fallbackFee,
allCollectorsAreExempt,
}: RoyaltyFeeType): CustomRoyaltyFee {
return createRoyaltyFeeFunction({
collectorAccountId,
numerator,
denominator,
fallbackFee,
allCollectorsAreExempt,
});
}
}
5 changes: 4 additions & 1 deletion src/HederaNFTSDK.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { Client, NftId, PrivateKey } from '@hashgraph/sdk';
import { Client, CustomFee, NftId, PrivateKey } from '@hashgraph/sdk';
import { NetworkName } from '@hashgraph/sdk/lib/client/Client';
import { createCollectionFunction } from './functions/createCollection';
import { createJsonMetadataFromCSV } from './functions/createJsonMetadataFromCSV';
Expand Down Expand Up @@ -29,13 +29,15 @@ export class HederaNFTSDK {
treasuryAccount,
keys,
maxSupply,
customFees,
}: {
collectionName: string;
collectionSymbol: string;
treasuryAccountPrivateKey?: string;
treasuryAccount?: string;
keys?: CreateCollectionKeysType;
maxSupply?: number;
customFees?: CustomFee[];
}) {
return createCollectionFunction({
client: this.client,
Expand All @@ -46,6 +48,7 @@ export class HederaNFTSDK {
treasuryAccount,
treasuryAccountPrivateKey,
maxSupply,
customFees,
});
}

Expand Down
3 changes: 2 additions & 1 deletion src/functions/createCollection.ts
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,8 @@ export const createCollectionFunction = async ({
collectionName,
collectionSymbol,
treasuryAccount,
treasuryAccountPrivateKey
treasuryAccountPrivateKey,
customFees,
});

const treasuryAccountId = treasuryAccount
Expand Down
32 changes: 32 additions & 0 deletions src/functions/createFixedFeeFunction.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
import { FixedFeeType } from '../types/fees';
import { CustomFixedFee, Hbar } from '@hashgraph/sdk';
import { validatePropsForFixedFeeFunction } from '../utils/validateProps';

export const createFixedFeeFunction = ({
collectorAccountId,
hbarAmount,
amount,
denominatingTokenId,
allCollectorsAreExempt,
}: FixedFeeType): CustomFixedFee => {
validatePropsForFixedFeeFunction({ collectorAccountId, hbarAmount, amount, denominatingTokenId });
const fixedFee = new CustomFixedFee().setFeeCollectorAccountId(collectorAccountId);

if (hbarAmount) {
fixedFee.setHbarAmount(Hbar.fromString(hbarAmount.toString()));
}

if (amount) {
fixedFee.setAmount(amount);
}

if (denominatingTokenId) {
fixedFee.setDenominatingTokenId(denominatingTokenId);
}

if (allCollectorsAreExempt) {
fixedFee.setAllCollectorsAreExempt(allCollectorsAreExempt);
}

return fixedFee;
};
37 changes: 37 additions & 0 deletions src/functions/createRoyaltyFeeFunction.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
import { CustomRoyaltyFee } from '@hashgraph/sdk';
import { RoyaltyFeeType } from '../types/fees';
import { createFixedFeeFunction } from './createFixedFeeFunction';
import { validatePropsForRoyaltyFeeFunction } from '../utils/validateProps';

export const createRoyaltyFeeFunction = ({
collectorAccountId,
numerator,
denominator,
fallbackFee,
allCollectorsAreExempt,
}: RoyaltyFeeType) => {
validatePropsForRoyaltyFeeFunction({ collectorAccountId, numerator, denominator });

const royaltyFee = new CustomRoyaltyFee()
.setFeeCollectorAccountId(collectorAccountId)
.setNumerator(numerator)
.setDenominator(denominator);

if (allCollectorsAreExempt) {
royaltyFee.setAllCollectorsAreExempt(allCollectorsAreExempt);
}

if (fallbackFee) {
royaltyFee.setFallbackFee(
createFixedFeeFunction({
collectorAccountId: fallbackFee.collectorAccountId,
hbarAmount: fallbackFee.hbarAmount,
amount: fallbackFee.amount,
denominatingTokenId: fallbackFee.denominatingTokenId,
allCollectorsAreExempt: fallbackFee.allCollectorsAreExempt,
})
);
}

return royaltyFee;
};
15 changes: 15 additions & 0 deletions src/types/fees.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
export type FixedFeeType = {
collectorAccountId: string;
hbarAmount?: number;
amount?: number;
denominatingTokenId?: string;
allCollectorsAreExempt?: boolean;
};

export type RoyaltyFeeType = {
collectorAccountId: string;
numerator: number;
denominator: number;
fallbackFee?: FixedFeeType;
allCollectorsAreExempt?: boolean;
};
17 changes: 15 additions & 2 deletions src/types/validateProps.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { Client, NftId, PrivateKey } from '@hashgraph/sdk';
import { Client, NftId, PrivateKey, CustomFee } from '@hashgraph/sdk';

export type sharedMintingValidationProps = {
batchSize?: number;
Expand All @@ -23,11 +23,24 @@ export type increaseNFTSupplyValidationProps = {
supplyKey?: PrivateKey;
};


export type validateCreateCollectionProps = {
client?: Client;
collectionName?: string;
collectionSymbol?: string;
treasuryAccountPrivateKey?: string;
treasuryAccount?: string;
customFees?: CustomFee[];
};

export type fixedFeeValidationProps = {
collectorAccountId?: string;
hbarAmount?: number;
amount?: number;
denominatingTokenId?: string;
};

export type royaltyFeeValidationProps = {
collectorAccountId?: string;
numerator: number;
denominator: number;
};
6 changes: 6 additions & 0 deletions src/utils/constants/dictionary.ts
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,12 @@ export const dictionary = {
treasuryAccountPrivateKeySignRequired:
'If you want to use treasuryAccount to sign, you need to pass the treasuryAccountPrivateKey also',
collectionNotCreated: 'Something went wrong while creating the collection',
tooManyCustomFees: 'You can only have 10 custom fees',
collectorAccountIdRequired: 'collectorAccountId is required',
numeratorRequired: 'numerator is required',
denominatorRequired: 'denominator is required',
hbarAmountOrAmountAndDenominatingToken:
'Either hbarAmount should be set and both amount and denominatingTokenId should not be set, or amount and denominatingTokenId should be set and hbarAmount should not be set.',
},
csvToJson: {
errorInCellWithHeader: (line: number, column: number) =>
Expand Down
51 changes: 50 additions & 1 deletion src/utils/validateProps.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,8 @@ import {
validateCreateCollectionProps,
uniqueMintingValidationProps,
increaseNFTSupplyValidationProps,
fixedFeeValidationProps,
royaltyFeeValidationProps,
} from '../types/validateProps';
import { dictionary } from './constants/dictionary';

Expand All @@ -26,14 +28,61 @@ export const validatePropsForCreateCollection = (props: validateCreateCollection
validateCollectionSymbol(props);
validateCollectionName(props);
validateClient(props);
validateCustomFees(props);
};

export const validatePropsForFixedFeeFunction = (props: fixedFeeValidationProps) => {
validateCollectorAccountId(props);
hbarAmountOrAmountAndDenominatingToken(props);
};

export const validatePropsForRoyaltyFeeFunction = (props: royaltyFeeValidationProps) => {
validateCollectorAccountId(props);
validateNumerator(props);
validateDenominator(props);
};

const hbarAmountOrAmountAndDenominatingToken = (props: fixedFeeValidationProps) => {
if (
(props.hbarAmount && (props.amount || props.denominatingTokenId)) ||
(!props.hbarAmount && (!props.amount || !props.denominatingTokenId))
) {
throw new Error(dictionary.createCollection.hbarAmountOrAmountAndDenominatingToken);
}
};

const validateNumerator = (props: royaltyFeeValidationProps) => {
if (Object.prototype.hasOwnProperty.call(props, 'numerator')) {
if (!props.numerator) throw new Error(dictionary.createCollection.numeratorRequired);
}
};

const validateDenominator = (props: royaltyFeeValidationProps) => {
if (Object.prototype.hasOwnProperty.call(props, 'denominator')) {
if (!props.denominator) throw new Error(dictionary.createCollection.denominatorRequired);
}
};

const validateCollectorAccountId = (props: fixedFeeValidationProps) => {
if (Object.prototype.hasOwnProperty.call(props, 'collectorAccountId')) {
if (!props.collectorAccountId)
throw new Error(dictionary.createCollection.collectorAccountIdRequired);
}
};

const validateCustomFees = (props: validateCreateCollectionProps) => {
if (Object.prototype.hasOwnProperty.call(props, 'collectionSymbol')) {
if (props.customFees && props.customFees.length > 10)
throw new Error(dictionary.createCollection.tooManyCustomFees);
}
};

export const validatePropsForIncreaseNFTSupply = (props: increaseNFTSupplyValidationProps) => {
validateSupplyKey(props);
validateBatchSize(props);
validateNFTId(props);
validateAmount(props);
}
};

const validateAccountAndPrivateKey = (props: validateCreateCollectionProps) => {
if (
Expand Down
1 change: 1 addition & 0 deletions test/__mocks__/consts.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
export const myAccountId = '0.0.12345';
export const mySecondAccountId = '0.0.123';
export const myPrivateKey =
'123e020100300506032b657004220420f8e9f8de8f7e06f7e9f8de8f7e06f7e9f8de8f7e06f7e9f8de8f7e06f7e9f8de';

Expand Down
3 changes: 3 additions & 0 deletions test/e2e/e2eConsts.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
import { HederaNFTSDK } from '../../src/HederaNFTSDK';
import 'dotenv/config';
import { FeeFactory } from '../../src/FeeFactory';

export const operatorAccountId = process.env.FIRST_ACCOUNT_ID!;
export const operatorPrivateKey = process.env.FIRST_PRIVATE_KEY!;
Expand All @@ -8,3 +9,5 @@ export const secondAccountId = process.env.SECOND_ACCOUNT_ID!;
export const secondPrivateKey = process.env.SECOND_PRIVATE_KEY!;

export const nftSDK = new HederaNFTSDK(operatorAccountId, operatorPrivateKey, 'testnet');

export const feeFactoryInstance = new FeeFactory();
Loading

0 comments on commit 07353d2

Please sign in to comment.