Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: Allow arbitrary destination address for token creation #66

Merged
merged 6 commits into from
Sep 11, 2023
Merged
Show file tree
Hide file tree
Changes from 4 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
24 changes: 13 additions & 11 deletions src/api/services/queries/ledger.ts
Original file line number Diff line number Diff line change
Expand Up @@ -29,10 +29,10 @@
return useQueries({
queries: tokenList
? [...tokenList.symbols.entries()].map(([address]) => ({
queryKey: [neighborhood?.url, "tokens.info", address],
queryFn: async () => await neighborhood?.tokens.info({ address }),
enabled: !!neighborhood,
}))
queryKey: [neighborhood?.url, "tokens.info", address],
queryFn: async () => await neighborhood?.tokens.info({ address }),

Check warning on line 33 in src/api/services/queries/ledger.ts

View check run for this annotation

Codecov / codecov/patch

src/api/services/queries/ledger.ts#L33

Added line #L33 was not covered by tests
enabled: !!neighborhood,
}))
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Just auto-formatting. No code changes.

: [],
});
}
Expand All @@ -42,27 +42,29 @@

return useMutation(
async ({
address,
amount,
destination,
name,
symbol,
owner,
precision = 9,
symbol,
}: {
address: string;
amount: string;
destination: string;
name: string;
symbol: string;
owner: string;
precision?: number;
symbol: string;
Comment on lines -51 to +57
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I sorted the arg list so the diff is a little messy, but I'm removing address and replacing it with both owner and destination. The former will be the same as the sender and the latter will take the input address.

}) =>
await neighborhood?.tokens.create({
summary: {
name,
symbol: symbol.toUpperCase(),
precision: 9,
precision,
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

precision already defaulted to 9 so we don't need to hardcode a value here.

},
owner: address,
owner,
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The owner parameter will now be set to the owner argument above...

distribution: {
[address]: BigInt(parseInt(amount) * 10 ** precision),
[destination]: BigInt(parseInt(amount) * 10 ** precision),
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

...while the destination of the initial distribution will be set to the destination argument.

},
}),
{
Expand Down
54 changes: 27 additions & 27 deletions src/pages/services/ledger/create-token-modal.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -8,28 +8,22 @@
FormLabel,
Grid,
GridItem,
HStack,
Icon,
Input,
Modal,
Popover,
PopoverBody,
PopoverContent,
PopoverTrigger,
useToast,
} from "@liftedinit/ui";
import { NeighborhoodContext } from "api/neighborhoods";
import { useCreateToken } from "api/services";
import { useAccountsStore } from "features/accounts";
import { useContext } from "react";
import { Controller, SubmitHandler, useForm } from "react-hook-form";
import { FiInfo } from "react-icons/fi";

export interface CreateTokenInputs {
amount: string;
destination: string;
name: string;
owner: string;
symbol: string;
amount: string;
address: string;
}

export function CreateTokenModal({
Expand All @@ -56,12 +50,14 @@
const toast = useToast();

const onSubmit: SubmitHandler<CreateTokenInputs> = ({
amount,
destination,
name,
owner,
symbol,
amount,
}) => {
doCreateToken(
{ name, symbol, amount, address },
{ amount, destination, name, owner, symbol },
{
onSuccess: () => {
onClose();
Expand Down Expand Up @@ -148,22 +144,26 @@
</FormControl>
</GridItem>
<GridItem colSpan={5}>
<FormControl>
<HStack>
<FormLabel htmlFor="address">Destination Address</FormLabel>
<Popover trigger="hover">
<PopoverTrigger>
<Icon as={FiInfo} />
</PopoverTrigger>
<PopoverContent bg="brand.teal.700" color="white" p={3}>
<PopoverBody>
The destination address is the current user and cannot be
changed at this time.
</PopoverBody>
</PopoverContent>
</Popover>
</HStack>
<Input fontFamily="mono" isDisabled value={address} />
<FormControl isInvalid={!!errors.destination}>
<FormLabel htmlFor="destination">Destination Address</FormLabel>
<Controller
name="destination"
control={control}
rules={{
required: true,
validate: {
isManyAddress: (v) => new RegExp(/^m\w{24,}$/).test(v),

Check warning on line 155 in src/pages/services/ledger/create-token-modal.tsx

View check run for this annotation

Codecov / codecov/patch

src/pages/services/ledger/create-token-modal.tsx#L155

Added line #L155 was not covered by tests
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

A more accurate regex for detecting a valid Many address is welcome if we have one (I looked in the spec and many-rs).

},
}}
render={({ field }) => (
<Input fontFamily="mono" defaultValue={address} {...field} />

Check warning on line 159 in src/pages/services/ledger/create-token-modal.tsx

View check run for this annotation

Codecov / codecov/patch

src/pages/services/ledger/create-token-modal.tsx#L159

Added line #L159 was not covered by tests
)}
/>
{errors.destination && (
<FormErrorMessage>

Check warning on line 163 in src/pages/services/ledger/create-token-modal.tsx

View check run for this annotation

Codecov / codecov/patch

src/pages/services/ledger/create-token-modal.tsx#L163

Added line #L163 was not covered by tests
Must be a valid Many address.
</FormErrorMessage>
)}
</FormControl>
</GridItem>
</Grid>
Expand Down