-
Notifications
You must be signed in to change notification settings - Fork 52
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
add tagging module with integration tests (#48)
* add tagging module
- Loading branch information
1 parent
48d3e28
commit d53145e
Showing
12 changed files
with
478 additions
and
12 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,53 @@ | ||
import { AxiosInstance } from "axios"; | ||
import { PublicClient, WalletClient } from "viem"; | ||
|
||
import { handleError } from "../utils/errors"; | ||
import { TaggingModuleConfig } from "../abi/taggingModule.abi"; | ||
import { TaggingReadOnlyClient } from "./taggingReadOnly"; | ||
import { | ||
RemoveTagRequest, | ||
RemoveTagResponse, | ||
SetTagRequest, | ||
SetTagResponse, | ||
} from "../types/resources/tagging"; | ||
|
||
export class TaggingClient extends TaggingReadOnlyClient { | ||
private readonly wallet: WalletClient; | ||
|
||
constructor(httpClient: AxiosInstance, rpcClient: PublicClient, wallet: WalletClient) { | ||
super(httpClient, rpcClient); | ||
this.wallet = wallet; | ||
} | ||
|
||
public async setTag(request: SetTagRequest): Promise<SetTagResponse> { | ||
try { | ||
const { request: call } = await this.rpcClient.simulateContract({ | ||
...TaggingModuleConfig, | ||
functionName: "setTag", | ||
args: [request.tag, request.ipId], | ||
}); | ||
|
||
const txHash = await this.wallet.writeContract(call); | ||
|
||
return { txHash: txHash }; | ||
} catch (error) { | ||
handleError(error, "Failed to set tag"); | ||
} | ||
} | ||
|
||
public async removeTag(request: RemoveTagRequest): Promise<RemoveTagResponse> { | ||
try { | ||
const { request: call } = await this.rpcClient.simulateContract({ | ||
...TaggingModuleConfig, | ||
functionName: "removeTag", | ||
args: [request.tag, request.ipId], | ||
}); | ||
|
||
const txHash = await this.wallet.writeContract(call); | ||
|
||
return { txHash: txHash }; | ||
} catch (error) { | ||
handleError(error, "Failed to remove tag"); | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,50 @@ | ||
import { AxiosInstance } from "axios"; | ||
import { PublicClient } from "viem"; | ||
|
||
import { handleError } from "../utils/errors"; | ||
import { | ||
GetTagRequest, | ||
GetTagResponse, | ||
ListTagRequest, | ||
ListTagResponse, | ||
} from "../types/resources/tagging"; | ||
|
||
/** | ||
* TaggingReadOnlyClient allows you to view and search IP Assets on Story Protocol. | ||
*/ | ||
export class TaggingReadOnlyClient { | ||
protected readonly httpClient: AxiosInstance; | ||
protected readonly rpcClient: PublicClient; | ||
|
||
constructor(httpClient: AxiosInstance, rpcClient: PublicClient) { | ||
this.httpClient = httpClient; | ||
this.rpcClient = rpcClient; | ||
} | ||
|
||
/** | ||
* Get tags. | ||
* | ||
* @returns the response object that contains results from get tag query. | ||
*/ | ||
public async get(request: GetTagRequest): Promise<GetTagResponse> { | ||
try { | ||
const response = await this.httpClient.get(`/tags/${request.id}`); | ||
return response.data as GetTagResponse; | ||
} catch (error) { | ||
handleError(error, "Failed to get tags."); | ||
} | ||
} | ||
/** | ||
* List tags. | ||
* | ||
* @returns the response object that contains results from list tags query. | ||
*/ | ||
public async list(request?: ListTagRequest): Promise<ListTagResponse> { | ||
try { | ||
const response = await this.httpClient.post(`/tags`, request || {}); | ||
return response.data as ListTagResponse; | ||
} catch (error) { | ||
handleError(error, "Failed to list tags."); | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,43 @@ | ||
import { QueryOptions, TxOptions } from "../options"; | ||
|
||
export type Tag = { | ||
id: string; | ||
ipId: `0x${string}`; | ||
tag: string; | ||
}; | ||
|
||
export type ListTagRequest = { | ||
options?: QueryOptions; | ||
}; | ||
|
||
export type ListTagResponse = { | ||
data: Tag[]; | ||
}; | ||
|
||
export type GetTagRequest = { | ||
id: string; | ||
}; | ||
|
||
export type GetTagResponse = { | ||
data: Tag; | ||
}; | ||
|
||
export type SetTagRequest = { | ||
tag: string; | ||
ipId: `0x${string}`; | ||
txOptions?: TxOptions; | ||
}; | ||
|
||
export type SetTagResponse = { | ||
txHash: string; | ||
}; | ||
|
||
export type RemoveTagRequest = { | ||
tag: string; | ||
ipId: `0x${string}`; | ||
txOptions?: TxOptions; | ||
}; | ||
|
||
export type RemoveTagResponse = { | ||
txHash: string; | ||
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,92 @@ | ||
import { expect } from "chai"; | ||
import { StoryClient, StoryConfig, Client } from "../../src"; | ||
import { Hex, http } from "viem"; | ||
import { privateKeyToAccount } from "viem/accounts"; | ||
|
||
describe("Tagging Functions", () => { | ||
let client: Client; | ||
let senderAddress: string; | ||
|
||
before(function () { | ||
const config: StoryConfig = { | ||
transport: http(process.env.RPC_PROVIDER_URL), | ||
account: privateKeyToAccount((process.env.WALLET_PRIVATE_KEY || "0x") as Hex), | ||
}; | ||
|
||
senderAddress = config.account.address; | ||
client = StoryClient.newClient(config); | ||
}); | ||
|
||
describe("Set Tag", async function () { | ||
it("should be able to set tag and wait for transaction", async () => { | ||
const response = await expect( | ||
client.tagging.setTag({ | ||
tag: "testTag", | ||
ipId: "0xabCc2421F927c128B9F5a94B612F4541C8E624B6", | ||
txOptions: { | ||
waitForTransaction: true, | ||
}, | ||
}), | ||
).to.not.be.rejected; | ||
|
||
expect(response.txHash).to.be.a("string"); | ||
expect(response.txHash).not.empty; | ||
}); | ||
|
||
it("should be able to call set tag without waiting for transaction", async () => { | ||
const response = await expect( | ||
client.tagging.setTag({ | ||
tag: "testTag", | ||
ipId: "0xabCc2421F927c128B9F5a94B612F4541C8E624B6", | ||
txOptions: { | ||
waitForTransaction: false, | ||
}, | ||
}), | ||
).to.not.be.rejected; | ||
|
||
expect(response.txHash).to.be.a("string"); | ||
expect(response.txHash).not.empty; | ||
}); | ||
|
||
it("should revert setting tag with incorrect ipId type", async () => { | ||
const waitForTransaction: boolean = true; | ||
await expect( | ||
client.tagging.setTag({ | ||
tag: "testTag", | ||
//@ts-expect-error | ||
ipId: 0xabcc2421f927c128b9f5a94b612f4541c8e624b6, | ||
txOptions: { | ||
waitForTransaction: waitForTransaction, | ||
}, | ||
}), | ||
).to.be.rejected; | ||
}); | ||
|
||
it("should be able to remove tag", async () => { | ||
// Set tag first, then remove it | ||
const tagString = "bad-tag69"; | ||
const ipId = "0xabCc2421F927c128B9F5a94B612F4541C8E624B6"; | ||
const waitForTransaction: boolean = true; | ||
await client.tagging.setTag({ | ||
tag: tagString, | ||
ipId: ipId, | ||
txOptions: { | ||
waitForTransaction: waitForTransaction, | ||
}, | ||
}); | ||
|
||
const response = await expect( | ||
client.tagging.removeTag({ | ||
tag: tagString, | ||
ipId: ipId, | ||
txOptions: { | ||
waitForTransaction: waitForTransaction, | ||
}, | ||
}), | ||
).to.not.be.rejected; | ||
|
||
expect(response.txHash).to.be.a("string"); | ||
expect(response.txHash).not.empty; | ||
}); | ||
}); | ||
}); |
Oops, something went wrong.