Skip to content

Commit

Permalink
feat: store nonfngable manager against position
Browse files Browse the repository at this point in the history
  • Loading branch information
jackmellis committed Jan 8, 2024
1 parent 1aefb1a commit 4d69dff
Show file tree
Hide file tree
Showing 6 changed files with 29 additions and 8 deletions.
Original file line number Diff line number Diff line change
@@ -1,23 +1,22 @@
import { NonfungiblePositionManager } from '@nftx/abi';
import { NONFUNGIBLE_POSITION_MANAGER, Zero } from '@nftx/constants';
import type { Provider, TokenId } from '@nftx/types';
import { getChainConstant } from '@nftx/utils';
import { Zero } from '@nftx/constants';
import type { Address, Provider, TokenId } from '@nftx/types';

const MAX_UINT128 = 340282366920938463463374607431768211455n;

const fetchClaimableAmount = async ({
network,
tokenId,
provider,
manager,
}: {
provider: Provider;
network: number;
tokenId: TokenId;
manager: Address;
}): Promise<[bigint, bigint]> => {
try {
const ownerAddress = await provider.readContract({
abi: NonfungiblePositionManager,
address: getChainConstant(NONFUNGIBLE_POSITION_MANAGER, network),
address: manager,
functionName: 'ownerOf',
args: [BigInt(tokenId)],
});
Expand All @@ -26,7 +25,7 @@ const fetchClaimableAmount = async ({
result: [amount0, amount1],
} = await provider.simulateContract({
abi: NonfungiblePositionManager,
address: getChainConstant(NONFUNGIBLE_POSITION_MANAGER, network),
address: manager,
functionName: 'collect',
args: [
{
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ import { addressEqual } from '@nftx/utils';
import transformPosition from './transformPosition';
import { NotFoundError } from '@nftx/errors';
import fetchClaimableAmount from './fetchClaimableAmount';
import getManager from './getManager';

const getVaultByTokens = <V extends Pick<Vault, 'id'>>({
inputTokens,
Expand Down Expand Up @@ -64,9 +65,9 @@ const fetchPositionsSet = async ({
vaults,
});
const [claimable0, claimable1] = await fetchClaimableAmount({
network,
tokenId: position.tokenId as TokenId,
provider,
manager: getManager(network, position.timestampOpened),
});
return transformPosition({
network,
Expand Down
14 changes: 14 additions & 0 deletions packages/core/src/positions/fetchLiquidityPositions/getManager.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
import { NONFUNGIBLE_POSITION_MANAGER, Network } from '@nftx/constants';
import { getChainConstant } from '@nftx/utils';

const getManager = (network: number, timestampOpened: string) => {
const lastTimeOldContractUsed = 1704206700;
const timestamp = Number(timestampOpened);
// If older than this, it's the old contract
if (timestamp <= lastTimeOldContractUsed && network === Network.Sepolia) {
return '0x55bdc76262b1e6e791d0636a0bc61cee23cdfa87';
}
return getChainConstant(NONFUNGIBLE_POSITION_MANAGER, network);
};

export default getManager;
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,7 @@ const queryPositionData = ({
s.cumulativeDepositTokenAmounts,
s.cumulativeWithdrawTokenAmounts,
s.lockedUntil,
s.timestampOpened,
s.tickUpper((tick) => [tick.index]),
s.tickLower((tick) => [tick.index]),
s.pool((pool) => [
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ import type { PositionsResponse } from './types';
import { WETH_TOKEN, WeiPerEther, Zero } from '@nftx/constants';
import calculateVTokenEth from './calculateVTokenEth';
import { addressEqual, getChainConstant } from '@nftx/utils';
import getManager from './getManager';

type Position = PositionsResponse['positions'][0];

Expand Down Expand Up @@ -66,6 +67,8 @@ const transformPosition = ({
// TODO: get this from... somwhere?
const lifetimeRewards = Zero;

const manager = getManager(network, position.timestampOpened);

return {
id: position.id as Address,
poolId: position.pool.id as Address,
Expand All @@ -90,6 +93,7 @@ const transformPosition = ({
poolShare: Zero,
initialValue: value,
lockedUntil,
manager,
};
};

Expand Down
2 changes: 2 additions & 0 deletions packages/types/src/positions.ts
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,8 @@ export type LiquidityPosition = {
/** The amount claimable on this position in ETH */
claimableValue: bigint;
lockedUntil: number;
/** The address of the manager contract for this position */
manager: Address;
};

/** A user's Inventory Position, essentially a single xNFT */
Expand Down

0 comments on commit 4d69dff

Please sign in to comment.