Skip to content

Commit

Permalink
Refactor metadata param
Browse files Browse the repository at this point in the history
  • Loading branch information
bonnie57 committed May 14, 2024
1 parent e34f21e commit ea5f554
Show file tree
Hide file tree
Showing 5 changed files with 164 additions and 100 deletions.
80 changes: 39 additions & 41 deletions packages/core-sdk/src/resources/ipAsset.ts
Original file line number Diff line number Diff line change
Expand Up @@ -204,9 +204,9 @@ export class IPAssetClient {
* @param request.nftContract The address of the NFT collection.
* @param request.pilType The type of the PIL.
* @param request.metadata - The metadata for the IP.
* @param request.metadataURI The URI of the metadata for the IP.
* @param request.metadata [Optional] The metadata for the IP.
* @param request.nftMetadata The metadata for the IP NFT.
* @param request.metadata.metadataURI [Optional] The URI of the metadata for the IP.
* @param request.metadata.metadataHash [Optional] The metadata for the IP.
* @param request.metadata.nftMetadataHash [Optional] The metadata for the IP NFT.
* @param request.recipient [Optional] The address of the recipient of the minted NFT.
* @param request.mintingFee [Optional] The fee to be paid when minting a license.
* @param request.commercialRevShare [Optional] Percentage of revenue that must be shared with the licensor.
Expand All @@ -216,7 +216,7 @@ export class IPAssetClient {
* @emits IPRegistered (ipId, chainId, tokenContract, tokenId, name, uri, registrationDate)
* @emits LicenseTermsAttached (caller, ipId, licenseTemplate, licenseTermsId)
*/
public async createIpAssetWithPilTerms(
public async mintAndRegisterIpAssetWithPilTerms(
request: CreateIpAssetWithPilTermsRequest,
): Promise<CreateIpAssetWithPilTermsResponse> {
try {
Expand All @@ -237,20 +237,20 @@ export class IPAssetClient {
terms: licenseTerm,
metadata: {
metadataURI: "",
metadataHash: toHex("", { size: 32 }),
nftMetadataHash: toHex("", { size: 32 }),
metadataHash: toHex(0, { size: 32 }),
nftMetadataHash: toHex(0, { size: 32 }),
},
};
if (
request.metadata &&
request.metadata.metadataURI &&
request.metadata.metadata &&
request.metadata.nftMetadata
(request.metadata.metadataHash !== toHex(0, { size: 32 }) ||
request.metadata.metadataURI !== "" ||
request.metadata.nftMetadataHash !== toHex(0, { size: 32 }))
) {
object.metadata = {
metadataURI: request.metadata.metadataURI,
metadataHash: toHex(request.metadata.metadata, { size: 32 }),
nftMetadataHash: toHex(request.metadata.nftMetadata, { size: 32 }),
metadataURI: request.metadata.metadataURI || object.metadata.metadataURI,
metadataHash: request.metadata.metadataHash || object.metadata.metadataHash,
nftMetadataHash: request.metadata.nftMetadataHash || object.metadata.nftMetadataHash,
};
}
const txHash = await this.spgClient.mintAndRegisterIpAndAttachPilTerms(object);
Expand Down Expand Up @@ -278,9 +278,9 @@ export class IPAssetClient {
* @param request.tokenId The ID of the NFT.
* @param request.pilType The type of the PIL.
* @param request.metadata - [Optional] The desired metadata for the newly registered IP.
* @param request.metadataURI The the metadata for the IP hash.
* @param request.metadata The metadata for the IP.
* @param request.nftMetadata The metadata for the IP NFT.
* @param request.metadata.metadataURI [Optional] The the metadata for the IP hash.
* @param request.metadata.metadataHash [Optional] The metadata for the IP.
* @param request.metadata.nftMetadataHash [Optional] The metadata for the IP NFT.
* @param request.sigMetadata - [Optional] The signature data for execution via IP Account.
* @param request.sigMetadata.signer The address of the signer for execution with signature.
* @param request.sigMetadata.deadline The deadline for the signature.
Expand Down Expand Up @@ -320,8 +320,8 @@ export class IPAssetClient {
terms: licenseTerm,
metadata: {
metadataURI: "",
metadataHash: toHex("", { size: 32 }),
nftMetadataHash: toHex("", { size: 32 }),
metadataHash: toHex(0, { size: 32 }),
nftMetadataHash: toHex(0, { size: 32 }),
},
sigMetadata: {
signer: zeroAddress,
Expand All @@ -336,21 +336,21 @@ export class IPAssetClient {
};
if (
request.metadata &&
request.metadata.metadataURI &&
request.metadata.metadata &&
request.metadata.nftMetadata
(request.metadata.metadataHash !== toHex(0, { size: 32 }) ||
request.metadata.metadataURI !== "" ||
request.metadata.nftMetadataHash !== toHex(0, { size: 32 }))
) {
object.metadata = {
metadataURI: request.metadata.metadataURI,
metadataHash: toHex(request.metadata.metadata, { size: 32 }),
nftMetadataHash: toHex(request.metadata.nftMetadata, { size: 32 }),
metadataURI: request.metadata.metadataURI || object.metadata.metadataURI,
metadataHash: request.metadata.metadataHash || object.metadata.metadataHash,
nftMetadataHash: request.metadata.nftMetadataHash || object.metadata.nftMetadataHash,
};
}
if (
request.sigMetadata &&
request.sigMetadata.signature &&
request.sigMetadata.signer &&
request.sigMetadata.deadline
request.sigMetadata.signer !== zeroAddress &&
request.sigMetadata.deadline !== BigInt(0) &&
request.sigMetadata.signature.length !== 0
) {
object.sigMetadata = {
signer: getAddress(request.sigMetadata.signer),
Expand Down Expand Up @@ -383,9 +383,9 @@ export class IPAssetClient {
* @param request.sigRegister.deadline The deadline for the signature.
* @param request.sigRegister.signature The signature for the execution via IP Account.
* @param request.metadata - [Optional] The desired metadata for the newly registered IP.
* @param request.metadata.metadataURI The URI of the metadata for the IP.
* @param request.metadata.metadata The metadata for the IP.
* @param request.metadata.nftMetadata The the metadata for the IP NFT.
* @param request.metadata.metadataURI [Optional] The URI of the metadata for the IP.
* @param request.metadata.metadataHash [Optional] The metadata for the IP.
* @param request.metadata.nftMetadataHash [Optional] The the metadata for the IP NFT.
* @param request.sigMetadata - [Optional] Signature data for setAll (metadata) for the IP via the Core Metadata Module.
* @param request.sigMetadata.signer The address of the signer for execution with signature.
* @param request.sigMetadata.deadline The deadline for the signature.
Expand Down Expand Up @@ -445,17 +445,15 @@ export class IPAssetClient {
},
metadata: {
metadataURI: "",
metadataHash: toHex("", { size: 32 }),
nftMetadataHash: toHex("", { size: 32 }),
metadataHash: toHex(0, { size: 32 }),
nftMetadataHash: toHex(0, { size: 32 }),
},
};
if (
request.sigMetadata &&
request.sigMetadata.signature &&
request.sigMetadata.signature.length > 0 &&
request.sigMetadata.signer &&
request.sigMetadata.signer !== zeroAddress &&
request.sigMetadata.deadline !== 0n
request.sigMetadata.deadline !== BigInt(0) &&
request.sigMetadata.signature.length !== 0
) {
object.sigMetadata = {
signer: getAddress(request.sigMetadata.signer),
Expand All @@ -465,14 +463,14 @@ export class IPAssetClient {
}
if (
request.metadata &&
request.metadata.metadata &&
request.metadata.metadataURI &&
request.metadata.nftMetadata
(request.metadata.metadataHash !== toHex(0, { size: 32 }) ||
request.metadata.metadataURI !== "" ||
request.metadata.nftMetadataHash !== toHex(0, { size: 32 }))
) {
object.metadata = {
metadataURI: request.metadata.metadataURI,
metadataHash: toHex(request.metadata.metadata, { size: 32 }),
nftMetadataHash: toHex(request.metadata.nftMetadata, { size: 32 }),
metadataURI: request.metadata.metadataURI || object.metadata.metadataURI,
metadataHash: request.metadata.metadataHash || object.metadata.metadataHash,
nftMetadataHash: request.metadata.nftMetadataHash || object.metadata.nftMetadataHash,
};
}
const txHash = await this.spgClient.registerIpAndMakeDerivative(object);
Expand Down
18 changes: 9 additions & 9 deletions packages/core-sdk/src/types/resources/ipAsset.ts
Original file line number Diff line number Diff line change
Expand Up @@ -41,9 +41,9 @@ export type CreateIpAssetWithPilTermsRequest = {
nftContract: Address;
pilType: PIL_TYPE;
metadata?: {
metadataURI: string;
metadata: string;
nftMetadata: string;
metadataURI?: string;
metadataHash?: Hex;
nftMetadataHash?: Hex;
};
recipient?: Address;
mintingFee?: string;
Expand All @@ -68,9 +68,9 @@ export type RegisterIpAndMakeDerivativeRequest = {
licenseTemplate?: Address;
};
metadata?: {
metadataURI: string;
metadata: string;
nftMetadata: string;
metadataURI?: string;
metadataHash?: Hex;
nftMetadataHash?: Hex;
};
sigMetadata?: {
signer: Address;
Expand All @@ -94,9 +94,9 @@ export type RegisterIpAndAttachPilTermsRequest = {
nftContract: Address;
tokenId: bigint | string | number;
metadata: {
metadataURI: string;
metadata: string;
nftMetadata: string;
metadataURI?: string;
metadataHash?: Hex;
nftMetadataHash?: Hex;
};
pilType: PIL_TYPE;
sigMetadata: {
Expand Down
2 changes: 1 addition & 1 deletion packages/core-sdk/test/integration/ipAccount.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -63,7 +63,7 @@ describe("Ip Account functions", () => {
expect(response.txHash).to.be.a("string").and.not.empty;
});

it.skip("should not throw error when executeWithSig setting permission", async () => {
it("should not throw error when executeWithSig setting permission", async () => {
const account = privateKeyToAccount(process.env.SEPOLIA_WALLET_PRIVATE_KEY as Hex);
const deadline = (await getBlockTimestamp()) + 100n;
const state = await client.ipAccount.getIpAccountNonce(ipId);
Expand Down
62 changes: 30 additions & 32 deletions packages/core-sdk/test/integration/ipAsset.test.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import chai from "chai";
import { StoryClient } from "../../src";
import { Hex, encodeFunctionData, getAddress, toFunctionSelector, Address } from "viem";
import { Hex, encodeFunctionData, getAddress, toFunctionSelector, Address, toHex } from "viem";
import chaiAsPromised from "chai-as-promised";
import { MockERC721, getBlockTimestamp, getStoryClientInSepolia, getTokenId } from "./utils/util";
import { PIL_TYPE } from "../../src/types/resources/license";
Expand Down Expand Up @@ -183,60 +183,60 @@ describe("IP Asset Functions ", () => {

describe("should not throw error when mint and register ip and attach pil terms", async () => {
it("Non-Commercial Remix", async () => {
const result = await client.ipAsset.createIpAssetWithPilTerms({
const result = await client.ipAsset.mintAndRegisterIpAssetWithPilTerms({
nftContract,
pilType: PIL_TYPE.NON_COMMERCIAL_REMIX,
metadata: {
metadataURI: "test-uri",
metadata: "test-metadata-hash",
nftMetadata: "test-nft-metadata-hash",
metadataHash: toHex("test-metadata-hash", { size: 32 }),
nftMetadataHash: toHex("test-nft-metadata-hash", { size: 32 }),
},
});
expect(result.txHash).to.be.a("string").and.not.empty;
});

it("Commercial Use", async () => {
const result = await client.ipAsset.createIpAssetWithPilTerms({
const result = await client.ipAsset.mintAndRegisterIpAssetWithPilTerms({
nftContract,
pilType: PIL_TYPE.COMMERCIAL_USE,
commercialRevShare: 10,
mintingFee: "100",
currency: MockERC20.address,
metadata: {
metadataURI: "test-uri",
metadata: "test-metadata-hash",
nftMetadata: "test-nft-metadata-hash",
metadataHash: toHex("test-metadata-hash", { size: 32 }),
nftMetadataHash: toHex("test-nft-metadata-hash", { size: 32 }),
},
});
expect(result.txHash).to.be.a("string").and.not.empty;
});

it("Commercial Remix", async () => {
const result = await client.ipAsset.createIpAssetWithPilTerms({
const result = await client.ipAsset.mintAndRegisterIpAssetWithPilTerms({
nftContract,
pilType: PIL_TYPE.COMMERCIAL_REMIX,
commercialRevShare: 10,
mintingFee: "100",
currency: MockERC20.address,
metadata: {
metadataURI: "test-uri",
metadata: "test-metadata-hash",
nftMetadata: "test-nft-metadata-hash",
metadataHash: toHex("test-metadata-hash", { size: 32 }),
nftMetadataHash: toHex("test-nft-metadata-hash", { size: 32 }),
},
});
expect(result.txHash).to.be.a("string").and.not.empty;
});
it("should get the related log when createIpAssetWithPilTerms given waitForTransaction is true ", async () => {
const result = await client.ipAsset.createIpAssetWithPilTerms({
const result = await client.ipAsset.mintAndRegisterIpAssetWithPilTerms({
nftContract,
pilType: PIL_TYPE.COMMERCIAL_REMIX,
commercialRevShare: 10,
mintingFee: "100",
currency: MockERC20.address,
metadata: {
metadataURI: "test-uri",
metadata: "test-metadata-hash",
nftMetadata: "test-nft-metadata-hash",
metadataHash: toHex("test-metadata-hash", { size: 32 }),
nftMetadataHash: toHex("test-nft-metadata-hash", { size: 32 }),
},
txOptions: {
waitForTransaction: true,
Expand All @@ -251,21 +251,21 @@ describe("IP Asset Functions ", () => {

it("should not throw error when register registerDerivativeIp", async () => {
const tokenChildId = await getTokenId(nftContract);
const { ipId: parentIpId, licenseTermsId } = await client.ipAsset.createIpAssetWithPilTerms({
nftContract,
pilType: PIL_TYPE.NON_COMMERCIAL_REMIX,
commercialRevShare: 10,
mintingFee: "100",
currency: MockERC20.address,
metadata: {
metadataURI: "test-uri",
metadata: "test-metadata-hash",
nftMetadata: "test-nft-metadata-hash",
},
txOptions: {
waitForTransaction: true,
},
});
const { ipId: parentIpId, licenseTermsId } =
await client.ipAsset.mintAndRegisterIpAssetWithPilTerms({
nftContract,
pilType: PIL_TYPE.NON_COMMERCIAL_REMIX,
commercialRevShare: 10,
mintingFee: "100",
currency: MockERC20.address,
metadata: {
metadataHash: toHex("test-metadata-hash", { size: 32 }),
nftMetadataHash: toHex("test-nft-metadata-hash", { size: 32 }),
},
txOptions: {
waitForTransaction: true,
},
});
const childIpId = await client.ipAsset.getIpIdAddress(nftContract, tokenChildId!);
const deadline = (await getBlockTimestamp()) + 1000n;
const sigMetadata = await getPermissionSignatureForSpg({
Expand Down Expand Up @@ -293,8 +293,7 @@ describe("IP Asset Functions ", () => {
},
metadata: {
metadataURI: "test-uri",
metadata: "test-metadata-hash",
nftMetadata: "test-nft-metadata-hash",
metadataHash: toHex("test-metadata-hash", { size: 32 }),
},
sigMetadata: {
signer: process.env.SEPOLIA_TEST_WALLET_ADDRESS as Hex,
Expand Down Expand Up @@ -337,8 +336,7 @@ describe("IP Asset Functions ", () => {
tokenId: tokenId!,
metadata: {
metadataURI: "test-uri",
metadata: "test-metadata-hash",
nftMetadata: "test-nft-metadata-hash",
nftMetadataHash: toHex("test-nft-metadata-hash", { size: 32 }),
},
sigMetadata: {
signer: process.env.SEPOLIA_TEST_WALLET_ADDRESS as Address,
Expand Down
Loading

0 comments on commit ea5f554

Please sign in to comment.