11import detectEthereumProvider from '@metamask/detect-provider'
22import axios from 'axios'
33import { ethers } from 'ethers'
4- import {
5- ADDRESS_CHECK_ENDPOINT ,
6- ADDRESS_CHECK_ENDPOINT_DEV ,
7- } from './config/endpoint'
4+ import { API_ENDPOINT , API_ENDPOINT_DEV } from './config/endpoint'
85import DropKitCollectionABI from './contracts/DropKitCollection.json'
96import DropKitCollectionV2ABI from './contracts/DropKitCollectionV2.json'
107import DropKitCollectionV3ABI from './contracts/DropKitCollectionV3.json'
@@ -19,7 +16,9 @@ export default class DropKit {
1916 apiKey : string
2017 dev ?: boolean
2118 address : string
19+ collectionId ?: string
2220 contract : ethers . Contract | null
21+ walletAddress ?: string
2322 version : number
2423
2524 constructor ( key : string , isDev ?: boolean ) {
@@ -38,7 +37,7 @@ export default class DropKit {
3837 address : string
3938 } > {
4039 const { data } = await axios . get (
41- this . dev ? ADDRESS_CHECK_ENDPOINT_DEV : ADDRESS_CHECK_ENDPOINT ,
40+ ` ${ this . dev ? API_ENDPOINT_DEV : API_ENDPOINT } /drops/address` ,
4241 {
4342 headers : {
4443 'x-api-key' : this . apiKey ,
@@ -48,6 +47,7 @@ export default class DropKit {
4847
4948 if ( data ) {
5049 this . address = data . address
50+ this . collectionId = data . collectionId
5151 this . version = data . version
5252 const abi = abis [ data . version || 1 ]
5353
@@ -64,6 +64,7 @@ export default class DropKit {
6464 const provider = new ethers . providers . Web3Provider ( ethereum )
6565 const signerOrProvider = provider . getSigner ( )
6666
67+ this . walletAddress = await signerOrProvider . getAddress ( )
6768 this . contract = new ethers . Contract ( data . address , abi , signerOrProvider )
6869 }
6970
@@ -123,18 +124,43 @@ export default class DropKit {
123124 return mintedNfts . toNumber ( )
124125 }
125126
126- async mint ( quantity : number ) : Promise < boolean > {
127+ async generateProof ( ) : Promise < string > {
128+ const { data } = await axios . post (
129+ `${ this . dev ? API_ENDPOINT_DEV : API_ENDPOINT } /drops/list/${
130+ this . collectionId
131+ } `,
132+ {
133+ wallet : this . walletAddress ,
134+ }
135+ )
136+
137+ return data
138+ }
139+
140+ async mint ( quantity : number ) : Promise < void > {
127141 if ( ! this . contract ) {
128142 throw new Error ( 'Initialization failed' )
129143 }
130-
131144 const price = await this . price ( )
132145
146+ // Presale mint
147+ try {
148+ const proof = await this . generateProof ( )
149+ const results = await this . contract . presaleMint ( quantity , proof , {
150+ value : ethers . utils . parseEther ( price . toString ( ) ) . mul ( quantity ) ,
151+ } )
152+ await results . wait ( )
153+
154+ return
155+ } catch ( err ) {
156+ console . log ( err )
157+ }
158+
133159 const results = await this . contract . mint ( quantity , {
134160 value : ethers . utils . parseEther ( price . toString ( ) ) . mul ( quantity ) ,
135161 } )
136162 await results . wait ( )
137163
138- return true
164+ return
139165 }
140166}
0 commit comments