Skip to content
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
1 change: 0 additions & 1 deletion src/App.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,6 @@ import Home from "./Home";
import { DEFAULT_TIMEOUT } from "./connection";
import { clusterApiUrl } from "@solana/web3.js";
import { WalletAdapterNetwork } from "@solana/wallet-adapter-base";
import { BrowserRouter, Routes, Route, useParams } from "react-router-dom";
import {
getPhantomWallet,
getSlopeWallet,
Expand Down
42 changes: 33 additions & 9 deletions src/Home.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -17,8 +17,7 @@ import { useWallet } from "@solana/wallet-adapter-react";
import { WalletDialogButton } from "@solana/wallet-adapter-material-ui";
import {
awaitTransactionSignatureConfirmation,
CANDY_MACHINE_PROGRAM,
CandyMachineAccount,
CANDY_MACHINE_PROGRAM_ID,
createAccountsForMint,
getCandyMachineState,
getCollectionPDA,
Expand All @@ -32,6 +31,10 @@ import { GatewayProvider } from "@civic/solana-gateway-react";
import { WalletAdapterNetwork } from "@solana/wallet-adapter-base";
import { useMetaplex } from "./metaplex";
import { useParams } from "react-router-dom";
import {
CandyMachineAccount,
collectionMintIDsAndTokenAddressPair,
} from "./types";

const ConnectButton = styled(WalletDialogButton)`
width: 100%;
Expand Down Expand Up @@ -67,11 +70,14 @@ const Home = (props: HomeProps) => {
const [endDate, setEndDate] = useState<Date>();
const [itemsRemaining, setItemsRemaining] = useState<number>();
const [isWhitelistUser, setIsWhitelistUser] = useState(false);
const [whitelistToken, setWhitelistToken] =
useState<collectionMintIDsAndTokenAddressPair>();
const [isPresale, setIsPresale] = useState(false);
const [isValidBalance, setIsValidBalance] = useState(false);
const [needTxnSplit, setNeedTxnSplit] = useState(true);
const [setupTxn, setSetupTxn] = useState<SetupState>();
const { findCollectionMintIdsByOwner } = useMetaplex();
const [mintedNft, setMintedNft] = useState<anchor.web3.PublicKey>();
const { findCollectionMintIDsAndTokenAddress, utilizeNft } = useMetaplex();

const rpcUrl = props.rpcHost;
const wallet = useWallet();
Expand Down Expand Up @@ -155,12 +161,19 @@ const Home = (props: HomeProps) => {
}

try {
const collectionMintId = await findCollectionMintIdsByOwner();
isWLUser = collectionMintId.includes(
cndy.state.whitelistMintSettings.mint.toBase58()
);
const mintAndAddressPairs =
await findCollectionMintIDsAndTokenAddress(
cndy.state.whitelistMintSettings.mint.toBase58()
);

if (mintAndAddressPairs.length > 0) {
// has at least one whitelisted NFT
isWLUser = true;
// just use the first whitelist token
// todo(gtihtina): this has to be mintAndAddressPairs[1]??
setWhitelistToken(mintAndAddressPairs[0]);
}
// only whitelist the user if the balance > 0
setIsWhitelistUser(isWLUser);

if (cndy.state.isWhitelistOnly) {
active = isWLUser && (presale || active);
Expand Down Expand Up @@ -357,6 +370,7 @@ const Home = (props: HomeProps) => {
const mintResult = await mintOneToken(
candyMachine,
wallet.publicKey,
whitelistToken,
beforeTransactions,
afterTransactions,
setupMint ?? setupTxn
Expand All @@ -377,6 +391,7 @@ const Home = (props: HomeProps) => {
mintResult.metadataKey,
"processed"
);
setMintedNft(mintResult.mintAddress);
console.log("Metadata status: ", !!metadataStatus);
}

Expand Down Expand Up @@ -593,7 +608,7 @@ const Home = (props: HomeProps) => {
wallet={{
publicKey:
wallet.publicKey ||
new PublicKey(CANDY_MACHINE_PROGRAM),
new PublicKey(CANDY_MACHINE_PROGRAM_ID),
//@ts-ignore
signTransaction: wallet.signTransaction,
}}
Expand Down Expand Up @@ -628,6 +643,15 @@ const Home = (props: HomeProps) => {
/>
)}
</MintContainer>

<button
disabled={mintedNft == undefined}
onClick={() => {
mintedNft ? utilizeNft(mintedNft) : null;
}}
>
Utilize
</button>
</>
)}
</Paper>
Expand Down
3 changes: 2 additions & 1 deletion src/MintButton.tsx
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
import styled from "styled-components";
import Button from "@material-ui/core/Button";
import { CandyMachineAccount } from "./candy-machine";
import { CircularProgress } from "@material-ui/core";
import { GatewayStatus, useGateway } from "@civic/solana-gateway-react";
import { useEffect, useState, useRef, useCallback } from "react";
Expand All @@ -12,6 +11,7 @@ import {
removeAccountChangeListener,
} from "@identity.com/solana-gateway-ts";
import { CIVIC_GATEKEEPER_NETWORK } from "./utils";
import { CandyMachineAccount } from "./types";

export const CTAButton = styled(Button)`
width: 100%;
Expand Down Expand Up @@ -160,6 +160,7 @@ export const MintButton = ({
const handleClickBuy = useCallback(async () => {
console.log("Link to OpenSea");
}, []);

return !isActive ? (
<CTAButton
disabled={false}
Expand Down
99 changes: 38 additions & 61 deletions src/candy-machine.ts
Original file line number Diff line number Diff line change
@@ -1,14 +1,12 @@
/* eslint-disable */
import * as anchor from "@project-serum/anchor";

import { MintLayout, TOKEN_PROGRAM_ID, Token } from "@solana/spl-token";
import {
SystemProgram,
Transaction,
SYSVAR_SLOT_HASHES_PUBKEY,
} from "@solana/web3.js";
import { sendTransactions, SequenceType } from "./connection";

import {
CIVIC,
getAtaForMint,
Expand All @@ -17,55 +15,19 @@ import {
SPL_ASSOCIATED_TOKEN_ACCOUNT_PROGRAM_ID,
} from "./utils";

export const CANDY_MACHINE_PROGRAM = new anchor.web3.PublicKey(
"cndy3Z4yapfJBmL3ShUp5exZKqR3z33thTzeNMm2gRZ"
import {
CandyMachineAccount,
collectionMintIDsAndTokenAddressPair,
} from "./types";

export const CANDY_MACHINE_PROGRAM_ID = new anchor.web3.PublicKey(
"H8ovX9jHAbhZ2fap3c1P7JNwNooUjUCNL6TwBmJbY1Ep"
);

const TOKEN_METADATA_PROGRAM_ID = new anchor.web3.PublicKey(
"metaqbxxUerdq28cj1RbAWkYQm3ybzjb6a8bt518x1s"
);

interface CandyMachineState {
authority: anchor.web3.PublicKey;
itemsAvailable: number;
itemsRedeemed: number;
itemsRemaining: number;
treasury: anchor.web3.PublicKey;
tokenMint: null | anchor.web3.PublicKey;
isSoldOut: boolean;
isActive: boolean;
isPresale: boolean;
isWhitelistOnly: boolean;
goLiveDate: null | anchor.BN;
price: anchor.BN;
gatekeeper: null | {
expireOnUse: boolean;
gatekeeperNetwork: anchor.web3.PublicKey;
};
endSettings: null | {
number: anchor.BN;
endSettingType: any;
};
whitelistMintSettings: null | {
mode: any;
mint: anchor.web3.PublicKey;
presale: boolean;
discountPrice: null | anchor.BN;
};
hiddenSettings: null | {
name: string;
uri: string;
hash: Uint8Array;
};
retainAuthority: boolean;
}

export interface CandyMachineAccount {
id: anchor.web3.PublicKey;
program: anchor.Program;
state: CandyMachineState;
}

export const awaitTransactionSignatureConfirmation = async (
txid: anchor.web3.TransactionSignature,
timeout: number,
Expand Down Expand Up @@ -173,8 +135,15 @@ export const getCandyMachineState = async (
});

const getProgramState = async (): Promise<[anchor.Program, any]> => {
const idl = await anchor.Program.fetchIdl(CANDY_MACHINE_PROGRAM, provider);
const program = new anchor.Program(idl!, CANDY_MACHINE_PROGRAM, provider);
const idl = await anchor.Program.fetchIdl(
CANDY_MACHINE_PROGRAM_ID,
provider
);
const program = new anchor.Program(
idl!,
CANDY_MACHINE_PROGRAM_ID,
provider
);
const state: any = await program.account.candyMachine.fetch(candyMachineId);
return [program, state];
};
Expand Down Expand Up @@ -206,14 +175,15 @@ export const getCandyMachineState = async (
isSoldOut: itemsRemaining === 0,
isActive: false,
isPresale: false,
isWhitelistOnly: false,
isWhitelistOnly: state.data.whitelistMintSettings !== null,
goLiveDate: state.data.goLiveDate,
treasury: state.wallet,
tokenMint: state.tokenMint,
gatekeeper: state.data.gatekeeper,
endSettings: state.data.endSettings,
whitelistMintSettings: state.data.whitelistMintSettings,
hiddenSettings: state.data.hiddenSettings,
uses: state.data.uses,
price: state.data.price,
retainAuthority: state.data.retainAuthority,
},
Expand Down Expand Up @@ -256,7 +226,7 @@ export const getCandyMachineCreator = async (
): Promise<[anchor.web3.PublicKey, number]> => {
return await anchor.web3.PublicKey.findProgramAddress(
[Buffer.from("candy_machine"), candyMachine.toBuffer()],
CANDY_MACHINE_PROGRAM
CANDY_MACHINE_PROGRAM_ID
);
};

Expand All @@ -265,7 +235,7 @@ export const getCollectionPDA = async (
): Promise<[anchor.web3.PublicKey, number]> => {
return await anchor.web3.PublicKey.findProgramAddress(
[Buffer.from("collection"), candyMachineAddress.toBuffer()],
CANDY_MACHINE_PROGRAM
CANDY_MACHINE_PROGRAM_ID
);
};

Expand Down Expand Up @@ -365,12 +335,14 @@ export const createAccountsForMint = async (

type MintResult = {
mintTxId: string;
mintAddress: anchor.web3.PublicKey;
metadataKey: anchor.web3.PublicKey;
};

export const mintOneToken = async (
candyMachine: CandyMachineAccount,
payer: anchor.web3.PublicKey,
whiteListToken: collectionMintIDsAndTokenAddressPair | undefined,
beforeTransactions: Transaction[] = [],
afterTransactions: Transaction[] = [],
setupState?: SetupState
Expand Down Expand Up @@ -457,21 +429,26 @@ export const mintOneToken = async (
});
}
}
if (candyMachine.state.whitelistMintSettings) {
const mint = new anchor.web3.PublicKey(
candyMachine.state.whitelistMintSettings.mint
if (
candyMachine.state.whitelistMintSettings &&
whiteListToken &&
candyMachine.state.whitelistMintSettings.mint.toBase58() ==
whiteListToken.collectionMint
) {
const ownedWhiteListNftMint = new anchor.web3.PublicKey(
whiteListToken.nftMint
);
const whitelistAta = (await getAtaForMint(ownedWhiteListNftMint, payer))[0];

const whitelistToken = (await getAtaForMint(mint, payer))[0];
remainingAccounts.push({
pubkey: whitelistToken,
pubkey: whitelistAta,
isWritable: true,
isSigner: false,
});

if (candyMachine.state.whitelistMintSettings.mode.burnEveryTime) {
remainingAccounts.push({
pubkey: mint,
pubkey: ownedWhiteListNftMint,
isWritable: true,
isSigner: false,
});
Expand Down Expand Up @@ -540,19 +517,17 @@ export const mintOneToken = async (
(await candyMachine.program.account.collectionPda.fetch(
collectionPDA
)) as CollectionData;
console.log(collectionData);
const collectionMint = collectionData.mint;
const collectionAuthorityRecord = await getCollectionAuthorityRecordPDA(
collectionMint,
collectionPDA
);
console.log(collectionMint);
if (collectionMint) {
const collectionMetadata = await getMetadata(collectionMint);
const collectionMasterEdition = await getMasterEdition(collectionMint);
console.log("Collection PDA: ", collectionPDA.toBase58());
console.log("Authority: ", candyMachine.state.authority.toBase58());
instructions.push(
let collectionInstruction =
await candyMachine.program.instruction.setCollectionDuringMint({
accounts: {
candyMachine: candyMachineAddress,
Expand All @@ -567,8 +542,9 @@ export const mintOneToken = async (
authority: candyMachine.state.authority,
collectionAuthorityRecord,
},
})
);
});
collectionInstruction.keys[7].isWritable = true;
instructions.push(collectionInstruction);
}
} catch (error) {
console.error(error);
Expand Down Expand Up @@ -597,6 +573,7 @@ export const mintOneToken = async (
const mintTxn = txns[0];
return {
mintTxId: mintTxn,
mintAddress: mint.publicKey,
metadataKey: metadataAddress,
};
} catch (e) {
Expand Down
Loading