Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
13 changes: 8 additions & 5 deletions core/state_transition.go
Original file line number Diff line number Diff line change
Expand Up @@ -312,7 +312,10 @@ func (st *StateTransition) buyAltTokenGas() error {
"fee", mgval,
)

tokenFee := types.EthToAlt(mgval, st.feeRate, st.tokenScale)
tokenFee, err := types.EthToAlt(mgval, st.feeRate, st.tokenScale)
if err != nil {
return err
}
feeLimit := tokenBalance
if st.msg.FeeLimit() != nil && st.msg.FeeLimit().Sign() > 0 {
feeLimit = cmath.BigMin(tokenBalance, st.msg.FeeLimit())
Expand Down Expand Up @@ -435,9 +438,6 @@ func (st *StateTransition) preCheck() error {
if err != nil {
return fmt.Errorf("get token rate failed %v", err)
}
if feeRate == nil || tokenScale == nil || feeRate.Sign() <= 0 || tokenScale.Sign() <= 0 {
return fmt.Errorf("token rate or scale is nil")
}
st.feeRate = feeRate
st.tokenScale = tokenScale
return st.buyAltTokenGas()
Expand Down Expand Up @@ -680,7 +680,10 @@ func (st *StateTransition) refundGas(refundQuotient uint64) {
log.Error("Failed to get token info for gas refund", "tokenID", st.msg.FeeTokenID(), "error", err)
return
}
tokenAmount := types.EthToAlt(remaining, st.feeRate, st.tokenScale)
tokenAmount, err := types.EthToAlt(remaining, st.feeRate, st.tokenScale)
if err != nil {
log.Error("Failed to convert exchange rate", "tokenID", st.msg.FeeTokenID(), "error", err)
}
if err = st.TransferAltTokenHybrid(
tokenInfo,
*st.evm.ChainConfig().Morph.FeeVaultAddress,
Expand Down
21 changes: 12 additions & 9 deletions core/types/token_fee.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,9 @@
package types

import "math/big"
import (
"errors"
"math/big"
)

// SuperAccount dual currency account
type SuperAccount struct {
Expand Down Expand Up @@ -46,35 +49,35 @@ func (dca *SuperAccount) SetAltAmount(id uint16, amount *big.Int) {
}

// EthToAlt altAmount = ethAmount / (tokenRate / tokenScale) = ethAmount * tokenScale / tokenRate
func EthToAlt(ethAmount, rate, tokenScale *big.Int) *big.Int {
func EthToAlt(ethAmount, rate, tokenScale *big.Int) (*big.Int, error) {
if rate == nil || rate.Sign() <= 0 {
panic("invalid token rate")
return nil, errors.New("invalid rate")
}
if tokenScale == nil || tokenScale.Sign() <= 0 {
panic("invalid token scale")
return nil, errors.New("invalid token scale")
}
altAmount := new(big.Int)
remainder := new(big.Int)
altAmount.QuoRem(new(big.Int).Mul(ethAmount, tokenScale), rate, remainder)
if remainder.Sign() != 0 {
altAmount.Add(altAmount, big.NewInt(1))
}
return altAmount
return altAmount, nil
}

// AltToEth ethAmount = altAmount * (tokenRate / tokenScale)
func AltToEth(erc20Amount, rate, tokenScale *big.Int) *big.Int {
func AltToEth(erc20Amount, rate, tokenScale *big.Int) (*big.Int, error) {
if rate == nil || rate.Sign() <= 0 {
panic("invalid token rate")
return nil, errors.New("invalid rate")
}
if tokenScale == nil || tokenScale.Sign() <= 0 {
panic("invalid token scale")
return nil, errors.New("invalid token scale")
}
ethAmount := new(big.Int)
remainder := new(big.Int)
ethAmount.QuoRem(new(big.Int).Mul(erc20Amount, rate), tokenScale, remainder)
if remainder.Sign() != 0 {
ethAmount.Add(ethAmount, big.NewInt(1))
}
return ethAmount
return ethAmount, nil
}
22 changes: 5 additions & 17 deletions rollup/fees/rate.go
Original file line number Diff line number Diff line change
Expand Up @@ -19,38 +19,26 @@ func TokenRate(state StateDB, tokenID uint16) (*big.Int, *big.Int, error) {
log.Error("Failed to get token info from storage", "tokenID", tokenID, "error", err)
return nil, nil, err
}

if info.Scale == nil || info.Scale.Sign() == 0 {
log.Error("Invalid token scale", "tokenID", tokenID, "tokenAddr", info.TokenAddress.Hex())
}

// If token address is zero, this is not a valid token
if info.TokenAddress == (common.Address{}) {
log.Error("Invalid token address", "tokenID", tokenID)
return nil, nil, err
return nil, nil, errors.New("invalid token address")
}

// If price is nil or zero, this token doesn't have a valid price
if rate == nil || rate.Sign() == 0 {
log.Error("Invalid token price", "tokenID", tokenID, "tokenAddr", info.TokenAddress.Hex())
return nil, nil, err
}

return rate, info.Scale, err
return rate, info.Scale, nil
}

func EthToAlt(state StateDB, tokenID uint16, amount *big.Int) (*big.Int, error) {
rate, tokenSacle, err := TokenRate(state, tokenID)
if err != nil {
return nil, err
}
return types.EthToAlt(amount, rate, tokenSacle), nil
return types.EthToAlt(amount, rate, tokenSacle)
}

func AltToETH(state StateDB, tokenID uint16, amount *big.Int) (*big.Int, error) {
rate, tokenSacle, err := TokenRate(state, tokenID)
rate, tokenScale, err := TokenRate(state, tokenID)
if err != nil {
return nil, err
}
return types.AltToEth(amount, rate, tokenSacle), nil
return types.AltToEth(amount, rate, tokenScale)
}