NFT Trader
Ask or search…
K

PostClient

Discover how the PostClient class works

Index

How to instantiate an object of the PostClient class?

The PostClient class provides the necessary functionality for the user to create social interactions within the platform. To understand what this implies, a look at the Concepts section may be necessary to understand the lifecycle of a Post. In summary, the basic functionalities of the PostClient class include the ability to create or delete Posts and to create PostReply, which are offers to exchange under the original Post.
The code snippets are presented in a context where React is used, but any framework for building a Single Page Application (SPA) is suitable.
import { PostClient } from "@nfttrader-io/sdk"
const MyComponent = () => {
const postClient = useRef<PostClient | null>(null)
useEffect(() => {
postClient.current = new PostClient({
apiKey: "<YOUR_API_KEY>",
})
return () => {}
}, [])
return <></>
}
export default MyComponent
Once the object is initialized, all the trading functionalities provided by the SDK will be accessible. In the above example, some aspects of this object instance can be noted. The parameter provided for the instance is as follows:
It is the key to request in order to interact with the NFT Trader platform. You can request it here.

Before we begin, the signedMessage

In order to correctly interact with the methods made available by the PostClient class, it is important to note that in order to properly validate the user's wallet when performing various operations such as creating and deleting Posts or PostReply, a parameter called signedMessage must be provided. This parameter can be obtained by signing a specific message provided by the PostClient class.
To sign the message, the personal_sign method provided by the current provider being used can simply be used. In the following example, this is how the message could be encoded using the PostClient class.
const signedMessage = await provider.send("personal_sign", [
ethers.utils.hexlify(
ethers.utils.toUtf8Bytes(`${PostClient._MESSAGE_TO_SIGN}`)
),
"0x0000000000000000000000000000000000000000", //address of the wallet
])

Create a Post

In order to create a Post, the PostClient class provides the createPost() method. This method takes in several parameters that we will see in the example below. We will then look in detail at how to correctly configure these parameters. For the moment, let's focus on the main operations described in this pseudocode.
//import....
const MyComponent = () => {
const postClientRef = useRef<PostClient | null>(null)
const createPost = () => {
const {current : sdk} = postClientRef
const createPost: CreatePost = {
assets: getAssets(), //imaginary method to get the assets
creatorAddress: getCreatorAddress(), //imaginary method to get the address of the author
expirationDate: getExpirationDate(), //imaginary method to get the expiration date of the post
messages: getMessages(), //imaginary method to get the messages of the post
networkId: getNetworkId(), //imaginary method to get the network id of the post
type: getPostType(), //imaginary method to get the post type
}
;(async () => {
const signedMessage : string = await getSignedMessage() //imaginary message to get the signed message required
sdk.createPost(createPost, signedMessage)
.then(() => {
//do whatever you want
})
.catch(error => {
//handle the error
})
})()
}
useEffect(() => {
createPost()
}, [])
return <></>
}
export default MyComponent
As described in the example, the createPost() method allows creating a Post within the system. This operation will prompt the user to sign with their own wallet. The message to be signed and all the logic related to it are already encapsulated within the SDK, so there will be no need to manage them directly.

Parameters

On the other hand, the parameters of the createPost() method must be composed correctly. Below we will explain how to perform this operation and provide an explanation for each required parameter.
First of all, let's analyze the signature of the createPost() method.
/**
* Create a post
*
* @param post - A post to be created
* @param signedMessage - A signed message by the user wallet (provide it only if the PostClient is initialized with an API key)
*/
public async createPost(post: CreatePost, signedMessage?: string) {...}
post
The post parameter is of type CreatePost. Its structure is described here.
signedMessage
The parameter signedMessage is of type string. It is a message that the user must sign with their own wallet. The ways in which the user must sign the message are described here.

Delete a Post

To delete a Post, the PostClient class provides the deletePost() method. This method takes two input parameters. Let's analyze the following pseudocode in detail to understand how it works.
//import....
const MyComponent = () => {
const postClientRef = useRef<PostClient | null>(null)
const deletePost = (postId : string) => {
const {current : sdk} = postClientRef
;(async () => {
const signedMessage : string = await getSignedMessage() //imaginary message to get the signed message required
sdk.deletePost(postId, signedMessage)
.then(() => {
//do whatever you want
})
.catch(error => {
//handle the error
})
})()
}
useEffect(() => {
deletePost("1234")
}, [])
return <></>
}
export default MyComponent
As described in the example, the deletePost() method allows deleting a Post within the system. This operation will prompt the user to sign with their own wallet. The message to be signed and all the logic related to it are already encapsulated within the SDK, so there will be no need to manage them directly.

Parameters

On the other hand, the parameters of the deletePost() method must be composed correctly. Below we will explain how to perform this operation and provide an explanation for each required parameter.
First of all, let's analyze the signature of the deletePost() method.
/**
* Delete a post by its id
*
* @param id - The id of the post to delete
* @param signedMessage - A signed message by the user wallet (provide it only if the PostClient is initialized with an API key)
*/
public async deletePost(id: string, signedMessage?: string): Promise<void> {...}
id
The parameter id is of type string and refers to the id of the post that you want to delete.
signedMessage
The parameter signedMessage is of type string. It represents a message that the user must sign with their own wallet. The methods through which the user must sign the message are described here.

Create a PostReply

In order to create an offer related to a post, the PostClient class provides the createPostReply() method. Let's analyze the following pseudocode in detail to understand how it works.
//import....
const MyComponent = () => {
const postClientRef = useRef<PostClient | null>(null)
const createPostReply = (postId : string) => {
const {current : sdk} = postClientRef
const reply: CreatePostReply = {
creatorAddress: getCreatorAddress(),
assets: getAssets(),
messages: getMessages(),
networkId: getNetworkId(),
}
;(async () => {
const signedMessage : string = await getSignedMessage() //imaginary message to get the signed message required
sdk.createPostReply(reply, postId, signedMessage)
.then((response) => {
//do whatever you want
})
.catch(error => {
//handle the error
})
})()
}
useEffect(() => {
createPostReply("1234") //post parent id
}, [])
return <></>
}
export default MyComponent
As described in the example, the createPostReply() method allows creating a PostReply within the system. This operation will prompt the user to sign with their own wallet. The message to be signed and all the logic related to it are already encapsulated within the SDK, so there will be no need to manage them directly.

Parameters

On the other hand, the parameters of the createPostReply() method must be composed correctly. Below we will explain how to perform this operation and provide an explanation for each required parameter.
First of all, let's analyze the signature of the createPostReply() method.
/**
* Create a post reply
*
* @param reply - A reply to be created in the context of a parent post
* @param parentId - The parent post id to whom this reply belongs to
* @param signedMessage - A signed message by the user wallet (provide it only if the PostClient is initialized with an API key)
*/
public async createPostReply(
reply: CreatePostReply,
parentId: string,
signedMessage?: string
) {...}
reply
The reply parameter is of type CreatePostReply, the structure of which is described here. The reply is the object that contains information related to the offer.
parentId
The parentId parameter is of type string. It refers to the original Post to which the user is making an offer.
signedMessage
The parameter signedMessage is of type string. It represents a message that the user must sign with their own wallet. The ways in which the user must perform the message signature are described here.

Get Post

It is possible to obtain information about a single post using the getPost() method provided by the PostClient class. Let's analyze the following pseudocode to understand a possible implementation.
//import
const MyComponent = () => {
const postClientRef = useRef<PostClient | null>(null)
const getPost = (postId : string) => {
const {current : sdk} = postClientRef
sdk.getPost(postId)
.then((post: Maybe<Post>) => {
//do whatever you want
})
.catch(error => {
//handle error
})
}
useEffect(() => {
getPost("1234") //get post with id 1234
}, [])
return <></>
}
export default MyComponent
This method returns an object of type Maybe<Post>. The structure of this object is described in this section.
Parameters
Let's analyze the parameters of the function starting from the method signature.
/**
* Get a post by its id
*
* @param id - The id of the post
* @param creatorAddress - The creator of the post
*/
public async getPost(
id: string,
creatorAddress?: string
): Promise<Maybe<Post>> {...}
id
The parameter id is of string type and refers to the identifier of the Post for which one wants to obtain information.
creatorAddress
The optional parameter creatorAddress is of type string and refers to the address of the wallet that created the Post. It is used to ensure that the isCreator field of the Post object returned by the method is set to true if the creatorAddress matches the address of the wallet that created the Post.

List Posts

It is possible to obtain information about all the Posts created by users using the listPosts() method provided by the PostClient class. The listPosts() method can be used to build a feed page, in which all the posts created by users can be displayed. Let's analyze the following pseudocode to understand a possible implementation.
//import
const MyComponent = () => {
const postClientRef = useRef<PostClient | null>(null)
const getPosts = () => {
const {current : sdk} = postClient
const filters = {
owner: getOwner(), //imaginary method to get the owner of the posts, or undefined if we want to get posts of each user
collections: getCollections(), //imaginary method to get the posts that contains specific collections or undefined if we want to get every post
status: getStatus(), //imaginary method to get posts that has a specific status (ACTIVE, COMPLETED, etc..) or undefined if we want to get every post
type: getType(), //imaginary method to get posts that has a specific type (A1, A2, B1, etc..) or undefined if we want to get every post
verified: getVerified(), //imaginary method to get posts that has only verified collections or undefined if we want to get every post
network: getNetwork(), //imaginary method to get posts that are in a specific network (ETHEREUM, POLYGON, etc..) or undefined if we want to get every post
offers: getOffers(), //imaginary method to get posts that has a minimum number of offers or undefined if we want to get every post
}
const order = {
field: "creationDate", //expirationDate | assetsOffered | assetsWanted
direction: "ASC" //or DESC
}
const skip = 0
const take = 30
sdk.listPosts(filters, orders, skip, take)
.then((response: ListPostsResponse) => {
//do whatever you want
})
.catch(err => {
//handle error
})
}
useEffect(() => {
getPosts()
}, [])
return <></>
}
export default MyComponent
As you can see, the method listPosts() takes in a set of parameters that determine the final outcome. The resulting object is of type ListPostResponse and its structure can be analyzed here.
Parameters
The parameters involved are mainly the filters and the type of sorting desired. In addition, it is possible to paginate the results obtained through the use of the last two parameters. However, let's break down the method signature and thoroughly analyze the parameters accepted by the method.
/**
* List posts that match the filter and order object
*
* @param filtersOptions - An object that contains filter options, to see available filter options visit [this link](https://docs.nfttrader.io)
* @param orderOptions - An object that contains order options, to see available order options visit [this link](https://docs.nfttrader.io)
* @param skip - A number representing the number of posts to skip (used for the pagination)
* @param take - A number representing the number of posts to take (used for the pagination)
* @param creatorAddress - A string representing the address of the wallet owner. Used to set the isCreator value to true in the case a post is created by this address
*/
public async listPosts(
filtersOptions?: ListPostsFilters,
orderOptions?: ListPostsOrder,
skip?: number,
take?: number,
creatorAddress?: string
): Promise<ListPostsResponse> {...}
filtersOptions
An object of type ListPostsFilters. This object is used to filter the posts, so that the resulting output matches the desired characteristics.
The filtersOptions object in turn contains the following properties:
  • owner property of type string. It is used to filter all posts created by the indicated username or address.
  • collections property of type Array<{address: string; network: Network}>. It is used to filter all posts that contain one or more collections specified by their address and network.
  • status property of type PostStatusName or PostStatus[PostStatusName]. It is used to filter posts that have a specific status. Remember that the post statuses are described in the Post Lifecycle section.
  • type property of type PostTypeName or PostType[PostTypeName]. It is used to filter posts of a specific type. Remember that the post types are described in the Types of Posts section.
  • typeWanted property of type BitmapWanted. It is used to filter posts that have a specific sequence of assets in the wanted side. Remember that the types of assets that can be referenced in a post are described in the Post Assets section.
  • typeOffered property of type BitmapOffered. It is used to filter posts that have a specific sequence of assets in the offered side. Remember that the types of assets that can be referenced in a post are described in the Post Assets section.
  • verified property of type boolean. It is used to filter all posts that contain only verified collections.
  • network property of type Network. It is used to filter all posts that belong to a specific blockchain.
  • offers property of type number. It is used to filter all posts that have at least the number of offers indicated by this filter.
  • expirationDate property of type number. It is used to filter all posts that have an expiration date before the date indicated by this filter. The date value is expressed in seconds.
  • includeParent property of type boolean. It is used to include in the ListPostResponse object the information about the parent post for all posts classified as PostReply.
ordersOptions
An object of type ListPostsOrders. This object is used to sort the posts, so that the resulting output matches the desired characteristics.
The orderOptions object in turn contains the following properties:
  • field property of type string. It is used to indicate the type of sorting desired. Possible values are creationDate, expirationDate, assetsOffered or assetsWanted. - sorting with the value of creationDate sorts posts by creation date. - sorting with the value of expirationDate sorts posts by expiration date. - sorting with the value of assetsOffered sorts posts by number of offered assets. - sorting with the value of assetsWanted sorts posts by number of wanted assets.
  • direction optional property of type string. It is used to set the sorting direction, whether it is ascending (ASC) or descending (DESC).
skip
A number representing the number of posts to skip (used for the pagination).
take
A number representing the number of posts to take (used for the pagination).
creatorAddress
A string representing the address of the wallet owner. Used to set the isCreator value to true in the case a post is created by this address.

Get Post Replies

It is possible to obtain a list of offers related to a Post through the getPostReplies() method provided by the PostClient class. Let's analyze the following pseudocode to see a hypothetical example of implementation.
//import
const MyComponent = () => {
const postClientRef = useRef<PostClient | null>(null)
const getPostReplies = (postId : string) => {
const {current : sdk} = postClient
const skip = 0
const take = 10
const orders = {
field: "creationDate", // or "score"
direction: "ASC" // or "DESC"
}
sdk.getPostReplies(
postId,
orders,
skip,
take,
)
.then((response: ListPostsResponse) => {
//do whatever you want
})
.catch(err => {
//handle error
})
}
useEffect(() => {
getPostReplies("1234") //get Post Replies of the post with the id "1234"
}, [])
return <></>
}
export default MyComponent
As you can see, the method getPostReplies() takes in a set of parameters that determine the final outcome. The resulting object is of type ListPostResponse and its structure can be analyzed here.
Parameters
The parameters involved are mainly the type of sorting desired. In addition, it is possible to paginate the results obtained through the use of the last two parameters. However, let's break down the method signature and thoroughly analyze the parameters accepted by the method.
/**
* Get a post replies by the parent post id
*
* @param id - The id of the parent post
* @param orderOptions - An object that contains order options, to see available order options visit [this link](https://docs.nfttrader.io)
* @param skip - A number representing the number of posts to skip (used for the pagination)
* @param take - A number representing the number of posts to take (used for the pagination)
* @param creatorAddress - A string representing the address of the wallet owner. Used to set the isCreator value to true in the case a post is created by this address
*/
public async getPostReplies(
id: string,
orderOptions?: ListPostsRepliesOrder,
skip?: number,
take?: number,
creatorAddress?: string
): Promise<ListPostsResponse> {...}
id
The parameter id is of string type and refers to the identifier of the Post parent for which one wants to obtain information related to each PostReply linked to it.
ordersOptions
An object of type ListPostsRepliesOrders. This object is used to sort the posts, so that the resulting output matches the desired characteristics.
  • field property of type string. It is used to indicate the type of sorting desired. Possible values are creationDate or score. - sorting with the value of creationDate sorts post replies by creation date. - sorting with the value of score sorts post replies by the score.
  • direction optional property of type string. It is used to set the sorting direction, whether it is ascending (ASC) or descending (DESC).
Last modified 7mo ago