-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathsmartroutelagrange.py
More file actions
1201 lines (1007 loc) · 49.3 KB
/
smartroutelagrange.py
File metadata and controls
1201 lines (1007 loc) · 49.3 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
# -*- coding: utf-8 -*-
"""SmartRouteLagrange.ipynb
# Lagrange Method for Smart Routing
## Initialize Near protocol parameters
"""
#Beginning methods were borrowed/adapted from Marco's index-helper code base on Ref-UI
try:
from rpc_info import TESTNET_RPC_URL, MAINNET_RPC_URL
except ImportError:
TESTNET_RPC_URL= ["https://rpc.testnet.near.org", ]
MAINNET_RPC_URL= ["https://rpc.mainnet.near.org", ]
try:
from indexer_info import INDEXER_DSN, INDEXER_UID, INDEXER_PWD, INDEXER_HOST, INDEXER_PORT
except ImportError:
INDEXER_DSN = "mainnet_explorer"
INDEXER_UID = "public_readonly"
INDEXER_PWD = "nearprotocol"
INDEXER_HOST = "104.199.89.51"
INDEXER_PORT = "5432"
"""## Set up Ref Coin database for both mainnet and testnet"""
"""
"""
class Cfg:
NETWORK_ID = "MAINNET"
NETWORK = {
"TESTNET": {
"NEAR_RPC_URL": TESTNET_RPC_URL,
"FARMING_CONTRACT": "v2.ref-farming.testnet",
"REF_CONTRACT": "ref-finance-101.testnet",
"REDIS_KEY": "FARMS_TESTNET",
"REDIS_POOL_KEY": "POOLS_TESTNET",
"REDIS_POOL_BY_TOKEN_KEY": "POOLS_BY_TOKEN_TESTNET",
"REDIS_TOP_POOL_KEY": "TOP_POOLS_TESTNET",
"REDIS_TOKEN_PRICE_KEY": "TOKEN_PRICE_TESTNET",
"REDIS_TOKEN_METADATA_KEY": "TOKEN_METADATA_TESTNET",
"REDIS_WHITELIST_KEY": "WHITELIST_TESTNET",
"INDEXER_DSN": "testnet_explorer",
"INDEXER_UID": "public_readonly",
"INDEXER_PWD": "nearprotocol",
"INDEXER_HOST": "35.184.214.98",
"INDEXER_PORT": "5432",
},
"MAINNET": {
"NEAR_RPC_URL": MAINNET_RPC_URL,
"FARMING_CONTRACT": "v2.ref-farming.near",
"REF_CONTRACT": "v2.ref-finance.near",
"REDIS_KEY": "FARMS_MAINNET",
"REDIS_POOL_BY_TOKEN_KEY": "POOLS_BY_TOKEN_MAINNET",
"REDIS_POOL_KEY": "POOLS_MAINNET",
"REDIS_TOP_POOL_KEY": "TOP_POOLS_MAINNET",
"REDIS_TOKEN_PRICE_KEY": "TOKEN_PRICE_MAINNET",
"REDIS_TOKEN_METADATA_KEY": "TOKEN_METADATA_MAINNET",
"REDIS_WHITELIST_KEY": "WHITELIST_MAINNET",
"INDEXER_DSN": INDEXER_DSN,
"INDEXER_UID": INDEXER_UID,
"INDEXER_PWD": INDEXER_PWD,
"INDEXER_HOST": INDEXER_HOST,
"INDEXER_PORT": INDEXER_PORT,
}
}
TOKENS = {
"TESTNET": [
{"SYMBOL": "near", "NEAR_ID": "wrap.testnet", "MD_ID": "near", "DECIMAL": 24},
{"SYMBOL": "nDAI", "NEAR_ID": "ndai.ft-fin.testnet", "MD_ID": "dai", "DECIMAL": 8},
{"SYMBOL": "nUSDT", "NEAR_ID": "nusdt.ft-fin.testnet", "MD_ID": "tether", "DECIMAL": 6},
{"SYMBOL": "ref", "NEAR_ID": "rft.tokenfactory.testnet", "MD_ID": "ref-finance.testnet|24|wrap.testnet", "DECIMAL": 8},
],
"MAINNET": [
{"SYMBOL": "near", "NEAR_ID": "wrap.near", "MD_ID": "near", "DECIMAL": 24},
{"SYMBOL": "nUSDC", "NEAR_ID": "a0b86991c6218b36c1d19d4a2e9eb0ce3606eb48.factory.bridge.near", "MD_ID": "usd-coin", "DECIMAL": 6},
{"SYMBOL": "nUSDT", "NEAR_ID": "dac17f958d2ee523a2206206994597c13d831ec7.factory.bridge.near", "MD_ID": "tether", "DECIMAL": 6},
{"SYMBOL": "nDAI", "NEAR_ID": "6b175474e89094c44da98b954eedeac495271d0f.factory.bridge.near", "MD_ID": "dai", "DECIMAL": 18},
{"SYMBOL": "nWETH", "NEAR_ID": "c02aaa39b223fe8d0a0e5c4f27ead9083c756cc2.factory.bridge.near", "MD_ID": "weth", "DECIMAL": 18},
{"SYMBOL": "n1INCH", "NEAR_ID": "111111111117dc0aa78b770fa6a738034120c302.factory.bridge.near", "MD_ID": "1inch", "DECIMAL": 18},
{"SYMBOL": "nGRT", "NEAR_ID": "c944e90c64b2c07662a292be6244bdf05cda44a7.factory.bridge.near", "MD_ID": "the-graph", "DECIMAL": 18},
{"SYMBOL": "SKYWARD", "NEAR_ID": "token.skyward.near", "MD_ID": "v2.ref-finance.near|0|wrap.near", "DECIMAL": 18},
{"SYMBOL": "REF", "NEAR_ID": "token.v2.ref-finance.near", "MD_ID": "v2.ref-finance.near|79|wrap.near", "DECIMAL": 18},
{"SYMBOL": "BANANA", "NEAR_ID": "berryclub.ek.near", "MD_ID": "v2.ref-finance.near|5|wrap.near", "DECIMAL": 18},
{"SYMBOL": "nHT", "NEAR_ID": "6f259637dcd74c767781e37bc6133cd6a68aa161.factory.bridge.near", "MD_ID": "huobi-token", "DECIMAL": 18},
{"SYMBOL": "nGTC", "NEAR_ID": "de30da39c46104798bb5aa3fe8b9e0e1f348163f.factory.bridge.near", "MD_ID": "gitcoin", "DECIMAL": 18},
{"SYMBOL": "nUNI", "NEAR_ID": "1f9840a85d5af5bf1d1762f925bdaddc4201f984.factory.bridge.near", "MD_ID": "uniswap", "DECIMAL": 18},
{"SYMBOL": "nWBTC", "NEAR_ID": "2260fac5e5542a773aa44fbcfedf7c193bc2c599.factory.bridge.near", "MD_ID": "wrapped-bitcoin", "DECIMAL": 8},
{"SYMBOL": "nLINK", "NEAR_ID": "514910771af9ca656af840dff83e8264ecf986ca.factory.bridge.near", "MD_ID": "chainlink", "DECIMAL": 18},
{"SYMBOL": "PARAS", "NEAR_ID": "token.paras.near", "MD_ID": "v2.ref-finance.near|377|wrap.near", "DECIMAL": 18},
{"SYMBOL": "STNEAR", "NEAR_ID": "meta-pool.near", "MD_ID": "v2.ref-finance.near|535|wrap.near", "DECIMAL": 24},
{"SYMBOL": "marmaj", "NEAR_ID": "marmaj.tkn.near", "MD_ID": "v2.ref-finance.near|11|wrap.near", "DECIMAL": 18},
{"SYMBOL": "PULSE", "NEAR_ID": "52a047ee205701895ee06a375492490ec9c597ce.factory.bridge.near", "MD_ID": "v2.ref-finance.near|852|wrap.near", "DECIMAL": 18},
{"SYMBOL": "ETH", "NEAR_ID": "aurora", "MD_ID": "ethereum", "DECIMAL": 18},
{"SYMBOL": "AURORA", "NEAR_ID": "aaaaaa20d9e0e2461697782ef11675f668207961.factory.bridge.near", "MD_ID": "v2.ref-finance.near|1395|wrap.near", "DECIMAL": 18},
],
}
MARKET_URL = "api.coingecko.com"
import requests
import base64
import json
import networkx as nx
# import nxviz as nv
import itertools
import numpy as np
import pandas as pd
import matplotlib.pyplot as plt
from decimal import Decimal
#from config import Cfg
REF_CONTRACT = 'v2.ref-finance.near'
FARMING = 'v2.ref-farming.near'
INDEXER_URL = 'https://indexer.ref-finance.net'
class MultiNodeJsonProviderError(Exception):
pass
# Adapted from Marco's index-helper code base on Ref-UI
class MultiNodeJsonProvider(object):
def __init__(self, network_id):
nodes = Cfg.NETWORK[network_id]["NEAR_RPC_URL"]
best_height = 0
best_node = None
for node in nodes:
self._rpc_addr = node
node_status = self.ping_node()
print(node, node_status)
if not node_status['syncing'] and node_status['latest_block_height'] > best_height + 10:
best_height = node_status['latest_block_height']
best_node = node
if best_node is not None:
print("Choose near rpc node", best_node)
self._rpc_addr = best_node
else:
raise MultiNodeJsonProviderError("No available nodes")
def rpc_addr(self):
return self._rpc_addr
def json_rpc(self, method, params, timeout=2):
j = {
'method': method,
'params': params,
'id': 'dontcare',
'jsonrpc': '2.0'
}
r = requests.post(self.rpc_addr(), json=j, timeout=timeout)
r.raise_for_status()
content = json.loads(r.content)
if "error" in content:
raise MultiNodeJsonProviderError(content["error"])
return content["result"]
def send_tx(self, signed_tx):
return self.json_rpc('broadcast_tx_async', [base64.b64encode(signed_tx).decode('utf8')])
def send_tx_and_wait(self, signed_tx, timeout):
return self.json_rpc('broadcast_tx_commit', [base64.b64encode(signed_tx).decode('utf8')], timeout=timeout)
def get_status(self):
# r = requests.get("%s/status" % self.rpc_addr(), timeout=2)
# r.raise_for_status()
# return json.loads(r.content)
return self.json_rpc('status', [None])
def get_validators(self):
return self.json_rpc('validators', [None])
def query(self, query_object):
return self.json_rpc('query', query_object)
def get_account(self, account_id, finality='optimistic'):
return self.json_rpc('query', {"request_type": "view_account", "account_id": account_id, "finality": finality})
def get_access_key_list(self, account_id, finality='optimistic'):
return self.json_rpc('query', {"request_type": "view_access_key_list", "account_id": account_id, "finality": finality})
def get_access_key(self, account_id, public_key, finality='optimistic'):
return self.json_rpc('query', {"request_type": "view_access_key", "account_id": account_id,
"public_key": public_key, "finality": finality})
def view_call(self, account_id, method_name, args, finality='optimistic'):
return self.json_rpc('query', {"request_type": "call_function", "account_id": account_id,
"method_name": method_name, "args_base64": base64.b64encode(args).decode('utf8'), "finality": finality})
def get_block(self, block_id):
return self.json_rpc('block', [block_id])
def get_chunk(self, chunk_id):
return self.json_rpc('chunk', [chunk_id])
def get_tx(self, tx_hash, tx_recipient_id):
return self.json_rpc('tx', [tx_hash, tx_recipient_id])
def get_changes_in_block(self, changes_in_block_request):
return self.json_rpc('EXPERIMENTAL_changes_in_block', changes_in_block_request)
def ping_node(self):
ret = {'latest_block_height': 0, 'syncing': True}
try:
status = self.get_status()
if "sync_info" in status:
ret['latest_block_height'] = status['sync_info']['latest_block_height']
ret['syncing'] = status['sync_info']['syncing']
except MultiNodeJsonProviderError as e:
print("ping node MultiNodeJsonProviderError: ", e)
except Exception as e:
print("ping node Exception: ", e)
return ret
def view_call_readable(self, account_id, method_name, args, finality='optimistic'):
"""utility function for converting view functions binary outputs in to a
human-readable python dictionary format"""
ret = self.view_call(account_id,method_name,args,finality=finality)
b = "".join([chr(x) for x in ret["result"]])
obj = json.loads(b)
return obj
conn = MultiNodeJsonProvider("MAINNET")
"""## Ref-Finance Utility Functions"""
def getTokenMetadata(tokenContract):
return conn.view_call_readable(tokenContract, 'ft_metadata',b'{}')
def getPool(poolId,refContract=REF_CONTRACT):
argString = f'{{"pool_id":{poolId}}}'
return conn.view_call_readable(refContract, 'get_pool', argString.encode('utf-8'))
def getPools(from_index=0,limit=100,refContract=REF_CONTRACT):
argString = f'{{"from_index":{from_index},"limit":{limit}}}'
return conn.view_call_readable(refContract, 'get_pools', argString.encode('utf-8'))
def getPoolShares(poolId,accountId,refContract=REF_CONTRACT):
argString = f'{{"pool_id":{poolId}, "account_id":"{accountId}"}}'
return conn.view_call_readable(refContract, 'get_pool_shares', argString.encode('utf-8'))
def getPoolTotalShares(poolId,refContract=REF_CONTRACT):
argString = f'{{"pool_id":{poolId}}}'
return conn.view_call_readable(refContract, 'get_pool_total_shares', argString.encode('utf-8'))
def getNumberOfPools(refContract=REF_CONTRACT):
return conn.view_call_readable(refContract, 'get_number_of_pools', b'{}')
def getUserDeposits(accountId, refContract=REF_CONTRACT):
argString = f'{{"account_id":"{accountId}"}}'
return conn.view_call_readable(refContract, 'get_deposits', argString.encode('utf-8'))
def getReturn(poolId,tokenIn,tokenOut,amountIn,refContract=REF_CONTRACT):
argString = f'{{"token_in":"{tokenIn}","token_out":"{tokenOut}","pool_id":{poolId},"amount_in":"{amountIn}"}}'
return conn.view_call_readable(refContract, 'get_return', argString.encode('utf-8'))
"""## Ref Network, Graph, and Pool Functions"""
def getAllPools(refContract=REF_CONTRACT):
"""Iterates over all pools and returns data structures for current characteristics
of each pool using the build-in Ref Finance contract function.
Adds attributes for pool ID and pool reserves (a key-value pairing of token
contract to the reserves in the pool)
"""
pools = []
numberOfPools = getNumberOfPools(refContract)
from_index = 0
delta = 400
while from_index < numberOfPools:
additionalPools = getPools(from_index,limit=delta)
pools.extend(additionalPools)
from_index += delta
for i,pool in enumerate(pools):
pool['id'] = i
pool['reserves'] = {pool['token_account_ids'][i]:pool['amounts'][i] for i in range(len(pool['token_account_ids']))}
# pool['token_account_ids'][1]:pool['amounts'][1]}
return pools
def getAllNonzeroPools(pools=None,refContract=REF_CONTRACT):
'''Filter out the pools that contain 0 reserves of at least one token.
Return a list of pool structs.
'''
if pools is None:
pools = getAllPools(refContract)
nzPools = [p for p in pools if '0' not in p['amounts']]
return nzPools
def plotGraphOfPools(pools=None, transitions = None):
if transitions is None:
transitions = getTransitions(pools)
g = nx.Graph()
g.add_edges_from(transitions)
plt.figure()
nx.draw_kamada_kawai(g, with_labels=True)
def plotNetwork(pools=None, refContract = REF_CONTRACT):
""" Plot the graph of the Ref Finance network.
Tokens are represented by nodes in the graph, and 2-token swap pools are
represented by edges between the nodes.
"""
if pools is None:
pools = getAllNonzeroPools(refContract=refContract)
pools = [p for p in pools if len(p['token_account_ids'])==2] #ignore non-pair-pools for now
transitions = [(getTokenMetadata(p['token_account_ids'][0])['symbol'],
getTokenMetadata(p['token_account_ids'][1])['symbol']) for p in pools]
g = nx.Graph()
g.add_edges_from(transitions)
nx.draw_circular(g,with_labels=True)
return g
#nv.CircosPlot(g)
def getShortestPath(token1, token2, pools=None, g = None, refContract = REF_CONTRACT):
"""calculates up to 30 of the shortest paths between input tokens 'token1' and
'token2'.
"""
if pools is None:
pools = getAllNonzeroPools(refContract=refContract)
pools = [p for p in pools if len(p['token_account_ids'])==2]
if g is None:
g = getGraph(getTransitions(pools))
try:
gen = nx.algorithms.simple_paths.shortest_simple_paths(g,token1,token2)
shortestPath = list(itertools.islice(gen, 30))
except:
print(f'NO PATH EXISTS BETWEEN TOKENS {token1} and {token2}')
shortestPath = []
return shortestPath
def getTransitions(pools=None, refContract = REF_CONTRACT):
"""Given a list of pools, this function iterates over the pools containing
two types of tokens and returns a list of lists of token pairs.
"""
if pools is None:
pools = getAllNonzeroPools(refContract=refContract)
pairPools = [p for p in pools if len(p['token_account_ids'])==2]
otherPools = [p for p in pools if p not in pairPools]
transitions = [p['token_account_ids'] for p in pairPools]
for p in otherPools:
# enumerate pairings for larger pools, such as tri-pools
#transitions.extend(list(itertools.combinations(p['token_account_ids'],2)))
pass
return transitions
def getGraph(transitions):
"""Given a list of transition pairs, this function builds an undirected graph
obect.
"""
g = nx.Graph()
g.add_edges_from(transitions)
return g
def getAllPaths(pools=None,g=None,refContract = REF_CONTRACT):
if pools is None:
pools = getAllNonzeroPools(refContract=refContract)
pools = [p for p in pools if len(p['token_account_ids'])==2]
if g is None:
g = getGraph(getTransitions(pools))
tokens = getAllTokens(pools, refContract)
paths = []
for i,token1 in enumerate(tokens):
print(i/len(tokens)*100)
for j,token2 in enumerate(tokens):
if j>i:
path = getShortestPath(token1, token2, pools, g)
if path:
paths.append(path)
return paths
def getAllTokens(pools=None,refContract = REF_CONTRACT):
"""Given a list of pools, this function returns a list of all the unique
token contract names.
"""
if pools is None:
pools = getAllNonzeroPools(refContract=refContract)
tokens = []
for p in pools:
for t in p['token_account_ids']:
if t not in tokens:
tokens.append(t)
return tokens
def getShortestPathLengthMatrix(pools=None,g=None,refContract=REF_CONTRACT):
"""Build a shortest-path-length matrix between each pair of pools.
"""
if pools is None:
pools = getAllNonzeroPools(refContract=refContract)
pools = [p for p in pools if len(p['token_account_ids'])==2]
if g is None:
g = getGraph(getTransitions(pools))
tokens = getAllTokens(pools, refContract)
matrix = np.zeros((len(tokens),len(tokens)))
for i,token1 in enumerate(tokens):
print(i/len(tokens)*100)
for j,token2 in enumerate(tokens):
if j!=i:
if nx.algorithms.has_path(g,token1,token2):
matrix[i,j] = nx.shortest_paths.shortest_path_length(g,token1, token2)
else:
matrix[i,j] = np.inf
return matrix
def getPathLengthStatsFromShortestPathMatrix(m):
"""Store a frequency dictionary of the number of times a particular minimum
number of hops occurs for a given pair of tokens in the input shortest path
matrix.
"""
data = list(m.flatten())
d = {0:data.count(0)} # get the direct number of 0-hops
d.update({i:data.count(i)//2 for i in range(1,5)}) #divide other occurrences by 2 due to double-counting (A-->B and B-->A are counted as two separate occurrences. This corrects that.)
return d
def getPoolsWithToken(pools=None, token=None, refContract=REF_CONTRACT):
"""Get a list of only the pools that contain a given input token.
"""
if pools is None:
pools = getAllNonzeroPools(refContract=refContract)
tpools = [p for p in pools if token in p['token_account_ids']]
return tpools
def getPoolsWithToken1ORToken2(pools=None, token1=None, token2=None, refContract = REF_CONTRACT):
"""Get a list of pools containing token1, token2, or both.
"""
fpools = sorted([p for p in pools if (token1 in p['token_account_ids']) or (token2 in p['token_account_ids'])],key=lambda x:x['id'])
# fpools = [p for p in pools if (token1 in p['token_account_ids']) or (token2 in p['token_account_ids'])]
return fpools
def getPoolsWithToken1ANDToken2(pools=None, token1=None, token2=None, refContract = REF_CONTRACT):
'''Get a list of pools containing both token1 and token2.
'''
fpools = sorted([p for p in pools if (token1 in p['token_account_ids']) and (token2 in p['token_account_ids'])],key=lambda x:x['id'])
return fpools
def getDirectPoolNearestNeighborsForToken(pools=None, referenceToken=None):
"""Get a list of the nearest-neighbor tokens relative to referenceToken. That
is, if there exists a pool in the list of pools which facilitates a direct swap
between referenceToken and otherToken, add otherToken to the list of direct
pool nearest neighbors.
"""
fpools = [p for p in pools if referenceToken in p['token_account_ids']]
rTokenLinks = []
for p in fpools:
rTokenLinks.extend(p['token_account_ids'])
rTokenLinks = set(rTokenLinks)
rTokenLinks.remove(referenceToken)
return rTokenLinks
def getCommonNearestNeighborsForTokens(pools=None, token1=None, token2=None):
"""Given the nearest neighbors of token1 and token2, determine the set intersection
of the two sets of nearest neighbors. This can be used to determine which tokens
can act as an intermediary during a double-hop between token1 and token2.
"""
fpools = getPoolsWithToken1ORToken2(pools, token1,token2)
t1Pools = [p for p in fpools if token1 in p['token_account_ids']]
t2Pools = [p for p in fpools if token2 in p['token_account_ids']]
t1Links = getDirectPoolNearestNeighborsForToken(t1Pools, token1)
t2Links = getDirectPoolNearestNeighborsForToken(t2Pools, token2)
commonLinks = t1Links.intersection(t2Links)
return commonLinks
def getSubgraphForDoubleHop(ps=None, token1=None,token2=None, drawMe=False):
""" Captures all pools associated with single or double hops and returns the
graph object.
"""
if ps is None:
ps = getAllNonzeroPools()
twohoplinks = getCommonNearestNeighborsForTokens(ps, token1, token2)
directTransitions = getTransitions(getPoolsWithToken1ANDToken2(ps,token1,token2))
twoHopTransitionsForToken1 = []
twoHopTransitionsForToken2 = []
for common in twohoplinks:
twoHopTransitionsForToken1.extend(getTransitions(getPoolsWithToken1ANDToken2(ps,token1,common)))
twoHopTransitionsForToken2.extend(getTransitions(getPoolsWithToken1ANDToken2(ps,token2,common)))
g = nx.Graph()
g.add_edges_from(directTransitions)
g.add_edges_from(twoHopTransitionsForToken1)
g.add_edges_from(twoHopTransitionsForToken2)
nodes = g.nodes()
#marker shapes: 'so^>v<dph8'
node_colors=[]
for node in nodes:
if node in [token1,token2]:
node_colors.append('#98D7C2')
# node_shapes.append('s')
else:
node_colors.append('skyblue')
# node_shapes.append('o')
if drawMe:
plt.figure(figsize=(10,7))
nx.draw_planar(g,node_color=node_colors,with_labels=True)#,bbox=dict(facecolor="skyblue", edgecolor='black', boxstyle='round,pad=0.2'))
plt.show()
return g
def getPathsFromPools(pools=None, token1=None, token2=None):
"""Using the reduced sub-graph limiting to only single and double hops, this
function gets the shortest paths and returns them in a list of tokens in order
for each particular path.
"""
g = getSubgraphForDoubleHop(pools, token1, token2)
paths = list(nx.simple_paths.shortest_simple_paths(g, token1,token2))
return paths
# def solveForPhiFromPaths(paths, totalInput,pools):
# """Used to solve for lagrange multiplier variable given the particular paths
# and the total input of initial token.
# """
# betaSum = getBetaSumFromPaths(paths, pools)
# alphaSum = getAlphaSumFromPaths(paths,pools)
# phi = (Decimal(totalInput) + betaSum) /alphaSum
# return phi
def getPhiFromRoutes(routes,nodeRoutes, totalInput):
"""Solves for lagrange multiplier variable phi given a list of routes.
A route is defined as a series of pools in which to make swaps.
A node route is defined as a list of tokens, in order, trading from the first
token through pools until the last token is reached.
"""
alphaSum = getAlphaSumFromRoutes(routes, nodeRoutes)
betaSum = getBetaSumFromRoutes(routes, nodeRoutes)
# print(totalInput, alphaSum, betaSum)
phi = (Decimal(totalInput) + betaSum) / alphaSum
return phi
# def getBetaSumFromPaths(paths, pools):
# poolChains = getPoolChainFromPaths(paths, pools)
# routes = getRoutesFromPoolChain(poolChains)
# nodeRoutes = getNodeRoutesFromPathsAndPoolChains(paths, poolChains)
# betaSum = sum([getBetaForRoute(route, nodeRoute)/getEpsilonForRoute(route, nodeRoute) for route,nodeRoute in zip(routes,nodeRoutes)])
# return betaSum
# def getAlphaSumFromPaths(paths, pools):
# poolChains = getPoolChainFromPaths(paths, pools)
# routes = getRoutesFromPoolChain(poolChains)
# nodeRoutes = getNodeRoutesFromPathsAndPoolChains(paths, poolChains)
# alphaSum = sum([np.sqrt(getAlphaForRoute(route, nodeRoute))/getEpsilonForRoute(route,nodeRoute) for route,nodeRoute in zip(routes,nodeRoutes)])
# return alphaSum
def getAlphaSumFromRoutes(routes, nodeRoutes):
alphaSum = sum([np.sqrt(getAlphaForRoute(route, nodeRoute))/getEpsilonForRoute(route,nodeRoute) for route,nodeRoute in zip(routes,nodeRoutes)])
return alphaSum
def getBetaSumFromRoutes(routes, nodeRoutes):
betaSum = sum([getBetaForRoute(route, nodeRoute)/getEpsilonForRoute(route, nodeRoute) for route,nodeRoute in zip(routes,nodeRoutes)])
return betaSum
def getBetaForRoute(route, path):
if len(route) == 1:
#single hop case
p = route[0]
beta = Decimal(p['reserves'][path[0]])
elif len(route) == 2:
#double hop case
p1,p2 = route
beta = Decimal(p1['reserves'][path[0]])*Decimal(p2['reserves'][path[1]]) #??? should there be a p2 in here?
return beta
def getEpsilonForRoute(route,path):
if len(route) == 1:
#single hop case
p = route[0]
p['gamma'] = Decimal(Decimal(10000)-Decimal(p['total_fee']))/Decimal(10000)
epsilon = Decimal(p['gamma'])
elif len(route) == 2:
#double hop case
p1,p2 = route
p1['gamma'] = (Decimal(10000)-Decimal(p1['total_fee']))/Decimal(10000)
p2['gamma'] = (Decimal(10000)-Decimal(p2['total_fee']))/Decimal(10000)
epsilon = Decimal(p2['reserves'][path[1]])*Decimal(p1['gamma']) + Decimal(p1['reserves'][path[1]])*Decimal(p1['gamma'])*Decimal(p2['gamma']) # ??? Is this right?
return epsilon
def getAlphaForRoute(route,path):
if len(route) == 1:
#single hop case
p = route[0]
inputToken,outputToken = path
p['gamma'] = (Decimal(10000)-Decimal(p['total_fee']))/Decimal(10000)
alpha = Decimal(p['reserves'][inputToken]) * Decimal(p['reserves'][outputToken]) * Decimal(p['gamma'])
elif len(route) == 2:
#double hop case
p1,p2 = route
inputToken, middleToken, outputToken = path
p1['gamma'] = (Decimal(10000)-Decimal(p1['total_fee']))/Decimal(10000)
p2['gamma'] = (Decimal(10000)-Decimal(p2['total_fee']))/Decimal(10000)
alpha1 = Decimal(p1['reserves'][inputToken])*Decimal(p1['reserves'][middleToken])*p1['gamma']
alpha2 = Decimal(p2['reserves'][middleToken])*Decimal(p2['reserves'][outputToken])*p2['gamma']
alpha = alpha1 * alpha2
return alpha
def getAllocationForRoute(phi, route, path):
alpha = getAlphaForRoute(route, path)
beta = getBetaForRoute(route, path)
epsilon = getEpsilonForRoute(route, path)
return (abs(phi)*np.sqrt(alpha)-beta)/epsilon
# def getAllocationVectorForRoutes(phi,routes,paths,poolChains):
# nodeRoutes = getNodeRoutesFromPathsAndPoolChains(paths, poolChains)
# allocationVec = []
# for route,path in zip(routes, nodeRoutes):
# allocationVec.append(getAllocationForRoute(phi, route, path))
# return allocationVec
def getAllocationVectorForRoutes(phi,routes,nodeRoutes):
allocationVec = []
for route,path in zip(routes, nodeRoutes):
allocationVec.append(getAllocationForRoute(phi, route, path))
return allocationVec
def getOptimalAllocation(pools, totalInput, inputToken,outputToken):
paths = getPathsFromPools(pools,inputToken,outputToken)
poolChains = getPoolChainFromPaths(paths, pools)
routes = getRoutesFromPoolChain(poolChains)
nodeRoutes = getNodeRoutesFromPathsAndPoolChains(paths, poolChains)
#solve for phi. filter out routes that have negative allocation. resolve for phi
phi = getPhiFromRoutes(routes, nodeRoutes, totalInput)
allocations = getAllocationVectorForRoutes(phi, routes, nodeRoutes)
# if np.sum(allocations) < 0:
# allocations = [Decimal(-1.0) * a for a in allocations]
if any([a<=0 for a in allocations]):
allocations = reduceRoutes(routes, nodeRoutes, allocations, totalInput)
# allocations = reduceRoutes(routes,nodeRoutes,allocations,totalInput)
sumAllocations = np.abs(np.sum(allocations)) #normalize by expected total delta X to fix floating point rounding errors
allocations = [x/sumAllocations*Decimal(totalInput) for x in allocations]
return allocations
def getOptimalAllocationForRoutes(routes, nodeRoutes, totalInput):
phi = getPhiFromRoutes(routes, nodeRoutes, totalInput)
allocations = getAllocationVectorForRoutes(phi, routes, nodeRoutes)
if np.all(np.array(allocations) < 0):
print('all allocations were <= zero.')
allocations = [Decimal(-1.0) * a for a in allocations]
if any([a<=0 for a in allocations]):
# print(np.sum(np.array(allocations)<=0), ' allocations of ', len(allocations),' are <= zero')
allocations = reduceRoutes(routes, nodeRoutes, allocations, totalInput)
sumAllocations = np.sum(allocations) #normalize by expected total delta X to fix floating point rounding errors
allocations = [x/sumAllocations*Decimal(totalInput) for x in allocations]
return allocations
def reduceRoutes(routes,nodeRoutes,allocationVec,totalInput):
goodIndices = []
for i,dx in enumerate(allocationVec):
if dx > 0:
goodIndices.append(i)
newRoutes = [routes[i] for i in range(len(routes)) if i in goodIndices]
newNodeRoutes = [nodeRoutes[i] for i in range(len(routes)) if i in goodIndices]
# phi = getPhiFromRoutes(newRoutes, newNodeRoutes, totalInput)
allocationVec = getOptimalAllocationForRoutes(newRoutes,newNodeRoutes,totalInput)
allocationDict = dict(zip(goodIndices,allocationVec))
allocationVecNew = []
for i in range(len(routes)):
if i in goodIndices:
allocationVecNew.append(allocationDict[i])
else:
allocationVecNew.append(Decimal(0))
return allocationVecNew
def getNodeRoutesFromPathsAndPoolChains(paths, poolChains):
multiplicity = [np.prod([len(y) for y in x]) for x in poolChains]
nodeRoutes = []
for path, m in zip(paths,multiplicity):
for i in range(m):
nodeRoutes.append(path)
return nodeRoutes
def getLiquidityOfPoolsFromList(pools):
liquidities = []
for pool in pools:
liquidity = np.prod([Decimal(x) for x in pool['amounts']])
liquidities.append(liquidity)
return liquidities
def getNormalizedLiquiditiesFromList(pools):
liq = getLiquidityOfPoolsFromList(pools)
return [v/np.max(liq) for v in liq]
def cullPoolsWithInsufficientLiquidity(pools,threshold=0.001):
normLiq = getNormalizedLiquiditiesFromList(pools)
remPools = [p for i,p in enumerate(pools) if normLiq[i]>threshold]
return remPools
def getCulledPoolChains(poolChains,threshold=0.001):
newChains = []
for path in poolChains:
newPath = []
for leg in path:
culledPath = cullPoolsWithInsufficientLiquidity(leg,threshold)
newPath.append(culledPath)
newChains.append(newPath)
return newChains
def getPoolChainFromPaths(paths,pools,cull=True,threshold=0.00):
'''if cull is true, this will prune out parallel pools at a given hop stage that
do not have a relative liquidity above a certain threshold.'''
poolChains = []
for path in paths:
chain = []
pairs = [path[i:i+2] for i in range(len(path)-1)]
for pair in pairs:
tokenPools = getPoolsWithToken1ANDToken2(pools,pair[0],pair[1])
chain.append(tokenPools)
poolChains.append(chain)
# if any([len(c)==1 for c in pc]): #direct swap case
# #cull to direct swap?
# poolChains =
if cull:
poolChains = getCulledPoolChains(poolChains,threshold)
return poolChains
def getRoutesFromPoolChain(poolChains):
routes = []
for poolChain in poolChains:
#get cartesian product of each pool chain to get the list of routes.
newRoutes = itertools.product(*poolChain)
routes.extend(newRoutes)
return routes
def getOutputSingleHop(pool, inputToken, outputToken, totalInput):
totalInput = Decimal(totalInput)
if 'gamma' not in pool:
pool['gamma'] = (Decimal(10000)-Decimal(pool['total_fee']))/Decimal(10000)
num = totalInput * pool['gamma'] * Decimal(pool['reserves'][outputToken])
denom = Decimal(pool['reserves'][inputToken]) + totalInput * pool['gamma']
return num/denom
def getOutputDoubleHop(pools, inputToken, middleToken, outputToken, totalInput):
totalInput = Decimal(totalInput)
for p in pools:
if 'gamma' not in p:
p['gamma'] = (Decimal(10000)-Decimal(p['total_fee']))/Decimal(10000)
p1,p2 = pools
c1 = Decimal(p1['reserves'][middleToken])
a1 = Decimal(p1['reserves'][inputToken])
c2 = Decimal(p2['reserves'][middleToken])
b2 = Decimal(p2['reserves'][outputToken])
gamma1 = p1['gamma']
gamma2 = p2['gamma']
num = totalInput * c1 * b2 * gamma1 * gamma2
#Decimal(p1['reserves'][middleToken]) *Decimal(p2['reserves'][outputToken]) * p1['gamma'] * p2['gamma']
denom = c2*a1 + totalInput * (c2*gamma1 + c1*gamma1*gamma2)
#denom = Decimal(p1['reserves'][inputToken]) * Decimal(p2['reserves'][middleToken])
#denom = denom + totalInput * (p1['gamma'] * c2 + p1['gamma']*p2['gamma']*c1)
return num / denom
def getOutputFromRoute(route, nodeRoute, allocation):
if Decimal(allocation) == Decimal(0):
return Decimal(0)
if len(route) == 1:
#single hop
inputToken, outputToken = nodeRoute
pool = route[0]
output = getOutputSingleHop(pool, inputToken, outputToken, allocation)
elif len(route) == 2:
#double hop
inputToken, middleToken, outputToken = nodeRoute
pools = route
output = getOutputDoubleHop(pools, inputToken, middleToken, outputToken, allocation)
return output
def testRandomInputs(routes, nodeRoutes, totalInput,N = 100):
vvals = []
outputVals = []
for i in range(N):
vraw = np.random.rand(len(routes),)
v = vraw / np.sum(vraw) * totalInput
vvals.append(v)
outputVals.append(np.sum([getOutputFromRoute(r,nr,a) for r,nr,a in zip(routes,nodeRoutes,v)]))
return vvals, outputVals
def getOptOutputVec(routes,nodeRoutes,totalInput):
allocations = getOptimalAllocationForRoutes(routes,nodeRoutes,totalInput)
result = [getOutputFromRoute(r,nr,a) for r,nr,a in zip(routes,nodeRoutes,allocations)]
return result, allocations
def getOptOutput(routes, nodeRoutes, totalInput, refined=False):
if refined:
func = getOptOutputVecRefined
else:
func = getOptOutputVec
return np.sum(func(routes,nodeRoutes,totalInput))
def getBestOptOutput(routes, nodeRoutes, totalInput):
res1 = np.sum(getOptOutputVecRefined(routes,nodeRoutes,totalInput)[0])
res2 = np.sum(getOptOutputVec(routes,nodeRoutes,totalInput)[0])
return max(res1,res2)
def getBestOptInput(routes, nodeRoutes, totalInput):
outputsRef,inputsRef = getOptOutputVecRefined(routes,nodeRoutes,totalInput)
outputs,inputs = getOptOutputVec(routes,nodeRoutes,totalInput)
if np.sum(outputsRef) > np.sum(outputs):
return inputsRef
else:
return inputs
def getOptOutputVecRefined(routes,nodeRoutes,totalInput):
initLengthRoutes = len(routes)
directRouteInds = [i for i,r in enumerate(routes) if len(r)==1]
if not directRouteInds:
# print('WARNING -- NO DIRECT ROUTES FOUND...')
# print('RETURNING FULL RESULT.')
allocations = getOptimalAllocationForRoutes(routes,nodeRoutes,totalInput)
result = [getOutputFromRoute(r,nr,a) for r,nr,a in zip(routes,nodeRoutes,allocations)]
else:
droutes = [routes[i] for i in directRouteInds]
dnodeRoutes = [nodeRoutes[i] for i in directRouteInds]
dallocations = getOptimalAllocationForRoutes(droutes,dnodeRoutes,totalInput)
dallocDict = dict(zip(directRouteInds, dallocations))
allocations = []
for i in range(initLengthRoutes):
if i in directRouteInds:
allocations.append(dallocDict[i])
else:
allocations.append(Decimal(0))
result = [getOutputFromRoute(r,nr,a) for r,nr,a in zip(routes,nodeRoutes,allocations)]
return result, allocations
def getBestOptimalAllocationsAndOutputs(pools=None,inputToken='wrap.near',outputToken='dbio.near',totalInput=0):
if pools is None:
pools = getAllNonzeroPools()
paths = getPathsFromPools(pools,inputToken,outputToken)
poolChains = getPoolChainFromPaths(paths, pools)
routes = getRoutesFromPoolChain(poolChains)
nodeRoutes = getNodeRoutesFromPathsAndPoolChains(paths, poolChains)
allocations = getBestOptInput(routes, nodeRoutes, totalInput)
outputs = getBestOptOutput(routes, nodeRoutes, totalInput)
return allocations, outputs,routes, nodeRoutes
def getMiddleTokenTotals(routes,nodeRoutes,allocations):
mtt = {}
for route,nodeRoute,allocation in zip(routes,nodeRoutes,allocations):
if len(route) > 1:
middleToken = nodeRoute[1]
if middleToken not in mtt:
mtt[middleToken] = getOutputSingleHop(route[0],nodeRoute[0],middleToken,allocation)
else:
#print('middle token received multiple allocations')
mtt[middleToken] += getOutputSingleHop(route[0],nodeRoute[0],middleToken,allocation)
return mtt
def getSecondHopAllocations(routes,nodeRoutes,allocations):
'''To be used in determining input amounts for second hop in transactions.
'''
mtt = getMiddleTokenTotals(routes,nodeRoutes,allocations)
secondHopAllocations = {}
mtRoutes = {mt:{i:allocations[i] for i in range(len(routes)) if nodeRoutes[i][1]==mt} for mt in mtt}
#TODO: complete the following as checks on mid-token allocations
#now get unique pools per mtRoute second hop.
#then combine the allocation from mtRoutes per pool
#then get the total fraction per pool of initial route allocation.
#then multiply this fraction by the middle token total and use this as input for second transaction
raise NotImplementedError
middleTokens = [nodeRoute[1] for nodeRoute in nodeRoutes if len(nodeRoute)==3]
for i,route,nodeRoute,allocation in zip(range(len(routes)),routes,nodeRoutes,allocations):
if len(route) == 1:
secondHopAllocations[i] = Decimal(0)
continue
# make a dict of dict of middle token name to route id(s) to initial allocation per route id
def getTransactionListFromRoutesAndAllocations(routes, nodeRoutes, allocations,hopMultiplier = 0.99):
transactions = []
#consider all routes of length 2 with non-zero allocation. (double-hops)
# among these, check for parallel swaps. That is, check for common node routes
# for first hop. Then check for common node routes on second hop.
# when common node routes occur for the first hop:
# 1. Calculate the total expected output of intermediate token.
# 2.
# when common node routes occur for the second hop:
# 1. get a ratio of the input allocations of the full routes associated with
# these common node routes. allocate the total intermediate token output
# toward these 2nd hop routes in the same ratio as their route input allocations.
#middleTokenTotals = getMiddleTokenTotals(routes,nodeRoutes,allocations)
#TODO: complete this function with middle token checks.
for route, nodeRoute, allocation in zip(routes,nodeRoutes,allocations):
if allocation == 0:
# print('skipping case for 0 allocation...')
continue
if len(route) == 1:
#single hop. Only one transaction.
pool = route[0]
poolId = pool['id']
inputToken, outputToken = nodeRoute
transaction = {'poolId':poolId,
'inputToken':inputToken,
'outputToken':outputToken,
'amountIn': allocation}
transactions.append(transaction)
elif len(route) == 2:
#double hop. Two transactions.
pool1,pool2 = route
pool1Id = pool1['id']
pool2Id = pool2['id']
inputToken, middleToken, outputToken = nodeRoute
transaction1 = {'poolId':pool1Id,
'inputToken':inputToken,
'outputToken':middleToken,
'amountIn': allocation}
expectedAmountOut = getOutputSingleHop(pool1, inputToken, middleToken, allocation)
#multiplier to account for slippage:
expectedAmountOutReduced = expectedAmountOut * Decimal(hopMultiplier)
transaction2 = {'poolId':pool2Id,
'inputToken':middleToken,
'outputToken':outputToken,
'amountIn': expectedAmountOutReduced}
transactions.append(transaction1)
transactions.append(transaction2)
return transactions
def getTokensFromPools(pools):
tokens = []
for pool in pools:
for token in pool['token_account_ids']:
if token not in tokens:
tokens.append(token)
return tokens
def getSmartRouteSwapTransactionsORIG(pools=None, inputToken = 'wrap.near', outputToken = 'dbio.near', totalInput=0):
'''contains original code for generating transaction objects. treats all
routes as independent and does not try to combine same-pool transactions into
a single transaction.
'''
if not totalInput:
return []
if pools is None:
pools = getAllNonzeroPools()
allocations, outputs,routes, nodeRoutes = getBestOptimalAllocationsAndOutputs(pools, inputToken,outputToken,totalInput)
transactions = getTransactionListFromRoutesAndAllocations(routes, nodeRoutes, allocations,hopMultiplier = 0.99)
return transactions
#NEED TO ADD IN STABLESWAP POOL!!
# MAIN FUNCTION:
def getSmartRouteSwapTransactions(pools=None, inputToken = 'wrap.near', outputToken = 'dbio.near', totalInput=0):
"""
Main smart route swap function for generating the transactions necessary
to transfer totalInput amount of inputToken in order to yield maximum amount
of outputToken.
Parameters
----------
pools : list<pool object>, optional
list of simple CPMM pool objects collected from Ref Finance getPools() function.
The default is None, which causes the function to call getAllNonzeroPools().
inputToken : str
name of token contract for input token. The default is 'wrap.near'.
outputToken : str
name of token contract for output token. The default is 'dbio.near'.
totalInput : int, float, or Decimal (will be converted to Decimal)
total amount of inputToken to trade. Specified in units of The default is 0.
Returns