Skip to content

Commit

Permalink
Merge pull request #17 from klayrHQ/token-factory-next-create
Browse files Browse the repository at this point in the history
Token factory create token
  • Loading branch information
Theezr committed May 23, 2024
2 parents f9522f5 + 251fe96 commit 00a4bee
Show file tree
Hide file tree
Showing 32 changed files with 1,233 additions and 179 deletions.
4 changes: 3 additions & 1 deletion guides/token-factory/frontend/.env.example
Original file line number Diff line number Diff line change
@@ -1 +1,3 @@
NEXT_PUBLIC_WC_PROJECT_ID="paste WC project ID here"
NEXT_PUBLIC_WC_PROJECT_ID="paste WC project ID here"
NEXT_PUBLIC_LISK_SERVICE_URL="https://testnet-service.lisk.com"
NEXT_PUBLIC_TOKEN_FACTORY_SERVICE_URL="https://token-factory-service.klayr.dev"
81 changes: 41 additions & 40 deletions guides/token-factory/frontend/app/chain-tokens/page.tsx
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import {
Box, Button, IconButton,
Box,
Stack,
Table,
TableBody,
Expand All @@ -9,12 +9,10 @@ import {
TableRow,
Typography,
} from '@mui/material';
import { ReactElement } from 'react';
import { Settings } from '@mui/icons-material';
import { PageLayout } from '@/components/layout/pageLayout';

interface column {
value: string | ReactElement
value: string
props?: TableCellProps
}

Expand All @@ -27,51 +25,54 @@ const Page = () => {
{value: "KlayrToken"},
{value: "KLY"},
{value: "Klayr"},
{value: "5.000", props: {align: "right"}},
{value: <IconButton><Settings /></IconButton>, props: {sx: {width: "50px"}}},
{value: "5.000.000", props: {align: "right"}}
],
[
{value: "FactoryToken"},
{value: "FTK"},
{value: "Klayr"},
{value: "2.500.000", props: {align: "right"}}
]
]
const headColumns: columns = [
{ value: 'Token', props: {size: "small"}},
{ value: 'Symbol', props: {size: "small"}},
{ value: 'Creator', props: {size: "medium"}},
{ value: 'Amount', props: {size: "medium", align: "right"}},
{ value: ""},
{ value: 'Amount on chain', props: {size: "medium", align: "right"}}
]

return (
<PageLayout
title={"Owned Tokens"}
subTitle={"All tokens in your wallet"}
>
<Table>
<TableHead>
<TableRow>
{headColumns.map(({ value, props }, column) => {
return (
<TableCell key={`head-column-${column + 1}`} variant={'head'} sx={{fontSize: "small"}} {...props}>
{value}
</TableCell>
);
})}
</TableRow>
</TableHead>
<TableBody>
{
rows.map((columns, row) => (
<TableRow key={`row-${row + 1}`}>
{
columns.map(({value, props}, column) => (
<TableCell key={`row-${row + 1}-column-${column + 1}`} variant={"body"} {...props}>
{value}
</TableCell>
))
}
</TableRow>
))
}
</TableBody>
</Table>
<PageLayout title={"Tokens"} subTitle={"All tokens on the sidechain"}>
<Box className={"w-full overflow-x-auto"}>
<Table className={'w-full overflow-x-auto'}>
<TableHead>
<TableRow>
{headColumns.map(({ value, props }, column) => {
return (
<TableCell key={`head-column-${column + 1}`} variant={'head'} sx={{fontSize: "small"}} {...props}>
{value}
</TableCell>
);
})}
</TableRow>
</TableHead>
<TableBody>
{
rows.map((columns, row) => (
<TableRow key={`row-${row + 1}`}>
{
columns.map(({value, props}, column) => (
<TableCell key={`row-${row + 1}-column-${column + 1}`} variant={"body"} {...props}>
{value}
</TableCell>
))
}
</TableRow>
))
}
</TableBody>
</Table>
</Box>
</PageLayout>
)
}
Expand Down
4 changes: 1 addition & 3 deletions guides/token-factory/frontend/app/layout.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -2,10 +2,8 @@ import React from 'react';
import type { Metadata } from "next";
import { Inter } from "next/font/google";
import "./globals.css";
import { muiLightTheme } from '@/config/theme';
import { Box, CssBaseline, ThemeProvider } from '@mui/material';
import { Box, CssBaseline } from '@mui/material';
import { Header } from '@/components/layout/header';
import { WalletConnectProvider } from '@/providers/walletConnectProvider';
import { ProviderStructure } from '@/providers/providerStructure';

const inter = Inter({ subsets: ["latin"] });
Expand Down
83 changes: 50 additions & 33 deletions guides/token-factory/frontend/app/owned-tokens/page.tsx
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import {
Box,
Box, Button, IconButton,
Stack,
Table,
TableBody,
Expand All @@ -9,10 +9,13 @@ import {
TableRow,
Typography,
} from '@mui/material';
import { ReactElement } from 'react';
import { Settings } from '@mui/icons-material';
import { PageLayout } from '@/components/layout/pageLayout';
import { TokenActionsModal } from '@/components/tokens/tokenActionsModal';

interface column {
value: string
value: string | ReactElement
props?: TableCellProps
}

Expand All @@ -25,46 +28,60 @@ const Page = () => {
{value: "KlayrToken"},
{value: "KLY"},
{value: "Klayr"},
{value: "5.000.000", props: {align: "right"}}
{value: "5.000", props: {align: "right"}},
{value: <TokenActionsModal tokenName={"KlayrToken"} tokenID={"1234567800000001"}/>, props: {sx: {width: "50px"}}},
],
[
{value: "FactoryToken"},
{value: "FTK"},
{value: "Klayr"},
{value: "5.000", props: {align: "right"}},
{value: <TokenActionsModal tokenName={"FactoryToken"} tokenID={"1234567800000001"}/>, props: {sx: {width: "50px"}}},
]
]
const headColumns: columns = [
{ value: 'Token', props: {size: "small"}},
{ value: 'Symbol', props: {size: "small"}},
{ value: 'Creator', props: {size: "medium"}},
{ value: 'Amount on chain', props: {size: "medium", align: "right"}}
{ value: 'Amount', props: {size: "medium", align: "right"}},
{ value: ""},
]

return (
<PageLayout title={"Tokens"} subTitle={"All tokens on the sidechain"}>
<Table>
<TableHead>
<TableRow>
{headColumns.map(({ value, props }, column) => {
return (
<TableCell key={`head-column-${column + 1}`} variant={'head'} sx={{fontSize: "small"}} {...props}>
{value}
</TableCell>
);
})}
</TableRow>
</TableHead>
<TableBody>
{
rows.map((columns, row) => (
<TableRow key={`row-${row + 1}`}>
{
columns.map(({value, props}, column) => (
<TableCell key={`row-${row + 1}-column-${column + 1}`} variant={"body"} {...props}>
{value}
</TableCell>
))
}
</TableRow>
))
}
</TableBody>
</Table>
<PageLayout
title={"Owned Tokens"}
subTitle={"All tokens in your wallet"}
>
<Box className={"w-full overflow-x-auto"}>
<Table className={'w-full overflow-x-auto'}>
<TableHead>
<TableRow>
{headColumns.map(({ value, props }, column) => {
return (
<TableCell key={`head-column-${column + 1}`} variant={'head'} sx={{ fontSize: 'small' }} {...props}>
{value}
</TableCell>
);
})}
</TableRow>
</TableHead>
<TableBody>
{
rows.map((columns, row) => (
<TableRow key={`row-${row + 1}`}>
{
columns.map(({ value, props }, column) => (
<TableCell key={`row-${row + 1}-column-${column + 1}`} variant={'body'} {...props}>
{value}
</TableCell>
))
}
</TableRow>
))
}
</TableBody>
</Table>
</Box>
</PageLayout>
)
}
Expand Down
138 changes: 135 additions & 3 deletions guides/token-factory/frontend/components/clientPages/create-token.tsx
Original file line number Diff line number Diff line change
@@ -1,13 +1,145 @@
import { Box, Stack, Typography } from '@mui/material';
'use client';
import { Button, Input, InputLabel, Stack, Typography } from '@mui/material';
import { PageLayout } from '@/components/layout/pageLayout';
import { SubmitErrorHandler, SubmitHandler, useForm } from 'react-hook-form';
import { useWalletConnect } from '@/providers/walletConnectProvider';
import { createTransactionObject, returnIfString } from '@/utils/functions';
import { useSchemas } from '@/providers/schemaProvider';
import { useEffect, useState } from 'react';
import { ITransactionObject, TransactionStatus } from '@/types/transactions';
import { TransactionModal } from '../walletConnect/transactionModal';
import { api } from '@/utils/api';

interface CreateTokenProps {
name: string
symbol: string
totalSupply: number
}

export const CreateToken = () => {
const { register, handleSubmit, formState: { errors } } = useForm();
const { account, signTransaction, rpcResult } = useWalletConnect();
const { getSchema } = useSchemas();
const [openTransactionModal, setOpenTransactionModal] = useState<boolean>(false);
const [transactionModalType, setTransactionModalType] = useState<"approve" | "status">("status");
const [transactionStatus, setTransactionStatus] = useState<TransactionStatus>(TransactionStatus.PENDING);

const onSubmit: SubmitHandler<CreateTokenProps> = (data) => {
if(account) {
const { chainID, publicKey } = account;
const schema = getSchema(false, "tokenFactory:createToken")

createTransactionObject("createToken", account, data)
.then(({ transactionObject: rawTx, }) => {
console.log('transaction: ', rawTx);
/*setTransactionObject(rawTx);
setFeeTokenID(_feeTokenID);*/

signTransaction(chainID, publicKey, schema, rawTx);
setTransactionModalType("status")
setOpenTransactionModal(true);
})
.catch(error => {
console.log(error)
});
}
}

useEffect(() => {
if (rpcResult && rpcResult.valid) {
setTransactionModalType("approve");
if (!openTransactionModal) setOpenTransactionModal(true);
}
}, [rpcResult]);

//submit signed transaction
const onConfirmApproval = () => {
if (rpcResult?.result) {
console.log(rpcResult)
api.post("transactions", { transaction: rpcResult.result })
.then(r => {
if(r.error) {
setTransactionStatus(TransactionStatus.FAILURE);
setTransactionModalType("status");
}
else {
setTransactionStatus(TransactionStatus.SUCCESS);
setTransactionModalType('status');
}
})
.catch(error => {
console.log(error)
})
}
};

const onError: SubmitErrorHandler<CreateTokenProps> = (errors) => console.log(errors);

const getErrorText = (errorType: string | undefined, fieldType?: string | undefined) => {
let errorText = "Unknown input error";

if (errorType === "required") errorText = "This field is required";

if (errorType === "pattern") {
if(fieldType === "number") {
errorText = "This field only accepts numbers";
} else {
errorText = "Invalid input";
}
}

return <Typography className={"text-xs text-[#FF422D] mt-1"}>{errorText}</Typography>;
}

return (
<PageLayout title={"Create Token"} subTitle={"Create your own token"}>
<form>

<form className={"w-[350px] md:w-[500px]"}>
<Stack className={"gap-4"}>
<InputLabel className={"w-full"}>
<Typography>Token Name:</Typography>
<Input
className={"w-full"}
type={'text'}
error={!!errors.name}
placeholder={"Example Token"}
{...register("name", { required: true })}
/>
{errors.name && getErrorText(returnIfString(errors.name.type))}
</InputLabel>
<InputLabel className={'w-full'}>
<Typography>Token Symbol:</Typography>
<Input
className={'w-full'}
type={'text'}
error={!!errors.symbol}
placeholder={"TKN"}
{...register('symbol', { required: true })}
/>
{errors.symbol && getErrorText(returnIfString(errors.symbol.type))}
</InputLabel>
<InputLabel className={"w-full"}>
<Typography>Total Supply:</Typography>
<Input
className={"w-full"}
type={'text'}
error={!!errors.totalSupply}
placeholder={"10000"}
{...register("totalSupply", { required: true, pattern: /^[0-9]+$/i })}
/>
{errors.totalSupply && getErrorText(returnIfString(errors.totalSupply.type), "number")}
</InputLabel>
{/* @ts-ignore */}
<Button className={"mt-2"} variant={"input"} onClick={handleSubmit(onSubmit, onError)}><Typography>Submit</Typography></Button>
</Stack>
</form>
<TransactionModal
modalType={transactionModalType}
type={"createToken"}
open={openTransactionModal}
onClose={() => setOpenTransactionModal(false)}
onApprove={onConfirmApproval}
status={transactionStatus}
/>
</PageLayout>
)
}
Loading

0 comments on commit 00a4bee

Please sign in to comment.