-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #1208 from madfish-solutions/v3.2.12
V3.2.12
- Loading branch information
Showing
106 changed files
with
1,888 additions
and
438 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
161 changes: 161 additions & 0 deletions
161
src/modules/liquidity/api/blockchain/v3-liquidity-pool.api.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,161 @@ | ||
import { MichelsonMapKey } from '@taquito/michelson-encoder'; | ||
import { TezosToolkit } from '@taquito/taquito'; | ||
import BigNumber from 'bignumber.js'; | ||
|
||
import { sendBatch } from '@blockchain'; | ||
import { QUIPUSWAP_REFERRAL_CODE, ZERO_AMOUNT_BN } from '@config/constants'; | ||
import { DEX_V3_FACTORY_ADDRESS } from '@config/environment'; | ||
import { getContract, getStorageInfo } from '@shared/dapp'; | ||
import { bigNumberToString, defined, getUniqArray, isExist, getTransactionDeadline } from '@shared/helpers'; | ||
import { address, BigMap, int, nat, TokensValue, WithId } from '@shared/types'; | ||
|
||
import { FeeGrowth } from '../../types'; | ||
|
||
export namespace V3LiquidityPoolApi { | ||
export interface V3PoolTick { | ||
fee_growth_outside: FeeGrowth; | ||
liqudity_net: int; | ||
n_positions: nat; | ||
next: int; | ||
prev: int; | ||
seconds_outside: nat; | ||
seconds_per_liquidity_outside: nat; | ||
sqrt_price: nat; | ||
tick_cumulative_outside: nat; | ||
} | ||
|
||
export interface V3PoolPosition { | ||
fee_growth_inside_last: FeeGrowth; | ||
lower_tick_index: int; | ||
upper_tick_index: int; | ||
owner: string; | ||
liquidity: int; | ||
} | ||
|
||
type V3PoolPositionWithId = WithId<V3PoolPosition, nat>; | ||
|
||
export interface V3PoolStorage { | ||
constants: { | ||
ctez_burn_fee_bps: nat; | ||
dev_fee_bps: nat; | ||
factory_address: nat; | ||
fee_bps: nat; | ||
tick_spacing: nat; | ||
token_x: TokensValue; | ||
token_y: TokensValue; | ||
}; | ||
sqrt_price: nat; | ||
liquidity: nat; | ||
cur_tick_index: int; | ||
fee_growth: FeeGrowth; | ||
new_position_id: nat; | ||
positions: BigMap<nat, V3PoolPosition>; | ||
position_ids: BigMap<address, nat[]>; | ||
ticks: BigMap<int, V3PoolTick>; | ||
} | ||
|
||
export interface PositionWithTicks extends Omit<V3PoolPosition, 'lower_tick_index' | 'upper_tick_index'> { | ||
lower_tick: WithId<V3PoolTick, int>; | ||
upper_tick: WithId<V3PoolTick, int>; | ||
id: nat; | ||
} | ||
|
||
interface V3FactoryStorage { | ||
pools: BigMap<nat, address>; | ||
} | ||
|
||
export const getPool = async (tezos: TezosToolkit, id: BigNumber) => { | ||
const factoryStorage = await getStorageInfo<V3FactoryStorage>(tezos, DEX_V3_FACTORY_ADDRESS); | ||
const contractAddress = defined(await factoryStorage.pools.get(id), 'contractAddress'); | ||
|
||
return { | ||
contractAddress, | ||
storage: await getStorageInfo<V3PoolStorage>(tezos, contractAddress) | ||
}; | ||
}; | ||
|
||
export const claimFees = async ( | ||
tezos: TezosToolkit, | ||
contractAddress: string, | ||
positionsIds: BigNumber[], | ||
accountPkh: string, | ||
transactionDuration: BigNumber | ||
) => { | ||
const contract = await getContract(tezos, contractAddress); | ||
const transactionDeadline = await getTransactionDeadline(tezos, transactionDuration); | ||
|
||
return await sendBatch( | ||
tezos, | ||
positionsIds.map(id => | ||
contract.methods | ||
.update_position( | ||
id, | ||
ZERO_AMOUNT_BN, | ||
accountPkh, | ||
accountPkh, | ||
transactionDeadline, | ||
ZERO_AMOUNT_BN, | ||
ZERO_AMOUNT_BN, | ||
QUIPUSWAP_REFERRAL_CODE | ||
) | ||
.toTransferParams() | ||
) | ||
); | ||
}; | ||
|
||
const getUserPositionsIds = async (contractStorage: V3PoolStorage, accountPkh: string) => { | ||
const { position_ids } = contractStorage; | ||
|
||
return (await position_ids.get(accountPkh)) ?? []; | ||
}; | ||
|
||
const getPositions = async (contractStorage: V3PoolStorage, ids: BigNumber[]): Promise<V3PoolPositionWithId[]> => { | ||
const { positions } = contractStorage; | ||
|
||
const positionsMap = await positions.getMultipleValues(ids); | ||
|
||
return Array.from(positionsMap.entries()) | ||
.filter((entry): entry is [MichelsonMapKey, V3PoolPosition] => { | ||
const [, value] = entry; | ||
|
||
return isExist(value); | ||
}) | ||
.map(([id, position]) => ({ | ||
...position, | ||
id: id as BigNumber | ||
})); | ||
}; | ||
|
||
const getPositionsTicksMap = async (contractStorage: V3PoolStorage, positions: V3PoolPosition[]) => { | ||
const ticksIds = getUniqArray( | ||
positions.map(({ lower_tick_index, upper_tick_index }) => [lower_tick_index, upper_tick_index]).flat(), | ||
bigNumberToString | ||
); | ||
|
||
return await contractStorage.ticks.getMultipleValues(ticksIds); | ||
}; | ||
|
||
export const getUserPositionsWithTicks = async ( | ||
tezos: TezosToolkit, | ||
accountPkh: string, | ||
poolId: BigNumber | ||
): Promise<PositionWithTicks[]> => { | ||
const { storage: contractStorage } = await getPool(tezos, poolId); | ||
const userPositionsIds = await getUserPositionsIds(contractStorage, accountPkh); | ||
const userPositions = await getPositions(contractStorage, userPositionsIds); | ||
|
||
const ticksMap = await getPositionsTicksMap(contractStorage, userPositions); | ||
|
||
return userPositions.map(({ lower_tick_index, upper_tick_index, ...userPosition }) => ({ | ||
...userPosition, | ||
lower_tick: { | ||
...defined(ticksMap.get(lower_tick_index), `tick ${lower_tick_index.toFixed()}`), | ||
id: lower_tick_index | ||
}, | ||
upper_tick: { | ||
...defined(ticksMap.get(upper_tick_index), `tick ${upper_tick_index.toFixed()}`), | ||
id: upper_tick_index | ||
} | ||
})); | ||
}; | ||
} |
This file was deleted.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,8 +1,14 @@ | ||
import BigNumber from 'bignumber.js'; | ||
|
||
import { isExist } from '@shared/helpers'; | ||
import { Optional } from '@shared/types'; | ||
|
||
export const calculateV3ItemTvl = ( | ||
tokenXBalance: BigNumber, | ||
tokenYBalance: BigNumber, | ||
tokenXExchangeRate: BigNumber, | ||
tokenYExchangeRate: BigNumber | ||
) => tokenXBalance.multipliedBy(tokenXExchangeRate).plus(tokenYBalance.multipliedBy(tokenYExchangeRate)); | ||
tokenXExchangeRate: Optional<BigNumber>, | ||
tokenYExchangeRate: Optional<BigNumber> | ||
) => | ||
isExist(tokenXExchangeRate) && isExist(tokenYExchangeRate) | ||
? tokenXBalance.multipliedBy(tokenXExchangeRate).plus(tokenYBalance.multipliedBy(tokenYExchangeRate)) | ||
: null; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,9 @@ | ||
import BigNumber from 'bignumber.js'; | ||
|
||
import { DEFAULT_TOKEN_ID } from '@config/constants'; | ||
import { isEqual } from '@shared/helpers'; | ||
|
||
import { oppositeCurrentPrice } from '../pages/v3-item-page/helpers'; | ||
|
||
export const getCurrentPrice = (currentPrice: BigNumber, activeTokenIndex: number) => | ||
isEqual(DEFAULT_TOKEN_ID, activeTokenIndex) ? currentPrice : oppositeCurrentPrice(currentPrice); |
9 changes: 9 additions & 0 deletions
9
src/modules/liquidity/helpers/get-symbols-string-by-active-token.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,9 @@ | ||
import { DEFAULT_TOKEN_ID } from '@config/constants'; | ||
import { getSymbolsString, isEqual, RawOrMappedToken } from '@shared/helpers'; | ||
import { Nullable } from '@shared/types'; | ||
|
||
export const getSymbolsStringByActiveToken = (tokens: Array<Nullable<RawOrMappedToken>>, activeTokenIndex: number) => { | ||
const tokenDirection = isEqual(DEFAULT_TOKEN_ID, activeTokenIndex) ? tokens : tokens.reverse(); | ||
|
||
return getSymbolsString(tokenDirection); | ||
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.