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 objectconstsdk=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 logicpostBuilder.setNetworkId("1") //1 is the networkId related to the Ethereum main net networkpostBuilder.setPostCreator("0x69Fc3a866a963d4A0a05B2Fb27B167E0012dFEa5")//postBuilder.setPostTypeBy("0", "0") //looking for code as first parameter, offer code as second parameterpostBuilder.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 secondspostBuilder.setIntroMessage("2") //the intro message code for the Post. See the Message section below//i want NFTs from the Bored Ape Yacht Club CollectionpostBuilder.addWantedAsset({ address:"0xbc4ca0eda7647a8ab7c2061c2e118a18a936f13d", networkId:"1", type:"ERC721", tokenId:null amount: null})//i want NFTs from the Doodles CollectionpostBuilder.addWantedAsset({ address:"0x8a90cab2b38dba80c64b7734e58ee1db38b8992e", networkId:"1", type:"ERC721", tokenId:null amount: null})//i'm offering the NFT with the token 3455 from the Doodles CollectionpostBuilder.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
constcp=postBuilder.getCreatePost(postLike) //getCreatePostReply() if we're building a PostReply objectawaitsdk.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.