diff --git a/packages/core-sdk/src/resources/license.ts b/packages/core-sdk/src/resources/license.ts index d227f47f..ef92815c 100644 --- a/packages/core-sdk/src/resources/license.ts +++ b/packages/core-sdk/src/resources/license.ts @@ -2,15 +2,15 @@ import { AxiosInstance } from "axios"; import { PublicClient, WalletClient, encodeFunctionData, getAddress } from "viem"; import { handleError } from "../utils/errors"; -import { parseToBigInt } from "../utils/utils"; +import { parseToBigInt, waitTxAndFilterLog } from "../utils/utils"; import { LicenseReadOnlyClient } from "./licenseReadOnly"; import { IPAccountImplMerged } from "../abi/ipAccountImpl.abi"; -import { LicenseRegistryRaw } from "../abi/licenseRegistry.abi"; +import { LicenseRegistryConfig, LicenseRegistryRaw } from "../abi/licenseRegistry.abi"; import { linkIpToParentRequest, linkIpToParentResponse, - // mintLicenseRequest, - // mintLicenseResponse, + mintLicenseRequest, + mintLicenseResponse, // transferRequest, // transferResponse, } from "../types/resources/license"; @@ -23,51 +23,50 @@ export class LicenseClient extends LicenseReadOnlyClient { this.wallet = wallet; } - // public async mintLicense(request: mintLicenseRequest): Promise { - // try { - // const IPAccountConfig = { - // abi: IPAccountImplMerged, - // address: getAddress(request.licensorIp), - // }; + public async mintLicense(request: mintLicenseRequest): Promise { + try { + const IPAccountConfig = { + abi: IPAccountImplMerged, + // TODO: find out which ipId to use to call the execute fn + address: getAddress(request.licensorIps[0]), + }; - // const licenseRegistry = getAddress( - // process.env.LICENSE_REGISTRY || process.env.NEXT_PUBLIC_LICENSE_REGISTRY || "", - // ); + const licenseRegistry = getAddress( + process.env.LICENSE_REGISTRY || process.env.NEXT_PUBLIC_LICENSE_REGISTRY || "", + ); - // const { request: call } = await this.rpcClient.simulateContract({ - // ...IPAccountConfig, - // functionName: "execute", - // args: [ - // licenseRegistry, - // 0, - // encodeFunctionData({ - // abi: LicenseRegistryRaw, - // functionName: "mintLicense", - // args: [ - // request.policyId, - // request.licensorIp, - // request.mintAmount, - // request.receiverAddress, - // // TODO: - // ], - // }), - // ], - // account: this.wallet.account, - // }); - // const txHash = await this.wallet.writeContract(call); - // // if (request.txOptions?.waitForTransaction) { - // // const targetLog = await waitTxAndFilterLog(this.rpcClient, txHash, { - // // ...LicenseRegistryConfig, - // // eventName: "LicenseMinted", - // // }); - // // return { txHash: txHash, licenseId: targetLog?.args.account.toString() }; - // // } else { - // return { txHash: txHash }; - // // } - // } catch (error) { - // handleError(error, "Failed to mint license"); - // } - // } + const { request: call } = await this.rpcClient.simulateContract({ + ...IPAccountConfig, + functionName: "execute", + args: [ + licenseRegistry, + 0, + encodeFunctionData({ + abi: LicenseRegistryRaw, + functionName: "mintLicense", + args: [ + { policyId: parseToBigInt(request.policyId), licensorIpIds: request.licensorIps }, + parseToBigInt(request.mintAmount), + getAddress(request.receiverAddress), + ], + }), + ], + account: this.wallet.account, + }); + const txHash = await this.wallet.writeContract(call); + if (request.txOptions?.waitForTransaction) { + const targetLog = await waitTxAndFilterLog(this.rpcClient, txHash, { + ...LicenseRegistryConfig, + eventName: "LicenseMinted", + }); + return { txHash: txHash, licenseId: targetLog?.args?.licenseId.toString() }; + } else { + return { txHash: txHash }; + } + } catch (error) { + handleError(error, "Failed to mint license"); + } + } public async linkIpToParent(request: linkIpToParentRequest): Promise { try { diff --git a/packages/core-sdk/src/resources/policy.ts b/packages/core-sdk/src/resources/policy.ts index a245ba03..da760af9 100644 --- a/packages/core-sdk/src/resources/policy.ts +++ b/packages/core-sdk/src/resources/policy.ts @@ -3,10 +3,7 @@ import { PublicClient, WalletClient, encodeFunctionData, getAddress } from "viem import { handleError } from "../utils/errors"; import { LicenseRegistryConfig, LicenseRegistryRaw } from "../abi/licenseRegistry.abi"; -import { - parseToBigInt, - // waitTxAndFilterLog -} from "../utils/utils"; +import { parseToBigInt, waitTxAndFilterLog } from "../utils/utils"; import { PolicyReadOnlyClient } from "./policyReadOnly"; import { addPolicyRequest, @@ -93,18 +90,17 @@ export class PolicyClient extends PolicyReadOnlyClient { const txHash = await this.wallet.writeContract(call); // TODO: the emit event doesn't return anything - // if (request.txOptions?.waitForTransaction) { - // await waitTxAndFilterLog(this.rpcClient, txHash, { - // ...AccessControllerConfig, - // eventName: "PermissionSet", - // }); - // return { txHash: txHash }; - // } else { - - return { txHash: txHash }; + if (request.txOptions?.waitForTransaction) { + await waitTxAndFilterLog(this.rpcClient, txHash, { + ...LicenseRegistryConfig, + eventName: "PolicyAddedToIpId", + }); + return { txHash: txHash }; + } else { + return { txHash: txHash }; + } } catch (error) { handleError(error, "Failed to add policy to IP"); } - // TODO: use getIpAccount to get the ipId } } diff --git a/packages/core-sdk/src/types/resources/license.ts b/packages/core-sdk/src/types/resources/license.ts index 1845dc1d..c0462b57 100644 --- a/packages/core-sdk/src/types/resources/license.ts +++ b/packages/core-sdk/src/types/resources/license.ts @@ -1,3 +1,5 @@ +import { Address } from "viem"; + import { QueryOptions, TxOptions } from "../options"; export type License = { @@ -24,7 +26,8 @@ export type ListLicensesResponse = { export type mintLicenseRequest = { policyId: string; - licensorIp: string; + licensorIps: Address[]; + mintAmount: string; receiverAddress: string; txOptions?: TxOptions; @@ -32,7 +35,7 @@ export type mintLicenseRequest = { export type mintLicenseResponse = { txHash: string; - // licenseId?: number; + licenseId?: string; // isNew?: boolean; };