github.com/wormhole-foundation/wormhole-explorer/common@v0.0.0-20240604151348-09585b5b97c5/prices/volume.go (about)

     1  package prices
     2  
     3  import (
     4  	"math/big"
     5  
     6  	"github.com/shopspring/decimal"
     7  )
     8  
     9  // CalculatePriceUSD calculates the price in USD for a given notional value and amount of tokens
    10  func CalculatePriceUSD(notionalUSD decimal.Decimal, amount *big.Int, decimals int64) decimal.Decimal {
    11  
    12  	var exp int32
    13  	if decimals > 8 {
    14  		exp = 8
    15  	} else {
    16  		exp = int32(decimals)
    17  	}
    18  	tokenAmount := decimal.NewFromBigInt(amount, -exp)
    19  
    20  	// Compute the amount in USD
    21  	usdAmount := tokenAmount.Mul(notionalUSD)
    22  
    23  	return usdAmount
    24  }