Skip to content

Commit 2079ea4

Browse files
committed
Add getting collections
1 parent 5c19355 commit 2079ea4

File tree

3 files changed

+34
-26
lines changed

3 files changed

+34
-26
lines changed

lib/transactions/get-collections.ts

+23
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
import { Contract, ethers, Signer } from 'ethers';
2+
import abis from '../../abis/abis';
3+
4+
interface IProps {
5+
signer?: ethers.Signer | null;
6+
factoryContract: Contract;
7+
}
8+
export const getCollections = async ({ signer, factoryContract }: IProps) => {
9+
const collections: string[] = [];
10+
if (signer instanceof Signer) {
11+
const allCollectionDeployments = await factoryContract.getCollections();
12+
const address = await signer.getAddress();
13+
for (let i = 0; i < allCollectionDeployments.length; i++) {
14+
const collection = new Contract(allCollectionDeployments[i], abis.multiResourceAbi, signer);
15+
const owner = await collection.owner();
16+
17+
if (owner === address) {
18+
collections.push(allCollectionDeployments[i]);
19+
}
20+
}
21+
}
22+
return collections;
23+
};

lib/transactions/index.ts

+1
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
11
export * from './deploy-contract';
22
export * from './get-owned-nfts';
33
export * from './mint-nft';
4+
export * from './get-collections';

pages/multi-resource.tsx

+10-26
Original file line numberDiff line numberDiff line change
@@ -2,8 +2,7 @@ import { ConnectButton, useAddRecentTransaction } from "@rainbow-me/rainbowkit"
22
import type { NextPage } from "next"
33
import Head from "next/head"
44
import styles from "../styles/Home.module.css"
5-
import { useAccount, useContract, useProvider, useSigner } from "wagmi"
6-
import { Contract, Signer } from "ethers"
5+
import { useContract, useSigner } from "wagmi"
76
import NftList from "../components/nft-list"
87
import React, { useEffect, useState } from "react"
98
import Image from "next/image"
@@ -14,12 +13,15 @@ import {
1413
RMRKMultiResourceFactoryContractAddress,
1514
tokenContractDetails,
1615
} from "../constants"
17-
import { deployContract, getOwnedNfts, mintNft } from "../lib/transactions"
16+
import {
17+
deployContract,
18+
getCollections,
19+
getOwnedNfts,
20+
mintNft,
21+
} from "../lib/transactions"
1822

1923
const MultiResource: NextPage = () => {
20-
const provider = useProvider()
21-
const { data: signer, isSuccess } = useSigner()
22-
const { address, isConnected } = useAccount()
24+
const { data: signer } = useSigner()
2325
const addRecentTransaction = useAddRecentTransaction()
2426
const [currentRmrkDeployment, setCurrentRmrkDeployment] = useState<string>("")
2527
const [rmrkCollections, setRmrkCollections] = useState<string[]>([])
@@ -98,28 +100,10 @@ const MultiResource: NextPage = () => {
98100
setCurrentRmrkDeployment(rmrkCollections[Number(e.target.value)])
99101
}
100102

101-
async function queryCollections() {
102-
if (signer instanceof Signer) {
103-
const collections: string[] = []
104-
const allCollectionDeployments = await factoryContract.getCollections()
105-
for (let i = 0; i < allCollectionDeployments.length; i++) {
106-
const collection = new Contract(
107-
allCollectionDeployments[i],
108-
abis.multiResourceAbi,
109-
provider
110-
)
111-
if ((await collection.owner()) == address) {
112-
collections.push(allCollectionDeployments[i])
113-
}
114-
}
115-
116-
setRmrkCollections(collections)
117-
}
118-
}
119-
120103
const fetchData = () => {
121-
queryCollections().then((r) => {
104+
getCollections({ signer, factoryContract }).then((collections) => {
122105
setLoading(false)
106+
setRmrkCollections(collections)
123107
})
124108
if (currentRmrkDeployment.length > 0)
125109
getOwnedNfts({ signer, contractAddress: currentRmrkDeployment }).then(

0 commit comments

Comments
 (0)