Skip to content

Commit

Permalink
astroport spot price fix
Browse files Browse the repository at this point in the history
  • Loading branch information
p0mvn committed Apr 18, 2024
1 parent 8b7587f commit 2f61135
Show file tree
Hide file tree
Showing 2 changed files with 30 additions and 0 deletions.
28 changes: 28 additions & 0 deletions router/usecase/pools/routable_cw_pool.go
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,8 @@ import (
const (
// placeholder for the code id of the pool that is not a cosm wasm pool
notCosmWasmPoolCodeID = 0

astroportCodeID = 580
)

var _ sqsdomain.RoutablePool = &routableCosmWasmPoolImpl{}
Expand All @@ -35,6 +37,12 @@ type routableCosmWasmPoolImpl struct {
wasmClient wasmtypes.QueryClient "json:\"-\""
}

var (
// Assumming precision of 6, this is 10 units.
// This is naive since precision can be greater but should work for most cases.
tenE7 = sdk.NewInt(10_000_000)
)

// GetId implements sqsdomain.RoutablePool.
func (r *routableCosmWasmPoolImpl) GetId() uint64 {
return r.ChainPool.PoolId
Expand Down Expand Up @@ -119,6 +127,26 @@ func (r *routableCosmWasmPoolImpl) CalcSpotPrice(ctx context.Context, baseDenom
},
}

// If the pool is an Astroport pool, use an alternative method for
// calculating the spot price.
// Astroport spot price is an SMA (moving average) of all past trades.
codeID := r.ChainPool.CodeId
if codeID == astroportCodeID {
// Calculate the spot price using the pool's balances
out, err := r.CalculateTokenOutByTokenIn(ctx, sdk.NewCoin(baseDenom, tenE7))
// If error, proceed to querying cosmwasm
if err == nil {
spotPrice := osmomath.NewBigDecFromBigIntMut(out.Amount.BigIntMut()).QuoMut(osmomath.NewBigDecFromBigInt(tenE7.BigIntMut()))

// If spot price is not zero, return it
if !spotPrice.IsZero() {
return spotPrice, nil
}

// If spot price was truncated, proceed to querying cosmwasm via the general method
}
}

response := &msg.SpotPriceQueryMsgResponse{}
if err := queryCosmwasmContract(ctx, r.wasmClient, r.ChainPool.ContractAddress, &request, response); err != nil {
return osmomath.BigDec{}, err
Expand Down
2 changes: 2 additions & 0 deletions tokens/usecase/pricing/chain/pricing_chain.go
Original file line number Diff line number Diff line change
Expand Up @@ -194,6 +194,8 @@ func (c *chainPricing) computePrice(ctx context.Context, baseDenom string, quote
useAlternativeMethod = false
)

// If Astroport pool, use the alternative meth

for _, pool := range pools {
tempBaseDenom = pool.GetTokenOutDenom()

Expand Down

0 comments on commit 2f61135

Please sign in to comment.