42 lines
1.2 KiB
Go
42 lines
1.2 KiB
Go
package scanner
|
|
|
|
import (
|
|
"math/big"
|
|
|
|
"github.com/ethereum/go-ethereum/common"
|
|
)
|
|
|
|
// TokenDecimalMap provides decimal information for common tokens
|
|
var TokenDecimalMap = map[common.Address]int{
|
|
common.HexToAddress("0x82aF49447D8a07e3bd95BD0d56f35241523fBab1"): 18, // WETH
|
|
common.HexToAddress("0xFF970A61A04b1cA14834A43f5dE4533eBDDB5CC8"): 6, // USDC
|
|
common.HexToAddress("0xFd086bC7CD5C481DCC9C85ebE478A1C0b69FCbb9"): 6, // USDT
|
|
common.HexToAddress("0x2f2a2543B76A4166549F7aaB2e75Bef0aefC5B0f"): 8, // WBTC
|
|
common.HexToAddress("0x912CE59144191C1204E64559FE8253a0e49E6548"): 18, // ARB
|
|
}
|
|
|
|
// GetTokenDecimals returns decimals for a token (defaults to 18)
|
|
func GetTokenDecimals(token common.Address) int {
|
|
if decimals, ok := TokenDecimalMap[token]; ok {
|
|
return decimals
|
|
}
|
|
return 18
|
|
}
|
|
|
|
// NormalizeToEther converts token amount to ether equivalent considering decimals
|
|
func NormalizeToEther(amount *big.Int, token common.Address) *big.Float {
|
|
if amount == nil {
|
|
return big.NewFloat(0)
|
|
}
|
|
|
|
decimals := GetTokenDecimals(token)
|
|
divisor := new(big.Int).Exp(big.NewInt(10), big.NewInt(int64(decimals)), nil)
|
|
|
|
result := new(big.Float).Quo(
|
|
new(big.Float).SetInt(amount),
|
|
new(big.Float).SetInt(divisor),
|
|
)
|
|
|
|
return result
|
|
}
|