Skip to content

Commit

Permalink
feat: clean and fix linkdrop tools
Browse files Browse the repository at this point in the history
  • Loading branch information
matiasbenary committed Sep 9, 2024
1 parent 07b7c31 commit e46b609
Show file tree
Hide file tree
Showing 12 changed files with 225 additions and 231 deletions.
68 changes: 0 additions & 68 deletions src/components/tools/Linkdrops.tsx

This file was deleted.

Original file line number Diff line number Diff line change
@@ -1,11 +1,12 @@
import { Button, FileInput, Flex, Form, Input, openToast, Text } from '@near-pagoda/ui';
import { Button, Flex, Form, Input, openToast, Text } from '@near-pagoda/ui';
import { parseNearAmount } from 'near-api-js/lib/utils/format';
import { useContext, useEffect, useState } from 'react';
import type { SubmitHandler } from 'react-hook-form';
import { useForm } from 'react-hook-form';

import { NearContext } from '../WalletSelector';
import generateAndStore from '@/utils/keyPair';
import { parseNearAmount } from 'near-api-js/lib/utils/format';
import generateAndStore from '@/utils/linkdrops';

import { NearContext } from '../../WalletSelector';

type FormData = {
dropName: string;
Expand All @@ -25,12 +26,20 @@ function displayBalance(balance: number) {
return display;
}

const TokenDrop = () => {
const getDeposit = (amountPerLink: number, numberLinks: number) =>
parseNearAmount(((0.0426 + amountPerLink) * numberLinks).toString());

const CreateTokenDrop = () => {
const {
register,
handleSubmit,
formState: { errors, isSubmitting },
} = useForm<FormData>();
} = useForm<FormData>({
defaultValues: {
numberLinks: 1,
amountPerLink: 0,
},
});

const { wallet, signedAccountId } = useContext(NearContext);
const [currentNearAmount, setCurrentNearAmount] = useState(0);
Expand All @@ -42,7 +51,8 @@ const TokenDrop = () => {
try {
const balance = await wallet.getBalance(signedAccountId);
const requiredGas = 0.00005;
setCurrentNearAmount(balance - requiredGas);
const cost = 0.0426;
setCurrentNearAmount(balance - requiredGas - cost);
} catch (error) {
console.error(error);
}
Expand All @@ -51,8 +61,6 @@ const TokenDrop = () => {
loadBalance();
}, [wallet, signedAccountId]);

// get_drop_information

const onSubmit: SubmitHandler<FormData> = async (data) => {
if (!wallet) throw new Error('Wallet has not initialized yet');

Expand All @@ -65,8 +73,6 @@ const TokenDrop = () => {
public_keys: generateAndStore(data.dropName, data.numberLinks),
};

// const amount = parseNearAmount(0.1426.toString());
// if (!amount) throw new Error('Failed to parse amount');
await wallet.signAndSendTransactions({
transactions: [
{
Expand All @@ -78,7 +84,7 @@ const TokenDrop = () => {
methodName: 'create_drop',
args,
gas: '300000000000000',
deposit: parseNearAmount(((0.0426 + data.amountPerLink) * data.numberLinks).toString()),
deposit: getDeposit(data.amountPerLink, data.numberLinks),
},
},
],
Expand All @@ -103,7 +109,6 @@ const TokenDrop = () => {
});
}
};

return (
<>
<Text size="text-l" style={{ marginBottom: '12px' }}>
Expand All @@ -125,14 +130,14 @@ const TokenDrop = () => {
{...register('numberLinks', {
min: {
message: 'Must be greater than 0',
value: 1
value: 1,
},
max: {
message: `Must be equal to or less than 50`,
value: 50,
},
valueAsNumber: true,
required: 'Number of links is required'
required: 'Number of links is required',
})}
/>
<Input
Expand All @@ -147,14 +152,14 @@ const TokenDrop = () => {
{...register('amountPerLink', {
min: {
message: 'Must be greater than 0',
value: 0.0000000001
value: 0.0000000001,
},
max: {
message: `Must be equal to or less than ${currentNearAmount}`,
value: currentNearAmount,
},
valueAsNumber: true,
required: 'Amount per link is required'
required: 'Amount per link is required',
})}
/>
<Button label="Create links" variant="affirmative" type="submit" loading={isSubmitting} />
Expand All @@ -164,60 +169,4 @@ const TokenDrop = () => {
);
};

export default TokenDrop;

// Arguments: {
// "drop_id": "7bc5f708-dd9d-4dba-9f02-a117324346b5",
// "deposit_per_use": "100000000000000000000000",
// "metadata": "{\"dropName\":\"test\"}",
// "public_keys": [
// "ed25519:4JJwGg45WDmU14wsSG87K2Anf1g5wm6Yn1e4hEcfudAX",
// "ed25519:HKAZwSeN85DgPGUW5JGRRsMe5kPBhCbBqf1qHMNZfdgz"
// ]
// }

// Arguments: {
// "drop_id": "7bc5f708-dd9d-4dba-9f02-a117324346b5",
// "deposit_per_use": "100000000000000000000000",
// "metadata": "{\"dropName\":\"test\"}",
// "public_keys": [
// "ed25519:4JJwGg45WDmU14wsSG87K2Anf1g5wm6Yn1e4hEcfudAX",
// "ed25519:HKAZwSeN85DgPGUW5JGRRsMe5kPBhCbBqf1qHMNZfdgz"
// ]
// }

// Arguments: {
// "drop_id": "1725481443015",
// "deposit_per_use": "10000000000000000000000",
// "metadata": "{\"dropName\":\"test\"}",
// "public_keys": [
// "ed25519:FT6axDyL39LFE3cVN6PNJhejZZ6XMEDzvksW8uGhi2N5",
// "ed25519:9H93DK5GedgTgDTabU8qxihELBzNdMH5HRWwCpg2FUoh"
// ]
// }

// Arguments: {
// "deposit_per_use": "10000000000000000000000",
// "metadata": "{\"dropName\":\"test\"}",
// "public_keys": [
// "ed25519:Hzy9SMFyudC3bdNVKhwVZ4bCrhYnFaD9syW2bPNKsh6R",
// "ed25519:3pZ3ue2pWkFfGehej21qPAx2ZK5Nw3gkKvHGSyLuHCZW",
// "ed25519:3z8WguDt5whWoQ17t2XntvytxtBAL5XWSorD4zjtfeTM",
// "ed25519:9NM7ohNWzyVd3bqvWrWvxj2PXHQwDHtv9uF9cqy9k7s8",
// "ed25519:2z41giU3Mv696jDPEWnGmxS4Zp2m8P8nGs2feUHKngEw"
// ]
// }


// Arguments: {
// "drop_id": "1725547843208",
// "deposit_per_use": "10000000000000000000000",
// "metadata": "{\"dropName\":\"test keypom\"}",
// "public_keys": [
// "ed25519:GEzAmNgBE9vLVDfSvReHNSZ8aiPd1d43HMwyKKVGu6Ls",
// "ed25519:H7wUQgGLeLyKWFJ8aeVUgzbPhjyVjtGnW1gfCfHo2mZp",
// "ed25519:28F7bHSy73mfUErj2aQyyjRWtDrCGxYXPG6wRdsfE5Zp",
// "ed25519:211USudaQFT7vBY8ruvyfwymuESCtZnFfFzcAcHY3ucy",
// "ed25519:6ULFyadfVzoQBYqQLTmqrnQHZmFZtewHYcbFCVJe8KL5"
// ]
// }
export default CreateTokenDrop;
55 changes: 55 additions & 0 deletions src/components/tools/Linkdrops/ListTokenDrop.tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,55 @@
import { Accordion, Badge, Button, copyTextToClipboard, Flex, Text, Tooltip } from '@near-pagoda/ui';
import { Copy } from '@phosphor-icons/react';

import type { Drops } from '@/utils/types';

const getDropName = (drop: Drops) => {
return drop ? JSON.parse(drop.metadata).dropName : '';
};

const ListTokenDrop = ({ drops }: { drops: Drops[] }) => {
return (
<Accordion.Root type="multiple">
{drops.map((drop) => {
return (
<Accordion.Item key={drop.drop_id} value={drop.drop_id}>
<Accordion.Trigger>
{getDropName(drop)} - Claimed {drop.next_key_id - drop.registered_uses}/{drop.next_key_id}
</Accordion.Trigger>
<Accordion.Content>
{drop.keys &&
drop.keys.map((key) => {
const wasClaimed = !drop.information.some((info) => info.pk == key.public);
return (
<Flex align="center" justify="space-between" key={key.private}>
<Text style={{ maxWidth: '10rem' }} clampLines={1}>
{key.private}
</Text>
<Badge
label={wasClaimed ? 'Claimed' : 'Unclaimed'}
variant={wasClaimed ? 'success' : 'neutral'}
/>
<Tooltip content="Copy Account ID">
<Button
label="copy"
onClick={() => {
const url = 'https://app.mynearwallet.com' + '/linkdrop/v2.keypom.near/' + key.private;
copyTextToClipboard(url, url);
}}
size="small"
fill="outline"
icon={<Copy />}
/>
</Tooltip>
</Flex>
);
})}
</Accordion.Content>
</Accordion.Item>
);
})}
</Accordion.Root>
);
};

export default ListTokenDrop;
15 changes: 15 additions & 0 deletions src/components/tools/Linkdrops/index.tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
import type { Drops } from '@/utils/types';

import CreateTokenDrop from './CreateTokenDrop';
import ListTokenDrop from './ListTokenDrop';

const Linkdrops = ({ drops }: { drops: Drops[] }) => {
return (
<>
<CreateTokenDrop />
<ListTokenDrop drops={drops} />
</>
);
};

export default Linkdrops;
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ import { useContext } from 'react';
import type { SubmitHandler } from 'react-hook-form';
import { Controller, useForm } from 'react-hook-form';

import { NearContext } from '../WalletSelector';
import { NearContext } from '../../WalletSelector';

type FormData = {
title: string;
Expand Down
File renamed without changes.
2 changes: 1 addition & 1 deletion src/components/wallet-utilities/SendNear.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -48,7 +48,7 @@ export const SendNear = () => {
const validSubmitHandler: SubmitHandler<FormData> = async (data) => {
try {
if (!wallet) throw new Error('Wallet has not initialized yet');
const amount = utils.format.parseNearAmount(0.1426.toString());
const amount = utils.format.parseNearAmount(data.sendNearAmount.toString());
if (!amount) throw new Error('Failed to parse amount');

const sendNear = {
Expand Down
54 changes: 54 additions & 0 deletions src/hooks/useLinkdrops.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,54 @@
import { useContext, useEffect, useState } from 'react';

import { NearContext } from '@/components/WalletSelector';
import { getKeypomKeys } from '@/utils/linkdrops';
import type { Drops } from '@/utils/types';

const useLinkdrops = () => {
const { signedAccountId } = useContext(NearContext);
const [drops, setDrops] = useState<Drops[]>([]);

const { wallet } = useContext(NearContext);

useEffect(() => {
const fetchDropData = async () => {
if (!wallet || !signedAccountId) return;
const fetchedDrops: Drops[] = await wallet.viewMethod({
contractId: 'v2.keypom.near',
method: 'get_drops_for_owner',
args: { account_id: signedAccountId },
});

const filteredDrops = fetchedDrops.filter(
(drop) =>
drop.metadata &&
JSON.parse(drop.metadata).dropName &&
getKeypomKeys(JSON.parse(drop.metadata).dropName).length,
);

const fetchedInformationDrops = await Promise.all(
filteredDrops.map(async (drop) => {
const information = await wallet.viewMethod({
contractId: 'v2.keypom.near',
method: 'get_keys_for_drop',
args: { drop_id: drop.drop_id },
});
return { ...drop, information };
}),
);

const localDataDrops = fetchedInformationDrops.map((drop) => ({
...drop,
keys: getKeypomKeys(JSON.parse(drop.metadata).dropName),
}));

setDrops(localDataDrops);
};

fetchDropData();
}, [wallet, signedAccountId]);

return drops;
};

export default useLinkdrops;
Loading

0 comments on commit e46b609

Please sign in to comment.