Skip to content

Commit 3dc6167

Browse files
authored
Merge pull request #328 from hackdays-io/staging
Release: MTX deposit fee
2 parents 1779d50 + ee36a63 commit 3dc6167

File tree

4 files changed

+58
-9
lines changed

4 files changed

+58
-9
lines changed

frontend/src/components/organisms/CreateEventForm.tsx

Lines changed: 18 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -22,9 +22,14 @@ import { useLocale } from "../../hooks/useLocale";
2222
import Link from "next/link";
2323
import NFTAttributesForm from "./NFTAttributesForm";
2424
import { useIpfs } from "src/hooks/useIpfs";
25-
import { useCreateEvent, useOwnEventGroups } from "src/hooks/useEvent";
25+
import {
26+
useCalcMtxGasFee,
27+
useCreateEvent,
28+
useOwnEventGroups,
29+
} from "src/hooks/useEvent";
2630
import { Event } from "types/Event";
2731
import { NFT } from "types/NFT";
32+
import { formatEther } from "ethers/lib/utils";
2833

2934
type Props = {
3035
address: string;
@@ -78,6 +83,8 @@ const CreateEventForm: FC<Props> = ({ address }) => {
7883

7984
const { remove, append } = useFieldArray({ control, name: "nfts" });
8085

86+
const { gasFee } = useCalcMtxGasFee(watch("mintLimit"));
87+
8188
// state for loading event groups
8289
const { groups, isLoading: isLoadingEventGroups } = useOwnEventGroups();
8390
const { createEvent, isCreating, createError, createStatus, createdEventId } =
@@ -347,6 +354,16 @@ const CreateEventForm: FC<Props> = ({ address }) => {
347354
</>
348355
)}
349356
/>
357+
{watch("useMtx") === "true" && (
358+
<Text mt={2} fontSize="sm">
359+
{t.EVENT_ESTIMATED_GAS_MTX}
360+
<br />
361+
<Box as="span" fontWeight="bold" fontSize="md" pr={1}>
362+
{formatEther(gasFee || 0)}
363+
</Box>
364+
MATIC
365+
</Text>
366+
)}
350367
</FormControl>
351368

352369
<FormControl mb={5}>

frontend/src/hooks/useEvent.ts

Lines changed: 38 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@ import eventManagerABI from "../contracts/EventManager.json";
1111
import { useCallback, useEffect, useMemo, useState } from "react";
1212
import { useCurrentBlock } from "./useBlockChain";
1313
import { Event } from "types/Event";
14-
import { ethers } from "ethers";
14+
import { BigNumber, ethers } from "ethers";
1515
import { reverse } from "lodash";
1616
import { EVENT_BLACK_LIST } from "src/constants/event";
1717
import { useGenerateProof, useHashPoseidon } from "./useSecretPhrase";
@@ -124,6 +124,7 @@ export const useCreateEvent = (address: string) => {
124124
generateProof,
125125
} = useGenerateProof();
126126
const provider = useSDK()?.getProvider();
127+
const { getGasFee } = useCalcMtxGasFee();
127128

128129
const {
129130
mutateAsync,
@@ -179,12 +180,7 @@ export const useCreateEvent = (address: string) => {
179180

180181
let value!: ethers.BigNumber;
181182
if (params.useMtx) {
182-
const gasPrice = (await provider.getGasPrice())?.toNumber();
183-
value = ethers.utils.parseEther(
184-
`${
185-
gasPrice * params.mintLimit * 560220 * 2.1 * 0.000000000000000001
186-
}`
187-
);
183+
value = (await getGasFee(params.mintLimit)) || BigNumber.from(0);
188184
}
189185

190186
await mutateAsync({
@@ -206,7 +202,7 @@ export const useCreateEvent = (address: string) => {
206202
});
207203
} catch (_) {}
208204
},
209-
[mutateAsync, provider]
205+
[mutateAsync, provider, getGasFee]
210206
);
211207

212208
return {
@@ -246,3 +242,37 @@ export const useEventById = (id: number) => {
246242

247243
return { event, isLoading, error };
248244
};
245+
246+
export const useCalcMtxGasFee = (mintLimit?: number) => {
247+
const provider = useSDK()?.getProvider();
248+
const [gasFee, setGasFee] = useState<BigNumber | null>(null);
249+
250+
useEffect(() => {
251+
const fetch = async () => {
252+
if (!provider || !mintLimit) return;
253+
254+
const gasPrice = (await provider.getGasPrice())?.toNumber();
255+
const value = ethers.utils.parseEther(
256+
`${gasPrice * mintLimit * 660000 * 1 * 0.000000000000000001}`
257+
);
258+
setGasFee(value);
259+
};
260+
261+
fetch();
262+
}, [provider, mintLimit]);
263+
264+
const getGasFee = useCallback(
265+
async (_mintLimit: number) => {
266+
if (!provider) return;
267+
268+
const gasPrice = (await provider.getGasPrice())?.toNumber();
269+
const value = ethers.utils.parseEther(
270+
`${gasPrice * _mintLimit * 660000 * 1 * 0.000000000000000001}`
271+
);
272+
return value;
273+
},
274+
[provider]
275+
);
276+
277+
return { gasFee, getGasFee };
278+
};

frontend/src/locales/en.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -36,6 +36,7 @@ export default {
3636
EVENT_USE_MTX: "Taking on gas fee for participants",
3737
EVENT_USE_MTX_TRUE: "Yes",
3838
EVENT_USE_MTX_FALSE: "No",
39+
EVENT_ESTIMATED_GAS_MTX: "Estimated deposit amount required to take on",
3940
EVENT_SECRETPHRASE: "SecretPhrase to mint",
4041
EVENT_SECRETPHRASE_DESC:
4142
"Please do not forget this phrase. you can't get this phrase after submitting",

frontend/src/locales/ja.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -36,6 +36,7 @@ export default {
3636
"ガス代を肩代わりして、参加者が無料でNFTを受け取れるようにする。",
3737
EVENT_USE_MTX_TRUE: "肩代わりする",
3838
EVENT_USE_MTX_FALSE: "肩代わりしない",
39+
EVENT_ESTIMATED_GAS_MTX: "肩代わりに必要な予想デポジット金額",
3940
EVENT_SECRETPHRASE: "NFT受け取りのひみつの「あいことば」",
4041
EVENT_SECRETPHRASE_DESC:
4142
"ひみつの「あいことば」は忘れないようにしてください。あとから確認することはできません。",

0 commit comments

Comments
 (0)