Skip to content

Commit

Permalink
tmp
Browse files Browse the repository at this point in the history
  • Loading branch information
palango committed Oct 31, 2023
1 parent 5d0e2d4 commit 2048c60
Show file tree
Hide file tree
Showing 2 changed files with 30 additions and 34 deletions.
40 changes: 14 additions & 26 deletions core/txpool/legacypool/celo.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,21 +2,23 @@ package legacypool

import (
"errors"
"fmt"
"math/big"

"github.com/ethereum/go-ethereum/accounts/abi/bind"
"github.com/ethereum/go-ethereum/common"
contracts "github.com/ethereum/go-ethereum/contracts/celo"
"github.com/ethereum/go-ethereum/contracts/celo/abigen"
"github.com/ethereum/go-ethereum/core"
"github.com/ethereum/go-ethereum/core/state"
"github.com/ethereum/go-ethereum/log"
"github.com/ethereum/go-ethereum/params"
)

var (
unitRate = big.NewRat(1, 1)
)

type celoContext interface {
IsWhitelisted(feeCurrency common.Address) (bool, error)
IsWhitelisted(feeCurrency common.Address) bool
GetBalanceOf(account common.Address, feeCurrency common.Address) (*big.Int, error)
CompareValue(fee1 *big.Int, feCurrency1 common.Address, fee2 *big.Int, feeCurrency2 common.Address) int
}
Expand All @@ -41,27 +43,13 @@ func newCeloContext(config *params.ChainConfig, state *state.StateDB) *celoConte
return &celoContextImpl{backend: backend, exchangeRates: exchangeRates}
}

// TODO(pl): Can we use the data in `exchangeRate` here?
func (cc *celoContextImpl) IsWhitelisted(feeCurrency common.Address) (bool, error) {
whitelist, err := abigen.NewFeeCurrencyWhitelistCaller(contracts.FeeCurrencyWhitelistAddress, cc.backend)
if err != nil {
return false, fmt.Errorf("failed to access FeeCurrencyWhitelist: %w", err)
}

whitelistedTokens, err := whitelist.GetWhitelist(&bind.CallOpts{})
if err != nil {
return false, fmt.Errorf("failed to get whitelisted tokens: %w", err)
}
for _, tokenAddress := range whitelistedTokens {
if feeCurrency == tokenAddress {
return true, nil
}
}

return false, nil
func (cc *celoContextImpl) IsWhitelisted(feeCurrency common.Address) bool {
_, ok := cc.exchangeRates[feeCurrency]
return ok
}

func (cc *celoContextImpl) GetBalanceOf(account common.Address, feeCurrency common.Address) (*big.Int, error) {
// OPT: Cache fee currency caller?
token, err := abigen.NewFeeCurrencyCaller(feeCurrency, cc.backend)
if err != nil {
return nil, errors.New("failed to access fee currency token")
Expand All @@ -75,7 +63,7 @@ func (cc *celoContextImpl) GetBalanceOf(account common.Address, feeCurrency comm
return balance, nil
}

// CmpValues compares values of potentially different currencies
// Compares values in different currencies
// nil currency => native currency
func (cc *celoContextImpl) CompareValue(val1 *big.Int, feeCurrency1 *common.Address, val2 *big.Int, feeCurrency2 *common.Address) int {
// Short circuit if the fee currency is the same.
Expand All @@ -86,13 +74,13 @@ func (cc *celoContextImpl) CompareValue(val1 *big.Int, feeCurrency1 *common.Addr
var exchangeRate1, exchangeRate2 *big.Rat
ok1, ok2 := true, true
if feeCurrency1 == nil {
exchangeRate1 = big.NewRat(1, 1)
exchangeRate1 = unitRate
} else {
exchangeRate1, ok1 = cc.exchangeRates[*feeCurrency1]
}

if feeCurrency2 == nil {
exchangeRate2 = big.NewRat(1, 1)
exchangeRate2 = unitRate
} else {
exchangeRate2, ok2 = cc.exchangeRates[*feeCurrency2]
}
Expand All @@ -112,9 +100,9 @@ func (cc *celoContextImpl) CompareValue(val1 *big.Int, feeCurrency1 *common.Addr
}

// Below code block is basically evaluating this comparison:
// val1 * echangeRate1.denominator / echangeRate1.numerator < val2 * exchangeRate2.denominator / exchangeRate2.numerator
// val1 * exchangeRate1.denominator / exchangeRate1.numerator < val2 * exchangeRate2.denominator / exchangeRate2.numerator
// It will transform that comparison to this, to remove having to deal with fractional values.
// val1 * echangeRate1.denominator * exchangeRate2.numerator < val2 * exchangeRate2.denominator * c.toCELORate.numerator
// val1 * exchangeRate1.denominator * exchangeRate2.numerator < val2 * exchangeRate2.denominator * exchangeRate1.numerator
leftSide := new(big.Int).Mul(
val1,
new(big.Int).Mul(
Expand Down
24 changes: 16 additions & 8 deletions core/txpool/legacypool/celo_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -11,9 +11,9 @@ func Test_celoContextImpl_CompareFees(t *testing.T) {
currA := common.HexToAddress("0xA")
currB := common.HexToAddress("0xB")

exchangeRates := ExchangeRates{
currA: big.NewRat(1, 2), // token is worth 2 celo
currB: big.NewRat(2, 1), // token is worth 0.5 celo
exchangeRates := common.ExchangeRates{
currA: big.NewRat(47, 100),
currB: big.NewRat(45, 100),
}
type args struct {
val1 *big.Int
Expand Down Expand Up @@ -68,18 +68,18 @@ func Test_celoContextImpl_CompareFees(t *testing.T) {
}, {
name: "Different amounts of mixed currency 1",
args: args{
val1: big.NewInt(2),
val1: big.NewInt(100),
feeCurrency1: nil,
val2: big.NewInt(1),
val2: big.NewInt(47),
feeCurrency2: &currA,
},
want: 0,
}, {
name: "Different amounts of mixed currency 2",
args: args{
val1: big.NewInt(1),
val1: big.NewInt(100),
feeCurrency1: nil,
val2: big.NewInt(2),
val2: big.NewInt(45),
feeCurrency2: &currB,
},
want: 0,
Expand Down Expand Up @@ -112,13 +112,21 @@ func Test_celoContextImpl_CompareFees(t *testing.T) {
feeCurrency2: &currA,
},
want: -1,
}, {
name: "Different amounts of different currencies",
args: args{
val1: big.NewInt(1),
feeCurrency1: &currA,
val2: big.NewInt(1),
feeCurrency2: &currB,
},
want: -1,
},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
cc := &celoContextImpl{
backend: nil,
registry: nil,
exchangeRates: exchangeRates,
}
if got := cc.CompareValue(tt.args.val1, tt.args.feeCurrency1, tt.args.val2, tt.args.feeCurrency2); got != tt.want {
Expand Down

0 comments on commit 2048c60

Please sign in to comment.