From 1b84a3f9519a4f1e904636d39ff75fdf64213f8b Mon Sep 17 00:00:00 2001 From: Matias Benary Date: Wed, 4 Sep 2024 17:56:37 -0300 Subject: [PATCH 1/4] wip --- src/components/tools/Linkdrops.tsx | 12 ++ src/components/tools/TokenDrop.tsx | 173 +++++++++++++++++++ src/components/wallet-utilities/SendNear.tsx | 2 +- src/pages/tools.tsx | 3 +- src/utils/keyPair.ts | 15 ++ 5 files changed, 203 insertions(+), 2 deletions(-) create mode 100644 src/components/tools/Linkdrops.tsx create mode 100644 src/components/tools/TokenDrop.tsx create mode 100644 src/utils/keyPair.ts diff --git a/src/components/tools/Linkdrops.tsx b/src/components/tools/Linkdrops.tsx new file mode 100644 index 000000000..4089531a6 --- /dev/null +++ b/src/components/tools/Linkdrops.tsx @@ -0,0 +1,12 @@ +import TokenDrop from "./TokenDrop"; + + +const Linkdrops = () => { + return ( +
+ +
+ ); +} + +export default Linkdrops; \ No newline at end of file diff --git a/src/components/tools/TokenDrop.tsx b/src/components/tools/TokenDrop.tsx new file mode 100644 index 000000000..208c5bced --- /dev/null +++ b/src/components/tools/TokenDrop.tsx @@ -0,0 +1,173 @@ +import { Button, FileInput, Flex, Form, Input, openToast, Text } from '@near-pagoda/ui'; +import { useContext, useEffect, useState } from 'react'; +import type { SubmitHandler } from 'react-hook-form'; +import { Controller, useForm } from 'react-hook-form'; + +import { NearContext } from '../WalletSelector'; +import getKeysPair from '@/utils/keyPair'; +import { formatNearAmount, parseNearAmount } from 'near-api-js/lib/utils/format'; + +type FormData = { + dropName: string; + numberLinks: number; + amountPerLink: number; +}; + +function displayBalance(balance: number) { + let display = Math.floor(balance * 100) / 100; + + if (balance < 1) { + display = Math.floor(balance * 100000) / 100000; + if (balance && !display) return '< 0.00001'; + return display; + } + + return display; + } + +const TokenDrop = () => { + const { + register, + handleSubmit, + formState: { errors, isSubmitting }, + } = useForm(); + + const { wallet, signedAccountId } = useContext(NearContext); + const [currentNearAmount, setCurrentNearAmount] = useState(0); + + useEffect(() => { + if (!wallet || !signedAccountId) return; + + const loadBalance = async () => { + try { + const balance = await wallet.getBalance(signedAccountId); + const requiredGas = 0.00005; + setCurrentNearAmount(balance - requiredGas); + } catch (error) { + console.error(error); + } + }; + + loadBalance(); + }, [wallet, signedAccountId]); + + const onSubmit: SubmitHandler = async (data) => { + if (!wallet) throw new Error('Wallet has not initialized yet'); + try { + const args = { + drop_id: crypto.randomUUID(), + deposit_per_use: parseNearAmount(data.amountPerLink.toString()), + metadata: JSON.stringify({ + dropName: data.dropName, + }), + public_keys: getKeysPair(data.numberLinks), + }; + + // const amount = parseNearAmount(0.1426.toString()); + // if (!amount) throw new Error('Failed to parse amount'); + await wallet.signAndSendTransactions({ + transactions: [ + { + receiverId: 'v2.keypom.near', + actions: [ + { + type: 'FunctionCall', + params: { + methodName: 'create_drop', + args, + gas: '300000000000000', + deposit: parseNearAmount(((0.0426+data.amountPerLink)*data.numberLinks).toString()), + }, + }, + ], + }, + ], + }); + + openToast({ + type: 'success', + title: 'Form Submitted', + description: 'Your form has been submitted successfully', + duration: 5000, + }); + } catch (error) { + console.log(error); + + openToast({ + type: 'error', + title: 'Error', + description: 'Failed to submit form', + duration: 5000, + }); + } + }; + + return ( + <> + + Token Drop + +
+ + + + +