Skip to content

Commit

Permalink
Merge pull request #459 from beethovenxfi/add-price-rate-providers
Browse files Browse the repository at this point in the history
Add price rate providers
  • Loading branch information
franzns authored Dec 21, 2023
2 parents d50bb7d + a123edf commit fa57c2b
Show file tree
Hide file tree
Showing 9 changed files with 119 additions and 33 deletions.
12 changes: 12 additions & 0 deletions modules/pool/lib/pool-creator.service.ts
Original file line number Diff line number Diff line change
Expand Up @@ -250,9 +250,21 @@ export class PoolCreatorService {
return nestedPool.address === token.address;
});

let priceRateProvider;
if (pool.priceRateProviders) {
const data = pool.priceRateProviders.find(
(provider) => provider.token.address === token.address,
);
priceRateProvider = data?.address;
}

return {
id: token.id,
address: token.address,
priceRateProvider,
exemptFromProtocolYieldFee: token.isExemptFromYieldProtocolFee
? token.isExemptFromYieldProtocolFee
: false,
nestedPoolId: nestedPool?.id,
index: token.index || pool.tokensList.findIndex((address) => address === token.address),
};
Expand Down
1 change: 1 addition & 0 deletions modules/pool/lib/pool-gql-loader.service.ts
Original file line number Diff line number Diff line change
Expand Up @@ -1113,6 +1113,7 @@ export class PoolGqlLoaderService {
...poolToken.token,
__typename: 'GqlPoolToken',
priceRate: poolToken.dynamicData?.priceRate || '1.0',
priceRateProvider: poolToken.priceRateProvider,
balance: poolToken.dynamicData?.balance || '0',
index: poolToken.index,
weight: poolToken.dynamicData?.weight,
Expand Down
21 changes: 6 additions & 15 deletions modules/pool/pool.gql
Original file line number Diff line number Diff line change
Expand Up @@ -56,6 +56,8 @@ extend type Mutation {
poolBlackListRemovePool(poolId: String!): String!
poolDeletePool(poolId: String!): String!
poolSyncAllPoolTypesVersions: String!
poolSyncPriceRateProviders: String!
poolSyncProtocolYieldFeeExemptions: String!
poolInitOnChainDataForAllPools: String!
}

Expand Down Expand Up @@ -507,6 +509,7 @@ interface GqlPoolTokenBase {
symbol: String!
index: Int!
priceRate: BigDecimal!
priceRateProvider: String
weight: BigDecimal
#the total balance in the pool, regardless of nesting
totalBalance: BigDecimal!
Expand All @@ -523,6 +526,7 @@ type GqlPoolToken implements GqlPoolTokenBase {
#dynamic data
balance: BigDecimal!
priceRate: BigDecimal!
priceRateProvider: String
weight: BigDecimal
#the total balance in the pool, regardless of nesting
totalBalance: BigDecimal!
Expand All @@ -537,6 +541,7 @@ type GqlPoolTokenLinear implements GqlPoolTokenBase {
symbol: String!
index: Int!
priceRate: BigDecimal!
priceRateProvider: String
weight: BigDecimal

pool: GqlPoolLinearNested!
Expand All @@ -557,6 +562,7 @@ type GqlPoolTokenComposableStable implements GqlPoolTokenBase {
symbol: String!
index: Int!
priceRate: BigDecimal!
priceRateProvider: String
weight: BigDecimal
#the total balance in the pool, regardless of nesting
totalBalance: BigDecimal!
Expand Down Expand Up @@ -669,21 +675,6 @@ enum GqlPoolFilterCategory {
BLACK_LISTED
}

# enum GqlPoolFilterType {
# WEIGHTED
# STABLE
# META_STABLE
# PHANTOM_STABLE
# ELEMENT
# LINEAR
# UNKNOWN
# LIQUIDITY_BOOTSTRAPPING
# INVESTMENT
# GYRO
# GYRO3
# GYROE
# }

type GqlPoolTokenExpanded {
id: ID!
address: String!
Expand Down
21 changes: 12 additions & 9 deletions modules/pool/pool.prisma
Original file line number Diff line number Diff line change
Expand Up @@ -200,17 +200,20 @@ model PrismaPoolLinearDynamicData {
model PrismaPoolToken {
@@id([id, chain])
id String
poolId String
pool PrismaPool @relation(fields:[poolId, chain], references: [id, chain])
chain Chain
id String
poolId String
pool PrismaPool @relation(fields:[poolId, chain], references: [id, chain])
chain Chain
address String
token PrismaToken @relation(fields:[address, chain], references: [address, chain])
index Int
address String
token PrismaToken @relation(fields:[address, chain], references: [address, chain])
index Int
nestedPoolId String?
nestedPool PrismaPool? @relation(name: "PoolNestedInToken", fields:[nestedPoolId, chain], references: [id, chain])
nestedPoolId String?
nestedPool PrismaPool? @relation(name: "PoolNestedInToken", fields:[nestedPoolId, chain], references: [id, chain])
priceRateProvider String?
exemptFromProtocolYieldFee Boolean @default(false)
dynamicData PrismaPoolTokenDynamicData?
}
Expand Down
14 changes: 14 additions & 0 deletions modules/pool/pool.resolvers.ts
Original file line number Diff line number Diff line change
Expand Up @@ -321,6 +321,20 @@ const balancerResolvers: Resolvers = {

await poolService.syncPoolTypeAndVersionForAllPools();

return 'success';
},
poolSyncPriceRateProviders: async (parent, {}, context) => {
isAdminRoute(context);

await poolService.syncPriceRateProvidersForAllPools();

return 'success';
},
poolSyncProtocolYieldFeeExemptions: async (parent, {}, context) => {
isAdminRoute(context);

await poolService.syncProtocolYieldFeeExemptionsForAllPools();

return 'success';
},
},
Expand Down
52 changes: 52 additions & 0 deletions modules/pool/pool.service.ts
Original file line number Diff line number Diff line change
Expand Up @@ -361,6 +361,58 @@ export class PoolService {
await this.poolCreatorService.updatePoolTypesAndVersionForAllPools();
}

public async syncProtocolYieldFeeExemptionsForAllPools() {
const subgraphPools = await this.balancerSubgraphService.getAllPools({}, false);
for (const subgraphPool of subgraphPools) {
const poolTokens = subgraphPool.tokens || [];
for (let i = 0; i < poolTokens.length; i++) {
const token = poolTokens[i];
try {
await prisma.prismaPoolToken.update({
where: { id_chain: { id: token.id, chain: networkContext.chain } },
data: {
exemptFromProtocolYieldFee: token.isExemptFromYieldProtocolFee
? token.isExemptFromYieldProtocolFee
: false,
},
});
} catch (e) {
console.error('Failed to update token ', token.id, ' error is: ', e);
}
}
}
}

public async syncPriceRateProvidersForAllPools() {
const subgraphPools = await this.balancerSubgraphService.getAllPools({}, false);
for (const subgraphPool of subgraphPools) {
if (!subgraphPool.priceRateProviders || !subgraphPool.priceRateProviders.length) continue;

const poolTokens = subgraphPool.tokens || [];
for (let i = 0; i < poolTokens.length; i++) {
const token = poolTokens[i];

let priceRateProvider;
const data = subgraphPool.priceRateProviders.find(
(provider) => provider.token.address === token.address,
);
priceRateProvider = data?.address;
if (!priceRateProvider) continue;

try {
await prisma.prismaPoolToken.update({
where: { id_chain: { id: token.id, chain: networkContext.chain } },
data: {
priceRateProvider,
},
});
} catch (e) {
console.error('Failed to update token ', token.id, ' error is: ', e);
}
}
}
}

public async addToBlackList(poolId: string) {
const category = await prisma.prismaPoolCategory.findFirst({
where: { poolId, chain: this.chain, category: 'BLACK_LISTED' },
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -198,6 +198,12 @@ fragment BalancerPool on Pool {
w
z
dSq
priceRateProviders {
address
token {
address
}
}

tokens {
...BalancerPoolToken
Expand All @@ -213,6 +219,7 @@ fragment BalancerPoolToken on PoolToken {
balance
weight
priceRate
isExemptFromYieldProtocolFee
index
token {
latestFXPrice
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
-- AlterTable
ALTER TABLE "PrismaPoolToken" ADD COLUMN "exemptFromProtocolYieldFee" BOOLEAN NOT NULL DEFAULT false,
ADD COLUMN "priceRateProvider" TEXT;
21 changes: 12 additions & 9 deletions prisma/schema.prisma
Original file line number Diff line number Diff line change
Expand Up @@ -246,17 +246,20 @@ model PrismaPoolLinearDynamicData {
model PrismaPoolToken {
@@id([id, chain])
id String
poolId String
pool PrismaPool @relation(fields:[poolId, chain], references: [id, chain])
chain Chain
id String
poolId String
pool PrismaPool @relation(fields:[poolId, chain], references: [id, chain])
chain Chain
address String
token PrismaToken @relation(fields:[address, chain], references: [address, chain])
index Int
address String
token PrismaToken @relation(fields:[address, chain], references: [address, chain])
index Int
nestedPoolId String?
nestedPool PrismaPool? @relation(name: "PoolNestedInToken", fields:[nestedPoolId, chain], references: [id, chain])
nestedPoolId String?
nestedPool PrismaPool? @relation(name: "PoolNestedInToken", fields:[nestedPoolId, chain], references: [id, chain])
priceRateProvider String?
exemptFromProtocolYieldFee Boolean @default(false)
dynamicData PrismaPoolTokenDynamicData?
}
Expand Down

0 comments on commit fa57c2b

Please sign in to comment.