Skip to content

Commit

Permalink
fixup! Feat/sepolia (#12)
Browse files Browse the repository at this point in the history
  • Loading branch information
jackmellis committed Nov 14, 2023
1 parent 53a4338 commit b3dc303
Show file tree
Hide file tree
Showing 7 changed files with 44 additions and 8 deletions.
4 changes: 2 additions & 2 deletions packages/constants/src/subgraph.ts
Original file line number Diff line number Diff line change
Expand Up @@ -87,11 +87,11 @@ export const NFTX_SUBGRAPH = {
[Network.Goerli]:
'https://api.thegraph.com/subgraphs/name/gundamdweeb/nftx-v3-vaults',
[Network.Sepolia]:
'https://api.thegraph.com/subgraphs/name/gundamdweeb/nftx-v3-sepolia',
'https://api.thegraph.com/subgraphs/name/nftx-project/nftx-v3-vaults-sepolia',
};

export const NFTX_UNISWAP_SUBGRAPH = {
[Network.Goerli]: `https://api.thegraph.com/subgraphs/name/gundamdweeb/nftx-amm`,
[Network.Sepolia]:
'https://api.thegraph.com/subgraphs/name/gundamdweeb/nftx-amm-sepolia',
'https://api.thegraph.com/subgraphs/name/nftx-project/nftx-v3-amm-sepolia',
};
8 changes: 7 additions & 1 deletion packages/constants/src/values.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,11 @@
import { Network } from './networks';

export const WeiPerEther = 1000000000000000000n;
export const Zero = 0n;
export const MaxUint256 =
115792089237316195423570985008687907853269984665640564039457584007913129639935n;
export const PREMIUM_DURATION = 36000;

export const PREMIUM_DURATION = {
[Network.Goerli]: 36000, // 10 hours
[Network.Sepolia]: 600, // 10 minutes
};
2 changes: 1 addition & 1 deletion packages/core/src/prices/common.ts
Original file line number Diff line number Diff line change
Expand Up @@ -111,7 +111,7 @@ const makeFetchPremiumPrice =
vaultId: string;
}): Promise<[vToken: bigint, price: bigint]> => {
const now = Math.floor(Date.now() / 1000);
const premiumThreshold = now - PREMIUM_DURATION;
const premiumThreshold = now - getChainConstant(PREMIUM_DURATION, network);

if (holding.dateAdded < premiumThreshold) {
return [Zero, Zero];
Expand Down
12 changes: 11 additions & 1 deletion packages/core/src/prices/priceVaultBuy.ts
Original file line number Diff line number Diff line change
Expand Up @@ -24,11 +24,13 @@ const getIndexedPrice = ({
tokenIds,
vault,
now,
network,
}: {
vault: Pick<Vault, 'vTokenToEth' | 'prices'>;
tokenIds: TokenId[] | [TokenId, number][];
holdings: Pick<VaultHolding, 'dateAdded' | 'tokenId'>[];
now: number;
network: number;
}) => {
// We store the prices for buying up to 5 NFTs
// so we can save ourselves from having to make additional calls/calculations
Expand All @@ -44,6 +46,7 @@ const getIndexedPrice = ({
tokenIds,
vTokenToEth,
now,
network,
});

return {
Expand Down Expand Up @@ -90,6 +93,7 @@ const getRoughPrice = async ({
tokenIds,
vTokenToEth,
now,
network,
});

const price = vTokenPrice + feePrice + premiumPrice;
Expand Down Expand Up @@ -168,7 +172,13 @@ export const makePriceVaultBuy =
}

if (bypassIndexedPrice !== true && totalTokenIds <= 5) {
const result = getIndexedPrice({ holdings, tokenIds, vault, now });
const result = getIndexedPrice({
holdings,
tokenIds,
vault,
now,
network,
});
if (result) {
return result;
}
Expand Down
9 changes: 8 additions & 1 deletion packages/core/src/prices/priceVaultSwap.ts
Original file line number Diff line number Diff line change
Expand Up @@ -23,11 +23,13 @@ const getIndexedPrice = ({
tokenIds,
vault,
now,
network,
}: {
holdings: Pick<VaultHolding, 'dateAdded' | 'tokenId'>[];
tokenIds: TokenId[] | [TokenId, number][];
vault: Pick<Vault, 'prices' | 'vTokenToEth'>;
now: number;
network: number;
}) => {
const totalTokenIds = getTotalTokenIds(tokenIds);
const { vTokenToEth } = vault;
Expand All @@ -40,6 +42,7 @@ const getIndexedPrice = ({
tokenIds,
vTokenToEth,
now,
network,
});

return {
Expand All @@ -53,11 +56,13 @@ const getRoughPrice = ({
holdings,
vault,
now,
network,
}: {
holdings: Pick<VaultHolding, 'dateAdded' | 'tokenId'>[];
buyTokenIds: TokenId[] | [TokenId, number][];
vault: Pick<Vault, 'fees' | 'vTokenToEth'>;
now: number;
network: number;
}) => {
const totalTokenIds = getTotalTokenIds(buyTokenIds);
const { vTokenToEth } = vault;
Expand All @@ -73,6 +78,7 @@ const getRoughPrice = ({
tokenIds: buyTokenIds,
vTokenToEth,
now,
network,
});
const price = feePrice + premiumPrice;

Expand Down Expand Up @@ -158,13 +164,14 @@ export const makePriceVaultSwap =
tokenIds: buyTokenIds,
vault,
now,
network,
});
if (result) {
return result;
}
}

return getRoughPrice({ holdings, buyTokenIds, vault, now });
return getRoughPrice({ holdings, buyTokenIds, vault, now, network });
};

const priceVaultSwap = makePriceVaultSwap({ quoteVaultSwap });
Expand Down
9 changes: 8 additions & 1 deletion packages/utils/src/prices/__tests__/premiums.test.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
import { WeiPerEther, Zero } from '@nftx/constants';
import { estimatePremiumPrice } from '../premiums';
import { formatEther } from 'viem';
import { Network } from '@nftx/constants';

let holding: { dateAdded: number };
let vTokenToEth: bigint;
Expand All @@ -11,7 +12,13 @@ beforeEach(() => {
now = Date.now() / 1000;
holding = { dateAdded: 0 };
vTokenToEth = WeiPerEther;
run = () => estimatePremiumPrice({ holding, vTokenToEth, now });
run = () =>
estimatePremiumPrice({
holding,
vTokenToEth,
now,
network: Network.Goerli,
});
});

describe('when holding is older than 10 hours', () => {
Expand Down
8 changes: 7 additions & 1 deletion packages/utils/src/prices/premiums.ts
Original file line number Diff line number Diff line change
@@ -1,17 +1,20 @@
import { PREMIUM_DURATION, WeiPerEther, Zero } from '@nftx/constants';
import { TokenId } from '@nftx/types';
import { getExactTokenIds } from '../tokenIdUtils';
import { getChainConstant } from '../web3';

export const estimatePremiumPrice = ({
network,
holding,
vTokenToEth,
now,
}: {
network: number;
holding: { dateAdded: number } | undefined;
vTokenToEth: bigint;
now: number;
}): [vToken: bigint, price: bigint] => {
const premiumThreshold = now - PREMIUM_DURATION;
const premiumThreshold = now - getChainConstant(PREMIUM_DURATION, network);

if (!holding || holding.dateAdded < premiumThreshold) {
return [Zero, Zero];
Expand All @@ -37,11 +40,13 @@ export const estimateTotalPremiumPrice = ({
tokenIds,
vTokenToEth,
now,
network,
}: {
tokenIds: TokenId[] | [TokenId, number][];
holdings: { dateAdded: number; tokenId: TokenId }[];
vTokenToEth: bigint;
now: number;
network: number;
}): [vToken: bigint, price: bigint] => {
return getExactTokenIds(tokenIds).reduce(
(total, tokenId) => {
Expand All @@ -50,6 +55,7 @@ export const estimateTotalPremiumPrice = ({
holding,
vTokenToEth,
now,
network,
});
return [total[0] + premium[0], total[1] + premium[1]] as [bigint, bigint];
},
Expand Down

0 comments on commit b3dc303

Please sign in to comment.