NFT Trader
Ask or search…
K

PostBuilder

Discover how the PostBuilder class works

Index

  • Why a PostBuilder class exists?
  • How to instantiate an object of the PostBuilder class?
  • Setup your Post object for the PostClient and create a Post

Why a PostBuilder class exists?

The PostBuilder class has been designed as a utility to facilitate developers in constructing a Post object. Since this object has numerous properties and needs to be built following certain rules, a class that simplifies the operation can be very helpful without requiring writing more code than necessary.

How to instantiate an object of the PostBuilder class?

To instantiate an object of the PostBuilder class, you can use the following constructor..
/**
* @param networkId - the network id of the Post object
* @param creator - the creator of the Post object
*/
constructor(networkId?: string, creator?: Collector) {...}
As you can see, both parameters are optional, as in the case of networkId and creator, their values can be assigned using specific methods provided by the PostBuilder class.

Setup your Post object for the PostClient class

Let's now move on to the most important part regarding the PostBuilder class. How can we create an object of the Post class?
Let's analyze the following example, and then we will comment on each individual method provided by this utility class.
The following snippets are intended as possible use cases in all SPAs, regardless of the framework, library, or development environment.
//your application logic...
//it's time to create a Post through the PostBuilder object
const sdk = getPostSDKObject(...)
const postBuilder = new PostBuilder() //Example: i don't need to initialize the network id or the creator at the costructor level
//for some reason that depends on my application logic
postBuilder.setNetworkId("1") //1 is the networkId related to the Ethereum main net network
postBuilder.setPostCreator("0x69Fc3a866a963d4A0a05B2Fb27B167E0012dFEa5")
//postBuilder.setPostTypeBy("0", "0") //looking for code as first parameter, offer code as second parameter
postBuilder.setPostType(POST_TYPE.A1) //possible values are POST_TYPE.A2, POST_TYPE.B1, POST_TYPE.B2, POST_TYPE.C1, POST_TYPE.R1
postBuilder.setPostDuration(1683276661) //the expiration date of the Post in seconds
postBuilder.setIntroMessage("2") //the intro message code for the Post. See the Message section below
//i want NFTs from the Bored Ape Yacht Club Collection
postBuilder.addWantedAsset({
address: "0xbc4ca0eda7647a8ab7c2061c2e118a18a936f13d",
networkId: "1",
type: "ERC721",
tokenId: null
amount: null
})
//i want NFTs from the Doodles Collection
postBuilder.addWantedAsset({
address: "0x8a90cab2b38dba80c64b7734e58ee1db38b8992e",
networkId: "1",
type: "ERC721",
tokenId: null
amount: null
})
//i'm offering the NFT with the token 3455 from the Doodles Collection
postBuilder.addOfferedAsset({
address: "0x8a90cab2b38dba80c64b7734e58ee1db38b8992e",
networkId: "1",
type: "ERC721",
tokenId: "3455"
amount: null
})
const postLike : PostLike = postBuilder.factory() //we create a PostLike object, in this way we can setup correctly the post builder
const cp = postBuilder.getCreatePost(postLike) //getCreatePostReply() if we're building a PostReply object
await sdk.createPost(cp) //done! :)
We can notice how in the pseudocode, first we set all the values related to the Post, such as the network ID, creator's address, duration, offered and desired assets. Then, using the factory() method, followed by getCreatePost(), we prepare the instance that will be used by the createPost() method of the SDK.