diff --git a/src/modules/liquidity/pages/v3-item-page/components/forms/hooks/use-v3-remove-liquidity.ts b/src/modules/liquidity/pages/v3-item-page/components/forms/hooks/use-v3-remove-liquidity.ts index 0003f5539..90267b6b5 100644 --- a/src/modules/liquidity/pages/v3-item-page/components/forms/hooks/use-v3-remove-liquidity.ts +++ b/src/modules/liquidity/pages/v3-item-page/components/forms/hooks/use-v3-remove-liquidity.ts @@ -8,7 +8,13 @@ import { } from '@modules/liquidity/hooks'; import { useRootStore } from '@providers/root-store-provider'; import { useAccountPkh } from '@providers/use-dapp'; -import { getTransactionDeadline, isNull, getPercentageFromNumber, toAtomic } from '@shared/helpers'; +import { + getTransactionDeadline, + isNull, + getPercentageFromNumber, + toAtomic, + decreaseByPercentage +} from '@shared/helpers'; import { useSettingsStore } from '@shared/hooks/use-settings-store'; import { amplitudeService } from '@shared/services'; import { useConfirmOperation, useToasts } from '@shared/utils'; @@ -70,8 +76,12 @@ export const useV3RemoveLiquidity = () => { deadline, tokenX, tokenY, - toAtomic(new BigNumber(tokenXOutput), tokenX), - toAtomic(new BigNumber(tokenYOutput), tokenY), + toAtomic(decreaseByPercentage(new BigNumber(tokenXOutput), liquiditySlippage), tokenX).integerValue( + BigNumber.ROUND_DOWN + ), + toAtomic(decreaseByPercentage(new BigNumber(tokenYOutput), liquiditySlippage), tokenY).integerValue( + BigNumber.ROUND_DOWN + ), mutezToBurn ); await confirmOperation(operation.opHash, { message: t('liquidity|successfullyRemoved') }); diff --git a/src/modules/stableswap/api/add-stableswap-liquidity.api.ts b/src/modules/stableswap/api/add-stableswap-liquidity.api.ts index f1b865009..c8d0d2ce1 100644 --- a/src/modules/stableswap/api/add-stableswap-liquidity.api.ts +++ b/src/modules/stableswap/api/add-stableswap-liquidity.api.ts @@ -1,12 +1,15 @@ import { TezosToolkit, MichelsonMap } from '@taquito/taquito'; import { BigNumber } from 'bignumber.js'; -import { withApproveApiForManyTokens } from '@blockchain'; +import { getWithWtezMintOnInputParams, withApproveApiForManyTokens } from '@blockchain'; import { STABLESWAP_REFERRAL } from '@config/config'; import { DEFAULT_STABLESWAP_POOL_ID } from '@config/constants'; -import { isGreaterThanZero } from '@shared/helpers'; +import { TEZOS_TOKEN, WTEZ_TOKEN } from '@config/tokens'; +import { isGreaterThanZero, isTezosToken } from '@shared/helpers'; import { AmountToken, Nullable } from '@shared/types'; +import { getTotalTokenAmount } from '../helpers'; + export const addStableswapLiquidityApi = async ( tezos: TezosToolkit, stableswapPoolContractAddress: string, @@ -16,6 +19,7 @@ export const addStableswapLiquidityApi = async ( accountPkh: string, receiver: Nullable = null ) => { + const mutezToMint = getTotalTokenAmount(tokensAndAmounts, TEZOS_TOKEN); const receiverFixed = accountPkh === receiver ? null : receiver; const stableswapPoolContract = await tezos.wallet.at(stableswapPoolContractAddress); @@ -30,9 +34,15 @@ export const addStableswapLiquidityApi = async ( .invest(DEFAULT_STABLESWAP_POOL_ID, shares, michelsonAmounts, deadline, receiverFixed, STABLESWAP_REFERRAL) .toTransferParams(); - const cleanedTokensAmount = tokensAndAmounts.filter(({ amount }) => isGreaterThanZero(amount)); + const cleanedTokensAmount = tokensAndAmounts + .filter(({ amount }) => isGreaterThanZero(amount)) + .map(({ token, amount }) => ({ token: isTezosToken(token) ? WTEZ_TOKEN : token, amount })); - return await withApproveApiForManyTokens(tezos, stableswapPoolContractAddress, cleanedTokensAmount, accountPkh, [ - swableswapLiquidityParams - ]); + return await withApproveApiForManyTokens( + tezos, + stableswapPoolContractAddress, + cleanedTokensAmount, + accountPkh, + await getWithWtezMintOnInputParams(tezos, mutezToMint, accountPkh, [swableswapLiquidityParams]) + ); }; diff --git a/src/modules/stableswap/api/create-stableswap-pool.api.ts b/src/modules/stableswap/api/create-stableswap-pool.api.ts index e3b73f4a6..51e48fe38 100644 --- a/src/modules/stableswap/api/create-stableswap-pool.api.ts +++ b/src/modules/stableswap/api/create-stableswap-pool.api.ts @@ -1,13 +1,15 @@ import { MichelsonMap, TezosToolkit } from '@taquito/taquito'; import { BigNumber } from 'bignumber.js'; -import { withApproveApiForManyTokens } from '@blockchain'; +import { withApproveApiForManyTokens, getWithWtezMintOnInputParams } from '@blockchain'; import { STABLESWAP_REFERRAL } from '@config/config'; -import { QUIPU_TOKEN } from '@config/tokens'; -import { isGreaterThanZero, toAtomic } from '@shared/helpers'; +import { QUIPU_TOKEN, WTEZ_TOKEN } from '@config/tokens'; +import { isTezosToken, toAtomic } from '@shared/helpers'; import { mapTokensValue } from '@shared/mapping/map-token-value'; import { AmountToken, Nullable, Token, TokensValue } from '@shared/types'; +import { getTotalTokenAmount } from '../helpers'; + interface TokenInfo { rate_f: BigNumber; precision_multiplier_f: BigNumber; @@ -62,12 +64,13 @@ const prepareNewPoolData = (creationParams: Array, fees: Fees, c token: QUIPU_TOKEN, amount: creationPrice ? toAtomic(creationPrice, QUIPU_TOKEN) : new BigNumber('1') } - ].filter(({ amount }) => isGreaterThanZero(amount)); + ]; const tokensInfo = new MichelsonMap(); creationParams.forEach(({ token, reserves, rateF, precisionMultiplierF }, index) => { - inputTokens.push(mapTokensValue(token)); - amountTokenList.push(mapToAmountToken({ token, reserves })); + const wrappedToken = isTezosToken(token) ? WTEZ_TOKEN : token; + inputTokens.push(mapTokensValue(wrappedToken)); + amountTokenList.push(mapToAmountToken({ token: wrappedToken, reserves })); tokensInfo.set(new BigNumber(index), mapTokensInfo({ reserves, rateF, precisionMultiplierF })); }); @@ -93,6 +96,7 @@ export const createStableswapPoolApi = async ( fees, creationPrice ); + const mutezToMint = getTotalTokenAmount(amountTokenList, WTEZ_TOKEN); const stableswapPoolContract = await tezos.wallet.at(stableswapFactoryContractAddress); const addPoolTransferParams = stableswapPoolContract.methods @@ -116,8 +120,11 @@ export const createStableswapPoolApi = async ( .start_dex(inputs) .toTransferParams({ storageLimit: 60000 }); - return await withApproveApiForManyTokens(tezos, stableswapFactoryContractAddress, amountTokenList, accountPkh, [ - addPoolTransferParams, - startDexTransferParams - ]); + return await withApproveApiForManyTokens( + tezos, + stableswapFactoryContractAddress, + amountTokenList, + accountPkh, + await getWithWtezMintOnInputParams(tezos, mutezToMint, accountPkh, [addPoolTransferParams, startDexTransferParams]) + ); }; diff --git a/src/modules/stableswap/api/remove-stableswap-liquidity.api.ts b/src/modules/stableswap/api/remove-stableswap-liquidity.api.ts index c64460d84..4361661a5 100644 --- a/src/modules/stableswap/api/remove-stableswap-liquidity.api.ts +++ b/src/modules/stableswap/api/remove-stableswap-liquidity.api.ts @@ -1,11 +1,14 @@ import { MichelsonMap, TezosToolkit } from '@taquito/taquito'; import { BigNumber } from 'bignumber.js'; -import { sendBatch } from '@blockchain'; +import { withWtezBurnOnOutput } from '@blockchain'; import { STABLESWAP_REFERRAL } from '@config/config'; import { DEFAULT_STABLESWAP_POOL_ID } from '@config/constants'; +import { TEZOS_TOKEN } from '@config/tokens'; import { AmountToken, Nullable } from '@shared/types'; +import { getTotalTokenAmount } from '../helpers'; + const createMichelsonMap = (tokensAndAmounts: Array>) => { const michelsonAmounts = new MichelsonMap(); @@ -23,15 +26,16 @@ export const removeStableswapLiquidityBalancedApi = async ( accountPkh: string, receiver: Nullable = null ) => { + const mutezToBurn = getTotalTokenAmount(tokensAndAmounts, TEZOS_TOKEN); const receiverFixed = accountPkh === receiver ? null : receiver; const stableswapPoolContract = await tezos.wallet.at(stableswapPoolContractAddress); const michelsonAmounts = createMichelsonMap(tokensAndAmounts); - const removeSwableswapLiquidityParams = stableswapPoolContract.methods + const removeStableswapLiquidityParams = stableswapPoolContract.methods .divest(DEFAULT_STABLESWAP_POOL_ID, michelsonAmounts, shares, deadline, receiverFixed) .toTransferParams(); - return await sendBatch(tezos, [removeSwableswapLiquidityParams]); + return await withWtezBurnOnOutput(tezos, mutezToBurn, accountPkh, [removeStableswapLiquidityParams]); }; export const removeStableswapLiquidityImbalancedApi = async ( @@ -43,11 +47,12 @@ export const removeStableswapLiquidityImbalancedApi = async ( accountPkh: string, receiver: Nullable = null ) => { + const mutezToBurn = getTotalTokenAmount(tokensAndAmounts, TEZOS_TOKEN); const receiverFixed = accountPkh === receiver ? null : receiver; const stableswapPoolContract = await tezos.wallet.at(stableswapPoolContractAddress); const michelsonAmounts = createMichelsonMap(tokensAndAmounts); - const removeSwableswapLiquidityParams = stableswapPoolContract.methods + const removeStableswapLiquidityParams = stableswapPoolContract.methods .divest_imbalanced( DEFAULT_STABLESWAP_POOL_ID, michelsonAmounts, @@ -58,5 +63,5 @@ export const removeStableswapLiquidityImbalancedApi = async ( ) .toTransferParams(); - return await sendBatch(tezos, [removeSwableswapLiquidityParams]); + return await withWtezBurnOnOutput(tezos, mutezToBurn, accountPkh, [removeStableswapLiquidityParams]); }; diff --git a/src/modules/stableswap/helpers/get-total-token-amount.ts b/src/modules/stableswap/helpers/get-total-token-amount.ts new file mode 100644 index 000000000..ab9a1bce5 --- /dev/null +++ b/src/modules/stableswap/helpers/get-total-token-amount.ts @@ -0,0 +1,7 @@ +import { getSumOfNumbers, isTokenEqual } from '@shared/helpers'; +import { AmountToken, Token } from '@shared/types'; + +export const getTotalTokenAmount = (tokensAndAmounts: Array, token: Token) => + getSumOfNumbers( + tokensAndAmounts.filter(({ token: currentToken }) => isTokenEqual(token, currentToken)).map(({ amount }) => amount) + ); diff --git a/src/modules/stableswap/helpers/index.ts b/src/modules/stableswap/helpers/index.ts index b5ffdef91..69bef54cf 100644 --- a/src/modules/stableswap/helpers/index.ts +++ b/src/modules/stableswap/helpers/index.ts @@ -4,4 +4,5 @@ export * from './create-amount-michelson-map'; export * from './formik'; export * from './get-input-slug-by-index'; export * from './get-stableswap-title'; +export * from './get-total-token-amount'; export * from './apply-stableswap-fee'; diff --git a/src/modules/stableswap/stableswap-liquidity/pages/create/components/create-form/create-form.vm.tsx b/src/modules/stableswap/stableswap-liquidity/pages/create/components/create-form/create-form.vm.tsx index af3c3d373..040380e69 100644 --- a/src/modules/stableswap/stableswap-liquidity/pages/create/components/create-form/create-form.vm.tsx +++ b/src/modules/stableswap/stableswap-liquidity/pages/create/components/create-form/create-form.vm.tsx @@ -5,7 +5,7 @@ import { FormikHelpers, useFormik } from 'formik'; import { getUserTokenBalance } from '@blockchain'; import { ZERO_AMOUNT_BN } from '@config/constants'; -import { QUIPU_TOKEN, TEZOS_TOKEN } from '@config/tokens'; +import { QUIPU_TOKEN, WTEZ_TOKEN } from '@config/tokens'; import { CreationParams } from '@modules/stableswap/api'; import { useRootStore } from '@providers/root-store-provider'; import { @@ -137,7 +137,7 @@ export const useCreateFormViewModel = () => { const handleSelectTokensClick = useCallback(async () => { const chosenTokens = await chooseTokens({ tokens, - disabledTokens: [TEZOS_TOKEN], + disabledTokens: [WTEZ_TOKEN], min: MIN_QUANTITY_OF_TOKENS_IN_STABLEPOOL, max: MAX_QUANTITY_OF_TOKENS_IN_STABLEPOOL });