Skip to content

Instantly share code, notes, and snippets.

@olehmell
Last active October 22, 2021 13:56
Show Gist options
  • Save olehmell/8470383d1c9f6eab991a66094435328f to your computer and use it in GitHub Desktop.
Save olehmell/8470383d1c9f6eab991a66094435328f to your computer and use it in GitHub Desktop.
Subsocial SDK Examples
import { newFlatSubsocialApi } from '@subsocial/api'
import { ReactionId } from '@subsocial/types/substrate/interfaces'
import { idToBn } from '@subsocial/utils'
const MY_ACCOUNT = '3omeLMCdtrojRPf7KyvTg78EvLxyJMo7mb2bqM28EEvxmXFM'
const config = {
substrateNodeUrl: 'wss://rpc.subsocial.network',
offchainUrl: 'https://app.subsocial/network/offchain',
ipfsNodeUrl: 'https://app.subsocial/network/ipfs'
}
const example = async () => {
const api = await newFlatSubsocialApi(config)
// Get space data from Blockchain and content from IPFS
const spaceData = await api.findSpace({ id: idToBn('1') })
if (spaceData) {
const { struct, content } = spaceData
console.log(struct, content)
}
// Get posts
const spaces = await api.findPublicSpaces([ '1', '2', '3', '4' ])
const structs = []
const contents = []
spaces.forEach(({ struct, content }) => {
structs.push(struct)
contents.push(content)
})
const substrate = api.subsocial.substrate
// Get reply ids (comments) by parent post id and fetch posts by ids
const replyIds = await substrate.getReplyIdsByPostId(idToBn('1'))
const replies = await api.findPublicPosts(replyIds)
console.log('replies', replies)
//Get my space ids
const mySpaceIds = await substrate.spaceIdsByOwner(MY_ACCOUNT)
console.log('mySpaceIds', mySpaceIds)
// Check that account is follower the space
const isFollower = await substrate.isSpaceFollower(MY_ACCOUNT, idToBn('1'))
console.log('isFollower', isFollower)
// Get reactions (upvote/downvote) by owner in post ids [use multi requerst from blockchain]
const substrateApi = await substrate.api
const tuples = [ '1', '2', '3', '4' ].map(postId => [ MY_ACCOUNT, postId ])
const reactionIds = await substrateApi.query.reactions.postReactionIdByAccount.multi(tuples)
const reactions = await substrate.findReactions(reactionIds as ReactionId[])
console.log('reactions', reactions)
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment