Skip to content

Commit

Permalink
pools.type_data column
Browse files Browse the repository at this point in the history
  • Loading branch information
gmbronco committed Feb 7, 2024
1 parent 8b9ce39 commit 12c8d7b
Show file tree
Hide file tree
Showing 17 changed files with 79 additions and 81 deletions.
4 changes: 2 additions & 2 deletions modules/content/github-content.service.ts
Original file line number Diff line number Diff line change
Expand Up @@ -117,7 +117,7 @@ export class GithubContentService implements ContentService {
symbol: true,
name: true,
type: true,
staticTypeData: true,
typeData: true,
tokens: { orderBy: { index: 'asc' } },
},
});
Expand Down Expand Up @@ -152,7 +152,7 @@ export class GithubContentService implements ContentService {
});
}

const wrappedIndex = (pool?.staticTypeData as LinearData).wrappedIndex;
const wrappedIndex = (pool?.typeData as LinearData).wrappedIndex;
const wrappedLinearPoolToken = pools.find((pool) => pool.tokens[wrappedIndex]?.address === token.address);

if (wrappedLinearPoolToken && !tokenTypes.includes('LINEAR_WRAPPED_TOKEN')) {
Expand Down
4 changes: 2 additions & 2 deletions modules/content/sanity-content.service.ts
Original file line number Diff line number Diff line change
Expand Up @@ -186,7 +186,7 @@ export class SanityContentService implements ContentService {
});
}

const wrappedIndex = (pool?.staticTypeData as LinearData).wrappedIndex;
const wrappedIndex = (pool?.typeData as LinearData).wrappedIndex;
const wrappedLinearPoolToken = pools.find((pool) => pool.tokens[wrappedIndex]?.address === token.address);

if (wrappedLinearPoolToken && !tokenTypes.includes('LINEAR_WRAPPED_TOKEN')) {
Expand Down Expand Up @@ -216,7 +216,7 @@ export class SanityContentService implements ContentService {
symbol: true,
name: true,
type: true,
staticTypeData: true,
typeData: true,
tokens: { orderBy: { index: 'asc' } },
},
});
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -38,14 +38,14 @@ export class SpookySwapAprService implements PoolAprService {

for (const pool of expandedSpookyPools) {
if (
!(pool.staticTypeData as LinearData) ||
!(pool.typeData as LinearData) ||
!pool.dynamicData ||
pool.tokens[(pool.staticTypeData as LinearData).mainIndex].address !== this.booAddress
pool.tokens[(pool.typeData as LinearData).mainIndex].address !== this.booAddress
) {
continue;
}

const linearData = pool.staticTypeData as LinearData;
const linearData = pool.typeData as LinearData;
const wrappedToken = pool.tokens[linearData.wrappedIndex];
const tokenPrice = this.tokenService.getPriceForToken(tokenPrices, this.booAddress);
const wrappedTokens = parseFloat(wrappedToken.dynamicData?.balance || '0');
Expand Down
60 changes: 28 additions & 32 deletions modules/pool/lib/pool-gql-loader.service.ts
Original file line number Diff line number Diff line change
Expand Up @@ -455,7 +455,7 @@ export class PoolGqlLoaderService {
userWalletbalances: PrismaUserWalletBalance[] = [],
userStakedBalances: PrismaUserStakedBalance[] = [],
): GqlPoolUnion {
const { staticTypeData, ...poolWithoutStaticTypeData } = pool;
const { typeData, ...poolWithoutTypeData } = pool;

const bpt = pool.tokens.find((token) => token.address === pool.address);

Expand All @@ -477,71 +477,71 @@ export class PoolGqlLoaderService {
case 'STABLE':
return {
__typename: 'GqlPoolStable',
...poolWithoutStaticTypeData,
...(staticTypeData as StableData),
...poolWithoutTypeData,
...(typeData as StableData),
...mappedData,
tokens: mappedData.tokens as GqlPoolToken[],
};
case 'META_STABLE':
return {
__typename: 'GqlPoolMetaStable',
...poolWithoutStaticTypeData,
...(staticTypeData as StableData),
...poolWithoutTypeData,
...(typeData as StableData),
...mappedData,
tokens: mappedData.tokens as GqlPoolToken[],
};
case 'COMPOSABLE_STABLE':
return {
__typename: 'GqlPoolComposableStable',
...poolWithoutStaticTypeData,
...(staticTypeData as StableData),
...poolWithoutTypeData,
...(typeData as StableData),
...mappedData,
bptPriceRate: bpt?.dynamicData?.priceRate || '1.0',
};
case 'LINEAR':
return {
__typename: 'GqlPoolLinear',
...poolWithoutStaticTypeData,
...(staticTypeData as LinearData),
...poolWithoutTypeData,
...(typeData as LinearData),
...mappedData,
tokens: mappedData.tokens as GqlPoolToken[],
bptPriceRate: bpt?.dynamicData?.priceRate || '1.0',
};
case 'ELEMENT':
return {
__typename: 'GqlPoolElement',
...poolWithoutStaticTypeData,
...(staticTypeData as ElementData),
...poolWithoutTypeData,
...(typeData as ElementData),
...mappedData,
tokens: mappedData.tokens as GqlPoolToken[],
};
case 'LIQUIDITY_BOOTSTRAPPING':
return {
__typename: 'GqlPoolLiquidityBootstrapping',
...poolWithoutStaticTypeData,
...poolWithoutTypeData,
...mappedData,
};
case 'GYRO':
case 'GYRO3':
case 'GYROE':
return {
__typename: 'GqlPoolGyro',
...poolWithoutStaticTypeData,
...(staticTypeData as GyroData),
...poolWithoutTypeData,
...(typeData as GyroData),
...mappedData,
};
case 'FX':
return {
__typename: 'GqlPoolFx',
...poolWithoutStaticTypeData,
...poolWithoutTypeData,
...mappedData,
...(staticTypeData as FxData),
...(typeData as FxData),
};
}

return {
__typename: 'GqlPoolWeighted',
...poolWithoutStaticTypeData,
...poolWithoutTypeData,
...mappedData,
};
}
Expand Down Expand Up @@ -973,12 +973,8 @@ export class PoolGqlLoaderService {
const nestedPool = poolToken.nestedPool;
const options: GqlPoolInvestOption[] = [];

if (
nestedPool &&
nestedPool.type === 'LINEAR' &&
(nestedPool.staticTypeData as LinearData).mainIndex !== undefined
) {
const mainToken = nestedPool.tokens[(nestedPool.staticTypeData as LinearData).mainIndex];
if (nestedPool && nestedPool.type === 'LINEAR' && (nestedPool.typeData as LinearData).mainIndex !== undefined) {
const mainToken = nestedPool.tokens[(nestedPool.typeData as LinearData).mainIndex];
const isWrappedNativeAsset = isSameAddress(mainToken.address, networkContext.data.weth.address);

options.push({
Expand Down Expand Up @@ -1016,11 +1012,11 @@ export class PoolGqlLoaderService {
tokenOptions:
nestedToken.nestedPool &&
nestedToken.nestedPool.type === 'LINEAR' &&
(nestedToken.nestedPool.staticTypeData as LinearData).mainIndex !== undefined
(nestedToken.nestedPool.typeData as LinearData).mainIndex !== undefined
? [
this.mapPoolTokenToGql(
nestedToken.nestedPool.tokens[
(nestedToken.nestedPool.staticTypeData as LinearData).mainIndex
(nestedToken.nestedPool.typeData as LinearData).mainIndex
],
),
]
Expand All @@ -1036,11 +1032,11 @@ export class PoolGqlLoaderService {
if (
nestedToken.nestedPool &&
nestedToken.nestedPool.type === 'LINEAR' &&
(nestedToken.nestedPool.staticTypeData as LinearData).mainIndex !== undefined
(nestedToken.nestedPool.typeData as LinearData).mainIndex !== undefined
) {
return this.mapPoolTokenToGql(
nestedToken.nestedPool.tokens[
(nestedToken.nestedPool.staticTypeData as LinearData).mainIndex
(nestedToken.nestedPool.typeData as LinearData).mainIndex
],
);
}
Expand Down Expand Up @@ -1131,7 +1127,7 @@ export class PoolGqlLoaderService {
return {
__typename: 'GqlPoolLinearNested',
...pool,
...(pool.staticTypeData as LinearData)!,
...(pool.typeData as LinearData)!,
tokens: pool.tokens
.filter((token) => token.address !== pool.address)
.map((token) => {
Expand Down Expand Up @@ -1163,7 +1159,7 @@ export class PoolGqlLoaderService {
return {
__typename: 'GqlPoolComposableStableNested',
...pool,
...(pool.staticTypeData as StableData)!,
...(pool.typeData as StableData)!,
nestingType: this.getPoolNestingType(pool),
tokens: pool.tokens.map((token) => {
const nestedPool = token.nestedPool;
Expand Down Expand Up @@ -1223,7 +1219,7 @@ export class PoolGqlLoaderService {
wrappedTokenBalance: string;
totalMainTokenBalance: string;
} {
if (!poolToken.dynamicData || !(nestedPool.staticTypeData as LinearData) || !nestedPool.dynamicData) {
if (!poolToken.dynamicData || !(nestedPool.typeData as LinearData) || !nestedPool.dynamicData) {
return {
mainTokenBalance: '0',
wrappedTokenBalance: '0',
Expand All @@ -1234,8 +1230,8 @@ export class PoolGqlLoaderService {
const percentOfSupplyInPool =
parseFloat(poolToken.dynamicData.balance) / parseFloat(nestedPool.dynamicData.totalShares);

const mainToken = nestedPool.tokens[(nestedPool.staticTypeData as LinearData).mainIndex];
const wrappedToken = nestedPool.tokens[(nestedPool.staticTypeData as LinearData).wrappedIndex];
const mainToken = nestedPool.tokens[(nestedPool.typeData as LinearData).mainIndex];
const wrappedToken = nestedPool.tokens[(nestedPool.typeData as LinearData).wrappedIndex];

const wrappedTokenBalance = oldBnum(wrappedToken.dynamicData?.balance || '0').times(percentOfSupplyInPool);
const mainTokenBalance = oldBnum(mainToken.dynamicData?.balance || '0').times(percentOfSupplyInPool);
Expand Down
16 changes: 8 additions & 8 deletions modules/pool/lib/pool-on-chain-data.service.ts
Original file line number Diff line number Diff line change
Expand Up @@ -118,13 +118,13 @@ export class PoolOnChainDataService {
}

//only update if amp has changed
if ((pool.staticTypeData as StableData).amp !== amp) {
if ((pool.typeData as StableData).amp !== amp) {
operations.push(
prisma.prismaPool.update({
where: { id_chain: { id: pool.id, chain: this.options.chain } },
data: {
staticTypeData: {
...(pool.staticTypeData as StableData),
typeData: {
...(pool.typeData as StableData),
amp,
},
},
Expand All @@ -142,15 +142,15 @@ export class PoolOnChainDataService {
const upperTarget = formatFixed(onchainData.targets[1], 18);

if (
(pool.staticTypeData as LinearData).lowerTarget !== lowerTarget ||
(pool.staticTypeData as LinearData).upperTarget !== upperTarget
(pool.typeData as LinearData).lowerTarget !== lowerTarget ||
(pool.typeData as LinearData).upperTarget !== upperTarget
) {
operations.push(
prisma.prismaPool.update({
where: { id_chain: { id: pool.id, chain: this.options.chain } },
data: {
staticTypeData: {
...(pool.staticTypeData as LinearData),
typeData: {
...(pool.typeData as LinearData),
lowerTarget,
upperTarget,
},
Expand Down Expand Up @@ -229,7 +229,7 @@ export class PoolOnChainDataService {
// linear wrapped token rate
if (
onchainData.wrappedTokenRate &&
(pool.staticTypeData as LinearData)?.wrappedIndex === poolToken.index
(pool.typeData as LinearData)?.wrappedIndex === poolToken.index
) {
priceRate = onchainData.wrappedTokenRate;
}
Expand Down
2 changes: 1 addition & 1 deletion modules/pool/pool.prisma
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ model PrismaPool {
vaultVersion Int @default(2)
staticTypeData Json @default("{}")
typeData Json @default("{}") @map("type_data")
tokens PrismaPoolToken[]
Expand Down
16 changes: 8 additions & 8 deletions modules/pool/subgraph-mapper.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -46,40 +46,40 @@ describe('subgraphToPrismaCreate', () => {
it('should return correct object for stable pool', () => {
const result = subgraphToPrismaCreate(stablePool, 'MAINNET', 1, []);
expect(result.data.type).toBe('COMPOSABLE_STABLE');
expect(result.data.staticTypeData['amp']).toBe(stablePool.amp);
expect(result.data.typeData['amp']).toBe(stablePool.amp);
});

it('should return correct object for old stable pool', () => {
const result = subgraphToPrismaCreate(oldStablePool, 'MAINNET', 1, []);
expect(result.data.type).toBe('COMPOSABLE_STABLE');
expect(result.data.version).toBe(0);
expect(result.data.staticTypeData['amp']).toBe(oldStablePool.amp);
expect(result.data.typeData['amp']).toBe(oldStablePool.amp);
});

it('should return correct object for linear pool', () => {
const result = subgraphToPrismaCreate(linearPool, 'MAINNET', 1, []);
expect(result.data.type).toBe('LINEAR');
expect(result.data.staticTypeData['upperTarget']).toBe(linearPool.upperTarget);
expect(result.data.staticTypeData['wrappedIndex']).toBe(linearPool.wrappedIndex);
expect(result.data.typeData['upperTarget']).toBe(linearPool.upperTarget);
expect(result.data.typeData['wrappedIndex']).toBe(linearPool.wrappedIndex);
});

it('should return correct object for element pool', () => {
const result = subgraphToPrismaCreate(elementPool, 'MAINNET', 1, []);
expect(result.data.type).toBe('ELEMENT');
expect(result.data.staticTypeData['principalToken']).toBe(elementPool.principalToken);
expect(result.data.typeData['principalToken']).toBe(elementPool.principalToken);
});

it('should return correct object for gyro pool', () => {
const result = subgraphToPrismaCreate(gyroPool, 'MAINNET', 1, []);
expect(result.data.type).toBe('GYROE');
expect(result.data.staticTypeData['alpha']).toBe(gyroPool.alpha);
expect(result.data.staticTypeData['tauAlphaX']).toBe(gyroPool.tauAlphaX);
expect(result.data.typeData['alpha']).toBe(gyroPool.alpha);
expect(result.data.typeData['tauAlphaX']).toBe(gyroPool.tauAlphaX);
});

it('should return correct object for fx pool', () => {
const result = subgraphToPrismaCreate(fxPool, 'MAINNET', 1, []);
expect(result.data.type).toBe('FX');
expect(result.data.staticTypeData['alpha']).toBe(gyroPool.alpha);
expect(result.data.typeData['alpha']).toBe(gyroPool.alpha);
});

describe('nested pools', () => {
Expand Down
14 changes: 7 additions & 7 deletions modules/pool/subgraph-mapper.ts
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ export const subgraphToPrismaCreate = (
const prismaPoolRecordWithAssociations = {
data: {
...dbData.base,
staticTypeData: dbData.staticTypeData,
typeData: dbData.typeData,
tokens: {
createMany: {
data: dbData.tokens,
Expand Down Expand Up @@ -43,7 +43,7 @@ export const subgraphToPrismaUpdate = (

const prismaPoolRecordWithDataAssociations = {
...baseWithoutId,
staticTypeData: dbData.staticTypeData,
typeData: dbData.typeData,
tokens: {
update: dbData.tokens.map((token) => ({
where: {
Expand Down Expand Up @@ -94,10 +94,10 @@ const subgraphMapper = (
totalLiquidity: Math.max(parseFloat(pool.totalLiquidity), 0),
};

const staticTypeData: ReturnType<typeof staticTypeDataMapper[keyof typeof staticTypeDataMapper]> | {} = Object.keys(
staticTypeDataMapper,
const typeData: ReturnType<typeof typeDataMapper[keyof typeof typeDataMapper]> | {} = Object.keys(
typeDataMapper,
).includes(type)
? staticTypeDataMapper[type as keyof typeof staticTypeDataMapper](pool)
? typeDataMapper[type as keyof typeof typeDataMapper](pool)
: {};

const tokens =
Expand Down Expand Up @@ -128,7 +128,7 @@ const subgraphMapper = (
base,
dynamicData,
tokens,
staticTypeData,
typeData,
};
};

Expand Down Expand Up @@ -181,7 +181,7 @@ const mapPoolTypeVersion = (poolType: string, poolTypeVersion: number): number =
return version;
};

const staticTypeDataMapper = {
const typeDataMapper = {
ELEMENT: element,
FX: fx,
GYRO: gyro,
Expand Down
Loading

0 comments on commit 12c8d7b

Please sign in to comment.