Skip to content

Commit

Permalink
Merge pull request #142 from storyprotocol/dev
Browse files Browse the repository at this point in the history
1.0.0-rc.3
  • Loading branch information
edisonz0718 authored Apr 15, 2024
2 parents 838ca2c + bcac317 commit f9be48a
Show file tree
Hide file tree
Showing 14 changed files with 613 additions and 375 deletions.
4 changes: 2 additions & 2 deletions packages/core-sdk/package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "@story-protocol/core-sdk",
"version": "1.0.0-rc.2",
"version": "1.0.0-rc.3",
"description": "Story Protocol Core SDK",
"main": "dist/story-protocol-core-sdk.cjs.js",
"module": "dist/story-protocol-core-sdk.esm.js",
Expand Down Expand Up @@ -78,7 +78,7 @@
"src/index.ts",
"src/types/**/*"
],
"check-coverage": true,
"check-coverage": false,
"lines": 70,
"functions": 70,
"branches": 70,
Expand Down
135 changes: 133 additions & 2 deletions packages/core-sdk/src/abi/generated.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5130,6 +5130,30 @@ export class IpAssetRegistryClient extends IpAssetRegistryReadOnlyClient {

// Contract IpRoyaltyVaultImpl =============================================================

/**
* IpRoyaltyVaultImplRoyaltyTokensCollectedEvent
*
* @param ancestorIpId address
* @param royaltyTokensCollected uint256
*/
export type IpRoyaltyVaultImplRoyaltyTokensCollectedEvent = {
ancestorIpId: Address;
royaltyTokensCollected: bigint;
};

/**
* IpRoyaltyVaultImplSnapshotCompletedEvent
*
* @param snapshotId uint256
* @param snapshotTimestamp uint256
* @param unclaimedTokens uint32
*/
export type IpRoyaltyVaultImplSnapshotCompletedEvent = {
snapshotId: bigint;
snapshotTimestamp: bigint;
unclaimedTokens: number;
};

/**
* IpRoyaltyVaultImplClaimableRevenueRequest
*
Expand Down Expand Up @@ -5179,9 +5203,9 @@ export type IpRoyaltyVaultImplCollectRoyaltyTokensRequest = {
};

/**
* contract IpRoyaltyVaultImpl readonly method
* contract IpRoyaltyVaultImpl event
*/
export class IpRoyaltyVaultImplReadOnlyClient {
export class IpRoyaltyVaultImplEventClient {
protected readonly rpcClient: PublicClient;
public readonly address: Address;

Expand All @@ -5190,6 +5214,97 @@ export class IpRoyaltyVaultImplReadOnlyClient {
this.rpcClient = rpcClient;
}

/**
* event RoyaltyTokensCollected for contract IpRoyaltyVaultImpl
*/
public watchRoyaltyTokensCollectedEvent(
onLogs: (txHash: Hex, ev: Partial<IpRoyaltyVaultImplRoyaltyTokensCollectedEvent>) => void,
): WatchContractEventReturnType {
return this.rpcClient.watchContractEvent({
abi: ipRoyaltyVaultImplAbi,
address: this.address,
eventName: "RoyaltyTokensCollected",
onLogs: (evs) => {
evs.forEach((it) => onLogs(it.transactionHash, it.args));
},
});
}

/**
* parse tx receipt event RoyaltyTokensCollected for contract IpRoyaltyVaultImpl
*/
public parseTxRoyaltyTokensCollectedEvent(
txReceipt: TransactionReceipt,
): Array<IpRoyaltyVaultImplRoyaltyTokensCollectedEvent> {
const targetLogs: Array<IpRoyaltyVaultImplRoyaltyTokensCollectedEvent> = [];
for (const log of txReceipt.logs) {
try {
const event = decodeEventLog({
abi: ipRoyaltyVaultImplAbi,
eventName: "RoyaltyTokensCollected",
data: log.data,
topics: log.topics,
});
if (event.eventName === "RoyaltyTokensCollected") {
targetLogs.push(event.args);
}
} catch (e) {
/* empty */
}
}
return targetLogs;
}

/**
* event SnapshotCompleted for contract IpRoyaltyVaultImpl
*/
public watchSnapshotCompletedEvent(
onLogs: (txHash: Hex, ev: Partial<IpRoyaltyVaultImplSnapshotCompletedEvent>) => void,
): WatchContractEventReturnType {
return this.rpcClient.watchContractEvent({
abi: ipRoyaltyVaultImplAbi,
address: this.address,
eventName: "SnapshotCompleted",
onLogs: (evs) => {
evs.forEach((it) => onLogs(it.transactionHash, it.args));
},
});
}

/**
* parse tx receipt event SnapshotCompleted for contract IpRoyaltyVaultImpl
*/
public parseTxSnapshotCompletedEvent(
txReceipt: TransactionReceipt,
): Array<IpRoyaltyVaultImplSnapshotCompletedEvent> {
const targetLogs: Array<IpRoyaltyVaultImplSnapshotCompletedEvent> = [];
for (const log of txReceipt.logs) {
try {
const event = decodeEventLog({
abi: ipRoyaltyVaultImplAbi,
eventName: "SnapshotCompleted",
data: log.data,
topics: log.topics,
});
if (event.eventName === "SnapshotCompleted") {
targetLogs.push(event.args);
}
} catch (e) {
/* empty */
}
}
return targetLogs;
}
}

/**
* contract IpRoyaltyVaultImpl readonly method
*/
export class IpRoyaltyVaultImplReadOnlyClient extends IpRoyaltyVaultImplEventClient {
constructor(rpcClient: PublicClient, address?: Address) {
super(rpcClient, address);
}

/**
* method claimableRevenue for contract IpRoyaltyVaultImpl
*
Expand Down Expand Up @@ -5289,6 +5404,22 @@ export class IpRoyaltyVaultImplClient extends IpRoyaltyVaultImplReadOnlyClient {
});
return await this.wallet.writeContract(call as WriteContractParameters);
}

/**
* method snapshot for contract IpRoyaltyVaultImpl
*
* @param request IpRoyaltyVaultImplSnapshotRequest
* @return Promise<WriteContractReturnType>
*/
public async snapshot(): Promise<WriteContractReturnType> {
const { request: call } = await this.rpcClient.simulateContract({
abi: ipRoyaltyVaultImplAbi,
address: this.address,
functionName: "snapshot",
account: this.wallet.account,
});
return await this.wallet.writeContract(call as WriteContractParameters);
}
}

// Contract LicenseRegistry =============================================================
Expand Down
11 changes: 8 additions & 3 deletions packages/core-sdk/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -19,20 +19,25 @@ export type {
} from "./types/resources/ipAsset";

export type {
MintLicenseTokensResponse,
MintLicenseTokensRequest,
RegisterNonComSocialRemixingPILRequest,
RegisterCommercialUsePILRequest,
RegisterLicenseTermsResponse,
RegisterCommercialRemixPILRequest,
RegisterPILResponse,
AttachLicenseTermsRequest,
LicenseTermsIdResponse,
MintLicenseTokensRequest,
MintLicenseTokensResponse,
} from "./types/resources/license";

export type {
CollectRoyaltyTokensRequest,
CollectRoyaltyTokensResponse,
PayRoyaltyOnBehalfRequest,
PayRoyaltyOnBehalfResponse,
SnapshotRequest,
SnapshotResponse,
ClaimableRevenueRequest,
ClaimableRevenueResponse,
} from "./types/resources/royalty";

export type { SetPermissionsRequest, SetPermissionsResponse } from "./types/resources/permission";
Expand Down
43 changes: 40 additions & 3 deletions packages/core-sdk/src/resources/ipAsset.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import { Hex, PublicClient, getAddress, zeroAddress } from "viem";

import { chain, parseToBigInt } from "../utils/utils";
import { chain } from "../utils/utils";
import { SupportedChainIds } from "../types/config";
import { handleError } from "../utils/errors";
import {
Expand All @@ -13,6 +13,7 @@ import {
} from "../types/resources/ipAsset";
import {
IpAssetRegistryClient,
LicenseRegistryReadOnlyClient,
LicensingModuleClient,
PiLicenseTemplateClient,
SimpleWalletClient,
Expand All @@ -24,13 +25,15 @@ export class IPAssetClient {
public ipAssetRegistryClient: IpAssetRegistryClient;
public licensingModuleClient: LicensingModuleClient;
public licenseTemplateClient: PiLicenseTemplateClient;
private licenseRegistryReadOnlyClient: LicenseRegistryReadOnlyClient;

constructor(rpcClient: PublicClient, wallet: SimpleWalletClient, chainId: SupportedChainIds) {
this.rpcClient = rpcClient;
this.chainId = chainId;
this.ipAssetRegistryClient = new IpAssetRegistryClient(rpcClient, wallet);
this.licensingModuleClient = new LicensingModuleClient(this.rpcClient, wallet);
this.licenseTemplateClient = new PiLicenseTemplateClient(this.rpcClient, wallet);
this.licenseRegistryReadOnlyClient = new LicenseRegistryReadOnlyClient(this.rpcClient);
}

/**
Expand All @@ -43,8 +46,8 @@ export class IPAssetClient {
* @emits IPRegistered (ipId, chainId, tokenContract, tokenId, resolverAddr, metadataProviderAddress, metadata)
*/
public async register(request: RegisterRequest): Promise<RegisterIpResponse> {
const tokenId = parseToBigInt(request.tokenId);
try {
const tokenId = BigInt(request.tokenId);
const ipId = await this.isNFTRegistered(request.tokenContract, tokenId);
if (ipId !== "0x") {
return { ipId: ipId };
Expand Down Expand Up @@ -82,6 +85,31 @@ export class IPAssetClient {
request: RegisterDerivativeRequest,
): Promise<RegisterDerivativeResponse> {
try {
if (!(await this.isIpIdRegistered(request.childIpId))) {
throw new Error("IP asset must be registered before registering derivative");
}
for (const parentId of request.parentIpIds) {
if (!(await this.isIpIdRegistered(parentId))) {
throw new Error("Parent IP asset must be registered before registering derivative");
}
}
if (request.parentIpIds.length !== request.licenseTermsIds.length) {
throw new Error("Parent IP IDs and License terms IDs must be provided in pairs");
}
for (let i = 0; i < request.parentIpIds.length; i++) {
const isAttachedLicenseTerms =
await this.licenseRegistryReadOnlyClient.hasIpAttachedLicenseTerms({
ipId: request.parentIpIds[i],
licenseTemplate: request.licenseTemplate || this.licenseTemplateClient.address,
licenseTermsId: BigInt(request.licenseTermsIds[i]),
});
if (!isAttachedLicenseTerms) {
throw new Error(
"License terms must be attached to the parent ipId before registering derivative",
);
}
}

const txHash = await this.licensingModuleClient.registerDerivative({
childIpId: request.childIpId,
parentIpIds: request.parentIpIds,
Expand Down Expand Up @@ -115,6 +143,11 @@ export class IPAssetClient {
request: RegisterDerivativeWithLicenseTokensRequest,
): Promise<RegisterDerivativeWithLicenseTokensResponse> {
try {
if (!(await this.isIpIdRegistered(request.childIpId))) {
throw new Error(
"IP asset must be registered before registering derivative with license tokens",
);
}
const txHash = await this.licensingModuleClient.registerDerivativeWithLicenseTokens({
childIpId: request.childIpId,
licenseTokenIds: request.licenseTokenIds.map((id) => BigInt(id)),
Expand All @@ -133,11 +166,15 @@ export class IPAssetClient {

private async isNFTRegistered(tokenAddress: Hex, tokenId: bigint): Promise<Hex> {
const ipId = await this.ipAssetRegistryClient.ipId({
chainId: parseToBigInt(chain[this.chainId]),
chainId: BigInt(chain[this.chainId]),
tokenContract: tokenAddress,
tokenId: tokenId,
});
const isRegistered = await this.ipAssetRegistryClient.isRegistered({ id: ipId });
return isRegistered ? ipId : "0x";
}

private async isIpIdRegistered(ipId: Hex): Promise<boolean> {
return await this.ipAssetRegistryClient.isRegistered({ id: ipId });
}
}
Loading

0 comments on commit f9be48a

Please sign in to comment.