Skip to content

Commit

Permalink
Merge pull request #3 from RedDuck-Software/feat/game-page
Browse files Browse the repository at this point in the history
feat: fix answer
  • Loading branch information
NikiTaysRD authored Jan 9, 2025
2 parents 838fa5c + cb88ac2 commit 532b47a
Show file tree
Hide file tree
Showing 2 changed files with 45 additions and 19 deletions.
62 changes: 44 additions & 18 deletions src/hooks/contracts/write/use-make-prediction.ts
Original file line number Diff line number Diff line change
@@ -1,44 +1,70 @@
import { useWallet } from '@solana/wallet-adapter-react';
import { Transaction, SystemProgram } from '@solana/web3.js';
import { SystemProgram, Transaction } from '@solana/web3.js';
import { useMutation } from '@tanstack/react-query';

import { OwnerAddress } from '@/constants/addresses';
import { env } from '@/env';
import useSubmitTarotCards from '@/hooks/api/use-submit-cards';
import { network } from '@/lib/solana';
import { sendAndConfirmTransaction } from '@/lib/solana/utils';
import { getRandomTarotCards, showTxToast } from '@/lib/utils';
import { getRandomTarotCards } from '@/lib/utils';
import { toast } from 'react-toastify';

let toastId: string | number | null = null;

const notify = () => {
toastId = toast('Making prediction...', {
autoClose: false,
closeOnClick: false,
draggable: false,
isLoading: true,
type: 'default',
});
};

const updateToast = () => {
if (toastId !== null) {
toast.update(toastId, {
render: 'Done!',
type: 'success',
autoClose: 3000,
isLoading: false,
});
}
};

const useMakePrediction = () => {
const { publicKey, sendTransaction } = useWallet();
const { mutateAsync: submitCards, data: predictionAnswer } = useSubmitTarotCards();
const { mutateAsync: submitCards } = useSubmitTarotCards();

return useMutation({
async mutationFn(question: string) {
if (!publicKey) {
return;
}

await showTxToast('Making prediction', async () => {
const rawTx = new Transaction();
notify();

const rawTx = new Transaction();

rawTx.add(
SystemProgram.transfer({
fromPubkey: publicKey,
toPubkey: OwnerAddress[network],
lamports: Number(env.VITE_DEPOSIT_AMOUNT_SOL) * 1e9,
}),
);

rawTx.add(
SystemProgram.transfer({
fromPubkey: publicKey,
toPubkey: OwnerAddress[network],
lamports: Number(env.VITE_DEPOSIT_AMOUNT_SOL) * 1e9,
}),
);
const txHash = await sendAndConfirmTransaction(publicKey, rawTx, sendTransaction);
console.log('txHash', txHash);

const txHash = await sendAndConfirmTransaction(publicKey, rawTx, sendTransaction);
console.log('txHash', txHash);
const tarots = getRandomTarotCards(txHash + publicKey.toBase58());

const tarots = getRandomTarotCards(txHash + publicKey.toBase58());
const result = await submitCards({ tarots, hash: txHash, question });

await submitCards({ tarots, hash: txHash, question });
});
updateToast();

return predictionAnswer;
return result?.response || '';
},

onError(error) {
Expand Down
2 changes: 1 addition & 1 deletion src/pages/game-page.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,7 @@ export default function GamePage() {

useEffect(() => {
if (predictionAnswer) {
setValue('question', predictionAnswer.response + '\n' + watch('question'));
setValue('question', predictionAnswer + '\n' + watch('question'));
}
}, [isSuccess, predictionAnswer, setValue, watch]);

Expand Down

0 comments on commit 532b47a

Please sign in to comment.