Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Added feature to like or dislike NFTs & check if NFT is liked or not
- Loading branch information
1 parent
21e0e21
commit 7cfa49e
Showing
4 changed files
with
116 additions
and
6 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,71 @@ | ||
import { AppConstants } from '@/constants/AppConstants' | ||
|
||
const { signMessage } = require('@/utils/sign-message') | ||
|
||
const getNftLikeDislikeUrl = (tokenId, account) => { | ||
return AppConstants.nftApiBaseURL + '/log/like/:tokenId/:account' | ||
.replace(':tokenId', tokenId) | ||
.replace(':account', account) | ||
} | ||
|
||
const getIsNftLikedUrl = (tokenId, account) => { | ||
return AppConstants.nftApiBaseURL + '/insights/like/:tokenId/:account' | ||
.replace(':tokenId', tokenId) | ||
.replace(':account', account) | ||
} | ||
|
||
const likeOrDislikeNft = async ({ | ||
account, | ||
tokenId, | ||
library, | ||
signingMessage, | ||
onSuccess = () => {}, | ||
onError = e => e | ||
}) => { | ||
if (!account) return | ||
|
||
try { | ||
const signature = await signMessage(library, signingMessage) | ||
|
||
const url = getNftLikeDislikeUrl(tokenId, account) | ||
|
||
const res = await fetch(url, { | ||
method: 'POST', | ||
headers: { | ||
'Content-Type': 'application/json' | ||
}, | ||
body: JSON.stringify({ | ||
signature | ||
}) | ||
}) | ||
|
||
if (res.ok) { | ||
if (onSuccess) onSuccess() | ||
return | ||
} | ||
|
||
if (onError) onError() | ||
} catch (e) { | ||
if (onError) onError(e) | ||
} | ||
} | ||
|
||
const isNftLiked = async ({ account, tokenId }) => { | ||
if (!tokenId || !account) return false | ||
|
||
const url = getIsNftLikedUrl(tokenId, account) | ||
|
||
try { | ||
const res = await fetch(url) | ||
const json = await res.json() | ||
const liked = json?.data?.[0]?.liked | ||
|
||
return liked | ||
} catch (error) { | ||
console.log({ error }) | ||
} | ||
|
||
return false | ||
} | ||
|
||
export { likeOrDislikeNft, getNftLikeDislikeUrl, isNftLiked, getIsNftLikedUrl } |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,10 @@ | ||
const signMessage = async (library, message) => { | ||
if (!library || !library.getSigner) return '' | ||
|
||
const signer = library.getSigner() | ||
const res = await signer.signMessage(message) | ||
|
||
return res | ||
} | ||
|
||
export { signMessage } |