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

Added Custom Price Method Override #73

Open
wants to merge 4 commits into
base: main
Choose a base branch
from
Open
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
13 changes: 12 additions & 1 deletion src/components/MintModal.js
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,8 @@ export const MintModal = (props, ref) => {
const [isLoading, setIsLoading] = useState(false)
const [step, setStep] = useState(1)
const [quantity, setQuantity] = useState(1)
const [mintFunction, setMintfunction] = useState(1)
const [priceFunction, setPriceFunction] = useState(1)

const handleClose = () => {
setIsOpen(false);
Expand Down Expand Up @@ -86,6 +88,9 @@ export const MintModal = (props, ref) => {
{step === 1 && <QuantityModalStep
setTxHash={setTxHash}
setQuantity={setQuantity}
setPriceFunction={setPriceFunction}
setMintfunction={setMintfunction}
setQuantity={setQuantity}
setStep={setStep}
setIsLoading={setIsLoading}
/>}
Expand All @@ -98,10 +103,16 @@ export const MintModal = (props, ref) => {

export const modalRef = React.createRef();

export const showMintModal = (quantity) => {
export const showMintModal = (quantity, mintFunction, priceFunction) => {
if (quantity) {
modalRef.current?.setQuantity(quantity)
}
if (mintFunction) {
modalRef.current?.setMintfunction(mintFunction)
}
if (priceFunction) {
modalRef.current?.setPriceFunction(priceFunction)
}
modalRef.current?.setIsOpen(true);
}

Expand Down
6 changes: 4 additions & 2 deletions src/components/QuantityModalStep.js
Original file line number Diff line number Diff line change
Expand Up @@ -15,14 +15,16 @@ import { isEthereumContract } from "../contract";

export const QuantityModalStep = ({ setQuantity, setStep, setIsLoading, setTxHash }) => {
const [quantityValue, setQuantityValue] = useState(1)
const [priceFunction, setPriceFunction] = useState()
const [mintFunction, setMintedFunction] = useState()
const [maxTokens, setMaxTokens] = useState(getDefaultMaxTokensPerMint())
const [mintPrice, setMintPrice] = useState(undefined)
const [mintedNumber, setMintedNumber] = useState()
const [totalNumber, setTotalNumber] = useState()

useEffect(() => {
if (isEthereumContract()) {
getMintPrice().then(price => {
getMintPrice(priceFunction).then(price => {
if (price !== undefined) {
setMintPrice(Number((price) / 1e18))
}
Expand All @@ -45,7 +47,7 @@ export const QuantityModalStep = ({ setQuantity, setStep, setIsLoading, setTxHas

const onSuccess = async () => {
setIsLoading(true)
const { tx } = await mint(quantityValue)
const { tx } = await mint(quantityValue, mintFunction, priceFunction)
if (tx === undefined) {
setIsLoading(false)
}
Expand Down
12 changes: 10 additions & 2 deletions src/mint/ui.js
Original file line number Diff line number Diff line change
Expand Up @@ -16,10 +16,9 @@ export const updateMintButton = () => {
const initialBtnText = mintButton.textContent;
setButtonText(mintButton, "Loading...")
try {
const quantity = getMintQuantity();
const address = await getWalletAddressOrConnect(true)
if (address) {
showMintModal(quantity);
showMintModal(getMintQuantity(), getMintFunction(), getPriceFunction());
}
} catch (e) {
console.log("Error on pressing mint")
Expand Down Expand Up @@ -56,6 +55,15 @@ const getMintQuantity = () => {
const quantity = document.querySelector('#quantity-select')?.value
return quantity !== '' && quantity !== undefined ? Number(quantity) : undefined;
}
const getMintFunction = (element) => {
const f = element.dataset?.mint;
return f !== '' && f !== undefined ? String(f) : undefined;
}

const getPriceFunction = (element) => {
const f = element.dataset?.mint;
return f !== '' && f !== undefined ? String(f) : undefined;
}

const setButtonText = (btn, text) => {
if (btn.childElementCount > 0) {
Expand Down
18 changes: 12 additions & 6 deletions src/mint/web3.js
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,9 @@ const getMethodWithCustomName = (methodName) => {
return undefined
}

const getMintTx = ({ numberOfTokens }) => {
const getMintTx = ({ numberOfTokens, mintFunctionOverride }) => {
const overridingMintMethod = getMethodWithCustomName('mint')
if(overridingMintMethod) return overridingMintMethod(numberOfTokens);
const customMintMethod = getMethodWithCustomName('mint')
if (customMintMethod)
return customMintMethod(numberOfTokens)
Expand Down Expand Up @@ -50,7 +52,10 @@ const getDefaultMintPrice = () => {
return undefined
}

export const getMintPrice = async () => {
export const getMintPrice = async (priceFunctionOverride) => {
if(priceFunctionOverride) return getMethodWithCustomName(priceFunctionOverride)();
if(getMethodWithCustomName('price')) return getMethodWithCustomName('price')();

const matches = Object.keys(NFTContract.methods).filter(key =>
!key.includes("()") && (key.toLowerCase().includes('price') || key.toLowerCase().includes('cost'))
)
Expand Down Expand Up @@ -136,18 +141,19 @@ export const getMaxTokensPerMint = async () => {
return getDefaultMaxTokensPerMint()
}

export const mint = async (nTokens) => {
export const mint = async (nTokens, mintFunctionOverride, priceFunctionOverride) => {
const wallet = await getWalletAddressOrConnect(true);
const numberOfTokens = nTokens ?? 1;
const mintPrice = await getMintPrice();
console.log("in mint function, priceFunctionOverride: ", priceFunctionOverride, " mintFunctionOverride: ", mintFunctionOverride);
const mintPrice = await getMintPrice(priceFunctionOverride);
if (mintPrice === undefined)
return { tx: undefined }

const txParams = {
from: wallet,
value: formatValue(Number(mintPrice) * numberOfTokens),
}
const estimatedGas = await getMintTx({ numberOfTokens })
const estimatedGas = await getMintTx({ numberOfTokens, mintFunctionOverride })
.estimateGas(txParams).catch((e) => {
const { code, message } = parseTxError(e);
if (code === -32000) {
Expand All @@ -163,7 +169,7 @@ export const mint = async (nTokens) => {
const maxFeePerGas = [1, 4].includes(chainID) ? formatValue(maxGasPrice) : undefined;
const maxPriorityFeePerGas = [1, 4].includes(chainID) ? 2e9 : undefined;

const tx = getMintTx({ numberOfTokens })
const tx = getMintTx({ numberOfTokens, mintFunctionOverride })
.send({
...txParams,
gasLimit: estimatedGas + 5000,
Expand Down