11import { ethers } from "ethers" ;
22import TutorialTokenArtifact from "../contracts/TutorialToken.json" ;
33import contractAddress from "../contracts/contract-address.json" ;
4- import { useState } from "react" ;
4+ import { db } from '../lib/firebase' ;
5+ import { doc , getDocs , query , collection } from "firebase/firestore" ;
6+ import { StandardMerkleTree } from "@openzeppelin/merkle-tree" ;
57
68type useTutorialTokenReturnType = ( ) => {
7- mint : ( ) => Promise < string > ;
8- getBalance : ( ) => Promise < number > ;
9- ownerOf : ( ) => Promise < number > ;
9+ preMint : ( address : string , callBack : ( ) => { } ) => Promise < void > ;
10+ publicMint : ( callBack : ( ) => { } ) => Promise < void > ;
11+ totalSuply : ( ) => Promise < number > ;
12+ tokenOfOwnerByIndex : ( ) => Promise < number > ;
13+ getTokenIdList : ( ) => Promise < number [ ] > ;
14+ setPreSale : ( preSale : boolean ) => Promise < void > ;
15+ setPublicSale : ( publicSale : boolean ) => Promise < void > ;
1016}
1117
1218export const useTutorialToken : useTutorialTokenReturnType = ( ) => {
13- // const [provider, setProvider] = useState<ethers.providers.Web3Provider|null>(null);
14- // const [token, setToken] = useState<ethers.Contract|null>(null);
1519 let provider : ethers . providers . Web3Provider | null = null ;
1620 let token : ethers . Contract | null = null ;
1721
1822 async function _initializeEthers ( ) {
1923 provider = new ethers . providers . Web3Provider ( window . ethereum ) ;
20- // console.log(_provider);
21- // setProvider(_provider);
22- console . log ( provider ) ;
2324
2425 token = await new ethers . Contract (
2526 contractAddress . TutorialToken ,
@@ -29,30 +30,81 @@ export const useTutorialToken: useTutorialTokenReturnType = () => {
2930 console . log ( token ) ;
3031 }
3132
33+ async function preMint ( address : string , callBack : ( ) => { } ) : Promise < void > {
34+ if ( token === null && provider === null ) await _initializeEthers ( ) ;
35+ const q = query ( collection ( db , "allowList" ) ) ;
36+ const querySnapshot = await getDocs ( q ) ;
37+ const treeValues = [ ] ;
38+ querySnapshot . forEach ( ( doc ) => {
39+ const data = doc . data ( ) ;
40+ data . address && treeValues . push ( [ data . address ] ) ;
41+ } ) ;
42+ const tree = StandardMerkleTree . of ( treeValues , [ "address" ] ) ;
43+ let proof : string [ ] | undefined ;
44+ for ( const [ i , v ] of tree . entries ( ) ) {
45+ if ( v [ 0 ] === address ) {
46+ proof = tree . getProof ( i ) ;
47+ }
48+ }
49+ console . log ( proof ) ;
50+ const transaction = await token . preMint ( proof ) ;
51+ const res = await transaction . wait ( ) ;
52+ console . log ( res ) ;
53+ callBack ( ) ;
54+ }
55+
56+ async function publicMint ( callBack : ( ) => { } ) : Promise < void > {
57+ if ( token === null && provider === null ) await _initializeEthers ( ) ;
58+ const transaction = await token . mint ( ) ;
59+ const res = await transaction . wait ( ) ;
60+ console . log ( res ) ;
61+ callBack ( ) ;
62+ }
63+
64+ async function totalSuply ( ) : Promise < number > {
65+ if ( token === null && provider === null ) await _initializeEthers ( ) ;
66+ const totalSuply = await token . totalSupply ( ) ;
67+ console . log ( totalSuply ) ;
68+ return totalSuply . toNumber ( ) ;
69+ }
70+
71+ async function tokenOfOwnerByIndex ( ) : Promise < number > {
72+ if ( token === null && provider === null ) await _initializeEthers ( ) ;
73+ const address = await provider . getSigner ( ) . getAddress ( ) ;
74+ const count = await token . tokenOfOwnerByIndex ( address , 1 ) ;
75+ console . log ( 'tokenOfOwnerByIndex' , count . toNumber ( ) ) ;
76+ return count . toNumber ( ) ;
77+ }
78+
79+ async function getTokenIdList ( ) : Promise < number [ ] > {
80+ if ( token === null && provider === null ) await _initializeEthers ( ) ;
81+ const address = await provider . getSigner ( ) . getAddress ( ) ;
82+ const count = await token . balanceOf ( address ) ;
83+ const tokenIdList = [ ] ;
84+ for ( let i = 0 ; i < count . toNumber ( ) ; i ++ ) {
85+ const tokenId = await token . tokenOfOwnerByIndex ( address , i ) ;
86+ tokenIdList . push ( tokenId . toNumber ( ) ) ;
87+ }
88+ return tokenIdList ;
89+ }
90+
91+ async function setPreSale ( preSale : boolean ) : Promise < void > {
92+ if ( token === null && provider === null ) await _initializeEthers ( ) ;
93+ await token . setPreSale ( preSale ) ;
94+ }
95+
96+ async function setPublicSale ( publicSale : boolean ) : Promise < void > {
97+ if ( token === null && provider === null ) await _initializeEthers ( ) ;
98+ await token . setPublicSale ( publicSale ) ;
99+ }
100+
32101 return {
33- mint : async ( ) => {
34- await _initializeEthers ( ) ;
35- const res = await token . mint ( ) ;
36- console . log ( res ) ;
37- const receipt = await res ?. wait ( ) ;
38- console . log ( receipt ) ;
39- // return tokenId;
40- return 'hogehoge' ;
41- } ,
42-
43- ownerOf : async ( ) => {
44- if ( token === null && provider === null ) await _initializeEthers ( ) ;
45- const address = await token . ownerOf ( 1 ) ;
46- console . log ( address ) ;
47- } ,
48-
49- getBalance : async ( ) => {
50- if ( token === null && provider === null ) await _initializeEthers ( ) ;
51- const address = await provider . getSigner ( ) . getAddress ( ) ;
52- console . log ( address ) ;
53- const balance = await token . hoge ( ) ;
54- console . log ( balance ) ;
55- return 1 ;
56- } ,
102+ preMint,
103+ publicMint,
104+ totalSuply,
105+ tokenOfOwnerByIndex,
106+ getTokenIdList,
107+ setPreSale,
108+ setPublicSale,
57109 }
58110} ;
0 commit comments