diff --git a/src/typescript/frontend/src/components/pages/launch-emojicoin/hooks/use-register-market.ts b/src/typescript/frontend/src/components/pages/launch-emojicoin/hooks/use-register-market.ts index 7c4b8ed74..1d3365212 100644 --- a/src/typescript/frontend/src/components/pages/launch-emojicoin/hooks/use-register-market.ts +++ b/src/typescript/frontend/src/components/pages/launch-emojicoin/hooks/use-register-market.ts @@ -7,7 +7,11 @@ import { type UserTransactionResponse, } from "@aptos-labs/ts-sdk"; import { INTEGRATOR_ADDRESS } from "lib/env"; -import { MARKET_REGISTRATION_FEE, ONE_APT } from "@sdk/const"; +import { + MARKET_REGISTRATION_FEE, + MARKET_REGISTRATION_GAS_ESTIMATION_FIRST, + MARKET_REGISTRATION_GAS_ESTIMATION_NOT_FIRST, +} from "@sdk/const"; import { useEmojiPicker } from "context/emoji-picker-context"; import { SYMBOL_DATA } from "@sdk/emoji_data"; import { useNumMarkets } from "lib/hooks/queries/use-num-markets"; @@ -24,8 +28,10 @@ export const useRegisterMarket = () => { const { data: numMarkets } = useNumMarkets(); - const { data: gas } = useQuery({ - queryKey: ["register-market-cost", numMarkets, account?.address], + const emojiBytes = emojis.map((e) => SYMBOL_DATA.byEmoji(e)!.bytes); + + const { data: gasResult } = useQuery({ + queryKey: ["register-market-cost", numMarkets, account?.address, emojiBytes], queryFn: async () => { const publicKey = new Ed25519PublicKey( typeof account!.publicKey === "string" ? account!.publicKey : account!.publicKey[0] @@ -34,17 +40,30 @@ export const useRegisterMarket = () => { aptosConfig: aptos.config, registrant: account!.address, registrantPubKey: publicKey, - emojis: - numMarkets === 0 - ? [SYMBOL_DATA.byName("Virgo")!.bytes] - : emojis.map((e) => SYMBOL_DATA.byEmoji(e)!.bytes), + emojis: numMarkets === 0 ? [SYMBOL_DATA.byName("Virgo")!.bytes] : emojiBytes, }); return r; }, staleTime: 1000, - enabled: numMarkets !== undefined && account !== null, + enabled: + numMarkets !== undefined && account !== null && (numMarkets === 0 || emojis.length > 0), }); + let amount: number, unitPrice: number; + + if (gasResult && !gasResult.error) { + amount = gasResult.data.amount; + unitPrice = gasResult.data.unitPrice; + } else { + // If numMarkets is undefined (request not completed yet), we are ok with displaying the bigger number. + // And in most cases (every time except for the first market), it will actually be the correct one. + amount = + numMarkets === 0 + ? MARKET_REGISTRATION_GAS_ESTIMATION_FIRST / 100 + : MARKET_REGISTRATION_GAS_ESTIMATION_NOT_FIRST / 100; + unitPrice = 100; + } + const registerMarket = async () => { if (!account) { return; @@ -56,17 +75,9 @@ export const useRegisterMarket = () => { const builderArgs = { aptosConfig: aptos.config, registrant: account.address, - emojis: emojis.map((e) => SYMBOL_DATA.byEmoji(e)!.bytes), + emojis: emojiBytes, integrator: INTEGRATOR_ADDRESS, }; - let amount: number, unitPrice: number; - if (gas) { - amount = gas.amount; - unitPrice = gas.unitPrice; - } else { - amount = ONE_APT / 100; - unitPrice = 100; - } const builderLambda = () => RegisterMarket.builder({ ...builderArgs, @@ -99,12 +110,12 @@ export const useRegisterMarket = () => { // By default, just consider that this is the price, since in 99.99% of cases, this will be the most accurate estimate. let cost: number = Number(MARKET_REGISTRATION_FEE); - if (gas !== undefined) { - if (numMarkets === 0) { - cost = gas.unitPrice * gas.amount; - } else { - cost += gas.unitPrice * gas.amount; - } + // If numMarkets is undefined (request not completed yet), we are ok with choosing the second option. + // And in most cases (every time except for the first market), it will actually be the correct one. + if (numMarkets === 0) { + cost = amount * unitPrice; + } else { + cost += amount * unitPrice; } return { diff --git a/src/typescript/sdk/src/const.ts b/src/typescript/sdk/src/const.ts index 828451e85..afacd6419 100644 --- a/src/typescript/sdk/src/const.ts +++ b/src/typescript/sdk/src/const.ts @@ -60,6 +60,8 @@ export const POOL_FEE_RATE_BPS = 25; export const MARKET_REGISTRATION_FEE = 100_000_000n; export const MARKET_REGISTRATION_DEPOSIT = 400_000_000n; export const MARKET_REGISTRATION_ESTIMATION = MARKET_REGISTRATION_FEE + MARKET_REGISTRATION_DEPOSIT; +export const MARKET_REGISTRATION_GAS_ESTIMATION_NOT_FIRST = 6000; +export const MARKET_REGISTRATION_GAS_ESTIMATION_FIRST = ONE_APT * 0.6; export enum StateTrigger { PACKAGE_PUBLICATION = 0, diff --git a/src/typescript/sdk/src/emojicoin_dot_fun/emojicoin-dot-fun.ts b/src/typescript/sdk/src/emojicoin_dot_fun/emojicoin-dot-fun.ts index 064a5b239..1d98de392 100644 --- a/src/typescript/sdk/src/emojicoin_dot_fun/emojicoin-dot-fun.ts +++ b/src/typescript/sdk/src/emojicoin_dot_fun/emojicoin-dot-fun.ts @@ -298,9 +298,12 @@ export class RegisterMarket extends EntryFunctionPayloadBuilder { registrant: AccountAddressInput; // &signer registrantPubKey: PublicKey; emojis: Array; // vector> - }): Promise<{amount: number, unitPrice: number}> { + }): Promise<{ data: { amount: number; unitPrice: number }; error: boolean }> { const { aptosConfig } = args; - const payloadBuilder = new this({...args, integrator: "0x0000000000000000000000000000000000000000000000000000000000000001"}); + const payloadBuilder = new this({ + ...args, + integrator: "0x0000000000000000000000000000000000000000000000000000000000000001", + }); const aptos = new Aptos(aptosConfig); const transaction = await aptos.transaction.build.simple({ sender: payloadBuilder.primarySender, @@ -310,14 +313,20 @@ export class RegisterMarket extends EntryFunctionPayloadBuilder { }, }); const [userTransactionResponse] = await aptos.transaction.simulate.simple({ - signerPublicKey: args.registrantPubKey, - transaction, - options: { - estimateGasUnitPrice: true, - estimateMaxGasAmount: true, - } + signerPublicKey: args.registrantPubKey, + transaction, + options: { + estimateGasUnitPrice: true, + estimateMaxGasAmount: true, + }, }); - return {amount: Number(userTransactionResponse.gas_used), unitPrice: Number(userTransactionResponse.gas_unit_price)}; + return { + data: { + amount: Number(userTransactionResponse.gas_used), + unitPrice: Number(userTransactionResponse.gas_unit_price), + }, + error: !userTransactionResponse.success, + }; } static async builder(args: {