Skip to content

Commit

Permalink
Fix calculations and add prediction when fail happens
Browse files Browse the repository at this point in the history
  • Loading branch information
CRBl69 committed Sep 5, 2024
1 parent b5dfff7 commit 2d5192a
Show file tree
Hide file tree
Showing 3 changed files with 54 additions and 32 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -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";
Expand All @@ -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]
Expand All @@ -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;
Expand All @@ -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,
Expand Down Expand Up @@ -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 {
Expand Down
2 changes: 2 additions & 0 deletions src/typescript/sdk/src/const.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down
27 changes: 18 additions & 9 deletions src/typescript/sdk/src/emojicoin_dot_fun/emojicoin-dot-fun.ts
Original file line number Diff line number Diff line change
Expand Up @@ -298,9 +298,12 @@ export class RegisterMarket extends EntryFunctionPayloadBuilder {
registrant: AccountAddressInput; // &signer
registrantPubKey: PublicKey;
emojis: Array<HexInput>; // vector<vector<u8>>
}): 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,
Expand All @@ -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: {
Expand Down

0 comments on commit 2d5192a

Please sign in to comment.