Skip to content

Commit

Permalink
Add election of the airdropped token
Browse files Browse the repository at this point in the history
  • Loading branch information
af-afk committed Jul 25, 2024
1 parent ed02911 commit dc33ee9
Show file tree
Hide file tree
Showing 9 changed files with 132 additions and 32 deletions.
25 changes: 25 additions & 0 deletions web/app.fluidity.money/app/queries/addAirdropElection.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
import { jsonPost } from "~/util";

const BaseUrl =
"https://duvlwhscy2.execute-api.ap-southeast-2.amazonaws.com/default/flu-create-airdrop-elections";

type RequestAirdropElection = {
address: string;
option: number;
sig: string;
};

type ResponseAirdropElection = {
address: string;
updated: string;
error: string;
};

export const addAirdropElection = (address: string, option: number, sig: string) => {
const body = { address, option, sig };
return jsonPost<RequestAirdropElection, ResponseAirdropElection>(
BaseUrl,
body,
{}
);
};
1 change: 1 addition & 0 deletions web/app.fluidity.money/app/queries/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -12,3 +12,4 @@ export * from "./addReferralCode";
export * from "./useLootboxConfig";
export * from "./useLootBottles";
export * from "./useFLYOwed";
export * from "./addAirdropElection";
1 change: 1 addition & 0 deletions web/app.fluidity.money/app/queries/useFLYOwed.ts
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@ type ResponseFLYOwedForAddress = {
amount: number;
updated: string;
bottles: ResponseFLYOwedForAddressBottle[];
allocated: boolean;
error: string;
};

Expand Down
Original file line number Diff line number Diff line change
@@ -1,11 +1,5 @@
/* eslint-disable no-irregular-whitespace */

import type {
StakingRatioRes,
StakingDepositsRes,
} from "~/util/chainUtils/ethereum/transaction";
import type AugmentedToken from "~/types/AugmentedToken";

import { useState, useEffect, useContext, useMemo, useCallback } from "react";
import BN from "bn.js";
import {
Expand Down Expand Up @@ -36,13 +30,19 @@ import {
Provider,
Modal,
} from "@fluidity-money/surfing";
import type {
StakingRatioRes,
StakingDepositsRes,
} from "~/util/chainUtils/ethereum/transaction";
import { AirdropElection } from "~/util/chainUtils/ethereum/transaction";
import type AugmentedToken from "~/types/AugmentedToken";
import {
addDecimalToBn,
getTokenAmountFromUsd,
getUsdFromTokenAmount,
} from "~/util/chainUtils/tokens";
import { dayDifference } from ".";
import { useFLYOwedForAddress, Referral } from "~/queries";
import { useFLYOwedForAddress, addAirdropElection, Referral } from "~/queries";
import { BottleTiers } from "../../query/dashboard/airdrop";
import {
AnimatePresence,
Expand All @@ -55,7 +55,6 @@ import FluidityFacadeContext from "contexts/FluidityFacade";
import { FlyStakingContext } from "contexts/FlyStakingProvider";
import { CopyGroup } from "~/components/ReferralModal";
import ConnectWalletModal from "~/components/ConnectWalletModal";
import FLYClaimSubmitModal from "~/components/FLYClaimSubmitModal";
import { shorthandAmountFormatter } from "~/util";

// Epoch length
Expand Down Expand Up @@ -1767,7 +1766,7 @@ const RecapModal = ({
},
};

const { address } = useContext(FluidityFacadeContext);
const { address, signAirdropElection } = useContext(FluidityFacadeContext);

const { toggleVisibility: flyStakingModalToggleVisibility } =
useContext(FlyStakingContext);
Expand All @@ -1777,10 +1776,13 @@ const RecapModal = ({

const [walletModalVisibility, setWalletModalVisibility] = useState(false);
const [flyClaimModalState, setFlyClaimModalState] = useState<
"none" | "claim" | "stake"
"none" | "claim" | "stake" | "convert"
>("none");
const [isMidSigningAirdropElection, setIsMidSigningAirdropElection] = useState(false);
const [electMessage, setElectMessage] = useState("");

const [flyAmountOwed, setFLYAmountOwed] = useState(0);
const [isFLYAllocated, setIsFLYAllocated] = useState(false);

const [showTGEDetails, setShowTGEDetails] = useState(true);

Expand Down Expand Up @@ -1818,9 +1820,11 @@ const RecapModal = ({
console.warn(`Invalid response for airdrop request: ${resp}`);
return;
}
const { amount, error } = resp;
const { amount, allocated, error } = resp;
if (error) throw new Error(`Airdrop request error: ${error}`);
setFLYAmountOwed(amount);
setIsFLYAllocated(allocated);
if (allocated) setElectMessage("You have already elected to claim, stake, or convert!");
setCheckYourEligibilityButtonEnabled(true);
}
})();
Expand Down Expand Up @@ -1858,9 +1862,37 @@ const RecapModal = ({
);
};

const handleClaimYourFly = (type: "claim" | "stake") => {
const handleClaimYourFly = async (type: "claim" | "stake" | "convert") => {
if (!address) return;
setFlyClaimModalState(type);
// Get the user's address.by
if (!signAirdropElection || isMidSigningAirdropElection) return;
setIsMidSigningAirdropElection(true);
var option: number;
switch (type) {
case "claim":
option = AirdropElection.Claim;
break;
case "stake":
option = AirdropElection.Stake;
break;
case "convert":
option = AirdropElection.ConvertToSpn;
break;
}
const signature = await signAirdropElection(option);
console.log("signature for election", signature);
if (!signature) {
setIsMidSigningAirdropElection(true);
setElectMessage("Bad signature!");
return;
}
const { error } = await addAirdropElection(address, option, signature);
if (error) {
console.error("error setting airdrop election", error);
setElectMessage("Error setting option for distribution. Try again, and create a Discord ticket if another issue occurs.");
return;
}
setElectMessage("Success setting!");
};

const [termsAndConditionsModalVis, setTermsAndConditionsModalVis] =
Expand All @@ -1884,20 +1916,20 @@ const RecapModal = ({
const ClaimButtonsSpread = () => (
<div className="recap-fly-count-buttons-spread">
<GeneralButton
disabled={true}
disabled={isFLYAllocated || isMidSigningAirdropElection}
onClick={() => handleClaimYourFly("claim")}
>
Claim your FLY
</GeneralButton>
<GeneralButton
disabled={true}
disabled={isFLYAllocated || isMidSigningAirdropElection}
onClick={() => handleClaimYourFly("stake")}
>
Stake your $FLY airdrop
</GeneralButton>
<GeneralButton
disabled={true}
onClick={() => handleClaimYourFly("stake")}
disabled={isFLYAllocated || isMidSigningAirdropElection}
onClick={() => handleClaimYourFly("convert")}
>
Convert to $SPN points
</GeneralButton>
Expand Down Expand Up @@ -1936,6 +1968,7 @@ const RecapModal = ({
{numberToCommaSeparated(flyAmountOwed)}
</Heading>
</div>
<Heading as="h5" style={{ textAlign: "center" }}>{electMessage}</Heading>
<div className="recap-fly-count-buttons-spread-container recap-fly-count-eligible-buttons">
<ButtonsSpread />
</div>
Expand Down Expand Up @@ -2095,18 +2128,6 @@ const RecapModal = ({
</div>
</div>
</Modal>
<Modal id="fly-claim-submit" visible={flyClaimModalState !== "none"}>
<FLYClaimSubmitModal
showConnectWalletModal={() => setWalletModalVisibility(true)}
flyAmount={flyAmountOwed}
visible={flyClaimModalState !== "none"}
mode={flyClaimModalState === "none" ? "claim" : flyClaimModalState}
accumulatedPoints={day1Points}
close={() => setFlyClaimModalState("none")}
onStakingComplete={handleClaimStakingModalComplete}
onClaimComplete={handleClaimStakingModalComplete}
/>
</Modal>
<div className={`recap-container ${isMobile ? "recap-mobile" : ""}`}>
{/* Recap Heading */}
<div className={"recap-hero"}>
Expand Down
5 changes: 5 additions & 0 deletions web/app.fluidity.money/app/styles/dashboard/airdrop.css

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

5 changes: 5 additions & 0 deletions web/app.fluidity.money/app/styles/dashboard/airdrop.scss
Original file line number Diff line number Diff line change
Expand Up @@ -1100,6 +1100,11 @@ $holo: linear-gradient(
text-align: center;
}

.recap-fly-election-message {
font-size: 14px;
text-align: center;
}

.recap-fly-count-buttons-spread {
display: flex;
flex-direction: row;
Expand Down
29 changes: 27 additions & 2 deletions web/app.fluidity.money/app/util/chainUtils/ethereum/transaction.ts
Original file line number Diff line number Diff line change
Expand Up @@ -119,7 +119,7 @@ export const getUsdAmountMinted = async (
return Number(utils.formatUnits(amount, decimals));
};

const makeContractSwap = async (
export const makeContractSwap = async (
signer: Signer,
from: ContractToken,
to: ContractToken,
Expand Down Expand Up @@ -998,4 +998,29 @@ export const handleContractErrors = async (
}
};

export default makeContractSwap;
export enum AirdropElection {
Claim = 0,
Stake,
ConvertToSpn
};

export const signAirdropElection_ = async (
signer: Signer,
option: AirdropElection
): Promise<string> => {
const address = await signer.getAddress();
var optionStr = "";
switch (option) {
case AirdropElection.Claim:
optionStr = "claim";
break;
case AirdropElection.Stake:
optionStr = "stake";
break;
case AirdropElection.ConvertToSpn:
optionStr = "convert";
break;
}
const message = `I elect to ${optionStr} my FLY token.`;
return await signer.signMessage(utils.arrayify(utils.toUtf8Bytes(message)));
};
16 changes: 15 additions & 1 deletion web/app.fluidity.money/contexts/EthereumProvider.tsx
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
import type { ReactNode } from "react";
import {
confirmAccountOwnership_,
AirdropElection,
signAirdropElection_,
signOwnerAddress_,
StakingDepositsRes,
} from "~/util/chainUtils/ethereum/transaction";
Expand Down Expand Up @@ -39,7 +41,8 @@ import {
flyStakingFinaliseUnstake as doFlyStakingFinaliseUnstake,
flyStakingSecondsUntilSoonestUnstake as doFlyStakingSecondsUntilSoonestUnstake,
} from "~/util/chainUtils/ethereum/transaction";
import makeContractSwap, {
import {
makeContractSwap,
ContractToken,
getBalanceOfERC20,
} from "~/util/chainUtils/ethereum/transaction";
Expand Down Expand Up @@ -604,6 +607,16 @@ const EthereumFacade = ({
console.log(result);
};

const signAirdropElection = async (option: AirdropElection): Promise<string | undefined> => {
const signer = provider?.getSigner();

if (!signer) {
return undefined;
}

return signAirdropElection_(signer, option);
};

const merkleDistributorWithDeadlineEndTime = () => {
throw new Error("TODO");
};
Expand Down Expand Up @@ -808,6 +821,7 @@ const EthereumFacade = ({
getStakingRatios,
signOwnerAddress,
confirmAccountOwnership,
signAirdropElection,
merkleDistributorWithDeadlineEndTime,
merkleDistributorWithDeadlineClaim,
merkleDistributorWithDeadlineClaimAndStake,
Expand Down
3 changes: 3 additions & 0 deletions web/app.fluidity.money/contexts/FluidityFacade.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import type {
StakingDepositsRes,
StakingRedeemableRes,
FLYStakingDetailsRes,
AirdropElection,
} from "~/util/chainUtils/ethereum/transaction";

import type BN from "bn.js";
Expand Down Expand Up @@ -86,6 +87,8 @@ export interface IFluidityFacade {
address: string
) => Promise<void>;

signAirdropElection?: (option: AirdropElection) => Promise<string | undefined>;

merkleDistributorWithDeadlineEndTime?: () => Promise<number | undefined>;

merkleDistributorWithDeadlineClaim?: (
Expand Down

0 comments on commit dc33ee9

Please sign in to comment.