From a2f9d5922c86b08086620d1a5432c4b6ef652837 Mon Sep 17 00:00:00 2001 From: Kris Urbas <605420+krzysu@users.noreply.github.com> Date: Tue, 12 Sep 2023 14:02:13 +0200 Subject: [PATCH] feat: docs and examples for publication submodule --- examples/node/scripts/broadcast.ts | 58 ------ .../scripts/checkLensTransactionStatus.ts | 67 ------ .../node/scripts/createDAPostViaDispatcher.ts | 67 ------ .../node/scripts/createDAPostViaTypedData.ts | 69 ------- examples/node/scripts/createMomokaPost.ts | 40 ---- .../node/scripts/createPostViaDispatcher.ts | 63 ------ examples/node/scripts/fetchPublications.ts | 27 --- .../scripts/publication/commentOnChain.ts | 28 +++ .../publication/createMomokaPostTypedData.ts | 37 ++++ .../publication/createOnChainPostTypedData.ts | 50 +++++ examples/node/scripts/publication/fetch.ts | 4 +- examples/node/scripts/publication/fetchAll.ts | 18 +- examples/node/scripts/publication/hide.ts | 18 ++ .../node/scripts/publication/mirrorOnChain.ts | 27 +++ .../node/scripts/publication/postOnChain.ts | 30 +++ .../node/scripts/publication/postOnMomoka.ts | 23 +++ .../node/scripts/publication/quoteOnChain.ts | 28 +++ examples/node/scripts/publication/report.ts | 26 +++ examples/node/scripts/publication/tags.ts | 13 ++ .../waitUntilLensTransactionComplete.ts | 74 ------- examples/node/tsconfig.json | 4 +- packages/client/src/index.ts | 10 +- .../src/submodules/publication/Publication.ts | 194 ++++++++++++++++-- .../submodules/publication/helpers/helpers.ts | 17 ++ .../submodules/transaction/helpers/index.ts | 6 + 25 files changed, 513 insertions(+), 485 deletions(-) delete mode 100644 examples/node/scripts/broadcast.ts delete mode 100644 examples/node/scripts/checkLensTransactionStatus.ts delete mode 100644 examples/node/scripts/createDAPostViaDispatcher.ts delete mode 100644 examples/node/scripts/createDAPostViaTypedData.ts delete mode 100644 examples/node/scripts/createMomokaPost.ts delete mode 100644 examples/node/scripts/createPostViaDispatcher.ts delete mode 100644 examples/node/scripts/fetchPublications.ts create mode 100644 examples/node/scripts/publication/commentOnChain.ts create mode 100644 examples/node/scripts/publication/createMomokaPostTypedData.ts create mode 100644 examples/node/scripts/publication/createOnChainPostTypedData.ts create mode 100644 examples/node/scripts/publication/hide.ts create mode 100644 examples/node/scripts/publication/mirrorOnChain.ts create mode 100644 examples/node/scripts/publication/postOnChain.ts create mode 100644 examples/node/scripts/publication/postOnMomoka.ts create mode 100644 examples/node/scripts/publication/quoteOnChain.ts create mode 100644 examples/node/scripts/publication/report.ts create mode 100644 examples/node/scripts/publication/tags.ts delete mode 100644 examples/node/scripts/waitUntilLensTransactionComplete.ts diff --git a/examples/node/scripts/broadcast.ts b/examples/node/scripts/broadcast.ts deleted file mode 100644 index ceee84ab2d..0000000000 --- a/examples/node/scripts/broadcast.ts +++ /dev/null @@ -1,58 +0,0 @@ -import { isRelaySuccess } from "@lens-protocol/client"; -import { getActiveProfile } from "./shared/getActiveProfile"; -import { getAuthenticatedClientFromEthersWallet } from "./shared/getAuthenticatedClient"; -import { setupWallet } from "./shared/setupWallet"; - -async function main() { - const wallet = setupWallet(); - const address = await wallet.getAddress(); - const lensClient = await getAuthenticatedClientFromEthersWallet(wallet); - const activeProfile = await getActiveProfile(lensClient, address); - - // we need some typedData to sign and broadcast so let's set the dispatcher as an example - const setDispatcherResult = await lensClient.profile.crea({ - profileId: activeProfile.id, - }); - - // setDispatcherResult is a Result object - const data = setDispatcherResult.unwrap(); - - // sign with the wallet - const signedTypedData = await wallet._signTypedData( - data.typedData.domain, - data.typedData.types, - data.typedData.value - ); - - // broadcast - const broadcastResult = await lensClient.transaction.broadcast({ - id: data.id, - signature: signedTypedData, - }); - - // broadcastResult is a Result object - const broadcastResultValue = broadcastResult.unwrap(); - - if (!isRelaySuccess(broadcastResultValue)) { - console.log(`Something went wrong`, broadcastResultValue); - return; - } - - console.log( - `Transaction to set dispatcher for profile ${activeProfile.id} was successfuly broadcasted with txId ${broadcastResultValue.txId}` - ); - - // single check - const wasIndexedFirstCheck = await lensClient.transaction.wasIndexed(broadcastResultValue.txId); - console.log(`Transaction status: `, wasIndexedFirstCheck.unwrap()); - - // wait in a loop - console.log(`Waiting for the transaction to be indexed...`); - await lensClient.transaction.waitForIsIndexed(broadcastResultValue.txId); - - // now the transaction is indexed - const wasIndexedFinalCheck = await lensClient.transaction.wasIndexed(broadcastResultValue.txId); - console.log(`Transaction status: `, wasIndexedFinalCheck.unwrap()); -} - -main(); diff --git a/examples/node/scripts/checkLensTransactionStatus.ts b/examples/node/scripts/checkLensTransactionStatus.ts deleted file mode 100644 index c0f05ebe16..0000000000 --- a/examples/node/scripts/checkLensTransactionStatus.ts +++ /dev/null @@ -1,67 +0,0 @@ -import { LensClient, isRelaySuccess } from "@lens-protocol/client"; -import { Wallet } from "ethers"; -import { getAuthenticatedClientFromEthersWallet } from "./shared/getAuthenticatedClient"; -import { setupWallet } from "./shared/setupWallet"; - -async function signTypedDataAndBroadcast(lensClient: LensClient, wallet: Wallet) { - const typedDataResult = await lensClient.profile.createOnChainSetProfileMetadataTypedData({ - metadataURI: "your-metadata-uri", - }); - - const data = typedDataResult.unwrap(); - - const signedTypedData = await wallet._signTypedData( - data.typedData.domain, - data.typedData.types, - data.typedData.value - ); - - const broadcastResult = await lensClient.transaction.broadcastOnchain({ - id: data.id, - signature: signedTypedData, - }); - - const broadcastResultValue = broadcastResult.unwrap(); - - if (!isRelaySuccess(broadcastResultValue)) { - console.log(`Something went wrong`, broadcastResult); - return; - } - - return broadcastResultValue; -} - -async function main() { - const wallet = setupWallet(); - const lensClient = await getAuthenticatedClientFromEthersWallet(wallet); - - const broadcastResult = await signTypedDataAndBroadcast(lensClient, wallet); - - const result = await lensClient.transaction.status({ txId: broadcastResult.txId }); - - if (result.isFailure()) { - console.log(`Something went wrong`, result); - return; - } - - const transactionStatusResult = result.value; - - if (transactionStatusResult.__typename === "LensTransaction") { - console.log(`LensTransaction has status ${transactionStatusResult.status}`, { - txId: broadcastResult.txId, - txHash: broadcastResult.txHash, - reason: transactionStatusResult.reason, - extraInfo: transactionStatusResult.extraInfo, - }); - return; - } - - console.log(`LensMetadataTransaction has status ${transactionStatusResult.status}`, { - txId: broadcastResult.txId, - txHash: broadcastResult.txHash, - extraInfo: transactionStatusResult.extraInfo, - metadataFailedReason: transactionStatusResult.metadataFailedReason, - }); -} - -main(); diff --git a/examples/node/scripts/createDAPostViaDispatcher.ts b/examples/node/scripts/createDAPostViaDispatcher.ts deleted file mode 100644 index 421e7df356..0000000000 --- a/examples/node/scripts/createDAPostViaDispatcher.ts +++ /dev/null @@ -1,67 +0,0 @@ -import { isCreateDataAvailabilityPublicationResult } from "@lens-protocol/client"; -import { getAuthenticatedClient } from "./shared/getAuthenticatedClient"; -import { setupWallet } from "./shared/setupWallet"; -import { uploadWithBundlr } from "./shared/uploadWithBundlr"; -import { getActiveProfile } from "./shared/getActiveProfile"; -import { buildPublicationMetadata } from "./shared/buildPublicationMetadata"; - -async function main() { - const wallet = setupWallet(); - const address = await wallet.getAddress(); - const lensClient = await getAuthenticatedClient(wallet); - const profile = await getActiveProfile(lensClient, address); - const profileId = profile.id; - - // prepare metadata - const metadata = buildPublicationMetadata({ - content: "Data Availability Post created with LensClient SDK", - name: "Data Availability Post created with LensClient SDK", - }); - - // validate metadata - const validateResult = await lensClient.publication.validateMetadata(metadata); - - if (!validateResult.valid) { - throw new Error(`Metadata is not valid.`); - } - - // upload metadata to - const contentURI = await uploadWithBundlr(metadata); - - console.log(`Post metadata was uploaded to ${contentURI}`); - - // create a post via dispatcher, you need to have the dispatcher enabled for the profile - const createPostResult = await lensClient.publication.createDataAvailabilityPostViaDispatcher({ - from: profileId, - contentURI, - }); - - // createPostResult is a Result object - const createPostResultValue = createPostResult.unwrap(); - - if (!isCreateDataAvailabilityPublicationResult(createPostResultValue)) { - console.log(`Something went wrong`, createPostResultValue); - return; - } - - console.log(`DA post was created: `, createPostResultValue); - - // DA post is created instantly, so we can also comment on it right away - const createCommentResult = - await lensClient.publication.createDataAvailabilityCommentViaDispatcher({ - from: profileId, - contentURI, - commentOn: createPostResultValue.id, - }); - - const createCommentResultValue = createCommentResult.unwrap(); - - if (!isCreateDataAvailabilityPublicationResult(createCommentResultValue)) { - console.log(`Something went wrong`, createCommentResultValue); - return; - } - - console.log(`DA comment was created too: `, createCommentResultValue); -} - -main(); diff --git a/examples/node/scripts/createDAPostViaTypedData.ts b/examples/node/scripts/createDAPostViaTypedData.ts deleted file mode 100644 index 510152e8fa..0000000000 --- a/examples/node/scripts/createDAPostViaTypedData.ts +++ /dev/null @@ -1,69 +0,0 @@ -import { isCreateDataAvailabilityPublicationResult } from "@lens-protocol/client"; -import { getAuthenticatedClient } from "./shared/getAuthenticatedClient"; -import { setupWallet } from "./shared/setupWallet"; -import { uploadWithBundlr } from "./shared/uploadWithBundlr"; -import { getActiveProfile } from "./shared/getActiveProfile"; -import { buildPublicationMetadata } from "./shared/buildPublicationMetadata"; - -async function main() { - const wallet = setupWallet(); - const address = await wallet.getAddress(); - const lensClient = await getAuthenticatedClient(wallet); - const profile = await getActiveProfile(lensClient, address); - const profileId = profile.id; - - // prepare metadata - const metadata = buildPublicationMetadata({ - content: "Data Availability Post created with LensClient SDK", - name: "Data Availability Post created with LensClient SDK", - }); - - // validate metadata - const validateResult = await lensClient.publication.validateMetadata(metadata); - - if (!validateResult.valid) { - throw new Error(`Metadata is not valid.`); - } - - // upload metadata to - const contentURI = await uploadWithBundlr(metadata); - - console.log(`Post metadata was uploaded to ${contentURI}`); - - // fetch a create DA post typed data - const createPostTypedDataResult = - await lensClient.publication.createDataAvailabilityPostTypedData({ - from: profileId, - contentURI, - }); - - const createPostTypedDataValue = createPostTypedDataResult.unwrap(); - - console.log(`createDataAvailabilityPostTypedData result: `, createPostTypedDataValue); - - // sign with the wallet - const signedTypedData = await wallet._signTypedData( - createPostTypedDataValue.typedData.domain, - createPostTypedDataValue.typedData.types, - createPostTypedDataValue.typedData.value - ); - - console.log(`Broadcasting signed createDataAvailabilityPostTypedData...`); - - const broadcastResult = await lensClient.transaction.broadcastDataAvailability({ - id: createPostTypedDataValue.id, - signature: signedTypedData, - }); - - // broadcastResult is a Result object - const broadcastValue = broadcastResult.unwrap(); - - if (!isCreateDataAvailabilityPublicationResult(broadcastValue)) { - console.log(`Something went wrong`, broadcastValue); - return; - } - - console.log(`DA post was created: `, broadcastValue); -} - -main(); diff --git a/examples/node/scripts/createMomokaPost.ts b/examples/node/scripts/createMomokaPost.ts deleted file mode 100644 index 2c2507e874..0000000000 --- a/examples/node/scripts/createMomokaPost.ts +++ /dev/null @@ -1,40 +0,0 @@ -import { isMomokaRelayResult, isRelaySuccess } from "@lens-protocol/client"; -import { getAuthenticatedClientFromEthersWallet } from "./shared/getAuthenticatedClient"; -import { setupWallet } from "./shared/setupWallet"; -import { LensTransactionStatusType } from "@lens-protocol/client/src/graphql/types.generated"; - -async function main() { - const wallet = setupWallet(); - const lensClient = await getAuthenticatedClientFromEthersWallet(wallet); - - const typedDataResult = await lensClient.publication.createMomokaPostTypedData({ - contentURI: "your-content-uri", - }); - - const { id, typedData } = typedDataResult.unwrap(); - - // sign with the wallet - const signedTypedData = await wallet._signTypedData( - typedData.domain, - typedData.types, - typedData.value - ); - - const broadcastOnMomokaResult = await lensClient.transaction.broadcastOnMomoka({ - id, - signature: signedTypedData, - }); - - const momokaRelayResult = broadcastOnMomokaResult.unwrap(); - - if (!isMomokaRelayResult(momokaRelayResult)) { - console.log(`Something went wrong`); - return; - } - - console.log( - `Successfully broadcasted momoka transaction with id ${momokaRelayResult.id}, momokaId: ${momokaRelayResult.momokaId}, proof: ${momokaRelayResult.proof}` - ); -} - -main(); diff --git a/examples/node/scripts/createPostViaDispatcher.ts b/examples/node/scripts/createPostViaDispatcher.ts deleted file mode 100644 index 998cc4a341..0000000000 --- a/examples/node/scripts/createPostViaDispatcher.ts +++ /dev/null @@ -1,63 +0,0 @@ -import { isRelaySuccess } from "@lens-protocol/client"; -import { getAuthenticatedClientFromEthersWallet } from "./shared/getAuthenticatedClient"; -import { setupWallet } from "./shared/setupWallet"; -import { uploadWithBundlr } from "./shared/uploadWithBundlr"; -import { getActiveProfile } from "./shared/getActiveProfile"; -import { buildPublicationMetadata } from "./shared/buildPublicationMetadata"; - -async function main() { - const wallet = setupWallet(); - const address = await wallet.getAddress(); - const lensClient = await getAuthenticatedClientFromEthersWallet(wallet); - const profile = await getActiveProfile(lensClient, address); - const profileId = profile.id; - - // prepare metadata - const metadata = buildPublicationMetadata(); - - // validate metadata - const validateResult = await lensClient.publication.validateMetadata(metadata); - - if (!validateResult.valid) { - throw new Error(`Metadata is not valid.`); - } - - // upload metadata to - const contentURI = await uploadWithBundlr(metadata); - - console.log(`Post metadata was uploaded to ${contentURI}`); - - // create a post via dispatcher, you need to have the dispatcher enabled for the profile - const createPostResult = await lensClient.publication.createPostViaDispatcher({ - profileId, - contentURI, - collectModule: { - revertCollectModule: true, // collect disabled - }, - referenceModule: { - followerOnlyReferenceModule: false, // anybody can comment or mirror - }, - }); - - // createPostResult is a Result object - const createPostResultValue = createPostResult.unwrap(); - - if (!isRelaySuccess(createPostResultValue)) { - console.log(`Something went wrong`, createPostResultValue); - return; - } - - console.log( - `Transaction to create a post was successfuly broadcasted with txId ${createPostResultValue.txId}` - ); - - // wait in a loop - console.log(`Waiting for the transaction to be indexed...`); - await lensClient.transaction.waitForIsIndexed(createPostResultValue.txId); - - // now the transaction is indexed - const wasIndexedFinalCheck = await lensClient.transaction.wasIndexed(createPostResultValue.txId); - console.log(`Transaction status: `, wasIndexedFinalCheck.unwrap()); -} - -main(); diff --git a/examples/node/scripts/fetchPublications.ts b/examples/node/scripts/fetchPublications.ts deleted file mode 100644 index 08768eeb1d..0000000000 --- a/examples/node/scripts/fetchPublications.ts +++ /dev/null @@ -1,27 +0,0 @@ -import { LensClient, isPostPublication, production } from "@lens-protocol/client"; - -async function main() { - const lensClient = new LensClient({ - environment: production, - }); - - const publications = await lensClient.publication.fetchAll({ - publicationIds: ["0x014e11-0x01be", "0x018615-0x22"], - }); - - const posts = publications.items.filter(isPostPublication); - - console.log( - `Posts fetched by ids: `, - JSON.stringify( - posts.map((i) => ({ - id: i.id, - metadata: i.metadata, - })), - null, - 2 - ) - ); -} - -main(); diff --git a/examples/node/scripts/publication/commentOnChain.ts b/examples/node/scripts/publication/commentOnChain.ts new file mode 100644 index 0000000000..a30c936ec0 --- /dev/null +++ b/examples/node/scripts/publication/commentOnChain.ts @@ -0,0 +1,28 @@ +import { isRelaySuccess } from "@lens-protocol/client"; +import { getAuthenticatedClientFromEthersWallet } from "../shared/getAuthenticatedClient"; +import { setupWallet } from "../shared/setupWallet"; + +async function main() { + const wallet = setupWallet(); + const client = await getAuthenticatedClientFromEthersWallet(wallet); + + const result = await client.publication.commentOnChain({ + commentOn: "0x123-0x456", + contentURI: "ipfs://Qm...", // or arweave + }); + + const resultValue = result.unwrap(); + + if (!isRelaySuccess(resultValue)) { + console.log(`Something went wrong`, resultValue); + return; + } + + console.log(`Transaction was successfuly broadcasted with txId ${resultValue.txId}`); + + // wait in a loop + console.log(`Waiting for the transaction to be indexed...`); + await client.transaction.waitUntilComplete({ txId: resultValue.txId }); +} + +main(); diff --git a/examples/node/scripts/publication/createMomokaPostTypedData.ts b/examples/node/scripts/publication/createMomokaPostTypedData.ts new file mode 100644 index 0000000000..ca4bc91f72 --- /dev/null +++ b/examples/node/scripts/publication/createMomokaPostTypedData.ts @@ -0,0 +1,37 @@ +import { isMomokaRelayResult } from "@lens-protocol/client"; +import { getAuthenticatedClientFromEthersWallet } from "../shared/getAuthenticatedClient"; +import { setupWallet } from "../shared/setupWallet"; + +async function main() { + const wallet = setupWallet(); + const lensClient = await getAuthenticatedClientFromEthersWallet(wallet); + + const typedDataResult = await lensClient.publication.createMomokaPostTypedData({ + contentURI: "ipfs://Qm...", // or arweave + }); + + const { id, typedData } = typedDataResult.unwrap(); + + // sign with the wallet + const signedTypedData = await wallet._signTypedData( + typedData.domain, + typedData.types, + typedData.value + ); + + const broadcastResult = await lensClient.transaction.broadcastOnMomoka({ + id, + signature: signedTypedData, + }); + + const broadcastValue = broadcastResult.unwrap(); + + if (!isMomokaRelayResult(broadcastValue)) { + console.log(`Something went wrong`); + return; + } + + console.log(`Successfully broadcasted momoka transaction`, broadcastValue); +} + +main(); diff --git a/examples/node/scripts/publication/createOnChainPostTypedData.ts b/examples/node/scripts/publication/createOnChainPostTypedData.ts new file mode 100644 index 0000000000..e1c2b6d9fd --- /dev/null +++ b/examples/node/scripts/publication/createOnChainPostTypedData.ts @@ -0,0 +1,50 @@ +import { isRelaySuccess } from '@lens-protocol/client'; +import { getAuthenticatedClientFromEthersWallet } from '../shared/getAuthenticatedClient'; +import { setupWallet } from '../shared/setupWallet'; + +async function main() { + const wallet = setupWallet(); + const client = await getAuthenticatedClientFromEthersWallet(wallet); + + const resultTypedData = await client.publication.createOnChainPostTypedData({ + contentURI: 'ipfs://Qm...', // or arweave + referenceModule: { + followerOnlyReferenceModule: false, // anybody can comment or mirror + }, + }); + + const { id, typedData } = resultTypedData.unwrap(); + + console.log(`createOnChainPostTypedData result: `, typedData); + + // sign with the wallet + const signedTypedData = await wallet._signTypedData( + typedData.domain, + typedData.types, + typedData.value, + ); + + console.log(`Broadcasting signed createOnChainPostTypedData...`); + + const broadcastResult = await client.transaction.broadcastOnChain({ + id, + signature: signedTypedData, + }); + + const broadcastValue = broadcastResult.unwrap(); + + if (!isRelaySuccess(broadcastValue)) { + console.log(`Something went wrong`, broadcastValue); + return; + } + + console.log( + `Transaction to create a post was successfuly broadcasted with txId ${broadcastValue.txId}`, + ); + + // wait in a loop + console.log(`Waiting for the transaction to be indexed...`); + await client.transaction.waitUntilComplete({ txId: broadcastValue.txId }); +} + +main(); diff --git a/examples/node/scripts/publication/fetch.ts b/examples/node/scripts/publication/fetch.ts index 452b1107c5..ed0e82b74a 100644 --- a/examples/node/scripts/publication/fetch.ts +++ b/examples/node/scripts/publication/fetch.ts @@ -1,4 +1,4 @@ -import { LensClient, development } from "@lens-protocol/client"; +import { LensClient, development } from '@lens-protocol/client'; async function main() { const client = new LensClient({ @@ -6,7 +6,7 @@ async function main() { }); const result = await client.publication.fetch({ - for: "0x123-0x456", + for: '0x123-0x456', }); console.log(`Publication fetched by id: `, result); diff --git a/examples/node/scripts/publication/fetchAll.ts b/examples/node/scripts/publication/fetchAll.ts index 556368f608..890d43b589 100644 --- a/examples/node/scripts/publication/fetchAll.ts +++ b/examples/node/scripts/publication/fetchAll.ts @@ -1,4 +1,4 @@ -import { LensClient, development } from "@lens-protocol/client"; +import { LensClient, development, isPostPublication } from "@lens-protocol/client"; async function main() { const client = new LensClient({ @@ -13,11 +13,25 @@ async function main() { }); console.log( - `Publications from profileId ${profileId}: `, + `All publications from profileId ${profileId}: `, result.items.map((p) => ({ id: p.id, })) ); + + const posts = result.items.filter(isPostPublication); + + console.log( + `Only posts: `, + JSON.stringify( + posts.map((i) => ({ + id: i.id, + metadata: i.metadata, + })), + null, + 2 + ) + ); } main(); diff --git a/examples/node/scripts/publication/hide.ts b/examples/node/scripts/publication/hide.ts new file mode 100644 index 0000000000..2ea61a291c --- /dev/null +++ b/examples/node/scripts/publication/hide.ts @@ -0,0 +1,18 @@ +import { getAuthenticatedClientFromEthersWallet } from "../shared/getAuthenticatedClient"; +import { setupWallet } from "../shared/setupWallet"; + +async function main() { + const wallet = setupWallet(); + const client = await getAuthenticatedClientFromEthersWallet(wallet); + + // get user's publications + // hide one of them + + const result = await client.publication.hide({ + for: "0x014e-0x0a", + }); + + console.log(`Publication was hidden: `, result); +} + +main(); diff --git a/examples/node/scripts/publication/mirrorOnChain.ts b/examples/node/scripts/publication/mirrorOnChain.ts new file mode 100644 index 0000000000..c2c6d435f6 --- /dev/null +++ b/examples/node/scripts/publication/mirrorOnChain.ts @@ -0,0 +1,27 @@ +import { isRelaySuccess } from "@lens-protocol/client"; +import { getAuthenticatedClientFromEthersWallet } from "../shared/getAuthenticatedClient"; +import { setupWallet } from "../shared/setupWallet"; + +async function main() { + const wallet = setupWallet(); + const client = await getAuthenticatedClientFromEthersWallet(wallet); + + const result = await client.publication.mirrorOnChain({ + mirrorOn: "0x123-0x456", + }); + + const resultValue = result.unwrap(); + + if (!isRelaySuccess(resultValue)) { + console.log(`Something went wrong`, resultValue); + return; + } + + console.log(`Transaction was successfuly broadcasted with txId ${resultValue.txId}`); + + // wait in a loop + console.log(`Waiting for the transaction to be indexed...`); + await client.transaction.waitUntilComplete({ txId: resultValue.txId }); +} + +main(); diff --git a/examples/node/scripts/publication/postOnChain.ts b/examples/node/scripts/publication/postOnChain.ts new file mode 100644 index 0000000000..2a476806fb --- /dev/null +++ b/examples/node/scripts/publication/postOnChain.ts @@ -0,0 +1,30 @@ +import { isRelaySuccess } from "@lens-protocol/client"; +import { getAuthenticatedClientFromEthersWallet } from "../shared/getAuthenticatedClient"; +import { setupWallet } from "../shared/setupWallet"; + +async function main() { + const wallet = setupWallet(); + const client = await getAuthenticatedClientFromEthersWallet(wallet); + + const result = await client.publication.postOnChain({ + contentURI: "ipfs://Qm...", // or arweave + referenceModule: { + followerOnlyReferenceModule: false, // anybody can comment or mirror + }, + }); + + const resultValue = result.unwrap(); + + if (!isRelaySuccess(resultValue)) { + console.log(`Something went wrong`, resultValue); + return; + } + + console.log(`Transaction was successfuly broadcasted with txId ${resultValue.txId}`); + + // wait in a loop + console.log(`Waiting for the transaction to be indexed...`); + await client.transaction.waitUntilComplete({ txId: resultValue.txId }); +} + +main(); diff --git a/examples/node/scripts/publication/postOnMomoka.ts b/examples/node/scripts/publication/postOnMomoka.ts new file mode 100644 index 0000000000..e876e93864 --- /dev/null +++ b/examples/node/scripts/publication/postOnMomoka.ts @@ -0,0 +1,23 @@ +import { isCreateMomokaPublicationResult } from "@lens-protocol/client"; +import { getAuthenticatedClientFromEthersWallet } from "../shared/getAuthenticatedClient"; +import { setupWallet } from "../shared/setupWallet"; + +async function main() { + const wallet = setupWallet(); + const client = await getAuthenticatedClientFromEthersWallet(wallet); + + const result = await client.publication.postOnMomoka({ + contentURI: "ipfs://Qm...", // or arweave + }); + + const resultValue = result.unwrap(); + + if (!isCreateMomokaPublicationResult(resultValue)) { + console.log(`Something went wrong`, resultValue); + return; + } + + console.log(`Transaction was successful`, resultValue); +} + +main(); diff --git a/examples/node/scripts/publication/quoteOnChain.ts b/examples/node/scripts/publication/quoteOnChain.ts new file mode 100644 index 0000000000..2256d22bfd --- /dev/null +++ b/examples/node/scripts/publication/quoteOnChain.ts @@ -0,0 +1,28 @@ +import { isRelaySuccess } from "@lens-protocol/client"; +import { getAuthenticatedClientFromEthersWallet } from "../shared/getAuthenticatedClient"; +import { setupWallet } from "../shared/setupWallet"; + +async function main() { + const wallet = setupWallet(); + const client = await getAuthenticatedClientFromEthersWallet(wallet); + + const result = await client.publication.quoteOnChain({ + quoteOn: "0x123-0x456", + contentURI: "ipfs://Qm...", // or arweave + }); + + const resultValue = result.unwrap(); + + if (!isRelaySuccess(resultValue)) { + console.log(`Something went wrong`, resultValue); + return; + } + + console.log(`Transaction was successfuly broadcasted with txId ${resultValue.txId}`); + + // wait in a loop + console.log(`Waiting for the transaction to be indexed...`); + await client.transaction.waitUntilComplete({ txId: resultValue.txId }); +} + +main(); diff --git a/examples/node/scripts/publication/report.ts b/examples/node/scripts/publication/report.ts new file mode 100644 index 0000000000..5131cf25c2 --- /dev/null +++ b/examples/node/scripts/publication/report.ts @@ -0,0 +1,26 @@ +import { + PublicationReportingReason, + PublicationReportingSpamSubreason, +} from "@lens-protocol/client"; +import { getAuthenticatedClientFromEthersWallet } from "../shared/getAuthenticatedClient"; +import { setupWallet } from "../shared/setupWallet"; + +async function main() { + const wallet = setupWallet(); + const client = await getAuthenticatedClientFromEthersWallet(wallet); + + const result = await client.publication.report({ + for: "0x014e-0x0a", + reason: { + spamReason: { + reason: PublicationReportingReason.Spam, + subreason: PublicationReportingSpamSubreason.FakeEngagement, + }, + }, + additionalComments: "comment", + }); + + console.log(`Publication was hidden: `, result); +} + +main(); diff --git a/examples/node/scripts/publication/tags.ts b/examples/node/scripts/publication/tags.ts new file mode 100644 index 0000000000..b8c0e9d4f0 --- /dev/null +++ b/examples/node/scripts/publication/tags.ts @@ -0,0 +1,13 @@ +import { LensClient, development } from "@lens-protocol/client"; + +async function main() { + const client = new LensClient({ + environment: development, + }); + + const result = await client.publication.tags({}); + + console.log(`Result: `, result); +} + +main(); diff --git a/examples/node/scripts/waitUntilLensTransactionComplete.ts b/examples/node/scripts/waitUntilLensTransactionComplete.ts deleted file mode 100644 index b5a58184a2..0000000000 --- a/examples/node/scripts/waitUntilLensTransactionComplete.ts +++ /dev/null @@ -1,74 +0,0 @@ -import { LensClient, isRelaySuccess } from "@lens-protocol/client"; -import { Wallet } from "ethers"; -import { getAuthenticatedClientFromEthersWallet } from "./shared/getAuthenticatedClient"; -import { setupWallet } from "./shared/setupWallet"; - -async function signTypedDataAndBroadcast(lensClient: LensClient, wallet: Wallet) { - const typedDataResult = await lensClient.profile.createOnChainSetProfileMetadataTypedData({ - metadataURI: "your-metadata-uri", - }); - - const data = typedDataResult.unwrap(); - - const signedTypedData = await wallet._signTypedData( - data.typedData.domain, - data.typedData.types, - data.typedData.value - ); - - const broadcastResult = await lensClient.transaction.broadcastOnchain({ - id: data.id, - signature: signedTypedData, - }); - - const broadcastResultValue = broadcastResult.unwrap(); - - if (!isRelaySuccess(broadcastResultValue)) { - console.log(`Something went wrong`, broadcastResult); - return; - } - - return broadcastResultValue; -} - -async function main() { - const wallet = setupWallet(); - const lensClient = await getAuthenticatedClientFromEthersWallet(wallet); - - const broadcastResult = await signTypedDataAndBroadcast(lensClient, wallet); - - const result = await lensClient.transaction.waitUntilComplete({ txId: broadcastResult.txId }); - - if (result.isFailure()) { - console.log(`Something went wrong`, result); - return; - } - - const transactionStatusResult = result.value; - - if (transactionStatusResult.__typename === "LensTransaction") { - console.log( - `LensTransaction successfully completed - `, - { - txId: broadcastResult.txId, - txHash: broadcastResult.txHash, - lensTransactionStatus: transactionStatusResult.status, - reason: transactionStatusResult.reason, - extraInfo: transactionStatusResult.extraInfo, - } - ); - return; - } - - console.log(`LensMetadataTransaction successfully completed`, { - txId: broadcastResult.txId, - txHash: broadcastResult.txHash, - // a: transactionStatusResult. - lensTransactionStatus: transactionStatusResult.status, - extraInfo: transactionStatusResult.extraInfo, - metadataFailedReason: transactionStatusResult.metadataFailedReason, - }); -} - -main(); diff --git a/examples/node/tsconfig.json b/examples/node/tsconfig.json index 24c66a4a75..9b4132cb28 100644 --- a/examples/node/tsconfig.json +++ b/examples/node/tsconfig.json @@ -3,7 +3,9 @@ "transpileOnly": true }, "compilerOptions": { + "module": "NodeNext", "target": "ESNext", "moduleResolution": "NodeNext" - } + }, + "include": ["scripts"] } diff --git a/packages/client/src/index.ts b/packages/client/src/index.ts index 5b9221c86b..cd6952ccf0 100644 --- a/packages/client/src/index.ts +++ b/packages/client/src/index.ts @@ -66,4 +66,12 @@ export type { } from './graphql/types.generated'; // enums -export { PublicationReactionType } from './graphql/types.generated'; +export { + FeedEventItemType, + PublicationReactionType, + PublicationReportingFraudSubreason, + PublicationReportingIllegalSubreason, + PublicationReportingReason, + PublicationReportingSensitiveSubreason, + PublicationReportingSpamSubreason, +} from './graphql/types.generated'; diff --git a/packages/client/src/submodules/publication/Publication.ts b/packages/client/src/submodules/publication/Publication.ts index 62e9ce5411..e30ee800bf 100644 --- a/packages/client/src/submodules/publication/Publication.ts +++ b/packages/client/src/submodules/publication/Publication.ts @@ -185,7 +185,15 @@ export class Publication { } /** - * TODO + * Fetch tags + * + * @param request - Request object for the query + * @returns {@link TagResultFragment} wrapped in {@link PaginatedResult} + * + * @example + * ```ts + * const result = await client.publication.tags({}); + * ``` */ async tags(request: PublicationsTagsRequest): Promise> { return buildPaginatedQueryResult(async (currRequest) => { @@ -222,6 +230,21 @@ export class Publication { return result.data.result; } + /** + * Hide a publication + * + * ⚠️ Requires authenticated LensClient. + * + * @param request - Request object for the mutation + * @returns {@link PromiseResult} with void + * + * @example + * ```ts + * await client.publication.hide({ + * for: '0x014e-0x0a', + * }); + * ``` + */ async hide( request: HidePublicationRequest, ): PromiseResult { @@ -230,6 +253,30 @@ export class Publication { }); } + /** + * Report a publication with a reason + * + * ⚠️ Requires authenticated LensClient. + * + * @param request - Request object for the mutation + * @returns {@link PromiseResult} with void + * + * @example + * ```ts + * import { PublicationReportingReason, PublicationReportingSpamSubreason } from "@lens-protocol/client"; + * + * await client.publication.report({ + * for: '0x014e-0x0a', + * reason: { + * spamReason: { + * reason: PublicationReportingReason.Spam, + * subreason: PublicationReportingSpamSubreason.FakeEngagement, + * }, + * }, + * additionalComments: 'comment', + * }); + * ``` + */ async report( request: ReportPublicationRequest, ): PromiseResult { @@ -238,10 +285,28 @@ export class Publication { }); } - async postOnchain( + /** + * Create a post on chain. + * + * ⚠️ Requires authenticated LensClient. + * + * @param request - Request object for the mutation + * @returns {@link PromiseResult} with {@link RelaySuccessFragment} or {@link LensProfileManagerRelayErrorFragment} + * + * @example + * ```ts + * const result = await client.publication.postOnChain({ + * contentURI: 'ipfs://Qm...', // or arweave + * referenceModule: { + * followerOnlyReferenceModule: false, // anybody can comment or mirror + * }, + * }); + * ``` + */ + async postOnChain( request: OnchainPostRequest, ): PromiseResult< - LensProfileManagerRelayErrorFragment | RelaySuccessFragment, + RelaySuccessFragment | LensProfileManagerRelayErrorFragment, CredentialsExpiredError | NotAuthenticatedError > { return requireAuthHeaders(this.authentication, async (headers) => { @@ -250,10 +315,26 @@ export class Publication { }); } - async commentOnchain( + /** + * Create a comment on chain. + * + * ⚠️ Requires authenticated LensClient. + * + * @param request - Request object for the mutation + * @returns {@link PromiseResult} with {@link RelaySuccessFragment} or {@link LensProfileManagerRelayErrorFragment} + * + * @example + * ```ts + * const result = await client.publication.commentOnChain({ + * commentOn: "0x123-0x456", + * contentURI: "ipfs://Qm...", // or arweave + * }); + * ``` + */ + async commentOnChain( request: OnchainCommentRequest, ): PromiseResult< - LensProfileManagerRelayErrorFragment | RelaySuccessFragment, + RelaySuccessFragment | LensProfileManagerRelayErrorFragment, CredentialsExpiredError | NotAuthenticatedError > { return requireAuthHeaders(this.authentication, async (headers) => { @@ -262,10 +343,25 @@ export class Publication { }); } - async mirrorOnchain( + /** + * Create a mirror on chain. + * + * ⚠️ Requires authenticated LensClient. + * + * @param request - Request object for the mutation + * @returns {@link PromiseResult} with {@link RelaySuccessFragment} or {@link LensProfileManagerRelayErrorFragment} + * + * @example + * ```ts + * const result = await client.publication.mirrorOnChain({ + * mirrorOn: "0x123-0x456", + * }); + * ``` + */ + async mirrorOnChain( request: OnchainMirrorRequest, ): PromiseResult< - LensProfileManagerRelayErrorFragment | RelaySuccessFragment, + RelaySuccessFragment | LensProfileManagerRelayErrorFragment, CredentialsExpiredError | NotAuthenticatedError > { return requireAuthHeaders(this.authentication, async (headers) => { @@ -274,10 +370,26 @@ export class Publication { }); } - async quoteOnchain( + /** + * Create a quote on chain. + * + * ⚠️ Requires authenticated LensClient. + * + * @param request - Request object for the mutation + * @returns {@link PromiseResult} with {@link RelaySuccessFragment} or {@link LensProfileManagerRelayErrorFragment} + * + * @example + * ```ts + * const result = await client.publication.quoteOnChain({ + * quoteOn: "0x123-0x456", + * contentURI: "ipfs://Qm...", // or arweave + * }); + * ``` + */ + async quoteOnChain( request: OnchainQuoteRequest, ): PromiseResult< - LensProfileManagerRelayErrorFragment | RelaySuccessFragment, + RelaySuccessFragment | LensProfileManagerRelayErrorFragment, CredentialsExpiredError | NotAuthenticatedError > { return requireAuthHeaders(this.authentication, async (headers) => { @@ -286,6 +398,21 @@ export class Publication { }); } + /** + * Create a post on Momoka. + * + * ⚠️ Requires authenticated LensClient. + * + * @param request - Request object for the mutation + * @returns {@link PromiseResult} with {@link CreateMomokaPublicationResultFragment} or {@link LensProfileManagerRelayErrorFragment} + * + * @example + * ```ts + * const result = await client.publication.postOnMomoka({ + * contentURI: "ipfs://Qm...", // or arweave + * }); + * ``` + */ async postOnMomoka( request: MomokaPostRequest, ): PromiseResult< @@ -334,7 +461,28 @@ export class Publication { }); } - async createOnchainPostTypedData( + /** + * Create typed data for creating a post on chain. + * + * Typed data has to be signed by the profile's wallet and broadcasted with {@link Transaction.broadcastOnChain}. + * + * ⚠️ Requires authenticated LensClient. + * + * @param request - Request object for the mutation + * @param options - Configure returned typed data + * @returns Typed data for creating a post + * + * @example + * ```ts + * const result = await client.publication.createOnChainPostTypedData({ + * contentURI: "ipfs://Qm...", // or arweave + * referenceModule: { + * followerOnlyReferenceModule: false, // anybody can comment or mirror + * }, + * }); + * ``` + */ + async createOnChainPostTypedData( request: OnchainPostRequest, options?: TypedDataOptions, ): PromiseResult< @@ -354,7 +502,7 @@ export class Publication { }); } - async createOnchainCommentTypedData( + async createOnChainCommentTypedData( request: OnchainCommentRequest, options?: TypedDataOptions, ): PromiseResult< @@ -374,7 +522,7 @@ export class Publication { }); } - async createOnchainMirrorTypedData( + async createOnChainMirrorTypedData( request: OnchainMirrorRequest, options?: TypedDataOptions, ): PromiseResult< @@ -394,7 +542,7 @@ export class Publication { }); } - async createOnchainQuoteTypedData( + async createOnChainQuoteTypedData( request: OnchainQuoteRequest, options?: TypedDataOptions, ): PromiseResult< @@ -414,6 +562,24 @@ export class Publication { }); } + /** + * Create typed data for creating a post on Momoka. + * + * Typed data has to be signed by the profile's wallet and broadcasted with {@link Transaction.broadcastOnMomoka}. + * + * ⚠️ Requires authenticated LensClient. + * + * @param request - Request object for the mutation + * @param options - Configure returned typed data + * @returns Typed data for creating a post + * + * @example + * ```ts + * const result = await client.publication.createMomokaPostTypedData({ + * contentURI: "ipfs://Qm...", // or arweave + * }); + * ``` + */ async createMomokaPostTypedData( request: MomokaPostRequest, ): PromiseResult< @@ -489,7 +655,7 @@ export class Publication { async legacyCollect( request: LegacyCollectRequest, ): PromiseResult< - LensProfileManagerRelayErrorFragment | RelaySuccessFragment, + RelaySuccessFragment | LensProfileManagerRelayErrorFragment, CredentialsExpiredError | NotAuthenticatedError > { return requireAuthHeaders(this.authentication, async (headers) => { diff --git a/packages/client/src/submodules/publication/helpers/helpers.ts b/packages/client/src/submodules/publication/helpers/helpers.ts index 8e3dda2f86..19e5e1a5de 100644 --- a/packages/client/src/submodules/publication/helpers/helpers.ts +++ b/packages/client/src/submodules/publication/helpers/helpers.ts @@ -1,3 +1,8 @@ +import { + CreateMomokaPublicationResultFragment, + LensProfileManagerRelayErrorFragment, +} from '../../../graphql/fragments.generated'; + /** * @internal */ @@ -59,3 +64,15 @@ export function isQuotePublication>( ): publication is PickByTypename { return publication.__typename === 'Quote'; } + +/** + * Check if the result is a {@link CreateMomokaPublicationResultFragment}. + * + * @param result - result to check + * @returns true if the result is a {@link CreateMomokaPublicationResultFragment} + */ +export function isCreateMomokaPublicationResult( + result: CreateMomokaPublicationResultFragment | LensProfileManagerRelayErrorFragment, +): result is CreateMomokaPublicationResultFragment { + return result.__typename === 'CreateMomokaPublicationResult'; +} diff --git a/packages/client/src/submodules/transaction/helpers/index.ts b/packages/client/src/submodules/transaction/helpers/index.ts index e62ae72faf..54bdb530c3 100644 --- a/packages/client/src/submodules/transaction/helpers/index.ts +++ b/packages/client/src/submodules/transaction/helpers/index.ts @@ -29,6 +29,12 @@ export function isRelayError( return result.__typename === 'RelayError'; } +/** + * Check if the result is a {@link CreateMomokaPublicationResultFragment}. + * + * @param result - result to check + * @returns true if the result is a {@link CreateMomokaPublicationResultFragment} + */ export function isMomokaRelayResult( result: CreateMomokaPublicationResultFragment | RelayErrorFragment, ): result is CreateMomokaPublicationResultFragment {