Skip to content

Commit

Permalink
fix: sonic wrapped native
Browse files Browse the repository at this point in the history
  • Loading branch information
NgoKimPhu committed Dec 18, 2024
1 parent c555b4e commit 55fe428
Show file tree
Hide file tree
Showing 16 changed files with 59 additions and 52 deletions.
16 changes: 8 additions & 8 deletions pkg/liquidity-source/bancor-v3/pool_simulator.go
Original file line number Diff line number Diff line change
Expand Up @@ -226,8 +226,8 @@ func (s *PoolSimulator) verifyTokens(sourceToken, targetToken string) error {
}

func (s *PoolSimulator) transformTokens(tokenIn, tokenOut string) (string, string, bool, bool, error) {
weth := strings.ToLower(valueobject.WETHByChainID[s.chainID])
if tokenIn != weth && tokenOut != weth {
native := strings.ToLower(valueobject.WrappedNativeMap[s.chainID])
if tokenIn != native && tokenOut != native {
return tokenIn, tokenOut, false, false, nil
}

Expand All @@ -239,7 +239,7 @@ func (s *PoolSimulator) transformTokens(tokenIn, tokenOut string) (string, strin
)

var (
eth = strings.ToLower(valueobject.EtherAddress)
eth = strings.ToLower(valueobject.NativeAddress)

ethReserve *uint256.Int
wethReserve *uint256.Int
Expand All @@ -252,21 +252,21 @@ func (s *PoolSimulator) transformTokens(tokenIn, tokenOut string) (string, strin
ethReserve = ethPoolData.Liquidity.StakedBalance
}

wethPoolData, err := s.getPoolData(weth)
wethPoolData, err := s.getPoolData(native)
if err != nil {
wethReserve = number.Zero
} else {
wethReserve = wethPoolData.Liquidity.StakedBalance
}

if tokenIn == weth && ethReserve.Cmp(wethReserve) > 0 {
if tokenIn == native && ethReserve.Cmp(wethReserve) > 0 {
sourceToken = eth
isSourceNative = true
}

if (tokenOut == weth) &&
((tokenIn != weth && ethReserve.Cmp(wethReserve) > 0) ||
(tokenIn == weth && !isSourceNative)) {
if (tokenOut == native) &&
((tokenIn != native && ethReserve.Cmp(wethReserve) > 0) ||
(tokenIn == native && !isSourceNative)) {
targetToken = eth
isTargetNative = true
}
Expand Down
4 changes: 2 additions & 2 deletions pkg/liquidity-source/bancor-v3/pool_tracker.go
Original file line number Diff line number Diff line change
Expand Up @@ -177,10 +177,10 @@ func (t *PoolTracker) updatePool(
}
colByPool[pool] = col
reserves = append(reserves, poolData.Liquidity.StakedBalance.String())
if strings.EqualFold(pool, valueobject.EtherAddress) {
if strings.EqualFold(pool, valueobject.NativeAddress) {
nativeIdx = len(tokens)
tokens = append(tokens, &entity.PoolToken{
Address: strings.ToLower(valueobject.WETHByChainID[t.config.ChainID]),
Address: strings.ToLower(valueobject.WrappedNativeMap[t.config.ChainID]),
Swappable: true,
})

Expand Down
2 changes: 1 addition & 1 deletion pkg/liquidity-source/bancor-v3/pools_list_updater.go
Original file line number Diff line number Diff line change
Expand Up @@ -62,7 +62,7 @@ func (u *PoolsListUpdater) GetNewPools(ctx context.Context, metadataBytes []byte
)

for _, tokenAddress := range tokenAddresses {
addr := valueobject.WrapETHLower(tokenAddress, u.config.ChainID)
addr := valueobject.WrapNativeLower(tokenAddress, u.config.ChainID)
poolTokens = append(poolTokens, &entity.PoolToken{Address: addr, Swappable: true})
reserves = append(reserves, "0")
}
Expand Down
4 changes: 2 additions & 2 deletions pkg/liquidity-source/curve/shared/pools_list_updater.go
Original file line number Diff line number Diff line change
Expand Up @@ -132,8 +132,8 @@ func (u *PoolsListUpdater) GetNewPoolsFromDataSource(ctx context.Context, dataSo
for i := range result.Data.PoolData {
result.Data.PoolData[i].Address = strings.ToLower(result.Data.PoolData[i].Address)
for j := range result.Data.PoolData[i].Coins {
if strings.EqualFold(result.Data.PoolData[i].Coins[j].Address, valueobject.EtherAddress) {
result.Data.PoolData[i].Coins[j].Address = strings.ToLower(valueobject.WETHByChainID[u.config.ChainID])
if strings.EqualFold(result.Data.PoolData[i].Coins[j].Address, valueobject.NativeAddress) {
result.Data.PoolData[i].Coins[j].Address = strings.ToLower(valueobject.WrappedNativeMap[u.config.ChainID])
result.Data.PoolData[i].Coins[j].IsOrgNative = true
}
}
Expand Down
4 changes: 2 additions & 2 deletions pkg/liquidity-source/ether-vista/pool_simulator.go
Original file line number Diff line number Diff line change
Expand Up @@ -79,7 +79,7 @@ func (s *PoolSimulator) CalcAmountOut(param poolpkg.CalcAmountOutParams) (*poolp
}

// Take Router fee if swap from ETH -> Token
if valueobject.IsWETH(param.TokenAmountIn.Token, s.chainID) {
if valueobject.IsWrappedNative(param.TokenAmountIn.Token, s.chainID) {
fee, _ := uint256.FromBig(s.extra.USDCToETHBuyTotalFee)
amountIn.Sub(amountIn, fee)
}
Expand All @@ -103,7 +103,7 @@ func (s *PoolSimulator) CalcAmountOut(param poolpkg.CalcAmountOutParams) (*poolp
}

amountOut := s.getAmountOut(amountIn, reserveIn, reserveOut)
if valueobject.IsWETH(param.TokenOut, s.chainID) {
if valueobject.IsWrappedNative(param.TokenOut, s.chainID) {
fee, _ := uint256.FromBig(s.extra.USDCToETHSellTotalFee)
amountOut.Sub(amountOut, fee)
}
Expand Down
12 changes: 6 additions & 6 deletions pkg/liquidity-source/fluid/dex-t1/pool_list_updater.go
Original file line number Diff line number Diff line change
Expand Up @@ -73,8 +73,8 @@ func (u *PoolsListUpdater) GetNewPools(ctx context.Context, metadataBytes []byte

staticExtraBytes, err := json.Marshal(&StaticExtra{
DexReservesResolver: u.config.DexReservesResolver,
HasNative: strings.EqualFold(curPool.Token0Address.Hex(), valueobject.EtherAddress) ||
strings.EqualFold(curPool.Token1Address.Hex(), valueobject.EtherAddress),
HasNative: strings.EqualFold(curPool.Token0Address.Hex(), valueobject.NativeAddress) ||
strings.EqualFold(curPool.Token1Address.Hex(), valueobject.NativeAddress),
})
if err != nil {
return nil, nil, err
Expand Down Expand Up @@ -112,13 +112,13 @@ func (u *PoolsListUpdater) GetNewPools(ctx context.Context, metadataBytes []byte
},
Tokens: []*entity.PoolToken{
{
Address: valueobject.WrapETHLower(curPool.Token0Address.Hex(), u.config.ChainID),
Address: valueobject.WrapNativeLower(curPool.Token0Address.Hex(), u.config.ChainID),
Weight: 1,
Swappable: true,
Decimals: token0Decimals,
},
{
Address: valueobject.WrapETHLower(curPool.Token1Address.Hex(), u.config.ChainID),
Address: valueobject.WrapNativeLower(curPool.Token1Address.Hex(), u.config.ChainID),
Weight: 1,
Swappable: true,
Decimals: token1Decimals,
Expand Down Expand Up @@ -162,7 +162,7 @@ func (u *PoolsListUpdater) readTokensDecimals(ctx context.Context, token0 common

req := u.ethrpcClient.R().SetContext(ctx)

if strings.EqualFold(valueobject.EtherAddress, token0.String()) {
if strings.EqualFold(valueobject.NativeAddress, token0.String()) {
decimals0 = 18
} else {
req.AddCall(&ethrpc.Call{
Expand All @@ -173,7 +173,7 @@ func (u *PoolsListUpdater) readTokensDecimals(ctx context.Context, token0 common
}, []interface{}{&decimals0})
}

if strings.EqualFold(valueobject.EtherAddress, token1.String()) {
if strings.EqualFold(valueobject.NativeAddress, token1.String()) {
decimals1 = 18
} else {
req.AddCall(&ethrpc.Call{
Expand Down
8 changes: 4 additions & 4 deletions pkg/liquidity-source/fluid/vault-t1/pool_list_updater.go
Original file line number Diff line number Diff line change
Expand Up @@ -44,8 +44,8 @@ func (u *PoolsListUpdater) GetNewPools(ctx context.Context, metadataBytes []byte
for _, swapPath := range paths {
staticExtraBytes, err := json.Marshal(&StaticExtra{
VaultLiquidationResolver: u.config.VaultLiquidationResolver,
HasNative: strings.EqualFold(swapPath.TokenIn.Hex(), valueobject.EtherAddress) ||
strings.EqualFold(swapPath.TokenOut.Hex(), valueobject.EtherAddress),
HasNative: strings.EqualFold(swapPath.TokenIn.Hex(), valueobject.NativeAddress) ||
strings.EqualFold(swapPath.TokenOut.Hex(), valueobject.NativeAddress),
})
if err != nil {
return nil, nil, err
Expand All @@ -57,12 +57,12 @@ func (u *PoolsListUpdater) GetNewPools(ctx context.Context, metadataBytes []byte
Reserves: entity.PoolReserves{"0", "0"},
Tokens: []*entity.PoolToken{
{
Address: valueobject.WrapETHLower(swapPath.TokenIn.Hex(), u.config.ChainID),
Address: valueobject.WrapNativeLower(swapPath.TokenIn.Hex(), u.config.ChainID),
Weight: 1,
Swappable: true,
},
{
Address: valueobject.WrapETHLower(swapPath.TokenOut.Hex(), u.config.ChainID),
Address: valueobject.WrapNativeLower(swapPath.TokenOut.Hex(), u.config.ChainID),
Weight: 1,
Swappable: true,
},
Expand Down
2 changes: 1 addition & 1 deletion pkg/liquidity-source/frax/sfrxeth/pool_list_updater.go
Original file line number Diff line number Diff line change
Expand Up @@ -92,7 +92,7 @@ func (u *PoolsListUpdater) GetNewPools(ctx context.Context, _ []byte) ([]entity.
Timestamp: time.Now().Unix(),
Tokens: []*entity.PoolToken{
{
Address: valueobject.WrapETHLower(valueobject.EtherAddress, u.config.ChainID),
Address: valueobject.WrapNativeLower(valueobject.NativeAddress, u.config.ChainID),
Swappable: true,
},
{
Expand Down
2 changes: 1 addition & 1 deletion pkg/liquidity-source/uniswap-v1/pool_list_updater.go
Original file line number Diff line number Diff line change
Expand Up @@ -198,7 +198,7 @@ func (u *PoolsListUpdater) initPools(exchanges []ExchangeInfo) ([]entity.Pool, e

for _, exchange := range exchanges {
token0 := &entity.PoolToken{
Address: strings.ToLower(valueobject.WETHByChainID[u.config.ChainID]),
Address: strings.ToLower(valueobject.WrappedNativeMap[u.config.ChainID]),
Swappable: true,
}

Expand Down
2 changes: 1 addition & 1 deletion pkg/liquidity-source/uniswap-v1/pool_tracker.go
Original file line number Diff line number Diff line change
Expand Up @@ -119,7 +119,7 @@ func (d *PoolTracker) getReserves(
}

for i, token := range tokens {
if valueobject.IsWETH(token.Address, d.config.ChainID) {
if valueobject.IsWrappedNative(token.Address, d.config.ChainID) {
req.AddCall(&ethrpc.Call{
ABI: multicallABI,
Target: d.config.MulticallContractAddress,
Expand Down
10 changes: 5 additions & 5 deletions pkg/liquidity-source/velocore-v2/cpmm/pools_list_updater.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ import (
"github.com/KyberNetwork/kyberswap-dex-lib/pkg/valueobject"
)

var ErrWETHNotFound = errors.New("WETH not found")
var ErrWrappedNativeNotFound = errors.New("wrapped native not found")

type PoolsListUpdater struct {
config *Config
Expand Down Expand Up @@ -143,13 +143,13 @@ func (d *PoolsListUpdater) processBatch(ctx context.Context, poolAddresses []com
break
}

if strings.EqualFold(t, valueobject.EtherAddress) {
if strings.EqualFold(t, valueobject.NativeAddress) {
nativeTokenIndex = j
weth, ok := valueobject.WETHByChainID[d.config.ChainID]
native, ok := valueobject.WrappedNativeMap[d.config.ChainID]
if !ok {
return nil, ErrWETHNotFound
return nil, ErrWrappedNativeNotFound
}
t = strings.ToLower(weth)
t = strings.ToLower(native)
}

poolTokens = append(poolTokens, &entity.PoolToken{
Expand Down
4 changes: 2 additions & 2 deletions pkg/source/lido-steth/pool_simulator.go
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ type PoolSimulator struct {

func NewPoolSimulator(entityPool entity.Pool, chainID valueobject.ChainID) (*PoolSimulator, error) {
numTokens := len(entityPool.Tokens)
if numTokens != 2 || !valueobject.IsWETH(entityPool.Tokens[0].Address, chainID) {
if numTokens != 2 || !valueobject.IsWrappedNative(entityPool.Tokens[0].Address, chainID) {
return nil, fmt.Errorf("invalid pool tokens %v, %v", entityPool, numTokens)
}
if numTokens != len(entityPool.Reserves) {
Expand Down Expand Up @@ -53,7 +53,7 @@ func (p *PoolSimulator) CalcAmountOut(param pool.CalcAmountOutParams) (*pool.Cal
tokenOut := param.TokenOut
stEth := p.Info.Tokens[1]
// can only swap from ETH to stETH
if !valueobject.IsWETH(tokenAmountIn.Token, p.chainID) || !strings.EqualFold(tokenOut, stEth) {
if !valueobject.IsWrappedNative(tokenAmountIn.Token, p.chainID) || !strings.EqualFold(tokenOut, stEth) {
return nil, fmt.Errorf("Invalid tokenIn/Out %v %v", tokenAmountIn.Token, tokenOut)
}

Expand Down
8 changes: 4 additions & 4 deletions pkg/source/platypus/errors.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,10 +3,10 @@ package platypus
import "errors"

var (
ErrInvalidOracleType = errors.New("invalid oracle type")
ErrDivisionByZero = errors.New("division by zero")
ErrPoolPaused = errors.New("pool is paused")
ErrWETHNotFound = errors.New("weth not found")
ErrInvalidOracleType = errors.New("invalid oracle type")
ErrDivisionByZero = errors.New("division by zero")
ErrPoolPaused = errors.New("pool is paused")
ErrWrappedNativeNotFound = errors.New("wrapped native not found")

// ErrSameAddress swapping with tokenIn = tokenOut
ErrSameAddress = errors.New("SAME_ADDRESS")
Expand Down
8 changes: 4 additions & 4 deletions pkg/source/platypus/pool_simulator.go
Original file line number Diff line number Diff line change
Expand Up @@ -262,19 +262,19 @@ func (p *PoolSimulator) _quoteIdealToAmountSAvax(
toAsset Asset,
fromAmount *big.Int,
) (*big.Int, error) {
weth, ok := valueobject.WETHByChainID[p.ChainID]
native, ok := valueobject.WrappedNativeMap[p.ChainID]
if !ok {
return nil, ErrWETHNotFound
return nil, ErrWrappedNativeNotFound
}

fromToken := fromAsset.UnderlyingToken
toToken := toAsset.UnderlyingToken

if strings.EqualFold(toToken, weth) {
if strings.EqualFold(toToken, native) {
return wmul(fromAmount, p.SAvaxRate), nil
}

if strings.EqualFold(fromToken, weth) {
if strings.EqualFold(fromToken, native) {
return wdiv(fromAmount, p.SAvaxRate)
}

Expand Down
3 changes: 2 additions & 1 deletion pkg/valueobject/address.go
Original file line number Diff line number Diff line change
@@ -1,7 +1,8 @@
package valueobject

const (
EtherAddress = "0xEeeeeEeeeEeEeeEeEeEeeEEEeeeeEeeeeeeeEEeE"
EtherAddress = NativeAddress // deprecated: use the correctly named NativeAddress
NativeAddress = "0xEeeeeEeeeEeEeeEeEeEeeEEEeeeeEeeeeeeeEEeE"
ZeroAddress = "0x0000000000000000000000000000000000000000"
MKRTokenAddress = "0x9f8f72aa9304c8b593d555f12ef6589cc3a579a2"
DAITokenAddress = "0x6b175474e89094c44da98b954eedeac495271d0f"
Expand Down
22 changes: 14 additions & 8 deletions pkg/valueobject/weth.go → pkg/valueobject/wrapped_native.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,13 @@ import (
"strings"
)

var WETHByChainID = map[ChainID]string{
var (
WETHByChainID = WrappedNativeMap // deprecated: use the correctly named WrappedNativeMap
WrapETHLower = WrapNativeLower // deprecated: use the correctly named WrapNativeLower
IsWETH = IsWrappedNative // deprecated: use the correctly named IsWrappedNative
)

var WrappedNativeMap = map[ChainID]string{
ChainIDEthereum: "0xC02aaA39b223FE8D0A0e5C4F27eAD9083C756Cc2",
ChainIDEthereumW: "0xC02aaA39b223FE8D0A0e5C4F27eAD9083C756Cc2",
ChainIDRopsten: "0xc778417E063141139Fce010982780140Aa0cD5Ab",
Expand Down Expand Up @@ -33,17 +39,17 @@ var WETHByChainID = map[ChainID]string{
ChainIDScroll: "0x5300000000000000000000000000000000000004",
ChainIDBlast: "0x4300000000000000000000000000000000000004",
ChainIDMantle: "0x78c1b0C915c4FAA5FffA6CAbf0219DA63d7f4cb8",
ChainIDSonic: "0x309C92261178fA0CF748A855e90Ae73FDb79EBc7",
ChainIDSonic: "0x039e2fB66102314Ce7b64Ce5Ce3E5183bc94aD38",
}

// WrapETHLower wraps, if applicable, native token to wrapped token; and then lowercase it.
func WrapETHLower(token string, chainID ChainID) string {
if strings.EqualFold(token, EtherAddress) {
token = WETHByChainID[chainID]
// WrapNativeLower wraps, if applicable, native token to wrapped token; and then lowercase it.
func WrapNativeLower(token string, chainID ChainID) string {
if strings.EqualFold(token, NativeAddress) {
token = WrappedNativeMap[chainID]
}
return strings.ToLower(token)
}

func IsWETH(address string, chainID ChainID) bool {
return strings.EqualFold(address, WETHByChainID[chainID])
func IsWrappedNative(address string, chainID ChainID) bool {
return strings.EqualFold(address, WrappedNativeMap[chainID])
}

0 comments on commit 55fe428

Please sign in to comment.