Skip to content

Commit

Permalink
Merge pull request #1354 from madfish-solutions/QUIPU-842-implement-c…
Browse files Browse the repository at this point in the history
…reating-stableswap-pools-with-tez

Implement working with stableswap pools with tez
  • Loading branch information
keshan3262 authored Mar 3, 2023
2 parents fccffe1 + fe20caf commit c5d16ad
Show file tree
Hide file tree
Showing 7 changed files with 66 additions and 26 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -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';
Expand Down Expand Up @@ -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') });
Expand Down
22 changes: 16 additions & 6 deletions src/modules/stableswap/api/add-stableswap-liquidity.api.ts
Original file line number Diff line number Diff line change
@@ -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,
Expand All @@ -16,6 +19,7 @@ export const addStableswapLiquidityApi = async (
accountPkh: string,
receiver: Nullable<string> = null
) => {
const mutezToMint = getTotalTokenAmount(tokensAndAmounts, TEZOS_TOKEN);
const receiverFixed = accountPkh === receiver ? null : receiver;
const stableswapPoolContract = await tezos.wallet.at(stableswapPoolContractAddress);

Expand All @@ -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])
);
};
27 changes: 17 additions & 10 deletions src/modules/stableswap/api/create-stableswap-pool.api.ts
Original file line number Diff line number Diff line change
@@ -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;
Expand Down Expand Up @@ -62,12 +64,13 @@ const prepareNewPoolData = (creationParams: Array<CreationParams>, fees: Fees, c
token: QUIPU_TOKEN,
amount: creationPrice ? toAtomic(creationPrice, QUIPU_TOKEN) : new BigNumber('1')
}
].filter(({ amount }) => isGreaterThanZero(amount));
];
const tokensInfo = new MichelsonMap<BigNumber, TokenInfo>();

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 }));
});

Expand All @@ -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
Expand All @@ -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])
);
};
15 changes: 10 additions & 5 deletions src/modules/stableswap/api/remove-stableswap-liquidity.api.ts
Original file line number Diff line number Diff line change
@@ -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<AmountToken & Partial<{ index: number }>>) => {
const michelsonAmounts = new MichelsonMap<number, BigNumber>();

Expand All @@ -23,15 +26,16 @@ export const removeStableswapLiquidityBalancedApi = async (
accountPkh: string,
receiver: Nullable<string> = 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 (
Expand All @@ -43,11 +47,12 @@ export const removeStableswapLiquidityImbalancedApi = async (
accountPkh: string,
receiver: Nullable<string> = 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,
Expand All @@ -58,5 +63,5 @@ export const removeStableswapLiquidityImbalancedApi = async (
)
.toTransferParams();

return await sendBatch(tezos, [removeSwableswapLiquidityParams]);
return await withWtezBurnOnOutput(tezos, mutezToBurn, accountPkh, [removeStableswapLiquidityParams]);
};
7 changes: 7 additions & 0 deletions src/modules/stableswap/helpers/get-total-token-amount.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
import { getSumOfNumbers, isTokenEqual } from '@shared/helpers';
import { AmountToken, Token } from '@shared/types';

export const getTotalTokenAmount = (tokensAndAmounts: Array<AmountToken>, token: Token) =>
getSumOfNumbers(
tokensAndAmounts.filter(({ token: currentToken }) => isTokenEqual(token, currentToken)).map(({ amount }) => amount)
);
1 change: 1 addition & 0 deletions src/modules/stableswap/helpers/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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';
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand Down Expand Up @@ -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
});
Expand Down

0 comments on commit c5d16ad

Please sign in to comment.