Skip to content

Commit

Permalink
Merge pull request #11 from rsksmart/addingFungible
Browse files Browse the repository at this point in the history
feat : adding full fungible creation, and fixing error on not showing correct errors on responses error
  • Loading branch information
SebasGuaquetaRSK authored Aug 1, 2024
2 parents 9921d8b + b0ca0c3 commit aaa1e47
Show file tree
Hide file tree
Showing 11 changed files with 81 additions and 62 deletions.
22 changes: 3 additions & 19 deletions app/api/etch-rune/route.ts
Original file line number Diff line number Diff line change
Expand Up @@ -14,16 +14,7 @@ import {
export async function POST(request: NextRequest) {
const data = await request.json()
try {
const {
action,
name,
symbol,
premine,
amount,
cap,
divisibility,
tapLeafScript,
} = data
const { action, name, symbol, premine, amount, cap, tapLeafScript } = data
const deserializedTapLeafScript = tapLeafScript?.map((item: any) => ({
controlBlock: Buffer.from(item.controlBlock, 'base64'),
leafVersion: item.leafVersion,
Expand Down Expand Up @@ -61,18 +52,14 @@ export async function POST(request: NextRequest) {
amount,
cap,
symbol,
divisibility,
premine,
})
return NextResponse.json({ revealTxHash })
default:
return NextResponse.json({ error: 'Invalid action' }, { status: 400 })
}
} catch (error) {
return NextResponse.json(
{ error: 'Internal Server Error' },
{ status: 500 }
)
return NextResponse.json({ error: error }, { status: 500 })
}
}
export async function GET(request: NextRequest) {
Expand Down Expand Up @@ -114,9 +101,6 @@ export async function GET(request: NextRequest) {
return NextResponse.json({ error: 'Invalid action' }, { status: 400 })
}
} catch (error) {
return NextResponse.json(
{ error: 'Internal Server Error' },
{ status: 500 }
)
return NextResponse.json({ error: error }, { status: 500 })
}
}
12 changes: 4 additions & 8 deletions app/api/transfer-rune/route.ts
Original file line number Diff line number Diff line change
Expand Up @@ -28,10 +28,8 @@ export async function POST(request: NextRequest) {
return NextResponse.json({ error: 'Invalid action' }, { status: 400 })
}
} catch (error) {
return NextResponse.json(
{ error: 'Internal Server Error' },
{ status: 500 }
)
console.log('Error on request is :', error)
return NextResponse.json({ error: error }, { status: 500 })
}
}
export async function GET(request: NextRequest) {
Expand Down Expand Up @@ -63,9 +61,7 @@ export async function GET(request: NextRequest) {
return NextResponse.json({ error: 'Invalid action' }, { status: 400 })
}
} catch (error) {
return NextResponse.json(
{ error: 'Internal Server Error' },
{ status: 500 }
)
console.log('Error on request is :', error)
return NextResponse.json({ error: error }, { status: 500 })
}
}
8 changes: 6 additions & 2 deletions app/utils/apiRequests.tsx
Original file line number Diff line number Diff line change
@@ -1,10 +1,13 @@
import { toast } from 'react-toastify'

export const getRequest = async (url: string) => {
try {
const response = await fetch(url)
const json = await response.json()
return json
} catch (error) {
return { error: 'Internal Server Error' }
toast.error(`Error on request: ${error}`)
return { error: error }
}
}
export const postRequest = async (url: string, data: any) => {
Expand All @@ -19,6 +22,7 @@ export const postRequest = async (url: string, data: any) => {
const json = await response.json()
return json
} catch (error) {
return { error: 'Internal Server Error' }
toast.error(`Error on request: ${error}`)
return { error: error }
}
}
27 changes: 26 additions & 1 deletion app/utils/hooks/useRuneERC1155.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -11,10 +11,10 @@ export interface UseRuneERC1155Props {
name: string
symbol: string
receiver: string
isNft?: boolean
premine?: number
amount?: number
cap?: number
divisibility?: number
}
export interface FreezeTxData {
runeName: string
Expand Down Expand Up @@ -71,6 +71,30 @@ export const useRuneERC1155 = () => {
}
}

const mintFungible = async (runeData: UseRuneERC1155Props) => {
try {
const { uri, name, symbol, receiver, premine, amount, cap } = runeData
console.log('runeData in mint fungible is', runeData)

if (!cap || !premine || !amount) {
toast.error('Some values are undefined or null')
return
}
const txResponse = await contract.mintFungible(
`${uri} TODO: retrieve and save metadata`,
name,
symbol,
ethers.parseUnits(cap.toString(), 18),
ethers.parseUnits(premine.toString(), 18),
ethers.parseUnits(amount.toString(), 18),
receiver
)
return txResponse
} catch (error: any) {
toast.error('Error on minting token')
}
}

const freezeNonFungible = async (runeName: string) => {
try {
setTxFreezeStatus('processing')
Expand Down Expand Up @@ -125,6 +149,7 @@ export const useRuneERC1155 = () => {

return {
mintNonFungible,
mintFungible,
getUserRunes,
freezeNonFungible,
runes,
Expand Down
5 changes: 1 addition & 4 deletions app/utils/schemas/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -12,12 +12,9 @@ export const formSchema = z.object({
symbol: z
.string()
.length(1, { message: 'Symbol must be a single character.' }),
premine: z.number(),
premine: z.number().min(0, { message: 'Premine is required.' }),
amount: z.number().min(0, { message: 'Amount is required.' }),
cap: z.number().min(0, { message: 'Cap is required.' }),
divisibility: z
.number()
.max(38, { message: 'Divisibility cannot be higher than 38.' }),
receiver: z
.string()
.length(42, { message: 'Address cannot exceed 42 characters.' }),
Expand Down
3 changes: 2 additions & 1 deletion app/utils/types/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,6 @@ export interface FormData {
premine?: number
amount?: number
cap?: number
divisibility?: number
address?: string
}
export interface FormDataRuneToBTC {
Expand All @@ -16,4 +15,6 @@ export interface FormDataRuneToBTC {
export interface EtchTabProps {
setRuneProps: Function
setCommitTxHash: Function
isNft: boolean
setIsNft: Function
}
34 changes: 14 additions & 20 deletions components/tabs/EtchTab.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,8 @@ import { useAuth } from '@/app/context/AuthContext'
export default function EtchTab({
setRuneProps,
setCommitTxHash,
isNft,
setIsNft,
}: EtchTabProps): JSX.Element {
const [loading, setLoading] = useState(false)
const form = useForm<UseRuneERC1155Props>({
Expand All @@ -39,18 +41,19 @@ export default function EtchTab({
premine: 1,
amount: 1,
cap: 0,
divisibility: 0,
receiver: '',
},
})
const { address: walletAddress } = useAuth()

const [isNft, setIsNft] = useState<boolean>(true)

const onSubmit = (data: UseRuneERC1155Props) => {
setRuneProps(data)
localStorage.setItem('runeData', JSON.stringify({ runeProps: data }))
handleEtch(data)
const runesPropsData = { ...data, isNft }
setRuneProps(runesPropsData)
localStorage.setItem(
'runeData',
JSON.stringify({ runeProps: runesPropsData })
)
handleEtch(runesPropsData)
}

useEffect(() => {
Expand Down Expand Up @@ -122,13 +125,12 @@ export default function EtchTab({
<div className="flex gap-2">
<label className="flex relative items-center cursor-pointer">
<input
disabled
checked={isNft}
type="checkbox"
className="sr-only"
onChange={(e) => setIsNft(Boolean(e.target.checked))}
/>
<span className="w-11 h-6 bg-card rounded-full border border-input toggle-bg cursor-not-allowed"></span>
<span className="w-11 h-6 bg-card rounded-full border border-input toggle-bg"></span>
</label>
</div>
</FormControl>
Expand All @@ -149,7 +151,7 @@ export default function EtchTab({
<InputField
form={form}
name="premine"
tooltip="Premined runes to the rune etcher."
tooltip="Amount of runes minted to the rune creator."
placeholder="Enter premine amount"
type="number"
disabled={isNft}
Expand All @@ -159,34 +161,26 @@ export default function EtchTab({
<InputField
form={form}
name="amount"
tooltip="The amount of runes each mint produces."
tooltip="The amount of runes for each mint transaction."
placeholder="Enter token amount"
type="number"
disabled={isNft}
/>
<InputField
form={form}
name="cap"
tooltip="Total amount of mints."
tooltip="Total amount of mints, without counting premine."
placeholder="Enter token cap"
type="number"
disabled={isNft}
/>
<InputField
form={form}
name="divisibility"
tooltip="The number of subunits in a super unit of runes."
placeholder="Enter token divisibility"
type="number"
disabled={isNft}
/>
</div>
<InputField
form={form}
name="receiver"
tooltip="Enter the Rootstock address where runes will be minted into ERC20s"
placeholder="RSK address"
disabled={isNft}
disabled={true}
/>
<CardFooter className="px-0 relative z-0 justify-end">
<Button
Expand Down
22 changes: 16 additions & 6 deletions components/tabs/EtchingProgress.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -59,23 +59,32 @@ export default function EtchingProgress({

const [updateStatusInterval, setUpdateStatusInterval] =
useState<NodeJS.Timeout | null>(null)
const { mintNonFungible, isTxConfirmed } = useRuneERC1155()
const { mintNonFungible, isTxConfirmed, mintFungible } = useRuneERC1155()

const { uri, name, symbol, receiver } = runeProps
const { uri, name, symbol, receiver, isNft, amount, premine, cap } = runeProps

const executeMinting = useCallback(async () => {
try {
if (!name || !symbol || !receiver) return

const newRuneProps: UseRuneERC1155Props = {
uri,
uri: uri ?? 'TempURI',
receiver,
name,
symbol,
premine,
amount,
cap,
}
if (isNft) {
const { hash } = await mintNonFungible(newRuneProps)
localStorage.setItem('mintTxHash', hash)
setMintTxHash(hash)
} else {
const { hash } = await mintFungible(newRuneProps)
localStorage.setItem('mintTxHash', hash)
setMintTxHash(hash)
}
const { hash } = await mintNonFungible(newRuneProps)
localStorage.setItem('mintTxHash', hash)
setMintTxHash(hash)
} catch (error) {
toast.error('Error minting the rune')
}
Expand Down Expand Up @@ -149,6 +158,7 @@ export default function EtchingProgress({
commitTxHash: data.commitTxHash,
scriptP2trAddress: data.scriptP2trAddress,
tapLeafScript: data.tapLeafScript,
isNft: data.isNft,
revealTxHash: revealTxHash,
})
)
Expand Down
4 changes: 3 additions & 1 deletion components/tabs/index.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -13,14 +13,14 @@ export default function TabsSection() {
premine: 0,
amount: 0,
cap: 0,
divisibility: 0,
receiver: '',
} as UseRuneERC1155Props)
const [commitTxHash, setCommitTxHash] = useState<string | null>(null)
const [revealTxHash, setRevealTxHash] = useState<string | null>(null)
const [mintTxHash, setMintTxHash] = useState<string | null>(null)
const [etchedFinished, setEtchedFinished] = useState(false)
const [mintFinished, setMintFinished] = useState(false)
const [isNft, setIsNft] = useState<boolean>(true)

useEffect(() => {
const mintTxHash = localStorage.getItem('mintTxHash')
Expand Down Expand Up @@ -49,6 +49,8 @@ export default function TabsSection() {
<EtchTab
setRuneProps={setRunePropsState}
setCommitTxHash={setCommitTxHash}
isNft={isNft}
setIsNft={setIsNft}
/>
) : (
<EtchingProgress
Expand Down
5 changes: 5 additions & 0 deletions components/ui/InputField.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -53,6 +53,11 @@ function InputField({
type={type}
id={name}
disabled={disabled}
onChange={(e) =>
field.onChange(
type === 'number' ? e.target.valueAsNumber : e.target.value
)
}
/>
</FormControl>
<FormMessage>{form.formState.errors[name]?.message}</FormMessage>
Expand Down
1 change: 1 addition & 0 deletions contracts/RuneToken.sol
Original file line number Diff line number Diff line change
Expand Up @@ -47,6 +47,7 @@ contract RuneToken is ERC1155, Ownable {
* @param maxSupply Maximum supply of the token
* @param initialSupply Initial supply of the token
* @param defaultMintAmount Default amount to mint each time
* @param receiver Address to receive the minted tokens
*/
function mintFungible(
string memory tokenURI,
Expand Down

0 comments on commit aaa1e47

Please sign in to comment.