chore_: rename from and to props of the PathV2 to from and to chain

This commit is contained in:
Sale Djenic 2024-06-05 10:36:27 +02:00 committed by saledjenic
parent 99ead2d90c
commit 2149035619
3 changed files with 114 additions and 114 deletions

View File

@ -57,16 +57,16 @@ func isValidForNetworkComplianceV2(route []*PathV2, fromIncluded, fromExcluded m
)
for _, path := range route {
if path == nil || path.From == nil {
if path == nil || path.FromChain == nil {
logger.Debug("Invalid path", zap.Any("path", path))
return false
}
if _, ok := fromExcluded[path.From.ChainID]; ok {
logger.Debug("Excluded chain ID", zap.Uint64("chainID", path.From.ChainID))
if _, ok := fromExcluded[path.FromChain.ChainID]; ok {
logger.Debug("Excluded chain ID", zap.Uint64("chainID", path.FromChain.ChainID))
return false
}
if _, ok := fromIncluded[path.From.ChainID]; ok {
fromIncluded[path.From.ChainID] = true
if _, ok := fromIncluded[path.FromChain.ChainID]; ok {
fromIncluded[path.FromChain.ChainID] = true
}
}
@ -112,7 +112,7 @@ func filterCapacityValidationV2(routes [][]*PathV2, amountIn *big.Int, fromLocke
// hasSufficientCapacityV2 checks if a route has sufficient capacity to handle the required amount.
func hasSufficientCapacityV2(route []*PathV2, amountIn *big.Int, fromLockedAmount map[uint64]*hexutil.Big) bool {
for _, path := range route {
if amount, ok := fromLockedAmount[path.From.ChainID]; ok {
if amount, ok := fromLockedAmount[path.FromChain.ChainID]; ok {
requiredAmountIn := new(big.Int).Sub(amountIn, amount.ToInt())
restAmountIn := calculateRestAmountInV2(route, path)

View File

@ -25,12 +25,12 @@ var (
amount4 = hexutil.Big(*big.NewInt(400))
amount5 = hexutil.Big(*big.NewInt(500))
path0 = &PathV2{From: network4, AmountIn: &amount0}
path1 = &PathV2{From: network1, AmountIn: &amount1}
path2 = &PathV2{From: network2, AmountIn: &amount2}
path3 = &PathV2{From: network3, AmountIn: &amount3}
path4 = &PathV2{From: network4, AmountIn: &amount4}
path5 = &PathV2{From: network5, AmountIn: &amount5}
path0 = &PathV2{FromChain: network4, AmountIn: &amount0}
path1 = &PathV2{FromChain: network1, AmountIn: &amount1}
path2 = &PathV2{FromChain: network2, AmountIn: &amount2}
path3 = &PathV2{FromChain: network3, AmountIn: &amount3}
path4 = &PathV2{FromChain: network4, AmountIn: &amount4}
path5 = &PathV2{FromChain: network5, AmountIn: &amount5}
)
func routesEqual(t *testing.T, expected, actual [][]*PathV2) bool {
@ -58,8 +58,8 @@ func pathsEqual(t *testing.T, expected, actual []*PathV2) bool {
}
func pathEqual(t *testing.T, expected, actual *PathV2) bool {
if expected.From.ChainID != actual.From.ChainID {
t.Logf("expected chain ID '%d' , actual chain ID '%d'", expected.From.ChainID, actual.From.ChainID)
if expected.FromChain.ChainID != actual.FromChain.ChainID {
t.Logf("expected chain ID '%d' , actual chain ID '%d'", expected.FromChain.ChainID, actual.FromChain.ChainID)
return false
}
if expected.AmountIn.ToInt().Cmp(actual.AmountIn.ToInt()) != 0 {
@ -367,17 +367,17 @@ func TestFilterNetworkComplianceV2(t *testing.T) {
name: "Mixed routes with valid and invalid paths",
routes: [][]*PathV2{
{
{From: network1},
{From: network3},
{FromChain: network1},
{FromChain: network3},
},
{
{From: network2},
{From: network3},
{FromChain: network2},
{FromChain: network3},
},
{
{From: network1},
{From: network2},
{From: network3},
{FromChain: network1},
{FromChain: network2},
{FromChain: network3},
},
},
fromLockedAmount: map[uint64]*hexutil.Big{
@ -386,8 +386,8 @@ func TestFilterNetworkComplianceV2(t *testing.T) {
},
expected: [][]*PathV2{
{
{From: network1},
{From: network3},
{FromChain: network1},
{FromChain: network3},
},
},
},
@ -395,12 +395,12 @@ func TestFilterNetworkComplianceV2(t *testing.T) {
name: "All valid routes",
routes: [][]*PathV2{
{
{From: network1},
{From: network3},
{FromChain: network1},
{FromChain: network3},
},
{
{From: network1},
{From: network4},
{FromChain: network1},
{FromChain: network4},
},
},
fromLockedAmount: map[uint64]*hexutil.Big{
@ -408,12 +408,12 @@ func TestFilterNetworkComplianceV2(t *testing.T) {
},
expected: [][]*PathV2{
{
{From: network1},
{From: network3},
{FromChain: network1},
{FromChain: network3},
},
{
{From: network1},
{From: network4},
{FromChain: network1},
{FromChain: network4},
},
},
},
@ -421,12 +421,12 @@ func TestFilterNetworkComplianceV2(t *testing.T) {
name: "All invalid routes",
routes: [][]*PathV2{
{
{From: network2},
{From: network3},
{FromChain: network2},
{FromChain: network3},
},
{
{From: network4},
{From: network5},
{FromChain: network4},
{FromChain: network5},
},
},
fromLockedAmount: map[uint64]*hexutil.Big{
@ -447,23 +447,23 @@ func TestFilterNetworkComplianceV2(t *testing.T) {
name: "No locked amounts",
routes: [][]*PathV2{
{
{From: network1},
{From: network2},
{FromChain: network1},
{FromChain: network2},
},
{
{From: network3},
{From: network4},
{FromChain: network3},
{FromChain: network4},
},
},
fromLockedAmount: map[uint64]*hexutil.Big{},
expected: [][]*PathV2{
{
{From: network1},
{From: network2},
{FromChain: network1},
{FromChain: network2},
},
{
{From: network3},
{From: network4},
{FromChain: network3},
{FromChain: network4},
},
},
},
@ -471,9 +471,9 @@ func TestFilterNetworkComplianceV2(t *testing.T) {
name: "Single route with mixed valid and invalid paths",
routes: [][]*PathV2{
{
{From: network1},
{From: network2},
{From: network3},
{FromChain: network1},
{FromChain: network2},
{FromChain: network3},
},
},
fromLockedAmount: map[uint64]*hexutil.Big{
@ -486,9 +486,9 @@ func TestFilterNetworkComplianceV2(t *testing.T) {
name: "Routes with duplicate chain IDs",
routes: [][]*PathV2{
{
{From: network1},
{From: network1},
{From: network2},
{FromChain: network1},
{FromChain: network1},
{FromChain: network2},
},
},
fromLockedAmount: map[uint64]*hexutil.Big{
@ -496,9 +496,9 @@ func TestFilterNetworkComplianceV2(t *testing.T) {
},
expected: [][]*PathV2{
{
{From: network1},
{From: network1},
{From: network2},
{FromChain: network1},
{FromChain: network1},
{FromChain: network2},
},
},
},
@ -506,8 +506,8 @@ func TestFilterNetworkComplianceV2(t *testing.T) {
name: "Minimum and maximum chain IDs",
routes: [][]*PathV2{
{
{From: &params.Network{ChainID: 0}},
{From: &params.Network{ChainID: ^uint64(0)}},
{FromChain: &params.Network{ChainID: 0}},
{FromChain: &params.Network{ChainID: ^uint64(0)}},
},
},
fromLockedAmount: map[uint64]*hexutil.Big{
@ -516,8 +516,8 @@ func TestFilterNetworkComplianceV2(t *testing.T) {
},
expected: [][]*PathV2{
{
{From: &params.Network{ChainID: 0}},
{From: &params.Network{ChainID: ^uint64(0)}},
{FromChain: &params.Network{ChainID: 0}},
{FromChain: &params.Network{ChainID: ^uint64(0)}},
},
},
},
@ -527,8 +527,8 @@ func TestFilterNetworkComplianceV2(t *testing.T) {
var routes [][]*PathV2
for i := 0; i < 1000; i++ {
routes = append(routes, []*PathV2{
{From: &params.Network{ChainID: uint64(i + 1)}},
{From: &params.Network{ChainID: uint64(i + 1001)}},
{FromChain: &params.Network{ChainID: uint64(i + 1)}},
{FromChain: &params.Network{ChainID: uint64(i + 1001)}},
})
}
return routes
@ -541,8 +541,8 @@ func TestFilterNetworkComplianceV2(t *testing.T) {
var routes [][]*PathV2
for i := 0; i < 1; i++ {
routes = append(routes, []*PathV2{
{From: &params.Network{ChainID: uint64(i + 1)}},
{From: &params.Network{ChainID: uint64(i + 1001)}},
{FromChain: &params.Network{ChainID: uint64(i + 1)}},
{FromChain: &params.Network{ChainID: uint64(i + 1001)}},
})
}
return routes
@ -552,12 +552,12 @@ func TestFilterNetworkComplianceV2(t *testing.T) {
name: "Routes with missing data",
routes: [][]*PathV2{
{
{From: nil},
{From: network2},
{FromChain: nil},
{FromChain: network2},
},
{
{From: network1},
{From: nil},
{FromChain: network1},
{FromChain: nil},
},
},
fromLockedAmount: map[uint64]*hexutil.Big{
@ -570,12 +570,12 @@ func TestFilterNetworkComplianceV2(t *testing.T) {
name: "Consistency check",
routes: [][]*PathV2{
{
{From: network1},
{From: network2},
{FromChain: network1},
{FromChain: network2},
},
{
{From: network1},
{From: network3},
{FromChain: network1},
{FromChain: network3},
},
},
fromLockedAmount: map[uint64]*hexutil.Big{
@ -583,12 +583,12 @@ func TestFilterNetworkComplianceV2(t *testing.T) {
},
expected: [][]*PathV2{
{
{From: network1},
{From: network2},
{FromChain: network1},
{FromChain: network2},
},
{
{From: network1},
{From: network3},
{FromChain: network1},
{FromChain: network3},
},
},
},
@ -690,12 +690,12 @@ func TestFilterCapacityValidationV2(t *testing.T) {
name: "Sufficient capacity with multiple paths",
routes: [][]*PathV2{
{
{From: network1, AmountIn: (*hexutil.Big)(big.NewInt(100))},
{From: network2, AmountIn: (*hexutil.Big)(big.NewInt(200))},
{FromChain: network1, AmountIn: (*hexutil.Big)(big.NewInt(100))},
{FromChain: network2, AmountIn: (*hexutil.Big)(big.NewInt(200))},
},
{
{From: network1, AmountIn: (*hexutil.Big)(big.NewInt(50))},
{From: network2, AmountIn: (*hexutil.Big)(big.NewInt(100))},
{FromChain: network1, AmountIn: (*hexutil.Big)(big.NewInt(50))},
{FromChain: network2, AmountIn: (*hexutil.Big)(big.NewInt(100))},
},
},
amountIn: big.NewInt(150),
@ -705,12 +705,12 @@ func TestFilterCapacityValidationV2(t *testing.T) {
},
expectedRoutes: [][]*PathV2{
{
{From: network1, AmountIn: (*hexutil.Big)(big.NewInt(50)), AmountInLocked: true},
{From: network2, AmountIn: (*hexutil.Big)(big.NewInt(100)), AmountInLocked: true},
{FromChain: network1, AmountIn: (*hexutil.Big)(big.NewInt(50)), AmountInLocked: true},
{FromChain: network2, AmountIn: (*hexutil.Big)(big.NewInt(100)), AmountInLocked: true},
},
{
{From: network1, AmountIn: (*hexutil.Big)(big.NewInt(50)), AmountInLocked: true},
{From: network2, AmountIn: (*hexutil.Big)(big.NewInt(100)), AmountInLocked: true},
{FromChain: network1, AmountIn: (*hexutil.Big)(big.NewInt(50)), AmountInLocked: true},
{FromChain: network2, AmountIn: (*hexutil.Big)(big.NewInt(100)), AmountInLocked: true},
},
},
},
@ -718,8 +718,8 @@ func TestFilterCapacityValidationV2(t *testing.T) {
name: "Insufficient capacity",
routes: [][]*PathV2{
{
{From: network1, AmountIn: (*hexutil.Big)(big.NewInt(100))},
{From: network2, AmountIn: (*hexutil.Big)(big.NewInt(50))},
{FromChain: network1, AmountIn: (*hexutil.Big)(big.NewInt(100))},
{FromChain: network2, AmountIn: (*hexutil.Big)(big.NewInt(50))},
},
},
amountIn: big.NewInt(200),
@ -733,8 +733,8 @@ func TestFilterCapacityValidationV2(t *testing.T) {
name: "Exact capacity match",
routes: [][]*PathV2{
{
{From: network1, AmountIn: (*hexutil.Big)(big.NewInt(100))},
{From: network2, AmountIn: (*hexutil.Big)(big.NewInt(50))},
{FromChain: network1, AmountIn: (*hexutil.Big)(big.NewInt(100))},
{FromChain: network2, AmountIn: (*hexutil.Big)(big.NewInt(50))},
},
},
amountIn: big.NewInt(150),
@ -744,8 +744,8 @@ func TestFilterCapacityValidationV2(t *testing.T) {
},
expectedRoutes: [][]*PathV2{
{
{From: network1, AmountIn: (*hexutil.Big)(big.NewInt(100)), AmountInLocked: true},
{From: network2, AmountIn: (*hexutil.Big)(big.NewInt(50)), AmountInLocked: true},
{FromChain: network1, AmountIn: (*hexutil.Big)(big.NewInt(100)), AmountInLocked: true},
{FromChain: network2, AmountIn: (*hexutil.Big)(big.NewInt(50)), AmountInLocked: true},
},
},
},
@ -753,16 +753,16 @@ func TestFilterCapacityValidationV2(t *testing.T) {
name: "No locked amounts",
routes: [][]*PathV2{
{
{From: network1, AmountIn: (*hexutil.Big)(big.NewInt(100))},
{From: network2, AmountIn: (*hexutil.Big)(big.NewInt(50))},
{FromChain: network1, AmountIn: (*hexutil.Big)(big.NewInt(100))},
{FromChain: network2, AmountIn: (*hexutil.Big)(big.NewInt(50))},
},
},
amountIn: big.NewInt(150),
fromLockedAmount: map[uint64]*hexutil.Big{},
expectedRoutes: [][]*PathV2{
{
{From: network1, AmountIn: (*hexutil.Big)(big.NewInt(100)), AmountInLocked: false},
{From: network2, AmountIn: (*hexutil.Big)(big.NewInt(50)), AmountInLocked: false},
{FromChain: network1, AmountIn: (*hexutil.Big)(big.NewInt(100)), AmountInLocked: false},
{FromChain: network2, AmountIn: (*hexutil.Big)(big.NewInt(50)), AmountInLocked: false},
},
},
},
@ -771,7 +771,7 @@ func TestFilterCapacityValidationV2(t *testing.T) {
name: "Single route with sufficient capacity",
routes: [][]*PathV2{
{
{From: network1, AmountIn: (*hexutil.Big)(big.NewInt(200))},
{FromChain: network1, AmountIn: (*hexutil.Big)(big.NewInt(200))},
},
},
amountIn: big.NewInt(150),
@ -784,7 +784,7 @@ func TestFilterCapacityValidationV2(t *testing.T) {
name: "Single route with insufficient capacity",
routes: [][]*PathV2{
{
{From: network1, AmountIn: (*hexutil.Big)(big.NewInt(100))},
{FromChain: network1, AmountIn: (*hexutil.Big)(big.NewInt(100))},
},
},
amountIn: big.NewInt(150),
@ -807,8 +807,8 @@ func TestFilterCapacityValidationV2(t *testing.T) {
name: "Routes with duplicate chain IDs",
routes: [][]*PathV2{
{
{From: network1, AmountIn: (*hexutil.Big)(big.NewInt(100))},
{From: network1, AmountIn: (*hexutil.Big)(big.NewInt(100))},
{FromChain: network1, AmountIn: (*hexutil.Big)(big.NewInt(100))},
{FromChain: network1, AmountIn: (*hexutil.Big)(big.NewInt(100))},
},
},
amountIn: big.NewInt(150),
@ -821,9 +821,9 @@ func TestFilterCapacityValidationV2(t *testing.T) {
name: "Partial locked amounts",
routes: [][]*PathV2{
{
{From: network1, AmountIn: (*hexutil.Big)(big.NewInt(100))},
{From: network2, AmountIn: (*hexutil.Big)(big.NewInt(100))},
{From: network3, AmountIn: (*hexutil.Big)(big.NewInt(200))},
{FromChain: network1, AmountIn: (*hexutil.Big)(big.NewInt(100))},
{FromChain: network2, AmountIn: (*hexutil.Big)(big.NewInt(100))},
{FromChain: network3, AmountIn: (*hexutil.Big)(big.NewInt(200))},
},
},
amountIn: big.NewInt(250),
@ -838,8 +838,8 @@ func TestFilterCapacityValidationV2(t *testing.T) {
name: "Mixed networks with sufficient capacity",
routes: [][]*PathV2{
{
{From: network1, AmountIn: (*hexutil.Big)(big.NewInt(100))},
{From: network3, AmountIn: (*hexutil.Big)(big.NewInt(200))},
{FromChain: network1, AmountIn: (*hexutil.Big)(big.NewInt(100))},
{FromChain: network3, AmountIn: (*hexutil.Big)(big.NewInt(200))},
},
},
amountIn: big.NewInt(250),
@ -849,8 +849,8 @@ func TestFilterCapacityValidationV2(t *testing.T) {
},
expectedRoutes: [][]*PathV2{
{
{From: network1, AmountIn: (*hexutil.Big)(big.NewInt(100)), AmountInLocked: true},
{From: network3, AmountIn: (*hexutil.Big)(big.NewInt(200)), AmountInLocked: true},
{FromChain: network1, AmountIn: (*hexutil.Big)(big.NewInt(100)), AmountInLocked: true},
{FromChain: network3, AmountIn: (*hexutil.Big)(big.NewInt(200)), AmountInLocked: true},
},
},
},
@ -858,8 +858,8 @@ func TestFilterCapacityValidationV2(t *testing.T) {
name: "Mixed networks with insufficient capacity",
routes: [][]*PathV2{
{
{From: network1, AmountIn: (*hexutil.Big)(big.NewInt(100))},
{From: network3, AmountIn: (*hexutil.Big)(big.NewInt(100))},
{FromChain: network1, AmountIn: (*hexutil.Big)(big.NewInt(100))},
{FromChain: network3, AmountIn: (*hexutil.Big)(big.NewInt(100))},
},
},
amountIn: big.NewInt(250),

View File

@ -32,8 +32,8 @@ type RouteInputParams struct {
type PathV2 struct {
BridgeName string
From *params.Network // Source chain
To *params.Network // Destination chain
FromChain *params.Network // Source chain
ToChain *params.Network // Destination chain
FromToken *walletToken.Token // Token on the source chain
AmountIn *hexutil.Big // Amount that will be sent from the source chain
AmountInLocked bool // Is the amount locked
@ -63,7 +63,7 @@ type PathV2 struct {
}
func (p *PathV2) Equal(o *PathV2) bool {
return p.From.ChainID == o.From.ChainID && p.To.ChainID == o.To.ChainID
return p.FromChain.ChainID == o.FromChain.ChainID && p.ToChain.ChainID == o.ToChain.ChainID
}
type SuggestedRoutesV2 struct {
@ -134,7 +134,7 @@ func buildGraphV2(AmountIn *big.Int, routes []*PathV2, level int, sourceChainIDs
for _, route := range routes {
found := false
for _, chainID := range sourceChainIDs {
if chainID == route.From.ChainID {
if chainID == route.FromChain.ChainID {
found = true
break
}
@ -156,7 +156,7 @@ func buildGraphV2(AmountIn *big.Int, routes []*PathV2, level int, sourceChainIDs
if newAmountIn.Sign() > 0 {
newSourceChainIDs := make([]uint64, len(sourceChainIDs))
copy(newSourceChainIDs, sourceChainIDs)
newSourceChainIDs = append(newSourceChainIDs, route.From.ChainID)
newSourceChainIDs = append(newSourceChainIDs, route.FromChain.ChainID)
node.Children = buildGraphV2(newAmountIn, newRoutes, level+1, newSourceChainIDs)
if len(node.Children) == 0 {
@ -422,8 +422,8 @@ func (r *Router) SuggestedRoutesV2(ctx context.Context, input *RouteInputParams)
mu.Lock()
candidates = append(candidates, &PathV2{
BridgeName: bridge.Name(),
From: network,
To: network,
FromChain: network,
ToChain: network,
FromToken: token,
AmountIn: (*hexutil.Big)(amountToSend),
AmountInLocked: amountLocked,
@ -471,12 +471,12 @@ func (r *Router) SuggestedRoutesV2(ctx context.Context, input *RouteInputParams)
if path.requiredTokenBalance != nil && path.requiredTokenBalance.Cmp(big.NewInt(0)) > 0 {
tokenBalance := big.NewInt(1)
if input.SendType == ERC1155Transfer {
tokenBalance, err = r.getERC1155Balance(ctx, path.From, path.FromToken, input.AddrFrom)
tokenBalance, err = r.getERC1155Balance(ctx, path.FromChain, path.FromToken, input.AddrFrom)
if err != nil {
return nil, err
}
} else if input.SendType != ERC721Transfer {
tokenBalance, err = r.getBalance(ctx, path.From, path.FromToken, input.AddrFrom)
tokenBalance, err = r.getBalance(ctx, path.FromChain, path.FromToken, input.AddrFrom)
if err != nil {
return nil, err
}
@ -487,12 +487,12 @@ func (r *Router) SuggestedRoutesV2(ctx context.Context, input *RouteInputParams)
}
}
nativeToken := r.tokenManager.FindToken(path.From, path.From.NativeCurrencySymbol)
nativeToken := r.tokenManager.FindToken(path.FromChain, path.FromChain.NativeCurrencySymbol)
if nativeToken == nil {
return nil, errors.New("native token not found")
}
nativeBalance, err := r.getBalance(ctx, path.From, nativeToken, input.AddrFrom)
nativeBalance, err := r.getBalance(ctx, path.FromChain, nativeToken, input.AddrFrom)
if err != nil {
return nil, err
}