Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

💄 Display owner's listing NFT #11

Merged
merged 1 commit into from
Jan 18, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
40 changes: 33 additions & 7 deletions pages/owned/index.vue
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@
<tbody>
<tr
v-for="item, i in ownedList"
:key="`${item.classId}_${item.id}`"
:key="makeKey(item.classId, item.id)"
>
<td>#{{ i + 1 }}</td>
<td>
Expand All @@ -28,7 +28,11 @@
<button @click="viewClassListings(item.classId)">View NFT Class Listing</button>
</td>
<td>
<button @click="newNFTListing(item.classId, item.id)">Sell</button>
<section v-if="item.price">
<div>{{ new BigNumber(item.price).shiftedBy(-9).toFixed() }}LIKE</div>
<div>till {{ item.expiration }}</div>
</section>
<button v-else @click="newNFTListing(item.classId, item.id)">Sell</button>
</td>
</tr>
</tbody>
Expand All @@ -38,6 +42,7 @@
</template>

<script setup lang="ts">
import BigNumber from 'bignumber.js';
import { storeToRefs } from 'pinia';
import { useWalletStore } from '~/stores/wallet';
import { queryOwnedNFTClasses } from '../../utils/cosmos';
Expand All @@ -46,7 +51,7 @@ const router = useRouter();

const store = useWalletStore();
const { wallet } = storeToRefs(store);
const ownedList = ref([] as any[]);
const ownedMap = ref({} as any);

onMounted(async () => {
if (!wallet.value) {
Expand All @@ -55,18 +60,37 @@ onMounted(async () => {
if (!wallet.value) {
return;
}
fetchOwnedNFTs();
fetchOwnedAndListingNFTs();
})

watch(wallet, (newWallet) => {
if (newWallet) fetchOwnedNFTs();
if (newWallet) fetchOwnedAndListingNFTs();
})


const { connect } = store;

async function fetchOwnedNFTs() {
ownedList.value = await queryOwnedNFTClasses(wallet.value);
function makeKey(classId: string, nftId: string) {
return `${classId}_${nftId}`;
}

async function fetchOwnedAndListingNFTs() {
const [
ownedList,
listingEvents
] = await Promise.all([
queryOwnedNFTClasses(wallet.value),
getNFTMarketplaceListing(wallet.value),
]);
ownedList.forEach((n) => {
ownedMap.value[makeKey(n.classId, n.id)] = n;
});

listingEvents.forEach((e: any) => {
const n = ownedMap.value[makeKey(e.class_id, e.nft_id)];
n.price = e.price;
n.expiration = e.expiration;
});
}

async function viewClassListings(classId: string) {
Expand All @@ -76,4 +100,6 @@ async function viewClassListings(classId: string) {
async function newNFTListing(classId: string, nftId: string) {
router.push({ path: `/sell/${classId}/${nftId}` });
}

const ownedList = computed(() => Object.values<any>(ownedMap.value));
</script>
15 changes: 15 additions & 0 deletions utils/cosmos.ts
Original file line number Diff line number Diff line change
Expand Up @@ -197,6 +197,21 @@ export async function getRecentBuyNFTEvents() {
return events;
}

export async function getNFTMarketplaceListing(creator: string) {
const items = [] as any[];
let key = '';
do {
const { data } = await axios.get(
`${LCD_URL}/likechain/likenft/v1/marketplace?type=listing&creator=${creator}&pagination.key=${key}`
);
key = data.pagination.next_key;
if (data.items) {
items.push(...data.items);
}
} while (key);
return items;
}

export async function getNFTs({ classId = '', owner = '', needCount = 0 }) {
const baseURL = `${LCD_URL}/cosmos/nft/v1beta1/nfts?owner=${owner}&class_id=${classId}`;
const nfts = [];
Expand Down