Skip to content

Commit

Permalink
🔀 Merge #860 ✨ Fetch nft class listing on chain in buy NFT functions
Browse files Browse the repository at this point in the history
  • Loading branch information
williamchong authored Jan 4, 2023
2 parents 3626ff7 + 5e38cb0 commit 958acbc
Show file tree
Hide file tree
Showing 12 changed files with 44,641 additions and 59,614 deletions.
34,480 changes: 14,793 additions & 19,687 deletions functions/package-lock.json

Large diffs are not rendered by default.

3 changes: 2 additions & 1 deletion functions/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@
"@google-cloud/firestore": "^4.2.0",
"@google-cloud/pubsub": "^3.2.1",
"@likecoin/edm": "^6.0.0",
"@likecoin/iscn-js": "^0.2.2",
"@likecoin/iscn-js": "^0.5.2",
"@likecoin/nuxt-google-optimize": "^0.5.3-like.11",
"@likecoin/ui-vue": "^0.7.1",
"@likecoin/wallet-connector": "^0.17.1",
Expand All @@ -32,6 +32,7 @@
"@walletconnect/browser-utils": "^1.8.0",
"agentkeepalive": "^4.1.3",
"axios": "^0.21.2",
"bignumber.js": "^9.1.1",
"bn.js": "^5.2.1",
"body-parser": "^1.18.3",
"chokidar": "^3.5.3",
Expand Down
95 changes: 76 additions & 19 deletions src/mixins/nft.js
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@ import {
NFT_INDEXER_LIMIT_MAX,
signTransferNFT,
signGrant,
signBuyNFT,
broadcastTx,
getNFTCountByClassId,
getISCNRecord,
Expand Down Expand Up @@ -97,6 +98,7 @@ export default {
'getNFTClassFeaturedSetByAddress',
'getNFTClassHiddenSetByAddress',
'getNFTClassPurchaseInfoById',
'getNFTClassListingInfoById',
'getNFTClassMetadataById',
'getNFTClassOwnerInfoById',
'getNFTClassOwnerCount',
Expand Down Expand Up @@ -125,7 +127,26 @@ export default {
return this.NFTClassMetadata.message || '';
},
purchaseInfo() {
return this.getNFTClassPurchaseInfoById(this.classId) || {};
const info = this.getNFTClassPurchaseInfoById(this.classId) || {};
const { price, totalPrice, metadata: { nextNewNFTId } = {} } = info;
return {
price,
totalPrice,
classId: this.classId,
nftId: nextNewNFTId,
seller: LIKECOIN_NFT_API_WALLET,
};
},
listingInfo() {
const list = this.getNFTClassListingInfoById(this.classId) || {};
const { price, nftId, seller } = list;
return {
price,
totalPrice: price,
classId: this.classId,
nftId,
seller,
};
},
ownerInfo() {
return this.getNFTClassOwnerInfoById(this.classId) || {};
Expand Down Expand Up @@ -174,8 +195,17 @@ export default {
nftExternalURL() {
return this.nftMetadata.external_url || this.NFTExternalUrl;
},
nftIsUseListingPrice() {
return (
this.listingInfo &&
this.listingInfo.price &&
(!this.purchaseInfo || this.listingInfo.price < this.purchaseInfo.price)
);
},
NFTPrice() {
return this.purchaseInfo.price;
return this.nftIsUseListingPrice
? this.listingInfo.price
: this.purchaseInfo.price;
},
nftIsCollectable() {
return this.NFTPrice && this.NFTPrice !== -1;
Expand Down Expand Up @@ -289,7 +319,9 @@ export default {
return ownNFT?.[0];
},
nftIdCollectNext() {
return this.purchaseInfo?.metadata?.nextNewNFTId;
return this.nftIsUseListingPrice
? this.listingInfo.nftId
: this.purchaseInfo?.nftId;
},
canCollectWithoutWallet() {
return (
Expand Down Expand Up @@ -385,6 +417,7 @@ export default {
...mapActions([
'lazyGetUserInfoByAddress',
'fetchNFTPurchaseInfo',
'fetchNFTListingInfo',
'fetchNFTClassMetadata',
'fetchNFTOwners',
'initIfNecessary',
Expand Down Expand Up @@ -413,6 +446,7 @@ export default {
},
async updateNFTPurchaseInfo() {
await catchAxiosError(this.fetchNFTPurchaseInfo(this.classId));
catchAxiosError(this.fetchNFTListingInfo(this.classId));
},
async fetchNFTPrices(classId) {
try {
Expand Down Expand Up @@ -567,7 +601,7 @@ export default {
await this.walletFetchLIKEBalance();
if (
this.walletLIKEBalance === 0 ||
this.walletLIKEBalance < this.purchaseInfo.totalPrice
this.walletLIKEBalance < this.NFTPrice
) {
logTrackerEvent(
this,
Expand All @@ -581,24 +615,42 @@ export default {
return undefined;
}

const { nftIsUseListingPrice } = this;
this.uiSetTxStatus(TX_STATUS.SIGN);
logTrackerEvent(
this,
'NFT',
'NFTCollectSignGrantRequested',
nftIsUseListingPrice
? 'NFTCollectSignBuyRequested'
: 'NFTCollectSignGrantRequested',
this.classId,
1
);
const signData = await signGrant({
senderAddress: this.getAddress,
amountInLIKE: this.purchaseInfo.totalPrice,
signer: this.getSigner,
});
let signData;
if (nftIsUseListingPrice) {
const { price, nftId, seller } = this.listingInfo;
signData = await signBuyNFT({
senderAddress: this.getAddress,
classId: this.classId,
nftId,
seller,
priceInLIKE: price,
signer: this.getSigner,
});
} else {
signData = await signGrant({
senderAddress: this.getAddress,
amountInLIKE: this.purchaseInfo.totalPrice,
signer: this.getSigner,
});
}
this.uiSetTxStatus(TX_STATUS.PROCESSING);
logTrackerEvent(
this,
'NFT',
'NFTCollectSignGrantApproved',
nftIsUseListingPrice
? 'NFTCollectSignBuyApproved'
: 'NFTCollectSignGrantApproved',
this.classId,
1
);
Expand All @@ -613,13 +665,18 @@ export default {
if (code !== 0) throw new Error(`TX_FAILED_WITH_CODE_${code}`);
if (txHash && this.uiIsOpenCollectModal) {
logTrackerEvent(this, 'NFT', 'NFTCollectPurchase', this.classId, 1);
const result = await this.$api.post(
postNFTPurchase({
txHash,
classId: this.classId,
ts: Date.now(),
})
);
let result;
if (nftIsUseListingPrice) {
result = { data: this.listingInfo.nftId };
} else {
result = await this.$api.post(
postNFTPurchase({
txHash,
classId: this.classId,
ts: Date.now(),
})
);
}
logTrackerEvent(
this,
'NFT',
Expand Down Expand Up @@ -761,7 +818,7 @@ export default {
this.uiSetTxError(error.response?.data || error.toString());
this.uiSetTxStatus(TX_STATUS.FAILED);
} finally {
if (this.nftIsWritingNFT) this.updateNFTPurchaseInfo();
this.updateNFTPurchaseInfo();
this.updateNFTHistory();
this.walletFetchLIKEBalance();
}
Expand Down
3 changes: 0 additions & 3 deletions src/mixins/portfolio.js
Original file line number Diff line number Diff line change
Expand Up @@ -281,9 +281,6 @@ export const createPorfolioMixin = ({
methods: {
...mapActions([
'fetchNFTListByAddress',
'fetchNFTClassMetadata',
'fetchNFTPurchaseInfo',
'fetchNFTOwners',
'fetchNFTDisplayStateListByAddress',
'lazyGetUserInfoByAddress',
]),
Expand Down
Loading

0 comments on commit 958acbc

Please sign in to comment.