Skip to content
This repository has been archived by the owner on Nov 8, 2024. It is now read-only.

Commit

Permalink
update
Browse files Browse the repository at this point in the history
  • Loading branch information
CJ13th committed May 5, 2024
1 parent 834981a commit 737006b
Show file tree
Hide file tree
Showing 5 changed files with 56 additions and 11 deletions.
2 changes: 0 additions & 2 deletions .env

This file was deleted.

2 changes: 0 additions & 2 deletions src/app/(dashboard)/leaderboard/page.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,6 @@ import { siteConfig } from '@/config/site';
import Image from 'next/image';
import { Icons } from '@/components/icons';
import { cn } from '@/lib/utils';
import api from '@/lib/polkadot';

type LeaderProps = {
points: number;
Expand All @@ -32,7 +31,6 @@ export function LeaderBoardCard({ points, winner }: LeaderProps) {
);
}
async function ChampionCard() {
const addresses = (await api.query.elections.nextEraNonReservedValidators()).toHuman();
return (
<>
<div className="flex justify-between">
Expand Down
51 changes: 50 additions & 1 deletion src/app/gameplay/page.tsx
Original file line number Diff line number Diff line change
@@ -1,7 +1,51 @@
'use client';

import { Shell } from '@/components/shell';
import { Button } from '@/components/ui/button';
import { ApiPromise, WsProvider } from '@polkadot/api';
import Image from 'next/image';
import { useState } from 'react';
import { web3Enable, web3FromAddress } from '@polkadot/extension-dapp';
import { getApi } from '@/lib/polkadot';

export default function GamePlay() {
const address = '5CS5SfC1xCGM9b16ZvqNuWU4VqzV67bmdsHDXe5arDBS3DBA'; // get this from local storage
const [guess, setGuess] = useState('');
const [isSubmitting, setIsSubmitting] = useState(false);

const handleGuessChange = (event: React.ChangeEvent<HTMLInputElement>) => {
setGuess(event.target.value);
};

const submitGuess = async () => {
if (!guess) return;

setIsSubmitting(true);

try {
const api = await getApi();
const extensions = await web3Enable('RealXChange');
const injected = await web3FromAddress(address);
const extrinsic = api.tx.system.remark(guess);
const signer = injected.signer;

const unsub = await extrinsic.signAndSend(address, { signer }, result => {
if (result.status.isInBlock) {
console.log(`Completed at block hash #${result.status.asInBlock.toString()}`);
} else if (result.status.isBroadcast) {
console.log('Broadcasting the guess...');
}
});

console.log('Transaction sent:', unsub);

setIsSubmitting(false);
} catch (error) {
console.error('Failed to submit guess:', error);
setIsSubmitting(false);
}
};

return (
<>
<Shell className="px-20">
Expand Down Expand Up @@ -63,10 +107,15 @@ export default function GamePlay() {
</p>
<input
type="text"
value={guess}
onChange={handleGuessChange}
className="mt-4 rounded-md border-[1px] border-[#57A0C5] bg-transparent p-3 placeholder:text-center"
placeholder="Enter your guess"
disabled={isSubmitting}
/>
<Button className="mt-4">Guess Now</Button>
<Button className="mt-4" onClick={submitGuess} disabled={isSubmitting}>
{isSubmitting ? 'Submitting...' : 'Guess Now'}
</Button>
</div>
<div className="flex w-1/3 justify-end">
<svg
Expand Down
10 changes: 5 additions & 5 deletions src/lib/polkadot.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import { ApiPromise, WsProvider } from '@polkadot/api';

const wsProvider = new WsProvider(process.env.RPC);

const apiPromise = await ApiPromise.create({ provider: wsProvider });

export default apiPromise;
export async function getApi() {
const wsProvider = new WsProvider(process.env.NEXT_PUBLIC_RPC);
const api = await ApiPromise.create({ provider: wsProvider });
return api;
}
2 changes: 1 addition & 1 deletion tsconfig.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"compilerOptions": {
"target": "es2017",
"target": "esnext",
"lib": ["dom", "dom.iterable", "esnext"],
"allowJs": true,
"skipLibCheck": true,
Expand Down

0 comments on commit 737006b

Please sign in to comment.