|
| 1 | +/** |
| 2 | + * The above code defines a PriorityQueue class and implements Dijkstra's algorithm to find the |
| 3 | + * shortest path between two nodes in a graph. |
| 4 | + * @param pools - The `pools` parameter is an array of objects representing pools. Each pool object has |
| 5 | + * the following properties: |
| 6 | + * @returns The `dijkstra` function returns an array of objects representing the path from the start |
| 7 | + * address to the end address in the given graph. Each object in the array contains the following |
| 8 | + * properties: |
| 9 | + * - `address`: the address of the current node in the path |
| 10 | + * - `previousAddress`: the address of the previous node in the path |
| 11 | + * - `reserve0`: the reserve of the base token in the current |
| 12 | + */ |
| 13 | +class PriorityQueue { |
| 14 | + constructor(comparator = (a, b) => a.distance < b.distance) { |
| 15 | + this._heap = []; |
| 16 | + this._comparator = comparator; |
| 17 | + } |
| 18 | + |
| 19 | + size() { |
| 20 | + return this._heap.length; |
| 21 | + } |
| 22 | + |
| 23 | + isEmpty() { |
| 24 | + return this.size() == 0; |
| 25 | + } |
| 26 | + |
| 27 | + peek() { |
| 28 | + return this._heap[0]; |
| 29 | + } |
| 30 | + |
| 31 | + enqueue(value) { |
| 32 | + this._heap.push(value); |
| 33 | + this._siftUp(); |
| 34 | + } |
| 35 | + |
| 36 | + dequeue() { |
| 37 | + const poppedValue = this.peek(); |
| 38 | + const bottomValue = this._heap.pop(); |
| 39 | + if (this.size() > 0) { |
| 40 | + this._heap[0] = bottomValue; |
| 41 | + this._siftDown(); |
| 42 | + } |
| 43 | + return poppedValue; |
| 44 | + } |
| 45 | + |
| 46 | + _greater(i, j) { |
| 47 | + return this._comparator(this._heap[i], this._heap[j]); |
| 48 | + } |
| 49 | + |
| 50 | + _swap(i, j) { |
| 51 | + [this._heap[i], this._heap[j]] = [this._heap[j], this._heap[i]]; |
| 52 | + } |
| 53 | + |
| 54 | + _siftUp() { |
| 55 | + let node = this.size() - 1; |
| 56 | + while (node > 0 && this._greater(Math.floor((node - 1) / 2), node)) { |
| 57 | + this._swap(node, Math.floor((node - 1) / 2)); |
| 58 | + node = Math.floor((node - 1) / 2); |
| 59 | + } |
| 60 | + } |
| 61 | + |
| 62 | + _siftDown() { |
| 63 | + let node = 0; |
| 64 | + while ( |
| 65 | + (node * 2 + 1 < this.size() && this._greater(node, node * 2 + 1)) || |
| 66 | + (node * 2 + 2 < this.size() && this._greater(node, node * 2 + 2)) |
| 67 | + ) { |
| 68 | + let maxChild = |
| 69 | + node * 2 + 2 < this.size() && this._greater(node * 2 + 1, node * 2 + 2) |
| 70 | + ? node * 2 + 2 |
| 71 | + : node * 2 + 1; |
| 72 | + this._swap(node, maxChild); |
| 73 | + node = maxChild; |
| 74 | + } |
| 75 | + } |
| 76 | +} |
| 77 | + |
| 78 | +/** |
| 79 | + * The function creates a graph data structure based on an array of pool objects. |
| 80 | + * @param pools - An array of objects representing pools. Each pool object has the following |
| 81 | + * properties: |
| 82 | + * @returns a graph object that represents the connections between pools. Each pool is represented by |
| 83 | + * its address, and for each pool, there is an array of objects representing the connections to other |
| 84 | + * pools. Each object in the array contains the address of the connected pool, the reserves of the |
| 85 | + * tokens in the pool, and the decimals of the tokens. |
| 86 | + */ |
| 87 | +function createGraph(pools) { |
| 88 | + const graph = {}; |
| 89 | + pools.forEach((pool) => { |
| 90 | + const { address0, address1 } = pool; |
| 91 | + if (!graph[address0]) graph[address0] = []; |
| 92 | + if (!graph[address1]) graph[address1] = []; |
| 93 | + graph[address0].push({ |
| 94 | + address: address1, |
| 95 | + reserve0: pool.base_token.attributes.reserves, |
| 96 | + reserve1: pool.quote_token.attributes.reserves, |
| 97 | + decimals0: pool.base_token.attributes.decimals, |
| 98 | + decimals1: pool.quote_token.attributes.decimals, |
| 99 | + }); |
| 100 | + graph[address1].push({ |
| 101 | + address: address0, |
| 102 | + reserve0: pool.quote_token.attributes.reserves, |
| 103 | + reserve1: pool.base_token.attributes.reserves, |
| 104 | + decimals0: pool.quote_token.attributes.decimals, |
| 105 | + decimals1: pool.base_token.attributes.decimals, |
| 106 | + }); |
| 107 | + }); |
| 108 | + return graph; |
| 109 | +} |
| 110 | + |
| 111 | +/** |
| 112 | + * The `dijkstra` function implements Dijkstra's algorithm to find the shortest path between two |
| 113 | + * addresses in a graph. |
| 114 | + * @param graph - The `graph` parameter is an object that represents the graph of addresses and their |
| 115 | + * connections. Each key in the object represents an address, and the corresponding value is an array |
| 116 | + * of neighboring addresses. |
| 117 | + * @param startAddress - The `startAddress` parameter is the address of the starting node in the graph. |
| 118 | + * It represents the node from which the shortest path will be calculated. |
| 119 | + * @param endAddress - The `endAddress` parameter in the `dijkstra` function represents the destination |
| 120 | + * address in the graph. It is the address that you want to find the shortest path to from the |
| 121 | + * `startAddress`. |
| 122 | + * @returns an array of objects representing the shortest path from the startAddress to the endAddress |
| 123 | + * in the given graph. Each object in the array represents a node in the path and contains the |
| 124 | + * following properties: address, previousAddress, reserve0, reserve1, decimals0, and decimals1. |
| 125 | + */ |
| 126 | +function dijkstra(graph, startAddress, endAddress) { |
| 127 | + const distances = {}; |
| 128 | + const previous = {}; |
| 129 | + const nodes = new PriorityQueue((a, b) => a.distance < b.distance); |
| 130 | + const path = []; |
| 131 | + |
| 132 | + // Set distances to all nodes to be infinity except the start node |
| 133 | + Object.keys(graph).forEach((address) => { |
| 134 | + distances[address] = Infinity; |
| 135 | + previous[address] = null; |
| 136 | + }); |
| 137 | + |
| 138 | + // Set start node distance to 0 and enqueue it |
| 139 | + distances[startAddress] = 0; |
| 140 | + nodes.enqueue({ address: startAddress, distance: 0 }); |
| 141 | + |
| 142 | + while (!nodes.isEmpty()) { |
| 143 | + const smallest = nodes.dequeue(); |
| 144 | + |
| 145 | + if (smallest.address === endAddress) { |
| 146 | + // We found the destination and can build the path |
| 147 | + let currentAddress = endAddress; |
| 148 | + while (previous[currentAddress]) { |
| 149 | + path.push({ |
| 150 | + address: currentAddress, |
| 151 | + previousAddress: previous[currentAddress].address, |
| 152 | + reserve0: previous[currentAddress].reserve0, |
| 153 | + reserve1: previous[currentAddress].reserve1, |
| 154 | + decimals0: previous[currentAddress].decimals0, |
| 155 | + decimals1: previous[currentAddress].decimals1, |
| 156 | + }); |
| 157 | + currentAddress = previous[currentAddress].address; |
| 158 | + } |
| 159 | + path.reverse(); |
| 160 | + return path; |
| 161 | + } |
| 162 | + |
| 163 | + if (smallest.address && graph[smallest.address]) { |
| 164 | + graph[smallest.address].forEach((neighbor) => { |
| 165 | + const alt = |
| 166 | + distances[smallest.address] + neighbor.reserve1 / neighbor.reserve0; // This line might need modification based on what 'distance' means in this context |
| 167 | + if (alt < distances[neighbor.address]) { |
| 168 | + distances[neighbor.address] = alt; |
| 169 | + previous[neighbor.address] = { |
| 170 | + address: smallest.address, |
| 171 | + reserve0: neighbor.reserve0, |
| 172 | + reserve1: neighbor.reserve1, |
| 173 | + decimals0: neighbor.decimals0, |
| 174 | + decimals1: neighbor.decimals1, |
| 175 | + }; |
| 176 | + nodes.enqueue({ |
| 177 | + address: neighbor.address, |
| 178 | + distance: alt, |
| 179 | + }); |
| 180 | + } |
| 181 | + }); |
| 182 | + } |
| 183 | + } |
| 184 | + |
| 185 | + return path; |
| 186 | +} |
0 commit comments