diff --git a/protostar.toml b/protostar.toml index 8082724..0a6272d 100644 --- a/protostar.toml +++ b/protostar.toml @@ -1,5 +1,5 @@ ["protostar.config"] -protostar_version = "0.3.0" +protostar_version = "0.4.2" ["protostar.project"] libs_path = "lib" diff --git a/src/contracts/amm_wrapper.cairo b/src/contracts/amm_wrapper.cairo index b863f95..ae6efa7 100644 --- a/src/contracts/amm_wrapper.cairo +++ b/src/contracts/amm_wrapper.cairo @@ -5,51 +5,51 @@ from src.contracts.amm_wrapper_library import AmmWrapper from starkware.cairo.common.uint256 import Uint256 @constructor -func constructor{syscall_ptr : felt*, pedersen_ptr : HashBuiltin*, range_check_ptr}( - jediswap_router : felt, jediswap_factory : felt -): - AmmWrapper.initializer(jediswap_router, jediswap_factory) - return () -end +func constructor{syscall_ptr: felt*, pedersen_ptr: HashBuiltin*, range_check_ptr}( + jediswap_router: felt, jediswap_factory: felt +) { + AmmWrapper.initializer(jediswap_router, jediswap_factory); + return (); +} @view -func get_pair{syscall_ptr : felt*, pedersen_ptr : HashBuiltin*, range_check_ptr}( - token0 : felt, token1 : felt -) -> (pair : felt): - return AmmWrapper.get_pair(token0, token1) -end +func get_pair{syscall_ptr: felt*, pedersen_ptr: HashBuiltin*, range_check_ptr}( + token0: felt, token1: felt +) -> (pair: felt) { + return AmmWrapper.get_pair(token0, token1); +} @view -func get_all_pairs{syscall_ptr : felt*, pedersen_ptr : HashBuiltin*, range_check_ptr}() -> ( - all_pairs_len : felt, all_pairs : felt* -): - return AmmWrapper.get_all_pairs() -end +func get_all_pairs{syscall_ptr: felt*, pedersen_ptr: HashBuiltin*, range_check_ptr}() -> ( + all_pairs_len: felt, all_pairs: felt* +) { + return AmmWrapper.get_all_pairs(); +} @view -func get_pair_token0{syscall_ptr : felt*, pedersen_ptr : HashBuiltin*, range_check_ptr}( - pair : felt -) -> (token0 : felt): - return AmmWrapper.get_pair_token0(pair) -end +func get_pair_token0{syscall_ptr: felt*, pedersen_ptr: HashBuiltin*, range_check_ptr}( + pair: felt +) -> (token0: felt) { + return AmmWrapper.get_pair_token0(pair); +} @view -func get_pair_token1{syscall_ptr : felt*, pedersen_ptr : HashBuiltin*, range_check_ptr}( - pair : felt -) -> (token1 : felt): - return AmmWrapper.get_pair_token1(pair) -end +func get_pair_token1{syscall_ptr: felt*, pedersen_ptr: HashBuiltin*, range_check_ptr}( + pair: felt +) -> (token1: felt) { + return AmmWrapper.get_pair_token1(pair); +} @view -func get_pair_reserves{syscall_ptr : felt*, pedersen_ptr : HashBuiltin*, range_check_ptr}( - pair : felt -) -> (reserve_0 : Uint256, reserve_1 : Uint256): - return AmmWrapper.get_pair_reserves(pair) -end +func get_pair_reserves{syscall_ptr: felt*, pedersen_ptr: HashBuiltin*, range_check_ptr}( + pair: felt +) -> (reserve_0: Uint256, reserve_1: Uint256) { + return AmmWrapper.get_pair_reserves(pair); +} @view -func get_amounts_out{syscall_ptr : felt*, pedersen_ptr : HashBuiltin*, range_check_ptr}( - amount_in : Uint256, path_len : felt, path : felt* -) -> (amounts_len : felt, amounts : Uint256*): - return AmmWrapper.get_amounts_out(amount_in, path_len, path) -end +func get_amounts_out{syscall_ptr: felt*, pedersen_ptr: HashBuiltin*, range_check_ptr}( + amount_in: Uint256, path_len: felt, path: felt* +) -> (amounts_len: felt, amounts: Uint256*) { + return AmmWrapper.get_amounts_out(amount_in, path_len, path); +} diff --git a/src/contracts/amm_wrapper_library.cairo b/src/contracts/amm_wrapper_library.cairo index dcc3bce..0dd9aa2 100644 --- a/src/contracts/amm_wrapper_library.cairo +++ b/src/contracts/amm_wrapper_library.cairo @@ -9,82 +9,82 @@ from src.data_types.data_types import Pair, Node from starkware.cairo.common.alloc import alloc @storage_var -func AmmWrapper_jediswap_router() -> (address : felt): -end +func AmmWrapper_jediswap_router() -> (address: felt) { +} @storage_var -func AmmWrapper_jediswap_factory() -> (address : felt): -end +func AmmWrapper_jediswap_factory() -> (address: felt) { +} -namespace AmmWrapper: - func initializer{syscall_ptr : felt*, pedersen_ptr : HashBuiltin*, range_check_ptr}( - jediswap_router : felt, jediswap_factory : felt - ): - AmmWrapper_jediswap_router.write(jediswap_router) - AmmWrapper_jediswap_factory.write(jediswap_factory) - return () - end +namespace AmmWrapper { + func initializer{syscall_ptr: felt*, pedersen_ptr: HashBuiltin*, range_check_ptr}( + jediswap_router: felt, jediswap_factory: felt + ) { + AmmWrapper_jediswap_router.write(jediswap_router); + AmmWrapper_jediswap_factory.write(jediswap_factory); + return (); + } - func get_pair{syscall_ptr : felt*, pedersen_ptr : HashBuiltin*, range_check_ptr}( - token0 : felt, token1 : felt - ) -> (pair : felt): - let (factory_address) = AmmWrapper_jediswap_factory.read() - let (pair) = IFactory.get_pair(factory_address, token0, token1) - return (pair) - end + func get_pair{syscall_ptr: felt*, pedersen_ptr: HashBuiltin*, range_check_ptr}( + token0: felt, token1: felt + ) -> (pair: felt) { + let (factory_address) = AmmWrapper_jediswap_factory.read(); + let (pair) = IFactory.get_pair(factory_address, token0, token1); + return (pair,); + } - func get_parsed_pairs{syscall_ptr : felt*, pedersen_ptr : HashBuiltin*, range_check_ptr}(): - let (parsed_pairs : Pair*) = alloc() + func get_parsed_pairs{syscall_ptr: felt*, pedersen_ptr: HashBuiltin*, range_check_ptr}() { + let (parsed_pairs: Pair*) = alloc(); - return () - end + return (); + } - func get_pair_tokens{syscall_ptr : felt*, pedersen_ptr : HashBuiltin*, range_check_ptr}( - pair_address : felt - ) -> (pair : felt): - let (token0) = get_pair_token0(pair_address) - let (token1) = get_pair_token1(pair_address) - tempvar pair : Pair = Pair(token0, token1) - return (pair) - end + func get_pair_tokens{syscall_ptr: felt*, pedersen_ptr: HashBuiltin*, range_check_ptr}( + pair_address: felt + ) -> (pair: felt) { + let (token0) = get_pair_token0(pair_address); + let (token1) = get_pair_token1(pair_address); + tempvar pair: Pair = Pair(token0, token1); + return (pair,); + } - func get_all_pairs{syscall_ptr : felt*, pedersen_ptr : HashBuiltin*, range_check_ptr}() -> ( - all_pairs_len : felt, all_pairs : felt* - ): - let (factory_address) = AmmWrapper_jediswap_factory.read() - let (all_pairs_len, all_pairs) = IFactory.get_all_pairs(factory_address) - return (all_pairs_len, all_pairs) - end + func get_all_pairs{syscall_ptr: felt*, pedersen_ptr: HashBuiltin*, range_check_ptr}() -> ( + all_pairs_len: felt, all_pairs: felt* + ) { + let (factory_address) = AmmWrapper_jediswap_factory.read(); + let (all_pairs_len, all_pairs) = IFactory.get_all_pairs(factory_address); + return (all_pairs_len, all_pairs); + } - func get_pair_token0{syscall_ptr : felt*, pedersen_ptr : HashBuiltin*, range_check_ptr}( - pair : felt - ) -> (token0 : felt): - let (token0) = IJediSwapPair.token0(pair) - return (token0) - end + func get_pair_token0{syscall_ptr: felt*, pedersen_ptr: HashBuiltin*, range_check_ptr}( + pair: felt + ) -> (token0: felt) { + let (token0) = IJediSwapPair.token0(pair); + return (token0,); + } - func get_pair_token1{syscall_ptr : felt*, pedersen_ptr : HashBuiltin*, range_check_ptr}( - pair : felt - ) -> (token1 : felt): - let (token1) = IJediSwapPair.token1(pair) - return (token1) - end + func get_pair_token1{syscall_ptr: felt*, pedersen_ptr: HashBuiltin*, range_check_ptr}( + pair: felt + ) -> (token1: felt) { + let (token1) = IJediSwapPair.token1(pair); + return (token1,); + } - func get_pair_reserves{syscall_ptr : felt*, pedersen_ptr : HashBuiltin*, range_check_ptr}( - pair : felt - ) -> (reserve_0 : Uint256, reserve_1 : Uint256): - let (reserve_0, reserve_1, _) = IJediSwapPair.get_reserves(pair) - return (reserve_0, reserve_1) - end + func get_pair_reserves{syscall_ptr: felt*, pedersen_ptr: HashBuiltin*, range_check_ptr}( + pair: felt + ) -> (reserve_0: Uint256, reserve_1: Uint256) { + let (reserve_0, reserve_1, _) = IJediSwapPair.get_reserves(pair); + return (reserve_0, reserve_1); + } - func get_amounts_out{syscall_ptr : felt*, pedersen_ptr : HashBuiltin*, range_check_ptr}( - amount_in : Uint256, path_len : felt, path : felt* - ) -> (amounts_len : felt, amounts : Uint256*): - alloc_locals - let (router_address) = AmmWrapper_jediswap_router.read() + func get_amounts_out{syscall_ptr: felt*, pedersen_ptr: HashBuiltin*, range_check_ptr}( + amount_in: Uint256, path_len: felt, path: felt* + ) -> (amounts_len: felt, amounts: Uint256*) { + alloc_locals; + let (router_address) = AmmWrapper_jediswap_router.read(); let (amounts_len, amounts) = IRouter.get_amounts_out( router_address, amount_in, path_len, path - ) - return (amounts_len, amounts) - end -end + ); + return (amounts_len, amounts); + } +} diff --git a/src/contracts/hubble.cairo b/src/contracts/hubble.cairo index 36b7900..f9057c5 100644 --- a/src/contracts/hubble.cairo +++ b/src/contracts/hubble.cairo @@ -7,37 +7,37 @@ from src.contracts.hubble_library import Hubble from cairo_graphs.data_types.data_types import Edge, Vertex @constructor -func constructor{syscall_ptr : felt*, pedersen_ptr : HashBuiltin*, range_check_ptr}( - amm_wrapper_contract : felt -): - Hubble.initializer(amm_wrapper_contract) - return () -end +func constructor{syscall_ptr: felt*, pedersen_ptr: HashBuiltin*, range_check_ptr}( + amm_wrapper_contract: felt +) { + Hubble.initializer(amm_wrapper_contract); + return (); +} @view -func get_minimal_route{syscall_ptr : felt*, pedersen_ptr : HashBuiltin*, range_check_ptr}( - token_from : felt, token_to : felt -) -> (path_len : felt, path : felt*, distance : felt): - return Hubble.get_minimal_route(token_from, token_to) -end +func get_minimal_route{syscall_ptr: felt*, pedersen_ptr: HashBuiltin*, range_check_ptr}( + token_from: felt, token_to: felt +) -> (path_len: felt, path: felt*, distance: felt) { + return Hubble.get_minimal_route(token_from, token_to); +} @view -func get_all_pairs{syscall_ptr : felt*, pedersen_ptr : HashBuiltin*, range_check_ptr}() -> ( - pairs_len : felt, pairs : Edge* -): - return Hubble.get_all_pairs() -end +func get_all_pairs{syscall_ptr: felt*, pedersen_ptr: HashBuiltin*, range_check_ptr}() -> ( + pairs_len: felt, pairs: Edge* +) { + return Hubble.get_all_pairs(); +} @view -func get_all_routes{syscall_ptr : felt*, pedersen_ptr : HashBuiltin*, range_check_ptr}( - token_from : felt, token_to : felt, max_hops : felt -) -> (routes_len : felt, routes : felt*): - return Hubble.get_all_routes(token_from, token_to, max_hops) -end +func get_all_routes{syscall_ptr: felt*, pedersen_ptr: HashBuiltin*, range_check_ptr}( + token_from: felt, token_to: felt, max_hops: felt +) -> (routes_len: felt, routes: felt*) { + return Hubble.get_all_routes(token_from, token_to, max_hops); +} @view -func get_best_route{syscall_ptr : felt*, pedersen_ptr : HashBuiltin*, range_check_ptr}( - amount_in : Uint256, token_from : felt, token_to : felt, max_hops : felt -) -> (route_len : felt, route : Uint256*, amount_out : Uint256): - return Hubble.get_best_route(amount_in, token_from, token_to, max_hops) -end +func get_best_route{syscall_ptr: felt*, pedersen_ptr: HashBuiltin*, range_check_ptr}( + amount_in: Uint256, token_from: felt, token_to: felt, max_hops: felt +) -> (route_len: felt, route: Uint256*, amount_out: Uint256) { + return Hubble.get_best_route(amount_in, token_from, token_to, max_hops); +} diff --git a/src/contracts/hubble_library.cairo b/src/contracts/hubble_library.cairo index 8eeec32..e4f2a26 100644 --- a/src/contracts/hubble_library.cairo +++ b/src/contracts/hubble_library.cairo @@ -4,216 +4,213 @@ from starkware.cairo.common.cairo_builtins import HashBuiltin from starkware.cairo.common.alloc import alloc from starkware.cairo.common.uint256 import Uint256, uint256_lt -from cairo_graphs.graph.graph import Graph +from cairo_graphs.graph.graph import GraphMethods from cairo_graphs.graph.dijkstra import Dijkstra -from cairo_graphs.data_types.data_types import Edge, Vertex +from cairo_graphs.data_types.data_types import Edge, Vertex, Graph from cairo_graphs.graph.dfs_all_paths import init_dfs from src.data_types.data_types import Pair, Node from src.interfaces.i_amm_wrapper import IAmmWrapper @storage_var -func Hubble_amm_wrapper_address() -> (address : felt): -end - -namespace Hubble: - func initializer{syscall_ptr : felt*, pedersen_ptr : HashBuiltin*, range_check_ptr}( - amm_wrapper_contract : felt - ): - Hubble_amm_wrapper_address.write(amm_wrapper_contract) - return () - end - - func get_all_pairs{syscall_ptr : felt*, pedersen_ptr : HashBuiltin*, range_check_ptr}() -> ( - pairs_len : felt, pairs : Edge* - ): - alloc_locals - let (amm_wrapper_address) = Hubble_amm_wrapper_address.read() - let (all_pairs_len, all_pairs : felt*) = IAmmWrapper.get_all_pairs(amm_wrapper_address) - let (local parsed_pairs : Edge*) = alloc() - let (parsed_pairs_len) = parse_all_pairs(all_pairs_len, all_pairs, parsed_pairs, 0) - return (parsed_pairs_len, parsed_pairs) - end - - func get_minimal_route{syscall_ptr : felt*, pedersen_ptr : HashBuiltin*, range_check_ptr}( - token_from : felt, token_to : felt - ) -> (path_len : felt, path : felt*, distance : felt): - alloc_locals - let (amm_wrapper_address) = Hubble_amm_wrapper_address.read() - - let (all_pairs_len, all_pairs : felt*) = IAmmWrapper.get_all_pairs(amm_wrapper_address) - let (local parsed_pairs : Edge*) = alloc() - let (parsed_pairs_len) = parse_all_pairs(all_pairs_len, all_pairs, parsed_pairs, 0) - - let (graph_len, graph, adj_vertices_count) = Graph.build_undirected_graph_from_edges( +func Hubble_amm_wrapper_address() -> (address: felt) { +} + +namespace Hubble { + func initializer{syscall_ptr: felt*, pedersen_ptr: HashBuiltin*, range_check_ptr}( + amm_wrapper_contract: felt + ) { + Hubble_amm_wrapper_address.write(amm_wrapper_contract); + return (); + } + + func get_all_pairs{syscall_ptr: felt*, pedersen_ptr: HashBuiltin*, range_check_ptr}() -> ( + pairs_len: felt, pairs: Edge* + ) { + alloc_locals; + let (amm_wrapper_address) = Hubble_amm_wrapper_address.read(); + let (all_pairs_len, all_pairs: felt*) = IAmmWrapper.get_all_pairs(amm_wrapper_address); + let (local parsed_pairs: Edge*) = alloc(); + let (parsed_pairs_len) = parse_all_pairs(all_pairs_len, all_pairs, parsed_pairs, 0); + return (parsed_pairs_len, parsed_pairs); + } + + func get_minimal_route{syscall_ptr: felt*, pedersen_ptr: HashBuiltin*, range_check_ptr}( + token_from: felt, token_to: felt + ) -> (path_len: felt, path: felt*, distance: felt) { + alloc_locals; + let (amm_wrapper_address) = Hubble_amm_wrapper_address.read(); + + let (all_pairs_len, all_pairs: felt*) = IAmmWrapper.get_all_pairs(amm_wrapper_address); + let (local parsed_pairs: Edge*) = alloc(); + let (parsed_pairs_len) = parse_all_pairs(all_pairs_len, all_pairs, parsed_pairs, 0); + + let (graph_len, graph, adj_vertices_count) = GraphMethods.build_undirected_graph_from_edges( parsed_pairs_len, parsed_pairs - ) + ); let (path_len, path, distance) = Dijkstra.shortest_path( graph_len, graph, adj_vertices_count, token_from, token_to - ) - return (path_len, path, distance) - end + ); + return (path_len, path, distance); + } - func get_all_routes{syscall_ptr : felt*, pedersen_ptr : HashBuiltin*, range_check_ptr}( - token_from : felt, token_to : felt, max_hops : felt - ) -> (routes_len : felt, routes : felt*): - alloc_locals - let (amm_wrapper_address) = Hubble_amm_wrapper_address.read() + func get_all_routes{syscall_ptr: felt*, pedersen_ptr: HashBuiltin*, range_check_ptr}( + token_from: felt, token_to: felt, max_hops: felt + ) -> (routes_len: felt, routes: felt*) { + alloc_locals; + let (amm_wrapper_address) = Hubble_amm_wrapper_address.read(); - let (all_pairs_len, all_pairs : felt*) = IAmmWrapper.get_all_pairs(amm_wrapper_address) - let (local parsed_pairs : Edge*) = alloc() - let (parsed_pairs_len) = parse_all_pairs(all_pairs_len, all_pairs, parsed_pairs, 0) + let (all_pairs_len, all_pairs: felt*) = IAmmWrapper.get_all_pairs(amm_wrapper_address); + let (local parsed_pairs: Edge*) = alloc(); + let (parsed_pairs_len) = parse_all_pairs(all_pairs_len, all_pairs, parsed_pairs, 0); - let (graph_len, graph, adj_vertices_count) = Graph.build_undirected_graph_from_edges( + let (graph_len, graph, adj_vertices_count) = GraphMethods.build_undirected_graph_from_edges( parsed_pairs_len, parsed_pairs - ) + ); let (saved_paths_len, saved_paths) = init_dfs( graph_len, graph, adj_vertices_count, token_from, token_to, max_hops - ) - return (saved_paths_len, saved_paths) - end - - func get_best_route{syscall_ptr : felt*, pedersen_ptr : HashBuiltin*, range_check_ptr}( - amount_in : Uint256, token_from : felt, token_to : felt, max_hops : felt - ) -> (route_len : felt, route : Uint256*, amount_out : Uint256): - let (amm_wrapper_address) = Hubble_amm_wrapper_address.read() - let (all_routes_len, all_routes) = get_all_routes(token_from, token_to, max_hops) - - let (best_route : Uint256*) = alloc() - # all routes[0] is the length of the first route, which starts at index = 1 + ); + return (saved_paths_len, saved_paths); + } + + func get_best_route{syscall_ptr: felt*, pedersen_ptr: HashBuiltin*, range_check_ptr}( + amount_in: Uint256, token_from: felt, token_to: felt, max_hops: felt + ) -> (route_len: felt, route: Uint256*, amount_out: Uint256) { + let (amm_wrapper_address) = Hubble_amm_wrapper_address.read(); + let (all_routes_len, all_routes) = get_all_routes(token_from, token_to, max_hops); + + let (best_route: Uint256*) = alloc(); + // all routes[0] is the length of the first route, which starts at index = 1 let (best_route_len, best_route, amount_out) = _get_best_route( amount_in, all_routes_len, all_routes, current_best_route_len=0, current_best_route=best_route, - ) - return (best_route_len, best_route, amount_out) - end - - func _get_best_route{syscall_ptr : felt*, pedersen_ptr : HashBuiltin*, range_check_ptr}( - amount_in : Uint256, - all_routes_len : felt, - all_routes : felt*, - current_best_route_len : felt, - current_best_route : Uint256*, - ) -> (best_route_len : felt, best_route : Uint256*, current_best_amount_out : Uint256): - alloc_locals - - # TODO fix this function - # end when all routes visited - if all_routes_len == 1: + ); + return (best_route_len, best_route, amount_out); + } + + func _get_best_route{syscall_ptr: felt*, pedersen_ptr: HashBuiltin*, range_check_ptr}( + amount_in: Uint256, + all_routes_len: felt, + all_routes: felt*, + current_best_route_len: felt, + current_best_route: Uint256*, + ) -> (best_route_len: felt, best_route: Uint256*, current_best_amount_out: Uint256) { + alloc_locals; + + // TODO fix this function + // end when all routes visited + if (all_routes_len == 1) { return ( current_best_route_len, current_best_route, current_best_route[current_best_route_len - Uint256.SIZE], - ) - end + ); + } - let route_to_eval_len = [all_routes] - let route_to_eval = all_routes + 1 + let route_to_eval_len = [all_routes]; + let route_to_eval = all_routes + 1; let (local amounts_len, local amounts, output_tokens) = evaluate_current_route( amount_in, route_to_eval_len, route_to_eval - ) + ); - let current_best_amount : Uint256 = current_best_route[current_best_route_len - Uint256.SIZE] - let (is_new_route_better) = uint256_lt(current_best_amount, output_tokens) - if is_new_route_better == 1: - # decrement len by (1-> it stores route_len) + route_len that was evaluated - # tempvar pedersen_ptr = pedersen_ptr + let current_best_amount: Uint256 = current_best_route[current_best_route_len - Uint256.SIZE]; + let (is_new_route_better) = uint256_lt(current_best_amount, output_tokens); + if (is_new_route_better == 1) { + // decrement len by (1-> it stores route_len) + route_len that was evaluated + // tempvar pedersen_ptr = pedersen_ptr return _get_best_route( amount_in=amount_in, all_routes_len=all_routes_len - route_to_eval_len + 1, all_routes=all_routes + route_to_eval_len + 1, current_best_route_len=amounts_len, current_best_route=amounts, - ) - end + ); + } return _get_best_route( amount_in=amount_in, all_routes_len=current_best_route_len - route_to_eval_len + 1, all_routes=all_routes, current_best_route_len=current_best_route_len, current_best_route=current_best_route, - ) - end -end - -func parse_all_pairs{syscall_ptr : felt*, pedersen_ptr : HashBuiltin*, range_check_ptr}( - pairs_addresses_len : felt, - pairs_addresses : felt*, - parsed_pairs : Edge*, - parsed_pairs_len : felt, -) -> (parsed_pairs_len : felt): - let (amm_wrapper_address) = Hubble_amm_wrapper_address.read() - if pairs_addresses_len == 0: - return (parsed_pairs_len) - end - let (token_0) = IAmmWrapper.get_pair_token0(amm_wrapper_address, [pairs_addresses]) - let (token_1) = IAmmWrapper.get_pair_token1(amm_wrapper_address, [pairs_addresses]) - assert [parsed_pairs] = Edge(token_0, token_1, 1) + ); + } +} + +func parse_all_pairs{syscall_ptr: felt*, pedersen_ptr: HashBuiltin*, range_check_ptr}( + pairs_addresses_len: felt, pairs_addresses: felt*, parsed_pairs: Edge*, parsed_pairs_len: felt +) -> (parsed_pairs_len: felt) { + let (amm_wrapper_address) = Hubble_amm_wrapper_address.read(); + if (pairs_addresses_len == 0) { + return (parsed_pairs_len,); + } + let (token_0) = IAmmWrapper.get_pair_token0(amm_wrapper_address, [pairs_addresses]); + let (token_1) = IAmmWrapper.get_pair_token1(amm_wrapper_address, [pairs_addresses]); + assert [parsed_pairs] = Edge(token_0, token_1, 1); return parse_all_pairs( pairs_addresses_len - 1, pairs_addresses + 1, parsed_pairs + Edge.SIZE, parsed_pairs_len + 1 - ) -end - -# func _get_best_route{syscall_ptr : felt*, pedersen_ptr : HashBuiltin*, range_check_ptr}( -# amount_in : Uint256, -# all_routes_len : felt, -# all_routes : felt*, -# current_best_route_len : felt, -# current_best_route : Uint256*, -# ) -> (best_route_len : felt, best_route : Uint256*, current_best_amount_out : Uint256): -# alloc_locals -# # end when all routes visited -# if all_routes_len == 0: -# return ( -# current_best_route_len, -# current_best_route, -# current_best_route[current_best_route_len - Uint256.SIZE], -# ) -# end - -# let route_to_eval_len = [all_routes] -# let route_to_eval = all_routes + 1 -# let (local amounts_len, local amounts, output_tokens) = evaluate_current_route( -# amount_in, route_to_eval_len, route_to_eval -# ) - -# let current_best_amount : Uint256 = current_best_route[current_best_route_len - Uint256.SIZE] -# let (is_new_route_better) = uint256_lt(current_best_amount, output_tokens) -# if is_new_route_better == 1: -# # decrement len by (1-> it stores route_len) + route_len that was evaluated -# # tempvar pedersen_ptr = pedersen_ptr -# return _get_best_route( -# amount_in=amount_in, -# all_routes_len=all_routes_len - route_to_eval_len + 1, -# all_routes=all_routes + route_to_eval_len + 1, -# current_best_route_len=amounts_len, -# current_best_route=amounts, -# ) -# end -# return _get_best_route( -# amount_in=amount_in, -# all_routes_len=current_best_route_len - route_to_eval_len + 1, -# all_routes=all_routes, -# current_best_route_len=current_best_route_len, -# current_best_route=current_best_route, -# ) -# end - -func evaluate_current_route{syscall_ptr : felt*, pedersen_ptr : HashBuiltin*, range_check_ptr}( - amount_in : Uint256, route_len : felt, route : felt* -) -> (amounts_len : felt, amounts : Uint256*, output_tokens : Uint256): - alloc_locals - # TODO fix this function - let (amm_wrapper_address) = Hubble_amm_wrapper_address.read() - local pedersen_ptr : HashBuiltin* = pedersen_ptr + ); +} + +// func _get_best_route{syscall_ptr : felt*, pedersen_ptr : HashBuiltin*, range_check_ptr}( +// amount_in : Uint256, +// all_routes_len : felt, +// all_routes : felt*, +// current_best_route_len : felt, +// current_best_route : Uint256*, +// ) -> (best_route_len : felt, best_route : Uint256*, current_best_amount_out : Uint256): +// alloc_locals +// # end when all routes visited +// if all_routes_len == 0: +// return ( +// current_best_route_len, +// current_best_route, +// current_best_route[current_best_route_len - Uint256.SIZE], +// ) +// end + +// let route_to_eval_len = [all_routes] +// let route_to_eval = all_routes + 1 +// let (local amounts_len, local amounts, output_tokens) = evaluate_current_route( +// amount_in, route_to_eval_len, route_to_eval +// ) + +// let current_best_amount : Uint256 = current_best_route[current_best_route_len - Uint256.SIZE] +// let (is_new_route_better) = uint256_lt(current_best_amount, output_tokens) +// if is_new_route_better == 1: +// # decrement len by (1-> it stores route_len) + route_len that was evaluated +// # tempvar pedersen_ptr = pedersen_ptr +// return _get_best_route( +// amount_in=amount_in, +// all_routes_len=all_routes_len - route_to_eval_len + 1, +// all_routes=all_routes + route_to_eval_len + 1, +// current_best_route_len=amounts_len, +// current_best_route=amounts, +// ) +// end +// return _get_best_route( +// amount_in=amount_in, +// all_routes_len=current_best_route_len - route_to_eval_len + 1, +// all_routes=all_routes, +// current_best_route_len=current_best_route_len, +// current_best_route=current_best_route, +// ) +// end + +func evaluate_current_route{syscall_ptr: felt*, pedersen_ptr: HashBuiltin*, range_check_ptr}( + amount_in: Uint256, route_len: felt, route: felt* +) -> (amounts_len: felt, amounts: Uint256*, output_tokens: Uint256) { + alloc_locals; + // TODO fix this function + let (amm_wrapper_address) = Hubble_amm_wrapper_address.read(); + local pedersen_ptr: HashBuiltin* = pedersen_ptr; let (amounts_len, amounts) = IAmmWrapper.get_amounts_out( amm_wrapper_address, amount_in, route_len, route - ) - let output_tokens = amounts[amounts_len - 1] - return (amounts_len, amounts, output_tokens) -end + ); + let output_tokens = amounts[amounts_len - 1]; + return (amounts_len, amounts, output_tokens); +} diff --git a/src/data_types/data_types.cairo b/src/data_types/data_types.cairo index 057519f..83e52ef 100644 --- a/src/data_types/data_types.cairo +++ b/src/data_types/data_types.cairo @@ -1,12 +1,12 @@ -# A node has an identifier (token address) and a list of neighbor nodes -struct Node: - member index : felt - member identifier : felt - member neighbor_nodes : Node* -end +// A node has an identifier (token address) and a list of neighbor nodes +struct Node { + index: felt, + identifier: felt, + neighbor_nodes: Node*, +} -# A pair containing 2 token identified by their address -struct Pair: - member token_0 : felt - member token_1 : felt -end +// A pair containing 2 token identified by their address +struct Pair { + token_0: felt, + token_1: felt, +} diff --git a/src/interfaces/i_amm_wrapper.cairo b/src/interfaces/i_amm_wrapper.cairo index b699d7e..559ec0a 100644 --- a/src/interfaces/i_amm_wrapper.cairo +++ b/src/interfaces/i_amm_wrapper.cairo @@ -5,24 +5,24 @@ from src.contracts.amm_wrapper_library import AmmWrapper from starkware.cairo.common.uint256 import Uint256 @contract_interface -namespace IAmmWrapper: - func get_pair(token0 : felt, token1 : felt) -> (pair : felt): - end +namespace IAmmWrapper { + func get_pair(token0: felt, token1: felt) -> (pair: felt) { + } - func get_all_pairs() -> (all_pairs_len : felt, all_pairs : felt*): - end + func get_all_pairs() -> (all_pairs_len: felt, all_pairs: felt*) { + } - func get_pair_token0(pair : felt) -> (token0 : felt): - end + func get_pair_token0(pair: felt) -> (token0: felt) { + } - func get_pair_token1(pair : felt) -> (token1 : felt): - end + func get_pair_token1(pair: felt) -> (token1: felt) { + } - func get_pair_reserves(pair : felt) -> (reserve_0 : Uint256, reserve_1 : Uint256): - end + func get_pair_reserves(pair: felt) -> (reserve_0: Uint256, reserve_1: Uint256) { + } - func get_amounts_out(amount_in : Uint256, path_len : felt, path : felt*) -> ( - amounts_len : felt, amounts : Uint256* - ): - end -end + func get_amounts_out(amount_in: Uint256, path_len: felt, path: felt*) -> ( + amounts_len: felt, amounts: Uint256* + ) { + } +} diff --git a/src/interfaces/i_factory.cairo b/src/interfaces/i_factory.cairo index 5b40e6b..87cc772 100644 --- a/src/interfaces/i_factory.cairo +++ b/src/interfaces/i_factory.cairo @@ -1,31 +1,31 @@ %lang starknet @contract_interface -namespace IFactory: - func get_pair(token0 : felt, token1 : felt) -> (pair : felt): - end +namespace IFactory { + func get_pair(token0: felt, token1: felt) -> (pair: felt) { + } - func get_all_pairs() -> (all_pairs_len : felt, all_pairs : felt*): - end + func get_all_pairs() -> (all_pairs_len: felt, all_pairs: felt*) { + } - func get_num_of_pairs() -> (num_of_pairs : felt): - end + func get_num_of_pairs() -> (num_of_pairs: felt) { + } - func get_fee_to() -> (address : felt): - end + func get_fee_to() -> (address: felt) { + } - func get_fee_to_setter() -> (address : felt): - end + func get_fee_to_setter() -> (address: felt) { + } - func get_pair_contract_class_hash() -> (class_hash : felt): - end + func get_pair_contract_class_hash() -> (class_hash: felt) { + } - func create_pair(tokenA : felt, tokenB : felt) -> (pair : felt): - end + func create_pair(tokenA: felt, tokenB: felt) -> (pair: felt) { + } - func set_fee_to(new_fee_to : felt): - end + func set_fee_to(new_fee_to: felt) { + } - func set_fee_to_setter(new_fee_to_setter : felt): - end -end + func set_fee_to_setter(new_fee_to_setter: felt) { + } +} diff --git a/src/interfaces/i_pair.cairo b/src/interfaces/i_pair.cairo index 5b6a092..488be62 100644 --- a/src/interfaces/i_pair.cairo +++ b/src/interfaces/i_pair.cairo @@ -3,37 +3,37 @@ from starkware.cairo.common.uint256 import Uint256 @contract_interface -namespace IJediSwapPair: - func token0() -> (address : felt): - end +namespace IJediSwapPair { + func token0() -> (address: felt) { + } - func token1() -> (address : felt): - end + func token1() -> (address: felt) { + } - func get_reserves() -> (reserve0 : Uint256, reserve1 : Uint256, block_timestamp_last : felt): - end + func get_reserves() -> (reserve0: Uint256, reserve1: Uint256, block_timestamp_last: felt) { + } - func price_0_cumulative_last() -> (res : Uint256): - end + func price_0_cumulative_last() -> (res: Uint256) { + } - func price_1_cumulative_last() -> (res : Uint256): - end + func price_1_cumulative_last() -> (res: Uint256) { + } - func klast() -> (res : Uint256): - end + func klast() -> (res: Uint256) { + } - func mint(to : felt) -> (liquidity : Uint256): - end + func mint(to: felt) -> (liquidity: Uint256) { + } - func burn(to : felt) -> (amount0 : Uint256, amount1 : Uint256): - end + func burn(to: felt) -> (amount0: Uint256, amount1: Uint256) { + } - func swap(amount0Out : Uint256, amount1Out : Uint256, to : felt, data_len : felt, data : felt*): - end + func swap(amount0Out: Uint256, amount1Out: Uint256, to: felt, data_len: felt, data: felt*) { + } - func skim(to : felt): - end + func skim(to: felt) { + } - func sync(): - end -end + func sync() { + } +} diff --git a/src/interfaces/i_router.cairo b/src/interfaces/i_router.cairo index 24220d0..d41ac0c 100644 --- a/src/interfaces/i_router.cairo +++ b/src/interfaces/i_router.cairo @@ -3,76 +3,76 @@ from starkware.cairo.common.uint256 import Uint256 @contract_interface -namespace IRouter: - func factory() -> (address : felt): - end +namespace IRouter { + func factory() -> (address: felt) { + } - func sort_tokens(tokenA : felt, tokenB : felt) -> (token0 : felt, token1 : felt): - end + func sort_tokens(tokenA: felt, tokenB: felt) -> (token0: felt, token1: felt) { + } - func quote(amountA : Uint256, reserveA : Uint256, reserveB : Uint256) -> (amountB : Uint256): - end + func quote(amountA: Uint256, reserveA: Uint256, reserveB: Uint256) -> (amountB: Uint256) { + } - func get_amount_out(amountIn : Uint256, reserveIn : Uint256, reserveOut : Uint256) -> ( - amountOut : Uint256 - ): - end + func get_amount_out(amountIn: Uint256, reserveIn: Uint256, reserveOut: Uint256) -> ( + amountOut: Uint256 + ) { + } - func get_amount_in(amountOut : Uint256, reserveIn : Uint256, reserveOut : Uint256) -> ( - amountIn : Uint256 - ): - end + func get_amount_in(amountOut: Uint256, reserveIn: Uint256, reserveOut: Uint256) -> ( + amountIn: Uint256 + ) { + } - func get_amounts_out(amountIn : Uint256, path_len : felt, path : felt*) -> ( - amounts_len : felt, amounts : Uint256* - ): - end + func get_amounts_out(amountIn: Uint256, path_len: felt, path: felt*) -> ( + amounts_len: felt, amounts: Uint256* + ) { + } - func get_amounts_in(amountOut : Uint256, path_len : felt, path : felt*) -> ( - amounts_len : felt, amounts : Uint256* - ): - end + func get_amounts_in(amountOut: Uint256, path_len: felt, path: felt*) -> ( + amounts_len: felt, amounts: Uint256* + ) { + } func add_liquidity( - tokenA : felt, - tokenB : felt, - amountADesired : Uint256, - amountBDesired : Uint256, - amountAMin : Uint256, - amountBMin : Uint256, - to : felt, - deadline : felt, - ) -> (amountA : Uint256, amountB : Uint256, liquidity : Uint256): - end + tokenA: felt, + tokenB: felt, + amountADesired: Uint256, + amountBDesired: Uint256, + amountAMin: Uint256, + amountBMin: Uint256, + to: felt, + deadline: felt, + ) -> (amountA: Uint256, amountB: Uint256, liquidity: Uint256) { + } func remove_liquidity( - tokenA : felt, - tokenB : felt, - liquidity : Uint256, - amountAMin : Uint256, - amountBMin : Uint256, - to : felt, - deadline : felt, - ) -> (amountA : Uint256, amountB : Uint256): - end + tokenA: felt, + tokenB: felt, + liquidity: Uint256, + amountAMin: Uint256, + amountBMin: Uint256, + to: felt, + deadline: felt, + ) -> (amountA: Uint256, amountB: Uint256) { + } func swap_exact_tokens_for_tokens( - amountIn : Uint256, - amountOutMin : Uint256, - path_len : felt, - path : felt*, - to : felt, - deadline : felt, - ) -> (amounts_len : felt, amounts : Uint256*): - end + amountIn: Uint256, + amountOutMin: Uint256, + path_len: felt, + path: felt*, + to: felt, + deadline: felt, + ) -> (amounts_len: felt, amounts: Uint256*) { + } func swap_tokens_for_exact_tokens( - amountOut : Uint256, - amountInMax : Uint256, - path_len : felt, - path : felt*, - to : felt, - deadline : felt, - ) -> (amounts_len : felt, amounts : Uint256*): - end -end + amountOut: Uint256, + amountInMax: Uint256, + path_len: felt, + path: felt*, + to: felt, + deadline: felt, + ) -> (amounts_len: felt, amounts: Uint256*) { + } +} diff --git a/src/utils/array_utils.cairo b/src/utils/array_utils.cairo index 3e207b0..53b3c79 100644 --- a/src/utils/array_utils.cairo +++ b/src/utils/array_utils.cairo @@ -1,25 +1,25 @@ from starkware.cairo.common.alloc import alloc from starkware.cairo.common.memcpy import memcpy -namespace Stack: - # Removes the last element from an array and returns it - func pop(stack_len : felt, stack : felt*) -> ( - new_stack_len : felt, new_stack : felt*, last_elem : felt - ): - alloc_locals +namespace Stack { + // Removes the last element from an array and returns it + func pop(stack_len: felt, stack: felt*) -> ( + new_stack_len: felt, new_stack: felt*, last_elem: felt + ) { + alloc_locals; - let (local res : felt*) = alloc() - memcpy(res, stack, stack_len - 1) - return (stack_len - 1, res, stack[stack_len - 1]) - end + let (local res: felt*) = alloc(); + memcpy(res, stack, stack_len - 1); + return (stack_len - 1, res, stack[stack_len - 1]); + } - func put(stack_len : felt, stack : felt*, element : felt) -> ( - new_stack_len : felt, new_stack : felt* - ): - alloc_locals + func put(stack_len: felt, stack: felt*, element: felt) -> ( + new_stack_len: felt, new_stack: felt* + ) { + alloc_locals; - assert stack[stack_len] = element - let new_stack_len = stack_len + 1 - return (new_stack_len, stack) - end -end + assert stack[stack_len] = element; + let new_stack_len = stack_len + 1; + return (new_stack_len, stack); + } +} diff --git a/temp_output.viz b/temp_output.viz new file mode 100644 index 0000000..33738aa --- /dev/null +++ b/temp_output.viz @@ -0,0 +1 @@ + \ No newline at end of file diff --git a/tests/test_amm_wrapper.cairo b/tests/test_amm_wrapper.cairo index 48bb212..e1a655d 100644 --- a/tests/test_amm_wrapper.cairo +++ b/tests/test_amm_wrapper.cairo @@ -7,82 +7,82 @@ from starkware.cairo.common.uint256 import Uint256 from starkware.cairo.common.alloc import alloc from starkware.cairo.common.registers import get_fp_and_pc -const JEDI_ROUTER = 19876081725 -const JEDI_FACTORY = 1786125 +const JEDI_ROUTER = 19876081725; +const JEDI_FACTORY = 1786125; -const TOKEN_A = 123 -const TOKEN_B = 456 -const TOKEN_C = 990 -const TOKEN_D = 982 +const TOKEN_A = 123; +const TOKEN_B = 456; +const TOKEN_C = 990; +const TOKEN_D = 982; -const RESERVE_A_B_0_LOW = 27890 -const RESERVE_A_B_1_LOW = 26789 +const RESERVE_A_B_0_LOW = 27890; +const RESERVE_A_B_1_LOW = 26789; -const PAIR_A_B = 90174089 -const PAIR_A_C = 90182194 -const PAIR_A_D = 90712441 +const PAIR_A_B = 90174089; +const PAIR_A_C = 90182194; +const PAIR_A_D = 90712441; -# TODO remove these useless tests +// TODO remove these useless tests -func before_each{syscall_ptr : felt*, pedersen_ptr : HashBuiltin*, range_check_ptr}(): - let (contract_address) = get_contract_address() +func before_each{syscall_ptr: felt*, pedersen_ptr: HashBuiltin*, range_check_ptr}() { + let (contract_address) = get_contract_address(); - # Store values in contract storage + // Store values in contract storage %{ store(ids.contract_address, "AmmWrapper_jediswap_router", [ids.JEDI_ROUTER]) %} %{ store(ids.contract_address, "AmmWrapper_jediswap_factory", [ids.JEDI_FACTORY]) %} - return () -end + return (); +} @external -func test_get_pair{syscall_ptr : felt*, pedersen_ptr : HashBuiltin*, range_check_ptr}(): - before_each() +func test_get_pair{syscall_ptr: felt*, pedersen_ptr: HashBuiltin*, range_check_ptr}() { + before_each(); %{ stop_mock = mock_call(ids.JEDI_FACTORY,"get_pair", [ids.PAIR_A_B]) %} - let (pair_a_b) = AmmWrapper.get_pair(TOKEN_A, TOKEN_B) + let (pair_a_b) = AmmWrapper.get_pair(TOKEN_A, TOKEN_B); %{ stop_mock() %} - assert pair_a_b = PAIR_A_B - return () -end + assert pair_a_b = PAIR_A_B; + return (); +} @external -func test_get_all_pairs{syscall_ptr : felt*, pedersen_ptr : HashBuiltin*, range_check_ptr}(): - before_each() +func test_get_all_pairs{syscall_ptr: felt*, pedersen_ptr: HashBuiltin*, range_check_ptr}() { + before_each(); %{ stop_mock = mock_call(ids.JEDI_FACTORY,"get_all_pairs", [3,ids.PAIR_A_B,ids.PAIR_A_C,ids.PAIR_A_D]) %} - let (all_pairs_len, all_pairs) = AmmWrapper.get_all_pairs() + let (all_pairs_len, all_pairs) = AmmWrapper.get_all_pairs(); %{ stop_mock() %} - assert all_pairs_len = 3 - assert all_pairs[0] = PAIR_A_B - assert all_pairs[1] = PAIR_A_C - assert all_pairs[2] = PAIR_A_D - return () -end + assert all_pairs_len = 3; + assert all_pairs[0] = PAIR_A_B; + assert all_pairs[1] = PAIR_A_C; + assert all_pairs[2] = PAIR_A_D; + return (); +} @external -func test_get_token_0{syscall_ptr : felt*, pedersen_ptr : HashBuiltin*, range_check_ptr}(): - before_each() +func test_get_token_0{syscall_ptr: felt*, pedersen_ptr: HashBuiltin*, range_check_ptr}() { + before_each(); %{ stop_mock = mock_call(ids.PAIR_A_B,"token0", [ids.TOKEN_A]) %} - let (token0) = AmmWrapper.get_pair_token0(PAIR_A_B) + let (token0) = AmmWrapper.get_pair_token0(PAIR_A_B); %{ stop_mock() %} - assert token0 = TOKEN_A - return () -end + assert token0 = TOKEN_A; + return (); +} @external -func test_get_token_1{syscall_ptr : felt*, pedersen_ptr : HashBuiltin*, range_check_ptr}(): - before_each() +func test_get_token_1{syscall_ptr: felt*, pedersen_ptr: HashBuiltin*, range_check_ptr}() { + before_each(); %{ stop_mock = mock_call(ids.PAIR_A_B,"token1", [ids.TOKEN_B]) %} - let (token1) = AmmWrapper.get_pair_token1(PAIR_A_B) + let (token1) = AmmWrapper.get_pair_token1(PAIR_A_B); %{ stop_mock() %} - assert token1 = TOKEN_B - return () -end + assert token1 = TOKEN_B; + return (); +} @external -func test_get_reserves{syscall_ptr : felt*, pedersen_ptr : HashBuiltin*, range_check_ptr}(): - before_each() +func test_get_reserves{syscall_ptr: felt*, pedersen_ptr: HashBuiltin*, range_check_ptr}() { + before_each(); %{ stop_mock = mock_call(ids.PAIR_A_B,"get_reserves", [ids.RESERVE_A_B_0_LOW, 0, ids.RESERVE_A_B_1_LOW, 0, 1]) %} - let res = AmmWrapper.get_pair_reserves(PAIR_A_B) + let res = AmmWrapper.get_pair_reserves(PAIR_A_B); %{ stop_mock() %} - assert res[0] = Uint256(RESERVE_A_B_0_LOW, 0) - assert res[1] = Uint256(RESERVE_A_B_1_LOW, 0) - return () -end + assert res[0] = Uint256(RESERVE_A_B_0_LOW, 0); + assert res[1] = Uint256(RESERVE_A_B_1_LOW, 0); + return (); +} diff --git a/tests/test_array_utils.cairo b/tests/test_array_utils.cairo index f5f44b2..1e989cc 100644 --- a/tests/test_array_utils.cairo +++ b/tests/test_array_utils.cairo @@ -4,32 +4,32 @@ from starkware.cairo.common.alloc import alloc from starkware.cairo.common.memcpy import memcpy @external -func test_pop_stack(): - let (my_stack : felt*) = alloc() - assert my_stack[0] = 1 - assert my_stack[1] = 2 - assert my_stack[2] = 3 - let stack_len = 3 - let (stack_len, my_stack, last_elem) = Stack.pop(stack_len, my_stack) - assert last_elem = 3 - assert stack_len = 2 - assert my_stack[0] = 1 - tempvar should_not_revert = my_stack[1] - assert should_not_revert = 2 +func test_pop_stack() { + let (my_stack: felt*) = alloc(); + assert my_stack[0] = 1; + assert my_stack[1] = 2; + assert my_stack[2] = 3; + let stack_len = 3; + let (stack_len, my_stack, last_elem) = Stack.pop(stack_len, my_stack); + assert last_elem = 3; + assert stack_len = 2; + assert my_stack[0] = 1; + tempvar should_not_revert = my_stack[1]; + assert should_not_revert = 2; %{ expect_revert() %} - tempvar should_revert = my_stack[2] - return () -end + tempvar should_revert = my_stack[2]; + return (); +} @external -func test_put_stack(): - let (my_stack : felt*) = alloc() - assert my_stack[0] = 1 - assert my_stack[1] = 2 - assert my_stack[2] = 3 - let stack_len = 3 - let (stack_len, my_stack) = Stack.put(stack_len, my_stack, 4) - assert stack_len = 4 - assert my_stack[3] = 4 - return () -end +func test_put_stack() { + let (my_stack: felt*) = alloc(); + assert my_stack[0] = 1; + assert my_stack[1] = 2; + assert my_stack[2] = 3; + let stack_len = 3; + let (stack_len, my_stack) = Stack.put(stack_len, my_stack, 4); + assert stack_len = 4; + assert my_stack[3] = 4; + return (); +} diff --git a/tests/test_dijkstra.cairo b/tests/test_dijkstra.cairo index 586d45f..29e99b1 100644 --- a/tests/test_dijkstra.cairo +++ b/tests/test_dijkstra.cairo @@ -6,55 +6,55 @@ from starkware.cairo.common.alloc import alloc from src.interfaces.i_router import IRouter -from cairo_graphs.graph.graph import Graph +from cairo_graphs.graph.graph import GraphMethods from cairo_graphs.graph.dijkstra import Dijkstra -from cairo_graphs.data_types.data_types import Edge, Vertex +from cairo_graphs.data_types.data_types import Edge, Vertex, Graph from cairo_graphs.graph.dfs_all_paths import init_dfs from src.contracts.amm_wrapper_library import AmmWrapper from src.contracts.hubble_library import Hubble, parse_all_pairs -const JEDI_ROUTER = 19876081725 -const JEDI_FACTORY = 1786125 +const JEDI_ROUTER = 19876081725; +const JEDI_FACTORY = 1786125; -const TOKEN_A = 123 -const TOKEN_B = 234 -const TOKEN_C = 345 -const TOKEN_D = 456 +const TOKEN_A = 123; +const TOKEN_B = 234; +const TOKEN_C = 345; +const TOKEN_D = 456; -const RESERVE_A_B_0_LOW = 27890 -const RESERVE_A_B_1_LOW = 26789 +const RESERVE_A_B_0_LOW = 27890; +const RESERVE_A_B_1_LOW = 26789; -# Pair addresses -const PAIR_A_B = 12345 -const PAIR_A_C = 13345 -const PAIR_B_C = 23456 -const PAIR_D_C = 43567 -const PAIR_D_B = 42567 +// Pair addresses +const PAIR_A_B = 12345; +const PAIR_A_C = 13345; +const PAIR_B_C = 23456; +const PAIR_D_C = 43567; +const PAIR_D_B = 42567; -const MAX_HOPS = 4 +const MAX_HOPS = 4; -func before_each{syscall_ptr : felt*, pedersen_ptr : HashBuiltin*, range_check_ptr}(): - let (contract_address) = get_contract_address() +func before_each{syscall_ptr: felt*, pedersen_ptr: HashBuiltin*, range_check_ptr}() { + let (contract_address) = get_contract_address(); - # Store values in contract storage + // Store values in contract storage %{ store(ids.contract_address, "AmmWrapper_jediswap_router", [ids.JEDI_ROUTER]) %} %{ store(ids.contract_address, "AmmWrapper_jediswap_factory", [ids.JEDI_FACTORY]) %} - return () -end + return (); +} @external -func test_e2e{syscall_ptr : felt*, pedersen_ptr : HashBuiltin*, range_check_ptr}(): - alloc_locals - before_each() +func test_e2e{syscall_ptr: felt*, pedersen_ptr: HashBuiltin*, range_check_ptr}() { + alloc_locals; + before_each(); %{ stop_mock = mock_call(ids.JEDI_FACTORY,"get_all_pairs", [4,ids.PAIR_A_B, ids.PAIR_A_C,ids.PAIR_B_C,ids.PAIR_D_C]) %} - let (all_pairs_len, all_pairs) = AmmWrapper.get_all_pairs() + let (all_pairs_len, all_pairs) = AmmWrapper.get_all_pairs(); %{ stop_mock() %} - assert all_pairs_len = 4 - assert all_pairs[0] = PAIR_A_B - assert all_pairs[1] = PAIR_A_C - assert all_pairs[2] = PAIR_B_C - assert all_pairs[3] = PAIR_D_C + assert all_pairs_len = 4; + assert all_pairs[0] = PAIR_A_B; + assert all_pairs[1] = PAIR_A_C; + assert all_pairs[2] = PAIR_B_C; + assert all_pairs[3] = PAIR_D_C; %{ stop_mock_ab_0 = mock_call(ids.PAIR_A_B,"token0", [ids.TOKEN_A]) @@ -67,42 +67,42 @@ func test_e2e{syscall_ptr : felt*, pedersen_ptr : HashBuiltin*, range_check_ptr} stop_mock_dc_1 = mock_call(ids.PAIR_D_C,"token1", [ids.TOKEN_C]) %} - # see details in test_dfs.cairo - let (local parsed_pairs : Edge*) = alloc() - # let (parsed_pairs_len) = parse_all_pairs(all_pairs_len, all_pairs, parsed_pairs, 0) - tempvar parsed_pairs_len = 4 - assert parsed_pairs[0] = Edge(TOKEN_A, TOKEN_B, 1) - assert parsed_pairs[1] = Edge(TOKEN_A, TOKEN_C, 1) - assert parsed_pairs[2] = Edge(TOKEN_B, TOKEN_C, 1) - assert parsed_pairs[3] = Edge(TOKEN_D, TOKEN_C, 1) + // see details in test_dfs.cairo + let (local parsed_pairs: Edge*) = alloc(); + // let (parsed_pairs_len) = parse_all_pairs(all_pairs_len, all_pairs, parsed_pairs, 0) + tempvar parsed_pairs_len = 4; + assert parsed_pairs[0] = Edge(TOKEN_A, TOKEN_B, 1); + assert parsed_pairs[1] = Edge(TOKEN_A, TOKEN_C, 1); + assert parsed_pairs[2] = Edge(TOKEN_B, TOKEN_C, 1); + assert parsed_pairs[3] = Edge(TOKEN_D, TOKEN_C, 1); - let (graph_len, graph, adj_vertices_count) = Graph.build_undirected_graph_from_edges( + let graph = GraphMethods.build_undirected_graph_from_edges( parsed_pairs_len, parsed_pairs - ) + ); - let (all_routes : felt*) = alloc() + let (all_routes: felt*) = alloc(); let (all_routes_len, all_routes) = init_dfs( - graph_len, graph, adj_vertices_count, TOKEN_A, TOKEN_D, MAX_HOPS - ) + graph, TOKEN_A, TOKEN_D, MAX_HOPS + ); - assert all_routes_len = 9 + assert all_routes_len = 9; - # Allocate an array. - let (route_1) = alloc() - let (route_2) = alloc() + // Allocate an array. + let (route_1) = alloc(); + let (route_2) = alloc(); - # Populate some values in the array. - # assert [route_1] = TOKEN_A - # assert [route_1 + 1] = TOKEN_B - # assert [route_1 + 2] = TOKEN_C - # assert [route_1 + 3] = TOKEN_D + // Populate some values in the array. + // assert [route_1] = TOKEN_A + // assert [route_1 + 1] = TOKEN_B + // assert [route_1 + 2] = TOKEN_C + // assert [route_1 + 3] = TOKEN_D - # assert [route_2] = TOKEN_A - # assert [route_2 + 2] = TOKEN_C - # assert [route_2 + 3] = TOKEN_D + // assert [route_2] = TOKEN_A + // assert [route_2 + 2] = TOKEN_C + // assert [route_2 + 3] = TOKEN_D - # assert all_routes = route_1 - # assert all_routes[1] = route_2 + // assert all_routes = route_1 + // assert all_routes[1] = route_2 %{ print(ids.all_routes_len) @@ -110,64 +110,64 @@ func test_e2e{syscall_ptr : felt*, pedersen_ptr : HashBuiltin*, range_check_ptr} print(memory[ids.all_routes+i]) %} - let amount_in = Uint256(1000, 0) + let amount_in = Uint256(1000, 0); - let path_1 = all_routes + 1 - let path_2 = all_routes + 4 + let path_1 = all_routes + 1; + let path_2 = all_routes + 4; - let (local amounts_out_1 : Uint256*) = alloc() - assert amounts_out_1[0] = Uint256(25, 0) - let (local amounts_out_2 : Uint256*) = alloc() + let (local amounts_out_1: Uint256*) = alloc(); + assert amounts_out_1[0] = Uint256(25, 0); + let (local amounts_out_2: Uint256*) = alloc(); %{ stop_mock = mock_call(ids.JEDI_ROUTER,"get_amounts_out", [1,ids.amounts_out_1.low, ids.amounts_out_1.high]) %} - let (amounts_len : felt, amounts : Uint256*) = AmmWrapper.get_amounts_out(amount_in, 3, path_1) + let (amounts_len: felt, amounts: Uint256*) = AmmWrapper.get_amounts_out(amount_in, 3, path_1); %{ stop_mock() %} - # %{ stop_mock = mock_call(ids.JEDI_ROUTER,"get_amounts_out", [2,ids.amount_out_2]) %} - # let (amounts_len : felt, amounts : Uint256*) = AmmWrapper.get_amounts_out(amount_in, 4, path_2) - # %{ stop_mock() %} - - # let (best_route : Uint256*) = alloc() - # let (best_route_len, best_route, amount_out) = Hubble._get_best_route( - # amount_in, - # all_routes_len, - # all_routes, - # current_best_route_len=0, - # current_best_route=best_route, - # ) - - # %{ - # print(amount_out) - # print(best_route_len) - # for i in range(best_route_len): - # print(memory[best_route+i]) - # %} - - # %{ - # stop_mock_ab_out = mock_call(ids.PAIR_A_B,"token0", [ids.TOKEN_A]) - # stop_mock_ac_out = mock_call(ids.PAIR_A_B,"token0", [ids.TOKEN_A]) - # stop_mock_bc_out = mock_call(ids.PAIR_A_B,"token0", [ids.TOKEN_A]) - # stop_mock_dc_out = mock_call(ids.PAIR_A_B,"token0", [ids.TOKEN_A]) - # stop_mock_db_out= mock_call(ids.PAIR_A_B,"token0", [ids.TOKEN_A]) - # %} - - # now that we have the paths -> we need to run IJediswapRouter.get_amounts_out with each path :) - - return () -end - -func test_get_best_route{syscall_ptr : felt*, pedersen_ptr : HashBuiltin*, range_check_ptr}(): - alloc_locals - before_each() + // %{ stop_mock = mock_call(ids.JEDI_ROUTER,"get_amounts_out", [2,ids.amount_out_2]) %} + // let (amounts_len : felt, amounts : Uint256*) = AmmWrapper.get_amounts_out(amount_in, 4, path_2) + // %{ stop_mock() %} + + // let (best_route : Uint256*) = alloc() + // let (best_route_len, best_route, amount_out) = Hubble._get_best_route( + // amount_in, + // all_routes_len, + // all_routes, + // current_best_route_len=0, + // current_best_route=best_route, + // ) + + // %{ + // print(amount_out) + // print(best_route_len) + // for i in range(best_route_len): + // print(memory[best_route+i]) + // %} + + // %{ + // stop_mock_ab_out = mock_call(ids.PAIR_A_B,"token0", [ids.TOKEN_A]) + // stop_mock_ac_out = mock_call(ids.PAIR_A_B,"token0", [ids.TOKEN_A]) + // stop_mock_bc_out = mock_call(ids.PAIR_A_B,"token0", [ids.TOKEN_A]) + // stop_mock_dc_out = mock_call(ids.PAIR_A_B,"token0", [ids.TOKEN_A]) + // stop_mock_db_out= mock_call(ids.PAIR_A_B,"token0", [ids.TOKEN_A]) + // %} + + // now that we have the paths -> we need to run IJediswapRouter.get_amounts_out with each path :) + + return (); +} + +func test_get_best_route{syscall_ptr: felt*, pedersen_ptr: HashBuiltin*, range_check_ptr}() { + alloc_locals; + before_each(); %{ stop_mock = mock_call(ids.JEDI_FACTORY,"get_all_pairs", [5,ids.PAIR_A_B, ids.PAIR_A_C,ids.PAIR_B_C,ids.PAIR_D_C,ids.PAIR_D_B]) %} - let (all_pairs_len, all_pairs) = AmmWrapper.get_all_pairs() + let (all_pairs_len, all_pairs) = AmmWrapper.get_all_pairs(); %{ stop_mock() %} - assert all_pairs_len = 5 - assert all_pairs[0] = PAIR_A_B - assert all_pairs[1] = PAIR_A_C - assert all_pairs[2] = PAIR_B_C - assert all_pairs[3] = PAIR_D_C - assert all_pairs[4] = PAIR_D_B + assert all_pairs_len = 5; + assert all_pairs[0] = PAIR_A_B; + assert all_pairs[1] = PAIR_A_C; + assert all_pairs[2] = PAIR_B_C; + assert all_pairs[3] = PAIR_D_C; + assert all_pairs[4] = PAIR_D_B; %{ stop_mock_ab_0 = mock_call(ids.PAIR_A_B,"token0", [ids.TOKEN_A]) @@ -181,5 +181,5 @@ func test_get_best_route{syscall_ptr : felt*, pedersen_ptr : HashBuiltin*, range stop_mock_db_0 = mock_call(ids.PAIR_D_B,"token0", [ids.TOKEN_D]) stop_mock_db_1 = mock_call(ids.PAIR_D_B,"token1", [ids.TOKEN_B]) %} - return () -end + return (); +} diff --git a/tests/test_e2e.cairo b/tests/test_e2e.cairo index 04cefd3..3b56a85 100644 --- a/tests/test_e2e.cairo +++ b/tests/test_e2e.cairo @@ -1,167 +1,167 @@ -# %lang starknet -# from starkware.cairo.common.cairo_builtins import HashBuiltin -# from starkware.starknet.common.syscalls import get_contract_address -# from starkware.cairo.common.uint256 import Uint256, uint256_lt -# from src.contracts.amm_wrapper_library import AmmWrapper -# from starkware.cairo.common.alloc import alloc -# from cairo_graphs.graph.dfs_all_paths import init_dfs -# from cairo_graphs.data_types.data_types import Edge, Vertex, AdjacentVertex -# from cairo_graphs.graph.graph import Graph, build_undirected_graph_from_edges_internal -# from src.contracts.hubble_library import Hubble, parse_all_pairs - -# const JEDI_ROUTER = 19876081725 -# const JEDI_FACTORY = 1786125 - -# const TOKEN_A = 123 -# const TOKEN_B = 456 -# const TOKEN_C = 990 -# const TOKEN_D = 982 - -# const RESERVE_A_B_0_LOW = 27890 -# const RESERVE_A_B_1_LOW = 26789 - -# const PAIR_A_B = 12345 -# const PAIR_A_C = 13345 -# const PAIR_B_C = 23456 -# const PAIR_D_C = 43567 -# const PAIR_D_B = 42567 - -# func before_each{syscall_ptr : felt*, pedersen_ptr : HashBuiltin*, range_check_ptr}(): -# let (contract_address) = get_contract_address() - -# # Store values in contract storage -# %{ store(ids.contract_address, "AmmWrapper_jediswap_router", [ids.JEDI_ROUTER]) %} -# %{ store(ids.contract_address, "AmmWrapper_jediswap_factory", [ids.JEDI_FACTORY]) %} -# return () -# end - -# func build_graph_before_each() -> ( -# graph : Vertex*, graph_len : felt, neighbors : felt*, neighbors_len : felt -# ): -# alloc_locals -# let (graph_len, graph, adj_vertices_count) = Graph.new_graph() - -# let (vertex_a_neighbors : AdjacentVertex*) = alloc() -# let (vertex_b_neighbors : AdjacentVertex*) = alloc() -# let (vertex_c_neighbors : AdjacentVertex*) = alloc() - -# local vertex_a : Vertex = Vertex(0, TOKEN_A, vertex_a_neighbors) -# local vertex_b : Vertex = Vertex(1, TOKEN_B, vertex_b_neighbors) -# local vertex_c : Vertex = Vertex(2, TOKEN_C, vertex_c_neighbors) - -# # populate graph -# assert graph[0] = vertex_a -# assert adj_vertices_count[0] = 0 -# assert graph[1] = vertex_b -# assert adj_vertices_count[1] = 0 -# assert graph[2] = vertex_c -# assert adj_vertices_count[2] = 0 -# let neighbors_len = 3 -# let graph_len = 3 -# return (graph, graph_len, adj_vertices_count, neighbors_len) -# end - -# @external -# func test_e2e{syscall_ptr : felt*, pedersen_ptr : HashBuiltin*, range_check_ptr}(): -# alloc_locals -# before_each() -# %{ stop_mock = mock_call(ids.JEDI_FACTORY,"get_all_pairs", [5,ids.PAIR_A_B, ids.PAIR_A_C,ids.PAIR_B_C,ids.PAIR_D_C,ids.PAIR_D_B]) %} -# let (all_pairs_len, all_pairs) = AmmWrapper.get_all_pairs() -# %{ stop_mock() %} -# assert all_pairs_len = 5 -# assert all_pairs[0] = PAIR_A_B -# assert all_pairs[1] = PAIR_A_C -# assert all_pairs[2] = PAIR_B_C -# assert all_pairs[3] = PAIR_D_C -# assert all_pairs[4] = PAIR_D_B - -# %{ -# stop_mock_ab_0 = mock_call(ids.PAIR_A_B,"token0", [ids.TOKEN_A]) -# stop_mock_ab_1 = mock_call(ids.PAIR_A_B,"token1", [ids.TOKEN_B]) -# stop_mock_ac_0 = mock_call(ids.PAIR_A_C,"token0", [ids.TOKEN_A]) -# stop_mock_ac_1 = mock_call(ids.PAIR_A_C,"token1", [ids.TOKEN_C]) -# stop_mock_bc_0 = mock_call(ids.PAIR_B_C,"token0", [ids.TOKEN_B]) -# stop_mock_bc_1 = mock_call(ids.PAIR_B_C,"token1", [ids.TOKEN_C]) -# stop_mock_dc_0 = mock_call(ids.PAIR_D_C,"token0", [ids.TOKEN_D]) -# stop_mock_dc_1 = mock_call(ids.PAIR_D_C,"token1", [ids.TOKEN_C]) -# stop_mock_db_0 = mock_call(ids.PAIR_D_B,"token0", [ids.TOKEN_D]) -# stop_mock_db_1 = mock_call(ids.PAIR_D_B,"token1", [ids.TOKEN_B]) -# %} - -# # see details in test_dfs.cairo -# let (local parsed_pairs : Edge*) = alloc() -# let (parsed_pairs_len) = parse_all_pairs(all_pairs_len, all_pairs, parsed_pairs, 0) -# assert parsed_pairs_len = 5 -# assert parsed_pairs[0] = Edge(TOKEN_A, TOKEN_B) -# assert parsed_pairs[1] = Edge(TOKEN_A, TOKEN_C) -# assert parsed_pairs[2] = Edge(TOKEN_B, TOKEN_C) -# assert parsed_pairs[3] = Edge(TOKEN_D, TOKEN_C) -# assert parsed_pairs[4] = Edge(TOKEN_D, TOKEN_B) - -# let (graph_len, graph, neighbors) = build_undirected_graph_from_edges_internal( -# pairs_len=parsed_pairs_len, pairs=parsed_pairs -# ) - -# let node_a = graph[0] -# let node_c = graph[2] -# let (saved_paths_len, saved_paths) = init_dfs(graph_len, graph, neighbors, node_a, node_c, 4) -# # %{ -# # print(ids.saved_paths_len) -# # for i in range(ids.saved_paths_len): -# # print(memory[ids.saved_paths+i]) -# # %} -# assert saved_paths_len = 12 -# assert saved_paths[0] = 2 # path 1 length -# assert saved_paths[3] = 4 # path 2 length -# assert saved_paths[8] = 3 # path 3 length -# assert saved_paths[1] = TOKEN_A -# assert saved_paths[2] = TOKEN_C -# assert saved_paths[4] = TOKEN_A -# assert saved_paths[5] = TOKEN_B -# assert saved_paths[6] = TOKEN_D -# assert saved_paths[7] = TOKEN_C -# assert saved_paths[9] = TOKEN_A -# assert saved_paths[10] = TOKEN_B -# assert saved_paths[11] = TOKEN_C - -# # let amount_in = Uint256(1000,0) - -# # %{ -# # stop_mock_ab_out = mock_call(ids.PAIR_A_B,"token0", [ids.TOKEN_A]) -# # stop_mock_ac_out = mock_call(ids.PAIR_A_B,"token0", [ids.TOKEN_A]) -# # stop_mock_bc_out = mock_call(ids.PAIR_A_B,"token0", [ids.TOKEN_A]) -# # stop_mock_dc_out = mock_call(ids.PAIR_A_B,"token0", [ids.TOKEN_A]) -# # stop_mock_db_out= mock_call(ids.PAIR_A_B,"token0", [ids.TOKEN_A]) -# # %} - -# # now that we have the paths -> we need to run IJediswapRouter.get_amounts_out with each path :) -# return () -# end - -# func test_get_best_route{syscall_ptr : felt*, pedersen_ptr : HashBuiltin*, range_check_ptr}(): -# alloc_locals -# before_each() -# %{ stop_mock = mock_call(ids.JEDI_FACTORY,"get_all_pairs", [5,ids.PAIR_A_B, ids.PAIR_A_C,ids.PAIR_B_C,ids.PAIR_D_C,ids.PAIR_D_B]) %} -# let (all_pairs_len, all_pairs) = AmmWrapper.get_all_pairs() -# %{ stop_mock() %} -# assert all_pairs_len = 5 -# assert all_pairs[0] = PAIR_A_B -# assert all_pairs[1] = PAIR_A_C -# assert all_pairs[2] = PAIR_B_C -# assert all_pairs[3] = PAIR_D_C -# assert all_pairs[4] = PAIR_D_B - -# %{ -# stop_mock_ab_0 = mock_call(ids.PAIR_A_B,"token0", [ids.TOKEN_A]) -# stop_mock_ab_1 = mock_call(ids.PAIR_A_B,"token1", [ids.TOKEN_B]) -# stop_mock_ac_0 = mock_call(ids.PAIR_A_C,"token0", [ids.TOKEN_A]) -# stop_mock_ac_1 = mock_call(ids.PAIR_A_C,"token1", [ids.TOKEN_C]) -# stop_mock_bc_0 = mock_call(ids.PAIR_B_C,"token0", [ids.TOKEN_B]) -# stop_mock_bc_1 = mock_call(ids.PAIR_B_C,"token1", [ids.TOKEN_C]) -# stop_mock_dc_0 = mock_call(ids.PAIR_D_C,"token0", [ids.TOKEN_D]) -# stop_mock_dc_1 = mock_call(ids.PAIR_D_C,"token1", [ids.TOKEN_C]) -# stop_mock_db_0 = mock_call(ids.PAIR_D_B,"token0", [ids.TOKEN_D]) -# stop_mock_db_1 = mock_call(ids.PAIR_D_B,"token1", [ids.TOKEN_B]) -# %} -# return () -# end +// %lang starknet +// from starkware.cairo.common.cairo_builtins import HashBuiltin +// from starkware.starknet.common.syscalls import get_contract_address +// from starkware.cairo.common.uint256 import Uint256, uint256_lt +// from src.contracts.amm_wrapper_library import AmmWrapper +// from starkware.cairo.common.alloc import alloc +// from cairo_graphs.graph.dfs_all_paths import init_dfs +// from cairo_graphs.data_types.data_types import Edge, Vertex, AdjacentVertex +// from cairo_graphs.graph.graph import Graph, build_undirected_graph_from_edges_internal +// from src.contracts.hubble_library import Hubble, parse_all_pairs + +// const JEDI_ROUTER = 19876081725 +// const JEDI_FACTORY = 1786125 + +// const TOKEN_A = 123 +// const TOKEN_B = 456 +// const TOKEN_C = 990 +// const TOKEN_D = 982 + +// const RESERVE_A_B_0_LOW = 27890 +// const RESERVE_A_B_1_LOW = 26789 + +// const PAIR_A_B = 12345 +// const PAIR_A_C = 13345 +// const PAIR_B_C = 23456 +// const PAIR_D_C = 43567 +// const PAIR_D_B = 42567 + +// func before_each{syscall_ptr : felt*, pedersen_ptr : HashBuiltin*, range_check_ptr}(): +// let (contract_address) = get_contract_address() + +// # Store values in contract storage +// %{ store(ids.contract_address, "AmmWrapper_jediswap_router", [ids.JEDI_ROUTER]) %} +// %{ store(ids.contract_address, "AmmWrapper_jediswap_factory", [ids.JEDI_FACTORY]) %} +// return () +// end + +// func build_graph_before_each() -> ( +// graph : Vertex*, graph_len : felt, neighbors : felt*, neighbors_len : felt +// ): +// alloc_locals +// let (graph_len, graph, adj_vertices_count) = GraphMethods.new_graph() + +// let (vertex_a_neighbors : AdjacentVertex*) = alloc() +// let (vertex_b_neighbors : AdjacentVertex*) = alloc() +// let (vertex_c_neighbors : AdjacentVertex*) = alloc() + +// local vertex_a : Vertex = Vertex(0, TOKEN_A, vertex_a_neighbors) +// local vertex_b : Vertex = Vertex(1, TOKEN_B, vertex_b_neighbors) +// local vertex_c : Vertex = Vertex(2, TOKEN_C, vertex_c_neighbors) + +// # populate graph +// assert graph[0] = vertex_a +// assert adj_vertices_count[0] = 0 +// assert graph[1] = vertex_b +// assert adj_vertices_count[1] = 0 +// assert graph[2] = vertex_c +// assert adj_vertices_count[2] = 0 +// let neighbors_len = 3 +// let graph_len = 3 +// return (graph, graph_len, adj_vertices_count, neighbors_len) +// end + +// @external +// func test_e2e{syscall_ptr : felt*, pedersen_ptr : HashBuiltin*, range_check_ptr}(): +// alloc_locals +// before_each() +// %{ stop_mock = mock_call(ids.JEDI_FACTORY,"get_all_pairs", [5,ids.PAIR_A_B, ids.PAIR_A_C,ids.PAIR_B_C,ids.PAIR_D_C,ids.PAIR_D_B]) %} +// let (all_pairs_len, all_pairs) = AmmWrapper.get_all_pairs() +// %{ stop_mock() %} +// assert all_pairs_len = 5 +// assert all_pairs[0] = PAIR_A_B +// assert all_pairs[1] = PAIR_A_C +// assert all_pairs[2] = PAIR_B_C +// assert all_pairs[3] = PAIR_D_C +// assert all_pairs[4] = PAIR_D_B + +// %{ +// stop_mock_ab_0 = mock_call(ids.PAIR_A_B,"token0", [ids.TOKEN_A]) +// stop_mock_ab_1 = mock_call(ids.PAIR_A_B,"token1", [ids.TOKEN_B]) +// stop_mock_ac_0 = mock_call(ids.PAIR_A_C,"token0", [ids.TOKEN_A]) +// stop_mock_ac_1 = mock_call(ids.PAIR_A_C,"token1", [ids.TOKEN_C]) +// stop_mock_bc_0 = mock_call(ids.PAIR_B_C,"token0", [ids.TOKEN_B]) +// stop_mock_bc_1 = mock_call(ids.PAIR_B_C,"token1", [ids.TOKEN_C]) +// stop_mock_dc_0 = mock_call(ids.PAIR_D_C,"token0", [ids.TOKEN_D]) +// stop_mock_dc_1 = mock_call(ids.PAIR_D_C,"token1", [ids.TOKEN_C]) +// stop_mock_db_0 = mock_call(ids.PAIR_D_B,"token0", [ids.TOKEN_D]) +// stop_mock_db_1 = mock_call(ids.PAIR_D_B,"token1", [ids.TOKEN_B]) +// %} + +// # see details in test_dfs.cairo +// let (local parsed_pairs : Edge*) = alloc() +// let (parsed_pairs_len) = parse_all_pairs(all_pairs_len, all_pairs, parsed_pairs, 0) +// assert parsed_pairs_len = 5 +// assert parsed_pairs[0] = Edge(TOKEN_A, TOKEN_B) +// assert parsed_pairs[1] = Edge(TOKEN_A, TOKEN_C) +// assert parsed_pairs[2] = Edge(TOKEN_B, TOKEN_C) +// assert parsed_pairs[3] = Edge(TOKEN_D, TOKEN_C) +// assert parsed_pairs[4] = Edge(TOKEN_D, TOKEN_B) + +// let (graph_len, graph, neighbors) = build_undirected_graph_from_edges_internal( +// pairs_len=parsed_pairs_len, pairs=parsed_pairs +// ) + +// let node_a = graph[0] +// let node_c = graph[2] +// let (saved_paths_len, saved_paths) = init_dfs(graph_len, graph, neighbors, node_a, node_c, 4) +// # %{ +// # print(ids.saved_paths_len) +// # for i in range(ids.saved_paths_len): +// # print(memory[ids.saved_paths+i]) +// # %} +// assert saved_paths_len = 12 +// assert saved_paths[0] = 2 # path 1 length +// assert saved_paths[3] = 4 # path 2 length +// assert saved_paths[8] = 3 # path 3 length +// assert saved_paths[1] = TOKEN_A +// assert saved_paths[2] = TOKEN_C +// assert saved_paths[4] = TOKEN_A +// assert saved_paths[5] = TOKEN_B +// assert saved_paths[6] = TOKEN_D +// assert saved_paths[7] = TOKEN_C +// assert saved_paths[9] = TOKEN_A +// assert saved_paths[10] = TOKEN_B +// assert saved_paths[11] = TOKEN_C + +// # let amount_in = Uint256(1000,0) + +// # %{ +// # stop_mock_ab_out = mock_call(ids.PAIR_A_B,"token0", [ids.TOKEN_A]) +// # stop_mock_ac_out = mock_call(ids.PAIR_A_B,"token0", [ids.TOKEN_A]) +// # stop_mock_bc_out = mock_call(ids.PAIR_A_B,"token0", [ids.TOKEN_A]) +// # stop_mock_dc_out = mock_call(ids.PAIR_A_B,"token0", [ids.TOKEN_A]) +// # stop_mock_db_out= mock_call(ids.PAIR_A_B,"token0", [ids.TOKEN_A]) +// # %} + +// # now that we have the paths -> we need to run IJediswapRouter.get_amounts_out with each path :) +// return () +// end + +// func test_get_best_route{syscall_ptr : felt*, pedersen_ptr : HashBuiltin*, range_check_ptr}(): +// alloc_locals +// before_each() +// %{ stop_mock = mock_call(ids.JEDI_FACTORY,"get_all_pairs", [5,ids.PAIR_A_B, ids.PAIR_A_C,ids.PAIR_B_C,ids.PAIR_D_C,ids.PAIR_D_B]) %} +// let (all_pairs_len, all_pairs) = AmmWrapper.get_all_pairs() +// %{ stop_mock() %} +// assert all_pairs_len = 5 +// assert all_pairs[0] = PAIR_A_B +// assert all_pairs[1] = PAIR_A_C +// assert all_pairs[2] = PAIR_B_C +// assert all_pairs[3] = PAIR_D_C +// assert all_pairs[4] = PAIR_D_B + +// %{ +// stop_mock_ab_0 = mock_call(ids.PAIR_A_B,"token0", [ids.TOKEN_A]) +// stop_mock_ab_1 = mock_call(ids.PAIR_A_B,"token1", [ids.TOKEN_B]) +// stop_mock_ac_0 = mock_call(ids.PAIR_A_C,"token0", [ids.TOKEN_A]) +// stop_mock_ac_1 = mock_call(ids.PAIR_A_C,"token1", [ids.TOKEN_C]) +// stop_mock_bc_0 = mock_call(ids.PAIR_B_C,"token0", [ids.TOKEN_B]) +// stop_mock_bc_1 = mock_call(ids.PAIR_B_C,"token1", [ids.TOKEN_C]) +// stop_mock_dc_0 = mock_call(ids.PAIR_D_C,"token0", [ids.TOKEN_D]) +// stop_mock_dc_1 = mock_call(ids.PAIR_D_C,"token1", [ids.TOKEN_C]) +// stop_mock_db_0 = mock_call(ids.PAIR_D_B,"token0", [ids.TOKEN_D]) +// stop_mock_db_1 = mock_call(ids.PAIR_D_B,"token1", [ids.TOKEN_B]) +// %} +// return () +// end diff --git a/tests/test_graph.cairo b/tests/test_graph.cairo index 048ccaa..90744cd 100644 --- a/tests/test_graph.cairo +++ b/tests/test_graph.cairo @@ -1,27 +1,29 @@ %lang starknet -from cairo_graphs.graph.graph import Graph -from cairo_graphs.data_types.data_types import Edge +from cairo_graphs.graph.graph import GraphMethods +from cairo_graphs.data_types.data_types import Edge, Graph from starkware.cairo.common.alloc import alloc -const TOKEN_A = 123 -const TOKEN_B = 456 -const TOKEN_C = 990 -const TOKEN_D = 982 +const TOKEN_A = 123; +const TOKEN_B = 456; +const TOKEN_C = 990; +const TOKEN_D = 982; @external -func test_build_graph(): - let edges : Edge* = alloc() - assert edges[0] = Edge(TOKEN_A, TOKEN_B, 1) - assert edges[1] = Edge(TOKEN_A, TOKEN_C, 1) - assert edges[2] = Edge(TOKEN_B, TOKEN_C, 1) +func test_build_graph() { + let edges: Edge* = alloc(); + assert edges[0] = Edge(TOKEN_A, TOKEN_B, 1); + assert edges[1] = Edge(TOKEN_A, TOKEN_C, 1); + assert edges[2] = Edge(TOKEN_B, TOKEN_C, 1); - let (graph_len, graph, adj_vertices_count) = Graph.build_undirected_graph_from_edges(3, edges) - assert graph_len = 3 - assert graph[0].identifier = TOKEN_A - assert graph[1].identifier = TOKEN_B - assert graph[2].identifier = TOKEN_C - assert adj_vertices_count[0] = 2 - assert adj_vertices_count[1] = 2 - assert adj_vertices_count[2] = 2 - return () -end + let graph = GraphMethods.build_undirected_graph_from_edges( + 3, edges + ); + assert graph.length = 3; + assert graph.vertices[0].identifier = TOKEN_A; + assert graph.vertices[1].identifier = TOKEN_B; + assert graph.vertices[2].identifier = TOKEN_C; + assert graph.adjacent_vertices_count[0] = 2; + assert graph.adjacent_vertices_count[1] = 2; + assert graph.adjacent_vertices_count[2] = 2; + return (); +}