forked from ethereum-optimism/op-geth
-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add tests Rebase tmp Add error PR review CHanges after discussion
- Loading branch information
Showing
6 changed files
with
276 additions
and
7 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,92 @@ | ||
package legacypool | ||
|
||
import ( | ||
"errors" | ||
"fmt" | ||
"math/big" | ||
|
||
"github.com/ethereum/go-ethereum/accounts/abi/bind" | ||
"github.com/ethereum/go-ethereum/common" | ||
"github.com/ethereum/go-ethereum/contracts/celo/abigen" | ||
) | ||
|
||
var ( | ||
unitRate = big.NewRat(1, 1) | ||
) | ||
|
||
// IsWhitelisted checks if a given fee currency is whitelisted | ||
func IsWhitelisted(exchangeRates common.ExchangeRates, feeCurrency *common.Address) bool { | ||
if feeCurrency == nil { | ||
return true | ||
} | ||
_, ok := exchangeRates[*feeCurrency] | ||
return ok | ||
} | ||
|
||
// Compares values in different currencies | ||
// nil currency => native currency | ||
func CompareValue(exchangeRates common.ExchangeRates, val1 *big.Int, feeCurrency1 *common.Address, val2 *big.Int, feeCurrency2 *common.Address) (int, error) { | ||
// Short circuit if the fee currency is the same. | ||
if areEqualAddresses(feeCurrency1, feeCurrency2) { | ||
return val1.Cmp(val2), nil | ||
} | ||
|
||
var exchangeRate1, exchangeRate2 *big.Rat | ||
var ok bool | ||
if feeCurrency1 == nil { | ||
exchangeRate1 = unitRate | ||
} else { | ||
exchangeRate1, ok = exchangeRates[*feeCurrency1] | ||
if !ok { | ||
return 0, fmt.Errorf("fee currency not registered: %s", feeCurrency1.Hex()) | ||
} | ||
} | ||
|
||
if feeCurrency2 == nil { | ||
exchangeRate2 = unitRate | ||
} else { | ||
exchangeRate2, ok = exchangeRates[*feeCurrency2] | ||
if !ok { | ||
return 0, fmt.Errorf("fee currency not registered: %s", feeCurrency1.Hex()) | ||
} | ||
} | ||
|
||
// Below code block is basically evaluating this comparison: | ||
// 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 * exchangeRate1.denominator * exchangeRate2.numerator < val2 * exchangeRate2.denominator * exchangeRate1.numerator | ||
leftSide := new(big.Int).Mul( | ||
val1, | ||
new(big.Int).Mul( | ||
exchangeRate1.Denom(), | ||
exchangeRate2.Num(), | ||
), | ||
) | ||
rightSide := new(big.Int).Mul( | ||
val2, | ||
new(big.Int).Mul( | ||
exchangeRate2.Denom(), | ||
exchangeRate1.Num(), | ||
), | ||
) | ||
|
||
return leftSide.Cmp(rightSide), nil | ||
} | ||
|
||
func areEqualAddresses(addr1, addr2 *common.Address) bool { | ||
return (addr1 == nil && addr2 == nil) || (addr1 != nil && addr2 != nil && *addr1 == *addr2) | ||
} | ||
|
||
func GetBalanceOf(backend *bind.ContractCaller, account common.Address, feeCurrency common.Address) (*big.Int, error) { | ||
token, err := abigen.NewFeeCurrencyCaller(feeCurrency, *backend) | ||
if err != nil { | ||
return nil, errors.New("failed to access fee currency token") | ||
} | ||
|
||
balance, err := token.BalanceOf(&bind.CallOpts{}, account) | ||
if err != nil { | ||
return nil, errors.New("failed to access token balance") | ||
} | ||
|
||
return balance, nil | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,175 @@ | ||
package legacypool | ||
|
||
import ( | ||
"math/big" | ||
"testing" | ||
|
||
"github.com/ethereum/go-ethereum/common" | ||
) | ||
|
||
var ( | ||
currA = common.HexToAddress("0xA") | ||
currB = common.HexToAddress("0xB") | ||
currX = common.HexToAddress("0xF") | ||
exchangeRates = common.ExchangeRates{ | ||
currA: big.NewRat(47, 100), | ||
currB: big.NewRat(45, 100), | ||
} | ||
) | ||
|
||
func TestCompareFees(t *testing.T) { | ||
type args struct { | ||
val1 *big.Int | ||
feeCurrency1 *common.Address | ||
val2 *big.Int | ||
feeCurrency2 *common.Address | ||
} | ||
tests := []struct { | ||
name string | ||
args args | ||
wantResult int | ||
wantErr bool | ||
}{ | ||
// Native currency | ||
{ | ||
name: "Same amount of native currency", | ||
args: args{ | ||
val1: big.NewInt(1), | ||
feeCurrency1: nil, | ||
val2: big.NewInt(1), | ||
feeCurrency2: nil, | ||
}, | ||
wantResult: 0, | ||
}, { | ||
name: "Different amounts of native currency 1", | ||
args: args{ | ||
val1: big.NewInt(2), | ||
feeCurrency1: nil, | ||
val2: big.NewInt(1), | ||
feeCurrency2: nil, | ||
}, | ||
wantResult: 1, | ||
}, { | ||
name: "Different amounts of native currency 2", | ||
args: args{ | ||
val1: big.NewInt(1), | ||
feeCurrency1: nil, | ||
val2: big.NewInt(5), | ||
feeCurrency2: nil, | ||
}, | ||
wantResult: -1, | ||
}, | ||
// Mixed currency | ||
{ | ||
name: "Same amount of mixed currency", | ||
args: args{ | ||
val1: big.NewInt(1), | ||
feeCurrency1: nil, | ||
val2: big.NewInt(1), | ||
feeCurrency2: &currA, | ||
}, | ||
wantResult: -1, | ||
}, { | ||
name: "Different amounts of mixed currency 1", | ||
args: args{ | ||
val1: big.NewInt(100), | ||
feeCurrency1: nil, | ||
val2: big.NewInt(47), | ||
feeCurrency2: &currA, | ||
}, | ||
wantResult: 0, | ||
}, { | ||
name: "Different amounts of mixed currency 2", | ||
args: args{ | ||
val1: big.NewInt(100), | ||
feeCurrency1: nil, | ||
val2: big.NewInt(45), | ||
feeCurrency2: &currB, | ||
}, | ||
wantResult: 0, | ||
}, | ||
// Two fee currencies | ||
{ | ||
name: "Same amount of same currency", | ||
args: args{ | ||
val1: big.NewInt(1), | ||
feeCurrency1: &currA, | ||
val2: big.NewInt(1), | ||
feeCurrency2: &currA, | ||
}, | ||
wantResult: 0, | ||
}, { | ||
name: "Different amounts of same currency 1", | ||
args: args{ | ||
val1: big.NewInt(3), | ||
feeCurrency1: &currA, | ||
val2: big.NewInt(1), | ||
feeCurrency2: &currA, | ||
}, | ||
wantResult: 1, | ||
}, { | ||
name: "Different amounts of same currency 2", | ||
args: args{ | ||
val1: big.NewInt(1), | ||
feeCurrency1: &currA, | ||
val2: big.NewInt(7), | ||
feeCurrency2: &currA, | ||
}, | ||
wantResult: -1, | ||
}, | ||
// Unregistered fee currency | ||
{ | ||
name: "Different amounts of different currencies", | ||
args: args{ | ||
val1: big.NewInt(1), | ||
feeCurrency1: &currA, | ||
val2: big.NewInt(1), | ||
feeCurrency2: &currX, | ||
}, | ||
wantErr: true, | ||
}, | ||
} | ||
for _, tt := range tests { | ||
t.Run(tt.name, func(t *testing.T) { | ||
got, err := CompareValue(exchangeRates, tt.args.val1, tt.args.feeCurrency1, tt.args.val2, tt.args.feeCurrency2) | ||
|
||
if tt.wantErr && err == nil { | ||
t.Error("Expected error in celoContextImpl.CompareFees") | ||
} | ||
if got != tt.wantResult { | ||
t.Errorf("celoContextImpl.CompareFees() = %v, want %v", got, tt.wantResult) | ||
} | ||
}) | ||
} | ||
} | ||
|
||
func TestIsWhitelisted(t *testing.T) { | ||
tests := []struct { | ||
name string | ||
feeCurrency *common.Address | ||
want bool | ||
}{ | ||
{ | ||
name: "no fee currency", | ||
feeCurrency: nil, | ||
want: true, | ||
}, | ||
{ | ||
name: "valid fee currency", | ||
feeCurrency: &currA, | ||
want: true, | ||
}, | ||
{ | ||
name: "invalid fee currency", | ||
feeCurrency: &currX, | ||
want: false, | ||
}, | ||
} | ||
for _, tt := range tests { | ||
t.Run(tt.name, func(t *testing.T) { | ||
if got := IsWhitelisted(exchangeRates, tt.feeCurrency); got != tt.want { | ||
t.Errorf("IsWhitelisted() = %v, want %v", got, tt.want) | ||
} | ||
}) | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters