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

feat(sdk): add metadata key #78

Merged
merged 1 commit into from
Jun 4, 2024
Merged
Show file tree
Hide file tree
Changes from all 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
2 changes: 2 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -319,6 +319,7 @@ const defaultWeights = {
kyc_key: 50,
pause_key: 50,
fee_schedule_key: 40,
metadata_key: 200,
},
properties: {
supply_type_infinite: 20,
Expand Down Expand Up @@ -393,6 +394,7 @@ const customWeights: Weights = {
kyc_key: 50,
pause_key: 50,
fee_schedule_key: 40,
metadata_key: 200,
},
properties: {
supply_type_infinite: 20,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,7 @@ function main() {
deleted: false,
expiry_timestamp: 1671048359743907659,
fee_schedule_key: null,
metadata_key: null,
freeze_default: false,
freeze_key: null,
initial_supply: '0',
Expand Down
9,272 changes: 9 additions & 9,263 deletions package-lock.json

Large diffs are not rendered by default.

2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -93,6 +93,7 @@
"dependencies": {
"@aws-sdk/client-s3": "3.515.0",
"@aws-sdk/lib-storage": "3.515.0",
"@hashgraph/sdk": "^2.45.0",
"@babel/cli": "^7.24.5",
"@babel/core": "^7.23.3",
"@babel/eslint-parser": "^7.23.3",
Expand All @@ -102,7 +103,6 @@
"@babel/preset-env": "^7.22.15",
"@babel/preset-typescript": "^7.24.1",
"@babel/register": "^7.22.5",
"@hashgraph/sdk": "^2.41.0",
"ajv": "^8.13.0",
"ajv-formats": "^3.0.1",
"axios": "^1.6.7",
Expand Down
4 changes: 4 additions & 0 deletions src/nftSDKFunctions/create-collection.ts
Original file line number Diff line number Diff line change
Expand Up @@ -110,6 +110,10 @@ export const createCollectionFunction = async ({
transaction = transaction.setTokenMemo(memo);
}

if (keys?.metadataKey) {
transaction = transaction.setMetadataKey(keys?.metadataKey);
}

transaction = transaction.freezeWith(client);

let signTx = await transaction.sign(<PrivateKey>treasuryAccountPrivateKeyId);
Expand Down
1 change: 1 addition & 0 deletions src/risk/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,7 @@ const defaultWeights: Weights = {
kyc_key: 50,
pause_key: 50,
fee_schedule_key: 40,
metadata_key: 200,
},
properties: {
supply_type_infinite: 20,
Expand Down
5 changes: 5 additions & 0 deletions src/test/__mocks__/exampleMetadata.ts
Original file line number Diff line number Diff line change
Expand Up @@ -78,6 +78,7 @@ export const exampleMetadata = {
deleted: false,
expiry_timestamp: 1658649527188146000,
fee_schedule_key: null,
metadata_key: null,
freeze_default: false,
freeze_key: null,
initial_supply: '0',
Expand Down Expand Up @@ -169,6 +170,10 @@ export const metadataWihAllKeysProvided = {
_type: 'ED25519',
key: '49efa5f54192470706914df50f17472f6f2310b8476293822e0d49b313ca2875',
},
metadata_key: {
_type: 'ED25519',
key: '49efa5f54192470706914df50f17472f6f2310b8476293822e0d49b313ca2875',
},
freeze_default: false,
freeze_key: {
_type: 'ED25519',
Expand Down
2 changes: 2 additions & 0 deletions src/test/e2e/calculate-risk-score-from-token-id-e2e.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -63,6 +63,7 @@ describe('calculateRiskScoreFromTokenIdE2E', () => {
kyc_key: 5,
pause_key: 5,
fee_schedule_key: 4,
metadata_key: 0,
},
properties: {
supply_type_infinite: 20,
Expand All @@ -88,6 +89,7 @@ describe('calculateRiskScoreFromTokenIdE2E', () => {
kyc_key: 50,
pause_key: 50,
fee_schedule_key: 40,
metadata_key: 100,
},
properties: {
supply_type_infinite: 200,
Expand Down
65 changes: 56 additions & 9 deletions src/test/e2e/create-collection/create-collection-e2e.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@ import { nftSDK, secondAccountId, secondPrivateKey } from '../e2e-consts';
import { LONG_E2E_TIMEOUT, MIRROR_NODE_DELAY } from '../../__mocks__/consts';
import { getTokenInfo } from '../../../utils/hedera/get-token-info';
import { add, milliseconds, millisecondsToSeconds } from 'date-fns';
import { dictionary } from '../../../utils/constants/dictionary';

describe('createCollectionFunction e2e', () => {
it(
Expand Down Expand Up @@ -94,8 +95,7 @@ describe('createCollectionFunction e2e', () => {
LONG_E2E_TIMEOUT
);

//This test is failing because of the issue in the Hedera SDK. When the issue is resolved, this test should be enabled (remove .failing).
it.failing(
it(
'creates a collection with auto renew account and period',
async () => {
const expectedAutoRenewPeriod = millisecondsToSeconds(milliseconds({ days: 30 }));
Expand All @@ -119,20 +119,44 @@ describe('createCollectionFunction e2e', () => {
);

it(
'creates a collection with auto renew period',
'creates a collection with auto renew account',
async () => {
const expectedAutoRenewPeriod = millisecondsToSeconds(milliseconds({ days: 30 }));
const tokenId = await nftSDK.createCollection({
collectionName: 'test_name_auto_renew_period',
collectionSymbol: 'TNARP',
collectionName: 'test_name_auto_renew_account',
collectionSymbol: 'TNARA',
treasuryAccountPrivateKey: secondPrivateKey,
treasuryAccount: secondAccountId,
autoRenewPeriod: expectedAutoRenewPeriod,
autoRenewAccount: secondAccountId,
autoRenewAccountPrivateKey: secondPrivateKey,
});

const tokenInfo = await getTokenInfo(tokenId, nftSDK.client);
expect(tokenInfo.autoRenewPeriod).toBeDefined();
expect(tokenInfo.autoRenewPeriod?.seconds).toEqual(new Long(expectedAutoRenewPeriod, 0));
expect(tokenInfo.autoRenewAccountId).toBeDefined();
expect(tokenInfo.autoRenewAccountId?.toString()).toEqual(secondAccountId);
},
LONG_E2E_TIMEOUT
);

it(
'fails to create a collection with only auto renew period',
async () => {
const expectedAutoRenewPeriod = millisecondsToSeconds(milliseconds({ days: 30 }));
let errorCaught: Error | null = null;

try {
await nftSDK.createCollection({
collectionName: 'test_name_auto_renew_period_only',
collectionSymbol: 'TNARPONLY',
treasuryAccountPrivateKey: secondPrivateKey,
treasuryAccount: secondAccountId,
autoRenewPeriod: expectedAutoRenewPeriod,
});
} catch (error) {
errorCaught = error as Error;
}

expect(errorCaught).toBeDefined();
expect(errorCaught!.message).toEqual(dictionary.createCollection.autoRenewPeriodValidation);
},
LONG_E2E_TIMEOUT
);
Expand Down Expand Up @@ -185,6 +209,7 @@ describe('createCollectionFunction e2e', () => {
const supplyKey = PrivateKey.generateED25519();
const feeScheduleKey = PrivateKey.generateED25519();
const pauseKey = PrivateKey.generateED25519();
const metadataKey = PrivateKey.generateED25519();

const tokenId = await nftSDK.createCollection({
collectionName: 'test_name_all_keys',
Expand All @@ -199,6 +224,7 @@ describe('createCollectionFunction e2e', () => {
supply: supplyKey,
feeSchedule: feeScheduleKey,
pause: pauseKey,
metadataKey,
},
});

Expand All @@ -210,6 +236,27 @@ describe('createCollectionFunction e2e', () => {
expect(tokenInfo.supplyKey?.toString()).toEqual(supplyKey.publicKey.toStringDer());
expect(tokenInfo.feeScheduleKey?.toString()).toEqual(feeScheduleKey.publicKey.toStringDer());
expect(tokenInfo.pauseKey?.toString()).toEqual(pauseKey.publicKey.toStringDer());
expect(tokenInfo.metadataKey?.toString()).toEqual(metadataKey.publicKey.toStringDer());
},
LONG_E2E_TIMEOUT
);

it(
'creates a collection when metadataKey is public',
async () => {
const metadataKey = PrivateKey.generateED25519().publicKey;
const tokenId = await nftSDK.createCollection({
collectionName: 'test_name_metadataKey_public',
collectionSymbol: 'TNMP',
treasuryAccountPrivateKey: secondPrivateKey,
treasuryAccount: secondAccountId,
keys: {
metadataKey,
},
});

const tokenInfo = await getTokenInfo(tokenId, nftSDK.client);
expect(tokenInfo.metadataKey?.toString()).toEqual(metadataKey.toStringDer());
},
LONG_E2E_TIMEOUT
);
Expand Down
23 changes: 23 additions & 0 deletions src/test/unit/create-collection/create-collection.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -62,6 +62,7 @@ jest.mock('@hashgraph/sdk', () => {
setPauseKey: jest.fn().mockReturnThis(),
setMaxSupply: jest.fn().mockReturnThis(),
setCustomFees: jest.fn().mockReturnThis(),
setMetadataKey: jest.fn().mockReturnThis(),
freezeWith: jest.fn().mockReturnThis(),
sign: jest.fn().mockResolvedValue({
execute: jest.fn().mockResolvedValue({
Expand Down Expand Up @@ -204,4 +205,26 @@ describe('createCollectionFunction', () => {

expect(tokenId).toEqual('1.2.1234');
});

it('should create a collection with metadataKey provided', async () => {
const client = Client.forTestnet();
const collectionName = 'test';
const collectionSymbol = 'test2';
const metadataKey = PrivateKey.generateED25519();
const keys = {
admin: PrivateKey.generateED25519(),
supply: myPrivateKey,
metadataKey,
};

const tokenId = await createCollectionFunction({
client,
myPrivateKey,
collectionName,
collectionSymbol,
keys,
});

expect(tokenId).toEqual('1.2.1234');
});
});
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,7 @@ const customWeights: Weights = {
kyc_key: 200,
pause_key: 200,
fee_schedule_key: 200,
metadata_key: 200,
},
properties: {
supply_type_infinite: 200,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -55,6 +55,7 @@ describe('calculateRiskScoreFromTokenId', () => {
kyc_key: 5,
pause_key: 5,
fee_schedule_key: 4,
metadata_key: 20,
},
properties: {
supply_type_infinite: 20,
Expand Down Expand Up @@ -83,6 +84,7 @@ describe('calculateRiskScoreFromTokenId', () => {
kyc_key: 50,
pause_key: 50,
fee_schedule_key: 40,
metadata_key: 500,
},
properties: {
supply_type_infinite: 200,
Expand Down Expand Up @@ -154,6 +156,7 @@ describe('calculateRiskScoreFromTokenId', () => {
kyc_key: 50,
pause_key: 50,
fee_schedule_key: 50,
metadata_key: 500,
},
properties: {
supply_type_infinite: 200,
Expand All @@ -173,7 +176,8 @@ describe('calculateRiskScoreFromTokenId', () => {
customWeights,
customRiskLevels,
});
expect(riskScore).toBe(1500);

expect(riskScore).toBe(2000);
expect(riskLevel).toBe('HIGH');
expect(riskScoreFactors).toEqual({
supply_key: 250,
Expand All @@ -183,6 +187,7 @@ describe('calculateRiskScoreFromTokenId', () => {
freeze_key: 100,
pause_key: 50,
wipe_key: 500,
metadata_key: 500,
});
});
});
1 change: 1 addition & 0 deletions src/types/create-collection.ts
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,7 @@ export type CreateCollectionKeysType = {
supply?: Key;
feeSchedule?: Key;
pause?: Key;
metadataKey?: Key;
};

export type CreateCollectionType = {
Expand Down
1 change: 1 addition & 0 deletions src/types/risk.ts
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@ export interface WeightKeys {
kyc_key: number;
pause_key: number;
fee_schedule_key: number;
metadata_key: number;
}

export interface MetadataPublicKey {
Expand Down
1 change: 1 addition & 0 deletions src/utils/constants/dictionary.ts
Original file line number Diff line number Diff line change
Expand Up @@ -76,6 +76,7 @@ export const dictionary = {
collectionNotCreated: 'Something went wrong while creating the collection',
autoRenewAccountPrivateKeySignRequired:
'If you want to use autoRenewAccount to sign, you need to pass the autoRenewAccountPrivateKey also',
autoRenewPeriodValidation: 'Both autoRenewAccount and autoRenewAccountPrivateKey must be provided if autoRenewPeriod is specified',
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.',
},
Expand Down
3 changes: 3 additions & 0 deletions src/utils/validate-props.ts
Original file line number Diff line number Diff line change
Expand Up @@ -125,6 +125,9 @@ const validateAutoRenewAccount = (props: validateCreateCollectionProps) => {
if ((props.autoRenewAccount && !props.autoRenewAccountPrivateKey) || (!props.autoRenewAccount && props.autoRenewAccountPrivateKey)) {
throw new Error(dictionary.createCollection.autoRenewAccountPrivateKeySignRequired);
}
if (props.autoRenewPeriod && (!props.autoRenewAccount || !props.autoRenewAccountPrivateKey)) {
throw new Error(dictionary.createCollection.autoRenewPeriodValidation);
}
}
};

Expand Down
Loading