From 3484de2624cb3e5164238c7ff7a4812824e4e726 Mon Sep 17 00:00:00 2001 From: Matias Benary Date: Fri, 30 Aug 2024 12:29:49 -0300 Subject: [PATCH 1/6] fix: broken styles for firefox in wallet utilities --- src/components/wallet-utilities/KeyTable.tsx | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/src/components/wallet-utilities/KeyTable.tsx b/src/components/wallet-utilities/KeyTable.tsx index 12a562516..26e4b53cf 100644 --- a/src/components/wallet-utilities/KeyTable.tsx +++ b/src/components/wallet-utilities/KeyTable.tsx @@ -87,14 +87,14 @@ const KeyTable: React.FC = () => { - + {data.access_key.permission.FunctionCall?.receiver_id ?? 'N/A'} - + {data.public_key} From 05c255d65bbf39e81ea97c0cc61170c81aef61cb Mon Sep 17 00:00:00 2001 From: Matias Benary Date: Fri, 30 Aug 2024 13:11:33 -0300 Subject: [PATCH 2/6] fix: broken text style in firefox --- src/components/wallet-utilities/KeyTable.tsx | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/src/components/wallet-utilities/KeyTable.tsx b/src/components/wallet-utilities/KeyTable.tsx index 26e4b53cf..c0488a57c 100644 --- a/src/components/wallet-utilities/KeyTable.tsx +++ b/src/components/wallet-utilities/KeyTable.tsx @@ -87,14 +87,14 @@ const KeyTable: React.FC = () => { - + {data.access_key.permission.FunctionCall?.receiver_id ?? 'N/A'} - + {data.public_key} From 1b84a3f9519a4f1e904636d39ff75fdf64213f8b Mon Sep 17 00:00:00 2001 From: Matias Benary Date: Wed, 4 Sep 2024 17:56:37 -0300 Subject: [PATCH 3/6] 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 + +
+ + + + +