Skip to content

Commit

Permalink
WIP
Browse files Browse the repository at this point in the history
  • Loading branch information
CRBl69 committed Nov 19, 2024
1 parent 4da33eb commit 2a81018
Show file tree
Hide file tree
Showing 9 changed files with 49 additions and 56 deletions.
15 changes: 15 additions & 0 deletions src/sh/generate-links.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
#!/bin/bash

url="https://emojicoin.fun/generate-qr-code"

for _ in $(seq 1 "$2");do
private="$(openssl genpkey -algorithm ED25519)"
public="$(echo "$private" | openssl pkey -pubout)"
formated_private="$(echo "$private" | head -n 2 | tail -n 1)"
formated_public="$(echo "$public" | head -n 2 | tail -n 1)"
echo "$formated_private,$formated_public,$url/$(echo -n "$formated_private" | base64 | jq -sRr @uri)" >> "$3"
done

publics="$(awk -F',' '{print $2}' "$3" | od -An -v -td1 | xargs -I _ echo "[_]" | tr -s ' \n' ',' | sed -E 's/^,(.*),$/\1/g' | tr '\n' ',' | sed -E 's/^(.*),$/\1/g')"

echo "aptos move run --function-id \"$1\"::emojicoin_dot_fun_claim_link::add_public_keys_and_fund_gas_escrows --args \"u8:[$publics]\" u64:10000000"
31 changes: 6 additions & 25 deletions src/typescript/frontend/src/app/generate-qr-code/page.tsx
Original file line number Diff line number Diff line change
@@ -1,46 +1,27 @@
"use client";

import Link from "next/link";
import { useMemo, useState } from "react";
import { useState } from "react";
import "./module.css";
import { stringifyJSON } from "utils";
import { type FreeSwapData } from "@/store/user-settings-store";

export default function GenerateQRCode() {
const [feePayerKey, setFeePayerKey] = useState<string>("");
const [claimCode, setClaimCode] = useState<string>("");

const data = useMemo(() => {
const pathData: FreeSwapData = { claimCode, feePayerKey };
return btoa(stringifyJSON(pathData));
}, [feePayerKey, claimCode]);
const [claimKey, setClaimKey] = useState<string>("");

return (
<div className="h-[100%] w-[100%] grid items-center">
<div className="m-auto flex flex-col gap-[16px]">
<textarea
autoFocus={true}
rows={4}
placeholder={"Fee payer key..."}
value={feePayerKey}
onChange={(v) => setFeePayerKey(v.currentTarget.value.replace(/\n/g, ""))}
className="bg-black text-3xl outline-none text-ec-blue p-[10px]"
style={{
border: "3px dashed var(--ec-blue)",
}}
/>
<textarea
autoFocus={true}
rows={4}
placeholder={"Claim code..."}
value={claimCode}
onChange={(v) => setClaimCode(v.currentTarget.value.replace(/\n/g, ""))}
placeholder={"Claim key..."}
value={claimKey}
onChange={(v) => setClaimKey(v.currentTarget.value.replace(/\n/g, ""))}
className="bg-black text-3xl outline-none text-ec-blue p-[10px]"
style={{
border: "3px dashed var(--ec-blue)",
}}
/>
<Link href={`/generate-qr-code/${encodeURIComponent(data)}`}>
<Link href={`/generate-qr-code/${encodeURIComponent(claimKey)}`}>
<button
type="submit"
className="text-ec-blue text-3xl uppercase w-[100%] p-[4px]"
Expand Down
17 changes: 6 additions & 11 deletions src/typescript/frontend/src/app/redeem/[data]/page.tsx
Original file line number Diff line number Diff line change
@@ -1,10 +1,8 @@
"use client";

import { type FreeSwapData } from "@/store/user-settings-store";
import { useUserSettings } from "context/event-store-context";
import { redirect } from "next/navigation";
import { useEffect, useMemo } from "react";
import { parseJSON } from "utils";
import { useEffect } from "react";

interface PageProps {
params: {
Expand All @@ -14,18 +12,15 @@ interface PageProps {
}

export default function GenerateQRCode(props: PageProps) {
const data = decodeURIComponent(props.params.data);
const setFreeSwap = useUserSettings((s) => s.setFreeSwapData);
const freeSwapData: FreeSwapData = useMemo(() => {
return parseJSON(atob(data));
}, [data]);
const claimKey = decodeURIComponent(props.params.data);
const setClaimKey = useUserSettings((s) => s.setClaimKey);

useEffect(() => {
if (setFreeSwap) {
setFreeSwap(freeSwapData);
if (setClaimKey) {
setClaimKey(claimKey);
redirect("/home");
}
}, [freeSwapData, setFreeSwap]);
}, [claimKey, setClaimKey]);

return (
<div className="h-[100%] w-[100%] block content-center">
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,11 @@ import { useAptos } from "context/wallet-context/AptosContextProvider";
import { toCoinTypes } from "@sdk/markets/utils";
import { type AccountAddressString } from "@sdk/emojicoin_dot_fun";
import { type Dispatch, type SetStateAction, useEffect, useCallback } from "react";
import { Ed25519PrivateKey, isUserTransactionResponse } from "@aptos-labs/ts-sdk";
import {
Account,
Ed25519PrivateKey,
isUserTransactionResponse,
} from "@aptos-labs/ts-sdk";
import { STRUCT_STRINGS } from "@sdk/utils";
import { useAnimationControls } from "framer-motion";
import { RewardsAnimation } from "./RewardsAnimation";
Expand Down Expand Up @@ -44,8 +48,8 @@ export const SwapButton = ({
const controls = useAnimationControls();
const { canTrade } = useCanTradeMarket(symbol);

const freeSwapData = useUserSettings((s) => s.freeSwapData);
const setFreeSwapData = useUserSettings((s) => s.setFreeSwapData);
const claimKey = useUserSettings((s) => s.claimKey);
const setFreeSwapData = useUserSettings((s) => s.setClaimKey);

const handleClick = useCallback(async () => {
if (!account) {
Expand All @@ -54,9 +58,10 @@ export const SwapButton = ({
let builderLambda: () => Promise<EntryFunctionTransactionBuilder>;
const { emojicoin, emojicoinLP } = toCoinTypes(marketAddress);
try {
if (freeSwapData !== undefined) {
const privateKey = new Ed25519PrivateKey(freeSwapData.feePayerKey);
if (claimKey !== undefined) {
const privateKey = new Ed25519PrivateKey(Buffer.from(claimKey, "base64").subarray(16));
const publicKey = privateKey.publicKey();
const feePayerAccount = Account.fromPrivateKey({ privateKey });
builderLambda = () =>
Redeem.builder({
aptosConfig: aptos.config,
Expand All @@ -66,7 +71,7 @@ export const SwapButton = ({
marketAddress,
typeTags: [emojicoin, emojicoinLP],
minOutputAmount: BigInt(minOutputAmount),
feePayer: freeSwapData.feePayerKey,
feePayer: feePayerAccount.accountAddress,
});
} else {
builderLambda = () =>
Expand Down Expand Up @@ -118,7 +123,7 @@ export const SwapButton = ({
submit,
controls,
minOutputAmount,
freeSwapData,
claimKey,
setFreeSwapData,
]);

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -67,7 +67,7 @@ export default function SwapComponent({
const { t } = translationFunction();
const searchParams = useSearchParams();

const hasFreeSwap = useUserSettings((s) => s.freeSwapData !== undefined);
const hasFreeSwap = useUserSettings((s) => s.claimKey !== undefined);

const claimAmount = useClaimAmount() / ONE_APT;

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ const Item = ({ emoji, change }: { emoji: string; change: number }) => {
};

export const PriceFeedInner = ({ data }: { data: Awaited<ReturnType<typeof fetchPriceFeed>> }) => {
const hasFreeSwap = useUserSettings((s) => s.freeSwapData !== undefined);
const hasFreeSwap = useUserSettings((s) => s.claimKey !== undefined);
return !hasFreeSwap ? (
<div className="w-full z-[10] relative">
<Carousel>
Expand Down
2 changes: 1 addition & 1 deletion src/typescript/frontend/src/components/rewards-banner.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ import { useUserSettings } from "context/event-store-context";
import Carousel from "components/carousel";

export const RewardsBanner = () => {
const hasFreeSwap = useUserSettings((s) => s.freeSwapData !== undefined);
const hasFreeSwap = useUserSettings((s) => s.claimKey !== undefined);
return hasFreeSwap ? (
<div className="w-full z-[10] relative">
<Carousel>
Expand Down
15 changes: 5 additions & 10 deletions src/typescript/frontend/src/lib/store/user-settings-store.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,20 +2,15 @@ import { LOCAL_STORAGE_KEYS } from "configs";
import { parseJSON, stringifyJSON } from "utils";
import { createStore } from "zustand";

export type FreeSwapData = {
claimCode: string;
feePayerKey: string;
};

export type UserSettingsState = {
animate: boolean;
freeSwapData: FreeSwapData | undefined;
claimKey: string | undefined;
};

export type UserSettingsActions = {
setAnimate: (value: boolean) => void;
toggleAnimate: () => void;
setFreeSwapData: (freeSwapData: FreeSwapData | undefined) => void;
setClaimKey: (claimKey: string | undefined) => void;
};

export type UserSettingsStore = UserSettingsState & UserSettingsActions;
Expand All @@ -26,7 +21,7 @@ const saveSettings = (state: UserSettingsStore) => {

const defaultValues: UserSettingsState = {
animate: true,
freeSwapData: undefined,
claimKey: undefined,
};

const readSettings = (): UserSettingsState => {
Expand Down Expand Up @@ -57,9 +52,9 @@ export const createUserSettingsStore = () =>
saveSettings(state);
return state;
}),
setFreeSwapData: (freeSwapData) =>
setClaimKey: (claimKey) =>
set((state) => {
state.freeSwapData = freeSwapData;
state.claimKey = claimKey;
saveSettings(state);
return state;
}),
Expand Down
2 changes: 2 additions & 0 deletions src/typescript/sdk/src/emojicoin_dot_fun/payload-builders.ts
Original file line number Diff line number Diff line change
Expand Up @@ -120,6 +120,7 @@ export class EntryFunctionTransactionBuilder {
/* eslint-disable-next-line import/no-unused-modules */
export type WalletInputTransactionData = {
sender?: AccountAddressInput;
feePayer?: AccountAddressInput;
// For now we only use entry functions. Eventually we could support script functions, too.
data: InputEntryFunctionData;
options?: InputGenerateTransactionOptions;
Expand Down Expand Up @@ -173,6 +174,7 @@ export abstract class EntryFunctionPayloadBuilder extends Serializable {

return {
sender: this.primarySender,
feePayer: this.feePayer,
data: {
...multiSigData,
function: `${this.moduleAddress.toString()}::${this.moduleName}::${this.functionName}`,
Expand Down

0 comments on commit 2a81018

Please sign in to comment.