Skip to content
This repository has been archived by the owner on Feb 23, 2024. It is now read-only.

Commit

Permalink
fix: get current blockheight to add on deadline (#342)
Browse files Browse the repository at this point in the history
* feat: get current blockheight

* fix: add await on get deadline

* chore: fix lint
  • Loading branch information
luizstacio committed Jun 23, 2022
1 parent 7a6473c commit 741db31
Show file tree
Hide file tree
Showing 7 changed files with 38 additions and 17 deletions.
2 changes: 1 addition & 1 deletion packages/app/src/config.ts
Expand Up @@ -18,7 +18,7 @@ export const SLIPPAGE_TOLERANCE = 0.005;
/** Small network fee */
export const NETWORK_FEE = 1;
/** Default deadline */
export const DEADLINE = 99999999999;
export const DEADLINE = 1000;
/** Max presentation units to avoid show 9 decimals on screen */
export const FIXED_UNITS = 3;
/** Min gas price required from the fuel-core */
Expand Down
7 changes: 5 additions & 2 deletions packages/app/src/systems/Core/hooks/useTransactionCost.ts
Expand Up @@ -7,9 +7,12 @@ import { emptyTransactionCost, getTransactionCost } from '../utils/gas';

import { useEthBalance } from './useEthBalance';

type ContractCallFuncPromise = () => Promise<ContractCall>;
type ContractCallFunc = () => ContractCall;

export function useTransactionCost(
queryKey: unknown[],
request: () => ContractCall,
request: ContractCallFunc | ContractCallFuncPromise,
options?: Omit<UseQueryOptions<TransactionCost>, 'queryKey' | 'queryFn'>
) {
const ethBalance = useEthBalance();
Expand All @@ -20,7 +23,7 @@ export function useTransactionCost(

const { data } = useQuery<TransactionCost>(
queryKey,
async () => getTransactionCost(request()),
async () => getTransactionCost(await request()),
options
);

Expand Down
11 changes: 11 additions & 0 deletions packages/app/src/systems/Core/utils/chain.ts
@@ -0,0 +1,11 @@
import type { Contract } from 'fuels';

import { toBigInt } from './math';

import { DEADLINE } from '~/config';

export async function getDeadline(contract: Contract, deadline?: bigint) {
const blockHeight = await contract.wallet!.provider.getBlockNumber();
const nexDeadline = blockHeight + (deadline || toBigInt(DEADLINE));
return nexDeadline;
}
1 change: 1 addition & 0 deletions packages/app/src/systems/Core/utils/index.ts
Expand Up @@ -4,3 +4,4 @@ export * from './constants';
export * from './math';
export * from './queryClient';
export * from './helpers';
export * from './chain';
6 changes: 3 additions & 3 deletions packages/app/src/systems/Pool/hooks/useAddLiquidity.ts
Expand Up @@ -5,9 +5,8 @@ import { useNavigate } from 'react-router-dom';

import { useUserPositions } from './useUserPositions';

import { DEADLINE } from '~/config';
import type { UseCoinInput } from '~/systems/Core';
import { useContract, toBigInt } from '~/systems/Core';
import { getDeadline, useContract, toBigInt } from '~/systems/Core';
import { getOverrides } from '~/systems/Core/utils/gas';
import type { Coin } from '~/types';

Expand Down Expand Up @@ -74,9 +73,10 @@ export function useAddLiquidity({
);
setStage(2);
// Create liquidity pool
const deadline = await getDeadline(contract);
const liquidityTokens = await contract.submit.add_liquidity(
1,
DEADLINE,
deadline,
getOverrides({
variableOutputs: 2,
gasLimit: toBigInt(1500000),
Expand Down
11 changes: 7 additions & 4 deletions packages/app/src/systems/Pool/utils/queries.ts
@@ -1,15 +1,17 @@
import type { Contract } from 'fuels';

import { CONTRACT_ID, DEADLINE } from '~/config';
import { CONTRACT_ID } from '~/config';
import { getDeadline } from '~/systems/Core';
import type { TransactionCost } from '~/systems/Core/utils/gas';
import { getOverrides } from '~/systems/Core/utils/gas';

export enum PoolQueries {
RemoveLiquidityNetworkFee = 'RemoveLiquidity-networkFee',
}

export function prepareRemoveLiquidity(contract: Contract) {
return contract.prepareCall.remove_liquidity(1, 1, DEADLINE, {
export async function prepareRemoveLiquidity(contract: Contract) {
const deadline = await getDeadline(contract);
return contract.prepareCall.remove_liquidity(1, 1, deadline, {
forward: [1, CONTRACT_ID],
variableOutputs: 2,
gasLimit: 100_000_000,
Expand All @@ -21,10 +23,11 @@ export async function submitRemoveLiquidity(
amount: bigint,
txCost: TransactionCost
) {
const deadline = await getDeadline(contract);
return contract.submit.remove_liquidity(
1,
1,
DEADLINE,
deadline,
getOverrides({
forward: [amount, CONTRACT_ID],
variableOutputs: 2,
Expand Down
17 changes: 10 additions & 7 deletions packages/app/src/systems/Swap/utils/queries.ts
@@ -1,8 +1,7 @@
import type { SwapState } from '../types';
import { ActiveInput } from '../types';

import { DEADLINE } from '~/config';
import { COIN_ETH } from '~/systems/Core';
import { COIN_ETH, getDeadline } from '~/systems/Core';
import type { TransactionCost } from '~/systems/Core/utils/gas';
import { getOverrides } from '~/systems/Core/utils/gas';
import type { ExchangeContractAbi } from '~/types/contracts';
Expand Down Expand Up @@ -58,6 +57,8 @@ export const swapTokens = async (
{ coinFrom, direction, amount }: SwapState,
txCost: TransactionCost
) => {
const deadline = await getDeadline(contract);

if (direction === ActiveInput.to && amount) {
const forwardAmount = await getSwapWithMaximumRequiredAmount(
contract,
Expand All @@ -69,7 +70,7 @@ export const swapTokens = async (
}
return contract.submitResult.swap_with_maximum(
amount,
DEADLINE,
deadline,
getOverrides({
forward: [forwardAmount.amount, coinFrom.assetId],
gasLimit: txCost.total,
Expand All @@ -85,7 +86,7 @@ export const swapTokens = async (

return contract.submitResult.swap_with_minimum(
minValue.amount,
DEADLINE,
deadline,
getOverrides({
forward: [amount, coinFrom.assetId],
gasLimit: txCost.total,
Expand All @@ -95,16 +96,18 @@ export const swapTokens = async (
}
};

export const queryNetworkFee = (contract: ExchangeContractAbi, direction?: ActiveInput) => {
export const queryNetworkFee = async (contract: ExchangeContractAbi, direction?: ActiveInput) => {
const directionValue = direction || ActiveInput.from;
const deadline = await getDeadline(contract);

if (directionValue === ActiveInput.to) {
return contract.prepareCall.swap_with_maximum(1, DEADLINE, {
return contract.prepareCall.swap_with_maximum(1, deadline, {
forward: [1, COIN_ETH],
variableOutputs: 2,
gasLimit: 1000000,
});
}
return contract.prepareCall.swap_with_minimum(1, DEADLINE, {
return contract.prepareCall.swap_with_minimum(1, deadline, {
forward: [1, COIN_ETH],
variableOutputs: 1,
gasLimit: 1000000,
Expand Down

0 comments on commit 741db31

Please sign in to comment.