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

Add support for custom SPL mints #157

Open
wants to merge 1 commit 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
63 changes: 50 additions & 13 deletions src/Home.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,8 @@ import Alert from "@material-ui/lab/Alert";

import * as anchor from "@project-serum/anchor";

import { LAMPORTS_PER_SOL } from "@solana/web3.js";
import {LAMPORTS_PER_SOL, PublicKey} from "@solana/web3.js";
import {TOKEN_PROGRAM_ID, ASSOCIATED_TOKEN_PROGRAM_ID, Token } from "@solana/spl-token";

import { useWallet } from "@solana/wallet-adapter-react";
import { WalletDialogButton } from "@solana/wallet-adapter-material-ui";
Expand Down Expand Up @@ -41,6 +42,8 @@ const Home = (props: HomeProps) => {
const [isActive, setIsActive] = useState(false); // true when countdown completes
const [isSoldOut, setIsSoldOut] = useState(false); // true when items remaining is zero
const [isMinting, setIsMinting] = useState(false); // true when user got to press MINT
const [tokenMint, setTokenMint] = useState<PublicKey | undefined>(undefined); // Custom spl token for mint price. Typically undefined
const [associatedTokenAccountAddress, setAssociatedTokenAccountAddress] = useState<PublicKey | undefined>(undefined); // Associated token for custom spl mint

const [alertState, setAlertState] = useState<AlertState>({
open: false,
Expand All @@ -53,6 +56,31 @@ const Home = (props: HomeProps) => {
const wallet = useWallet();
const [candyMachine, setCandyMachine] = useState<CandyMachine>();

const updateBalance = async () => {
if (wallet?.publicKey) {
if (tokenMint && associatedTokenAccountAddress) {
const token = new Token(
props.connection,
tokenMint,
TOKEN_PROGRAM_ID,
// @ts-ignore
wallet
)
const mintInfo = await token.getMintInfo();
try {
const associatedTokenAccountInfo = await token.getAccountInfo(associatedTokenAccountAddress);
setBalance(associatedTokenAccountInfo.amount.toNumber() / 10 ** mintInfo.decimals);
} catch (e) {
// if we cant fatch associated address, assume balance is 0
setBalance(0);
}
return;
}
const balance = await props.connection.getBalance(wallet.publicKey);
setBalance(balance / LAMPORTS_PER_SOL);
}
}

const onMint = async () => {
try {
setIsMinting(true);
Expand All @@ -61,7 +89,8 @@ const Home = (props: HomeProps) => {
candyMachine,
props.config,
wallet.publicKey,
props.treasury
props.treasury,
associatedTokenAccountAddress
);

const status = await awaitTransactionSignatureConfirmation(
Expand Down Expand Up @@ -111,22 +140,16 @@ const Home = (props: HomeProps) => {
severity: "error",
});
} finally {
if (wallet?.publicKey) {
const balance = await props.connection.getBalance(wallet?.publicKey);
setBalance(balance / LAMPORTS_PER_SOL);
}
await updateBalance();
setIsMinting(false);
}
};

useEffect(() => {
(async () => {
if (wallet?.publicKey) {
const balance = await props.connection.getBalance(wallet.publicKey);
setBalance(balance / LAMPORTS_PER_SOL);
}
await updateBalance();
})();
}, [wallet, props.connection]);
}, [wallet, props.connection, tokenMint]);

useEffect(() => {
(async () => {
Expand All @@ -145,15 +168,29 @@ const Home = (props: HomeProps) => {
signTransaction: wallet.signTransaction,
} as anchor.Wallet;

const { candyMachine, goLiveDate, itemsRemaining } =
const { candyMachine, goLiveDate, itemsRemaining, tokenMint } =
await getCandyMachineState(
anchorWallet,
props.candyMachineId,
props.connection
);



setIsSoldOut(itemsRemaining === 0);
setStartDate(goLiveDate);

if (tokenMint) {
const associatedTokenAccountAddress = await Token.getAssociatedTokenAddress(
ASSOCIATED_TOKEN_PROGRAM_ID,
TOKEN_PROGRAM_ID,
tokenMint,
wallet.publicKey
);
setTokenMint(tokenMint);
setAssociatedTokenAccountAddress(associatedTokenAccountAddress);
}

setCandyMachine(candyMachine);
})();
}, [wallet, props.candyMachineId, props.connection]);
Expand All @@ -165,7 +202,7 @@ const Home = (props: HomeProps) => {
)}

{wallet.connected && (
<p>Balance: {(balance || 0).toLocaleString()} SOL</p>
<p>Balance: {(balance || 0).toLocaleString()} {tokenMint ? "" : "SOL"}</p>
)}

<MintContainer>
Expand Down
14 changes: 13 additions & 1 deletion src/candy-machine.ts
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,8 @@ interface CandyMachineState {
itemsAvailable: number;
itemsRedeemed: number;
itemsRemaining: number;
goLiveDate: Date,
goLiveDate: Date;
tokenMint?: anchor.web3.PublicKey;
}

export const awaitTransactionSignatureConfirmation = async (
Expand Down Expand Up @@ -176,6 +177,7 @@ export const getCandyMachineState = async (
const itemsAvailable = state.data.itemsAvailable.toNumber();
const itemsRedeemed = state.itemsRedeemed.toNumber();
const itemsRemaining = itemsAvailable - itemsRedeemed;
const tokenMint = state.tokenMint;

let goLiveDate = state.data.goLiveDate.toNumber();
goLiveDate = new Date(goLiveDate * 1000);
Expand All @@ -186,6 +188,7 @@ export const getCandyMachineState = async (
itemsRedeemed,
itemsRemaining,
goLiveDate,
tokenMint
};
}

Expand Down Expand Up @@ -237,6 +240,7 @@ export const mintOneToken = async (
config: anchor.web3.PublicKey, // feels like this should be part of candyMachine?
payer: anchor.web3.PublicKey,
treasury: anchor.web3.PublicKey,
associatedTokenAccountAddress?: anchor.web3.PublicKey,
): Promise<string> => {
const mint = anchor.web3.Keypair.generate();
const token = await getTokenWallet(payer, mint.publicKey);
Expand All @@ -248,6 +252,13 @@ export const mintOneToken = async (
MintLayout.span
);

const remainingAccounts = [];
if (associatedTokenAccountAddress) {
console.log("here", associatedTokenAccountAddress);
remainingAccounts.push({ pubkey: associatedTokenAccountAddress, isWritable: true, isSigner: false });
remainingAccounts.push({ pubkey: payer, isWritable: false, isSigner: true });
}

return await program.rpc.mintNft({
accounts: {
config,
Expand All @@ -266,6 +277,7 @@ export const mintOneToken = async (
clock: anchor.web3.SYSVAR_CLOCK_PUBKEY,
},
signers: [mint],
remainingAccounts,
instructions: [
anchor.web3.SystemProgram.createAccount({
fromPubkey: payer,
Expand Down