Skip to content

Commit

Permalink
fix policy and license fns and update fn signature
Browse files Browse the repository at this point in the history
  • Loading branch information
allenchuang committed Feb 1, 2024
1 parent f6bfb64 commit 947a5e5
Show file tree
Hide file tree
Showing 3 changed files with 61 additions and 63 deletions.
93 changes: 46 additions & 47 deletions packages/core-sdk/src/resources/license.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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";
Expand All @@ -23,51 +23,50 @@ export class LicenseClient extends LicenseReadOnlyClient {
this.wallet = wallet;
}

// public async mintLicense(request: mintLicenseRequest): Promise<mintLicenseResponse> {
// try {
// const IPAccountConfig = {
// abi: IPAccountImplMerged,
// address: getAddress(request.licensorIp),
// };
public async mintLicense(request: mintLicenseRequest): Promise<mintLicenseResponse> {
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<linkIpToParentResponse> {
try {
Expand Down
24 changes: 10 additions & 14 deletions packages/core-sdk/src/resources/policy.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down Expand Up @@ -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
}
}
7 changes: 5 additions & 2 deletions packages/core-sdk/src/types/resources/license.ts
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
import { Address } from "viem";

import { QueryOptions, TxOptions } from "../options";

export type License = {
Expand All @@ -24,15 +26,16 @@ export type ListLicensesResponse = {

export type mintLicenseRequest = {
policyId: string;
licensorIp: string;
licensorIps: Address[];

mintAmount: string;
receiverAddress: string;
txOptions?: TxOptions;
};

export type mintLicenseResponse = {
txHash: string;
// licenseId?: number;
licenseId?: string;
// isNew?: boolean;
};

Expand Down

0 comments on commit 947a5e5

Please sign in to comment.