From 103c6e9b61a00991e64680902df2a299c52daf74 Mon Sep 17 00:00:00 2001 From: eli-d <64763513+eli-d@users.noreply.github.com> Date: Mon, 18 Nov 2024 16:11:39 +1030 Subject: [PATCH] source tvl from `Amounts` when calculating apr --- .../graph/schema.resolvers.go | 26 ++++++++++++------- 1 file changed, 17 insertions(+), 9 deletions(-) diff --git a/cmd/graphql.ethereum/graph/schema.resolvers.go b/cmd/graphql.ethereum/graph/schema.resolvers.go index 53743777..235eeb98 100644 --- a/cmd/graphql.ethereum/graph/schema.resolvers.go +++ b/cmd/graphql.ethereum/graph/schema.resolvers.go @@ -988,18 +988,26 @@ func (r *seawaterPoolResolver) Apr(ctx context.Context, obj *seawater.Pool) (mod if obj == nil { return model.Apr{}, fmt.Errorf("pool empty") } - // Get most recent TVL - tvlOverTime, err := r.TvlOverTime(ctx, obj) + // Get the TVL by scaling the current Amounts by their prices + pairAmount, err := r.Amounts(ctx, obj) if err != nil { - return model.Apr{}, fmt.Errorf("no tvl: %v", err) + return model.Apr{}, fmt.Errorf("amounts: %v", err) } - var tvlString string - if len(tvlOverTime.Daily) == 0 { - tvlString = "1" - } else { - tvlString = tvlOverTime.Daily[0] + price, err := r.Price(ctx, obj) + if err != nil { + return model.Apr{}, fmt.Errorf("price: %v", err) + } + fusdcUsd, err := pairAmount.Fusdc.UsdValue(price, r.C.FusdcAddr) + if err != nil { + return model.Apr{}, fmt.Errorf("fusdc usd value: %v", err) + } + tvl, _ := new(big.Rat).SetString(fusdcUsd) + token1Usd, err := pairAmount.Token1.UsdValue(price, r.C.FusdcAddr) + if err != nil { + return model.Apr{}, fmt.Errorf("token1 usd value: %v", err) } - tvl, _ := new(big.Rat).SetString(tvlString) + token1, _ := new(big.Rat).SetString(token1Usd) + tvl = tvl.Add(tvl, token1) // If TVL is 0, set to 1 to avoid division by 0 if tvl.Cmp(big.NewRat(0, 1)) == 0 { tvl.SetInt64(1)