-
Notifications
You must be signed in to change notification settings - Fork 7
/
tibet_lib.py
1599 lines (1376 loc) · 60.3 KB
/
tibet_lib.py
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
import asyncio
import os
import sys
import time
import requests
from pathlib import Path
from typing import List
from blspy import AugSchemeMPL
from blspy import PrivateKey
from cdv.cmds.rpc import get_client
from cdv.cmds.sim_utils import SIMULATOR_ROOT_PATH
from chia.consensus.default_constants import DEFAULT_CONSTANTS
from chia.rpc.full_node_rpc_client import FullNodeRpcClient
from chia.rpc.wallet_rpc_client import WalletRpcClient
from chia.simulator.simulator_full_node_rpc_client import \
SimulatorFullNodeRpcClient
from chia.types.blockchain_format.coin import Coin
from chia.types.blockchain_format.program import INFINITE_COST
from chia.types.blockchain_format.program import Program
from chia.types.blockchain_format.program import SerializedProgram
from chia.types.blockchain_format.sized_bytes import bytes32
from chia.types.coin_spend import CoinSpend
from chia.types.condition_opcodes import ConditionOpcode
from chia.types.spend_bundle import SpendBundle
from chia.util.bech32m import bech32_decode
from chia.util.bech32m import bech32_encode
from chia.util.bech32m import convertbits
from chia.util.bech32m import decode_puzzle_hash
from chia.util.bech32m import encode_puzzle_hash
from chia.util.condition_tools import conditions_dict_for_solution
from chia.util.config import load_config
from chia.util.hash import std_hash
from chia.util.ints import uint16
from chia.util.ints import uint32
from chia.util.ints import uint64
from chia.wallet.cat_wallet.cat_utils import SpendableCAT
from chia.wallet.cat_wallet.cat_utils import construct_cat_puzzle
from chia.wallet.cat_wallet.cat_utils import get_innerpuzzle_from_puzzle
from chia.wallet.cat_wallet.cat_utils import \
unsigned_spend_bundle_for_spendable_cats
from chia.wallet.derive_keys import master_sk_to_wallet_sk_unhardened
from chia.wallet.lineage_proof import LineageProof
from chia.wallet.puzzles.cat_loader import CAT_MOD
from chia.wallet.puzzles.cat_loader import CAT_MOD_HASH
from chia.wallet.puzzles.p2_conditions import puzzle_for_conditions
from chia.wallet.puzzles.p2_delegated_puzzle_or_hidden_puzzle import \
DEFAULT_HIDDEN_PUZZLE_HASH
from chia.wallet.puzzles.p2_delegated_puzzle_or_hidden_puzzle import \
calculate_synthetic_secret_key
from chia.wallet.puzzles.p2_delegated_puzzle_or_hidden_puzzle import \
puzzle_for_pk
from chia.wallet.puzzles.p2_delegated_puzzle_or_hidden_puzzle import \
puzzle_for_synthetic_public_key
from chia.wallet.puzzles.p2_delegated_puzzle_or_hidden_puzzle import \
solution_for_delegated_puzzle
from chia.wallet.puzzles.singleton_top_layer_v1_1 import SINGLETON_LAUNCHER
from chia.wallet.puzzles.singleton_top_layer_v1_1 import \
SINGLETON_LAUNCHER_HASH
from chia.wallet.puzzles.singleton_top_layer_v1_1 import SINGLETON_MOD
from chia.wallet.puzzles.singleton_top_layer_v1_1 import SINGLETON_MOD_HASH
from chia.wallet.puzzles.singleton_top_layer_v1_1 import generate_launcher_coin
from chia.wallet.puzzles.singleton_top_layer_v1_1 import \
launch_conditions_and_coinsol
from chia.wallet.puzzles.singleton_top_layer_v1_1 import \
lineage_proof_for_coinsol
from chia.wallet.puzzles.singleton_top_layer_v1_1 import puzzle_for_singleton
from chia.wallet.puzzles.singleton_top_layer_v1_1 import solution_for_singleton
from chia.wallet.puzzles.tails import GenesisById
from chia.wallet.sign_coin_spends import sign_coin_spends
from chia.wallet.trading.offer import OFFER_MOD
from chia.wallet.trading.offer import OFFER_MOD_HASH
from chia.wallet.trading.offer import Offer
from chia.wallet.util.puzzle_compression import compress_object_with_puzzles
from chia.wallet.util.puzzle_compression import decompress_object_with_puzzles
from chia.wallet.util.puzzle_compression import lowest_best_version
from chia_rs import run_chia_program
from clvm.casts import int_to_bytes
from clvm import SExp
from leaflet_client import LeafletFullNodeRpcClient
def load_clvm_hex(
filename
) -> Program:
clvm_hex = open(filename, "r").read().strip()
assert len(clvm_hex) != 0
clvm_blob = bytes.fromhex(clvm_hex)
return SerializedProgram.from_bytes(clvm_blob).to_program()
ROUTER_MOD: Program = load_clvm_hex("clvm/router.clvm.hex")
PAIR_MOD: Program = load_clvm_hex("clvm/pair.clvm.hex")
LIQUIDITY_TAIL_MOD: Program = load_clvm_hex("clvm/liquidity_tail.clvm.hex")
P2_SINGLETON_FLASHLOAN_MOD: Program = load_clvm_hex("clvm/p2_singleton_flashloan.clvm.hex")
ROUTER_MOD_HASH = ROUTER_MOD.get_tree_hash()
PAIR_MOD_HASH = PAIR_MOD.get_tree_hash()
LIQUIDITY_TAIL_MOD_HASH = LIQUIDITY_TAIL_MOD.get_tree_hash()
P2_SINGLETON_FLASHLOAN_MOD_HASH = P2_SINGLETON_FLASHLOAN_MOD.get_tree_hash()
# can be overriden for all func calls
DEFAULT_RETURN_ADDRESS = "xch10d09t9eqpr2y34thcayk54sjz34qhyv3tmhrejjp6xxvj598sfds5z0xch"
def get_router_puzzle():
return ROUTER_MOD.curry(
PAIR_MOD_HASH,
SINGLETON_MOD_HASH,
P2_SINGLETON_FLASHLOAN_MOD_HASH,
CAT_MOD_HASH,
LIQUIDITY_TAIL_MOD_HASH,
OFFER_MOD_HASH,
993,
SINGLETON_LAUNCHER_HASH,
ROUTER_MOD_HASH
)
def get_pair_inner_puzzle(singleton_launcher_id, tail_hash, liquidity, xch_reserve, token_reserve):
return PAIR_MOD.curry(
PAIR_MOD_HASH,
(SINGLETON_MOD_HASH, (singleton_launcher_id, SINGLETON_LAUNCHER_HASH)),
P2_SINGLETON_FLASHLOAN_MOD_HASH,
CAT_MOD_HASH,
LIQUIDITY_TAIL_MOD_HASH,
OFFER_MOD_HASH,
tail_hash,
993,
liquidity,
xch_reserve,
token_reserve
)
def get_pair_puzzle(singleton_launcher_id, tail_hash, liquidity, xch_reserve, token_reserve):
return puzzle_for_singleton(
singleton_launcher_id,
get_pair_inner_puzzle(singleton_launcher_id, tail_hash, liquidity, xch_reserve, token_reserve)
)
def pair_liquidity_tail_puzzle(pair_launcher_id):
return LIQUIDITY_TAIL_MOD.curry(
(SINGLETON_MOD_HASH, (pair_launcher_id, SINGLETON_LAUNCHER_HASH))
)
# https://github.com/Chia-Network/chia-blockchain/blob/main/chia/wallet/puzzles/singleton_top_layer_v1_1.py
def pay_to_singleton_flashloan_puzzle(launcher_id: bytes32) -> Program:
return P2_SINGLETON_FLASHLOAN_MOD.curry(SINGLETON_MOD_HASH, launcher_id, SINGLETON_LAUNCHER_HASH)
# https://github.com/Chia-Network/chia-blockchain/blob/main/chia/wallet/puzzles/singleton_top_layer_v1_1.py
def solution_for_p2_singleton_flashloan(
p2_singleton_coin: Coin,
singleton_inner_puzhash: bytes32,
extra_conditions = []
) -> Program:
solution: Program = Program.to([singleton_inner_puzhash, p2_singleton_coin.name(), extra_conditions])
return solution
async def get_full_node_client(
chia_root,
leaflet_url
) -> FullNodeRpcClient:
node_client = None
if leaflet_url is not None:
# use leaflet by default
node_client = LeafletFullNodeRpcClient(leaflet_url)
else:
root_path = Path(chia_root)
config = load_config(root_path, "config.yaml")
self_hostname = config["self_hostname"]
rpc_port = config["full_node"]["rpc_port"]
node_client: FullNodeRpcClient = await FullNodeRpcClient.create(
self_hostname, uint16(rpc_port), root_path, config
)
await node_client.healthz()
return node_client
async def get_sim_full_node_client(
chia_root: str
) -> SimulatorFullNodeRpcClient:
root_path = Path(chia_root)
config = load_config(root_path, "config.yaml")
self_hostname = config["self_hostname"]
rpc_port = config["full_node"]["rpc_port"]
node_client: SimulatorFullNodeRpcClient = await SimulatorFullNodeRpcClient.create(
self_hostname, uint16(rpc_port), root_path, config
)
await node_client.healthz()
return node_client
async def get_wallet_client(
chia_root: str
) -> FullNodeRpcClient:
root_path = Path(chia_root)
config = load_config(root_path, "config.yaml")
self_hostname = config["self_hostname"]
rpc_port = config["wallet"]["rpc_port"]
wallet_client: WalletRpcClient = await WalletRpcClient.create(
self_hostname, uint16(rpc_port), root_path, config
)
await wallet_client.healthz()
return wallet_client
async def launch_router_from_coin(parent_coin, parent_coin_puzzle, fee=0):
comment: List[Tuple[str, str]] = [("tibet", "v1")]
conds, launcher_coin_spend = launch_conditions_and_coinsol(parent_coin, get_router_puzzle(), comment, 1)
if parent_coin.amount > fee + 1:
conds.append(
[
ConditionOpcode.CREATE_COIN,
parent_coin.puzzle_hash,
parent_coin.amount - 1 - fee,
]
)
if fee > 0:
conds.append([
ConditionOpcode.RESERVE_FEE,
fee
])
p2_coin_spend = CoinSpend(
parent_coin,
parent_coin_puzzle,
solution_for_delegated_puzzle(Program.to((1, conds)), [])
)
sb = SpendBundle(
[launcher_coin_spend, p2_coin_spend],
AugSchemeMPL.aggregate([])
)
launcher_id = launcher_coin_spend.coin.name().hex()
return launcher_id, sb
async def create_test_cat(token_amount, coin, coin_puzzle):
coin_id = coin.name()
tail = GenesisById.construct([coin_id])
tail_hash = tail.get_tree_hash()
cat_inner_puzzle = coin_puzzle
cat_puzzle = construct_cat_puzzle(CAT_MOD, tail_hash, cat_inner_puzzle)
cat_puzzle_hash = cat_puzzle.get_tree_hash()
cat_creation_tx = CoinSpend(
coin,
cat_inner_puzzle, # same as this coin's puzzle
solution_for_delegated_puzzle(Program.to((1, [
[ConditionOpcode.CREATE_COIN, cat_puzzle_hash, token_amount * 1000],
[ConditionOpcode.CREATE_COIN, coin.puzzle_hash, coin.amount - token_amount * 1000],
])), [])
)
cat_coin = Coin(
coin.name(), # parent
cat_puzzle_hash,
token_amount * 1000
)
cat_inner_solution = solution_for_delegated_puzzle(
Program.to((1, [
[ConditionOpcode.CREATE_COIN, 0, -113, tail, []],
[ConditionOpcode.CREATE_COIN, cat_inner_puzzle.get_tree_hash(), cat_coin.amount]
])), []
)
cat_eve_spend_bundle = unsigned_spend_bundle_for_spendable_cats(
CAT_MOD,
[
SpendableCAT(
cat_coin,
tail_hash,
cat_inner_puzzle,
cat_inner_solution,
limitations_program_reveal=tail,
)
],
)
cat_eve_spend = cat_eve_spend_bundle.coin_spends[0]
sb = SpendBundle(
[cat_creation_tx, cat_eve_spend],
AugSchemeMPL.aggregate([])
)
return tail_hash.hex(), sb
async def create_pair_from_coin(
coin,
coin_puzzle,
tail_hash,
router_launcher_id,
current_router_coin,
current_router_coin_creation_spend,
fee=0
):
lineage_proof = lineage_proof_for_coinsol(current_router_coin_creation_spend)
router_inner_puzzle = get_router_puzzle()
router_inner_solution = Program.to([
current_router_coin.name(),
tail_hash
])
router_singleton_puzzle = puzzle_for_singleton(router_launcher_id, router_inner_puzzle)
router_singleton_solution = solution_for_singleton(lineage_proof, current_router_coin.amount, router_inner_solution)
router_singleton_spend = CoinSpend(current_router_coin, router_singleton_puzzle, router_singleton_solution)
pair_launcher_coin = Coin(current_router_coin.name(), SINGLETON_LAUNCHER_HASH, 2)
pair_puzzle = get_pair_puzzle(
pair_launcher_coin.name(),
tail_hash,
0, 0, 0
)
comment: List[Tuple[str, str]] = []
# launch_conditions_and_coinsol would not work here since we spend a coin with amount 2
# and the solution says the amount is 1 *-*
pair_launcher_solution = Program.to(
[
pair_puzzle.get_tree_hash(),
1,
comment,
]
)
assert_launcher_announcement = [
ConditionOpcode.ASSERT_COIN_ANNOUNCEMENT,
std_hash(pair_launcher_coin.name() + pair_launcher_solution.get_tree_hash()),
]
pair_launcher_spend = CoinSpend(
pair_launcher_coin,
SINGLETON_LAUNCHER,
pair_launcher_solution,
)
# first condition is CREATE_COIN, which we took care of
# important to note: router also takes care of assert_launcher_announcement, but we need it to link the fund spend to the bundle
conds = []
conds.append([ConditionOpcode.CREATE_COIN, coin.puzzle_hash, coin.amount - fee - 2])
if fee > 0:
conds.append([ConditionOpcode.RESERVE_FEE, fee])
conds.append(assert_launcher_announcement)
fund_spend = CoinSpend(
coin,
coin_puzzle,
solution_for_delegated_puzzle(Program.to((1, conds)), [])
)
pair_launcher_id = Coin(current_router_coin.name(), SINGLETON_LAUNCHER_HASH, 2).name().hex()
sb = SpendBundle(
[router_singleton_spend, pair_launcher_spend, fund_spend],
AugSchemeMPL.aggregate([])
)
return pair_launcher_id, sb
async def sync_router(full_node_client, last_router_id):
new_pairs = []
coin_record = await full_node_client.get_coin_record_by_name(last_router_id)
if not coin_record.spent:
# hack
current_router_coin, creation_spend, _ = await sync_router(full_node_client, coin_record.coin.parent_coin_info)
return current_router_coin, creation_spend, []
router_puzzle_hash = get_router_puzzle().get_tree_hash()
while coin_record.spent:
creation_spend = await full_node_client.get_puzzle_and_solution(last_router_id, coin_record.spent_block_index)
_, conditions_dict, __ = conditions_dict_for_solution(
creation_spend.puzzle_reveal,
creation_spend.solution,
INFINITE_COST
)
if coin_record.coin.puzzle_hash != SINGLETON_LAUNCHER_HASH:
solution_program = creation_spend.solution.to_program()
tail_hash = [_ for _ in solution_program.as_iter()][-1].as_python()[-1]
for cwa in conditions_dict[ConditionOpcode.CREATE_COIN]:
new_puzzle_hash = cwa.vars[0]
new_amount = cwa.vars[1]
if new_amount == b"\x01": # CREATE_COIN with amount=1 -> router recreated
new_router_coin = Coin(last_router_id, new_puzzle_hash, 1)
last_router_id = new_router_coin.name()
elif new_amount == b"\x02": # CREATE_COIN with amount=2 -> pair launcher deployed
assert new_puzzle_hash == SINGLETON_LAUNCHER_HASH
pair_launcher_coin = Coin(creation_spend.coin.name(), new_puzzle_hash, 2)
pair_launcher_id = pair_launcher_coin.name()
new_pairs.append((tail_hash.hex(), pair_launcher_id.hex()))
else:
print("Someone did something extremely weird with the router - time to call the cops.")
sys.exit(1)
coin_record = await full_node_client.get_coin_record_by_name(last_router_id)
return coin_record.coin, creation_spend, new_pairs
async def get_spend_bundle_in_mempool(full_node_client, coin):
try:
parent_id_hex = "0x" + coin.parent_coin_info.hex()
r = requests.post("http://localhost:1337/get_mempool_item_by_parent_coin_info", json={
"request_url": full_node_client.leaflet_url + "get_all_mempool_items",
"parent_coin_info": parent_id_hex
})
j = r.json()
if j["item"] is None:
return None
return SpendBundle.from_json_dict(j["item"])
except:
return await get_spend_bundle_in_mempool_full_node(full_node_client, coin.name())
async def get_spend_bundle_in_mempool_full_node(full_node_client, coin_id):
items = await full_node_client.fetch("get_all_mempool_items", {})
for sb_id, d in items["mempool_items"].items():
sb = SpendBundle.from_json_dict(d["spend_bundle"])
for cs in sb.coin_spends:
if cs.coin.name() == coin_id:
return sb
return None
def get_coin_spend_from_sb(sb, coin_name):
if sb is None:
return None
for cs in sb.coin_spends:
if cs.coin.name() == coin_name:
return cs
return None
async def sync_pair(full_node_client, last_synced_coin_id, tail_hash):
state = {
"liquidity": 0,
"xch_reserve": 0,
"token_reserve": 0
}
coin_record = await full_node_client.get_coin_record_by_name(last_synced_coin_id)
last_synced_coin = coin_record.coin
creation_spend = None
if coin_record.coin.puzzle_hash == SINGLETON_LAUNCHER_HASH:
creation_spend = await full_node_client.get_puzzle_and_solution(last_synced_coin_id, coin_record.spent_block_index)
_, conditions_dict, __ = conditions_dict_for_solution(
creation_spend.puzzle_reveal,
creation_spend.solution,
INFINITE_COST
)
last_synced_coin = Coin(coin_record.coin.name(), conditions_dict[ConditionOpcode.CREATE_COIN][0].vars[0], 1)
if not coin_record.spent:
# hack
current_pair_coin, creation_spend, state, sb_to_aggregate, last_synced_pair_id_on_blockchain = await sync_pair(full_node_client, coin_record.coin.parent_coin_info, tail_hash)
return current_pair_coin, creation_spend, state, sb_to_aggregate, last_synced_pair_id_on_blockchain
creation_spend = None
while coin_record.spent:
creation_spend = await full_node_client.get_puzzle_and_solution(last_synced_coin_id, coin_record.spent_block_index)
_, conditions_dict, __ = conditions_dict_for_solution(
creation_spend.puzzle_reveal,
creation_spend.solution,
INFINITE_COST
)
for cwa in conditions_dict.get(ConditionOpcode.CREATE_COIN, []):
new_puzzle_hash = cwa.vars[0]
new_amount = cwa.vars[1]
if new_amount == b"\x01": # CREATE_COIN with amount=1 -> pair recreation
last_synced_coin = Coin(last_synced_coin_id, new_puzzle_hash, 1)
last_synced_coin_id = last_synced_coin.name()
coin_record = await full_node_client.get_coin_record_by_name(last_synced_coin_id)
last_synced_pair_id_on_blockchain = last_synced_coin_id
# mempool - watch this aggregation!
last_coin_on_chain = coin_record.coin
last_coin_on_chain_id = last_coin_on_chain.name()
sb = await get_spend_bundle_in_mempool(full_node_client, last_coin_on_chain)
sb_to_aggregate = sb
coin_spend = get_coin_spend_from_sb(sb, last_coin_on_chain_id)
while coin_spend != None:
creation_spend = coin_spend
_, conditions_dict, __ = conditions_dict_for_solution(
creation_spend.puzzle_reveal,
creation_spend.solution,
INFINITE_COST
)
for cwa in conditions_dict.get(ConditionOpcode.CREATE_COIN, []):
new_puzzle_hash = cwa.vars[0]
new_amount = cwa.vars[1]
if new_amount == b"\x01": # CREATE_COIN with amount=1 -> pair recreation
last_synced_coin = Coin(last_synced_coin_id, new_puzzle_hash, 1)
last_synced_coin_id = last_synced_coin.name()
coin_spend = get_coin_spend_from_sb(sb, last_synced_coin_id)
if creation_spend.coin.puzzle_hash == SINGLETON_LAUNCHER_HASH:
return last_synced_coin, creation_spend, state, None, last_synced_coin.name()
creation_spend_inner_puzzle_args = creation_spend.puzzle_reveal.uncurry()[1].at("rf").uncurry()[1]
liquidity = creation_spend_inner_puzzle_args.at("r" * 8 + "f").as_int()
xch_reserve = creation_spend_inner_puzzle_args.at("r" * 9 + "f").as_int()
token_reserve = creation_spend_inner_puzzle_args.at("r" * 10 + "f").as_int()
creation_spend_inner_solution = creation_spend.solution.to_program().at("rrf")
action = creation_spend_inner_solution.at("rf").as_int()
params = creation_spend_inner_solution.at("rrf")
if action == 0: # deposit liquidity
token_amount = params.at("f").as_int()
if liquidity == 0:
xch_amount = params.at("rrrf").as_int()
liquidity = token_amount
xch_reserve = xch_amount
token_reserve = token_amount
else:
liquidity += token_amount * liquidity // token_reserve
xch_reserve += token_amount * xch_reserve // token_reserve
token_reserve += token_amount
elif action == 1: # remove liquidity
liquidity_tokens_amount = params.at("f").as_int()
xch_reserve -= liquidity_tokens_amount * xch_reserve // liquidity
token_reserve -= liquidity_tokens_amount * token_reserve // liquidity
liquidity -= liquidity_tokens_amount
elif action == 2: # xch to token
xch_amount = params.at("f").as_int()
token_reserve -= (token_reserve * xch_amount * 993) // (1000 * xch_reserve + 993 * xch_amount)
xch_reserve += xch_amount
elif action == 3: # token to xch
token_amount = params.at("f").as_int()
xch_reserve -= (xch_reserve * token_amount * 993) // (1000 * token_reserve + 993 * token_amount)
token_reserve += token_amount
state = {
"liquidity": liquidity,
"xch_reserve": xch_reserve,
"token_reserve": token_reserve
}
return last_synced_coin, creation_spend, state, sb_to_aggregate, last_synced_pair_id_on_blockchain
async def get_pair_reserve_info(
full_node_client,
pair_launcher_id,
pair_coin,
token_tail_hash,
creation_spend,
cached_sb
):
puzzle_announcements_asserts = []
_, conditions_dict, __ = conditions_dict_for_solution(
creation_spend.puzzle_reveal,
creation_spend.solution,
INFINITE_COST
)
for cwa in conditions_dict.get(ConditionOpcode.ASSERT_PUZZLE_ANNOUNCEMENT, []):
puzzle_announcements_asserts.append(cwa.vars[0])
if len(puzzle_announcements_asserts) == 0:
return None, None, None # new pair
p2_singleton_puzzle = pay_to_singleton_flashloan_puzzle(pair_launcher_id)
p2_singleton_puzzle_hash = p2_singleton_puzzle.get_tree_hash()
p2_singleton_cat_puzzle = construct_cat_puzzle(CAT_MOD, token_tail_hash, p2_singleton_puzzle)
p2_singleton_cat_puzzle_hash = p2_singleton_cat_puzzle.get_tree_hash()
spends = []
if cached_sb is None:
coin_record = await full_node_client.get_coin_record_by_name(pair_coin.name())
block_record = await full_node_client.get_block_record_by_height(coin_record.confirmed_block_index)
spends = await full_node_client.get_block_spends(block_record.header_hash)
else:
spends = cached_sb.coin_spends
xch_reserve_coin = None
token_reserve_coin = None
token_reserve_lineage_proof = []
for spend in spends:
_, conditions_dict, __ = conditions_dict_for_solution(
spend.puzzle_reveal,
spend.solution,
INFINITE_COST
)
for cwa in conditions_dict.get(ConditionOpcode.CREATE_PUZZLE_ANNOUNCEMENT, []):
ann_hash = std_hash(spend.coin.puzzle_hash + cwa.vars[0])
if ann_hash in puzzle_announcements_asserts:
amount = 0
for cwa2 in conditions_dict[ConditionOpcode.CREATE_COIN]:
if cwa2.vars[0] in [p2_singleton_puzzle_hash, p2_singleton_cat_puzzle_hash]:
amount = SExp.to(cwa2.vars[1]).as_int()
if spend.coin.puzzle_hash == OFFER_MOD_HASH:
xch_reserve_coin = Coin(spend.coin.name(), p2_singleton_puzzle_hash, amount)
else: # OFFER_MOD_HASH but wrapped in CAT puzzle
token_reserve_coin = Coin(spend.coin.name(), p2_singleton_cat_puzzle_hash, amount)
token_reserve_lineage_proof = [
spend.coin.parent_coin_info,
OFFER_MOD_HASH,
spend.coin.amount
]
break
return xch_reserve_coin, token_reserve_coin, token_reserve_lineage_proof
def get_announcements_asserts_for_notarized_payments(not_payments, puzzle_hash=OFFER_MOD_HASH):
_, conditions_dict, __ = conditions_dict_for_solution(
OFFER_MOD,
not_payments,
INFINITE_COST
)
announcement_asserts = []
for cwa in conditions_dict.get(ConditionOpcode.CREATE_PUZZLE_ANNOUNCEMENT, []): #cwa = condition with args
announcement_asserts.append([
ConditionOpcode.ASSERT_PUZZLE_ANNOUNCEMENT,
std_hash(puzzle_hash + cwa.vars[0])
])
return announcement_asserts
async def respond_to_deposit_liquidity_offer(
pair_launcher_id,
current_pair_coin,
creation_spend,
token_tail_hash,
pair_liquidity,
pair_xch_reserve,
pair_token_reserve,
offer_str,
last_xch_reserve_coin,
last_token_reserve_coin,
last_token_reserve_lineage_proof, # coin_parent_coin_info, inner_puzzle_hash, amount
return_address = DEFAULT_RETURN_ADDRESS
):
# 1. Detect ephemeral coins (those created by the offer that we're allowed to use)
offer = Offer.from_bech32(offer_str)
offer_spend_bundle = offer.to_spend_bundle()
offer_coin_spends = offer_spend_bundle.coin_spends
eph_xch_coin = None
eph_token_coin = None
eph_token_coin_creation_spend = None # needed when spending eph_token_coin since it's a CAT
announcement_asserts = [] # assert everything when the liquidity cat is minted
ephemeral_token_coin_puzzle = construct_cat_puzzle(CAT_MOD, token_tail_hash, OFFER_MOD)
ephemeral_token_coin_puzzle_hash = ephemeral_token_coin_puzzle.get_tree_hash()
cs_from_initial_offer = [] # all valid coin spends (i.e., not 'hints' for offered assets or coins)
for coin_spend in offer_coin_spends:
if coin_spend.coin.parent_coin_info == b"\x00" * 32: # 'hint' for offer requested coin (liquidity CAT)
continue
cs_from_initial_offer.append(coin_spend)
_, conditions_dict, __ = conditions_dict_for_solution(
coin_spend.puzzle_reveal,
coin_spend.solution,
INFINITE_COST
)
# is this spend creating an ephemeral coin?
for cwa in conditions_dict.get(ConditionOpcode.CREATE_COIN, []):
puzzle_hash = cwa.vars[0]
amount = SExp.to(cwa.vars[1]).as_int()
if puzzle_hash == OFFER_MOD_HASH:
eph_xch_coin = Coin(coin_spend.coin.name(), puzzle_hash, amount)
if puzzle_hash == ephemeral_token_coin_puzzle_hash:
eph_token_coin = Coin(coin_spend.coin.name(), puzzle_hash, amount)
eph_token_coin_creation_spend = coin_spend
# is there any announcement that we'll need to assert?
for cwa in conditions_dict.get(ConditionOpcode.CREATE_COIN_ANNOUNCEMENT, []): #cwa = condition with args
announcement_asserts.append([
ConditionOpcode.ASSERT_COIN_ANNOUNCEMENT,
std_hash(coin_spend.coin.name() + cwa.vars[0])
])
# 2. Math stuff
new_liquidity_token_amount = 0
for k, v in offer.get_requested_amounts().items():
new_liquidity_token_amount = v
deposited_token_amount = new_liquidity_token_amount
if pair_token_reserve != 0:
deposited_token_amount = eph_token_coin.amount * pair_liquidity // pair_token_reserve
deposited_xch_amount = eph_xch_coin.amount - new_liquidity_token_amount
if pair_xch_reserve != 0:
deposited_xch_amount = deposited_token_amount * pair_xch_reserve // pair_token_reserve
if deposited_xch_amount > eph_xch_coin.amount or deposited_token_amount > eph_token_coin.amount:
print(f"Uh... to few XCH/tokens :(")
return None
# 3. spend the token ephemeral coin to create the token reserve coin
p2_singleton_puzzle = pay_to_singleton_flashloan_puzzle(pair_launcher_id)
p2_singleton_puzzle_cat = construct_cat_puzzle(CAT_MOD, token_tail_hash, p2_singleton_puzzle)
eph_token_coin_notarized_payments = []
eph_token_coin_notarized_payments.append(
Program.to([
current_pair_coin.name(),
[p2_singleton_puzzle.get_tree_hash(), deposited_token_amount + pair_token_reserve]
])
)
# send extra tokens to return address
if eph_token_coin.amount > deposited_token_amount:
not_payment = Program.to([
current_pair_coin.name(),
[decode_puzzle_hash(return_address), eph_token_coin.amount - deposited_token_amount]
])
eph_token_coin_notarized_payments.append(not_payment)
for ann_assert in get_announcements_asserts_for_notarized_payments([not_payment], eph_token_coin.puzzle_hash):
announcement_asserts.append(ann_assert)
eph_token_coin_inner_solution = Program.to(eph_token_coin_notarized_payments)
spendable_cats_for_token_reserve = []
spendable_cats_for_token_reserve.append(
SpendableCAT(
eph_token_coin,
token_tail_hash,
OFFER_MOD,
eph_token_coin_inner_solution,
lineage_proof=LineageProof(
eph_token_coin_creation_spend.coin.parent_coin_info,
get_innerpuzzle_from_puzzle(eph_token_coin_creation_spend.puzzle_reveal).get_tree_hash(),
eph_token_coin_creation_spend.coin.amount
)
)
)
pair_singleton_inner_puzzle = get_pair_inner_puzzle(
pair_launcher_id,
token_tail_hash,
pair_liquidity,
pair_xch_reserve,
pair_token_reserve
)
if last_token_reserve_coin is not None:
spendable_cats_for_token_reserve.append(
SpendableCAT(
last_token_reserve_coin,
token_tail_hash,
p2_singleton_puzzle,
solution_for_p2_singleton_flashloan(
last_token_reserve_coin, pair_singleton_inner_puzzle.get_tree_hash()
),
lineage_proof=LineageProof(
last_token_reserve_lineage_proof[0],
last_token_reserve_lineage_proof[1],
last_token_reserve_lineage_proof[2]
)
)
)
token_reserve_creation_spend_bundle = unsigned_spend_bundle_for_spendable_cats(
CAT_MOD, spendable_cats_for_token_reserve
)
token_reserve_creation_spends = token_reserve_creation_spend_bundle.coin_spends
# 4. spend the xch ephemeral coin
liquidity_cat_tail = pair_liquidity_tail_puzzle(pair_launcher_id)
liquidity_cat_tail_hash = liquidity_cat_tail.get_tree_hash()
liquidity_cat_mint_coin_tail_solution = Program.to([pair_singleton_inner_puzzle.get_tree_hash()])
# output everything from solution
# this is usually not safe to use, but in this case we don't really care since we are accepting an offer
# that is asking for liquidity tokens - it's kind of circular; if we don't get out tokens,
# the transaction doesn't go through because a puzzle announcemet from the parents of eph coins would fail
liquidity_cat_mint_coin_inner_puzzle = Program.from_bytes(b"\x01")
liquidity_cat_mint_coin_inner_puzzle_hash = liquidity_cat_mint_coin_inner_puzzle.get_tree_hash()
liquidity_cat_mint_coin_puzzle = construct_cat_puzzle(
CAT_MOD, liquidity_cat_tail_hash, liquidity_cat_mint_coin_inner_puzzle
)
liquidity_cat_mint_coin_puzzle_hash = liquidity_cat_mint_coin_puzzle.get_tree_hash()
eph_xch_coin_settlement_things = [
Program.to([
current_pair_coin.name(),
[p2_singleton_puzzle.get_tree_hash(), pair_xch_reserve + deposited_xch_amount]
]),
Program.to([
current_pair_coin.name(),
[liquidity_cat_mint_coin_puzzle_hash, new_liquidity_token_amount]
])
]
# send extra XCH to return address
if eph_xch_coin.amount > deposited_xch_amount + new_liquidity_token_amount:
not_payment = Program.to([
current_pair_coin.name(),
[decode_puzzle_hash(return_address), eph_xch_coin.amount - deposited_xch_amount - new_liquidity_token_amount]
])
eph_xch_coin_settlement_things.append(not_payment)
for ann_assert in get_announcements_asserts_for_notarized_payments([not_payment]):
announcement_asserts.append(ann_assert)
eph_xch_coin_solution = Program.to(eph_xch_coin_settlement_things)
eph_xch_coin_spend = CoinSpend(eph_xch_coin, OFFER_MOD, eph_xch_coin_solution)
# 5. Re-create the pair singleton (spend it)
pair_singleton_puzzle = get_pair_puzzle(
pair_launcher_id,
token_tail_hash,
pair_liquidity,
pair_xch_reserve,
pair_token_reserve
)
pair_singleton_inner_solution = Program.to([
Program.to([
current_pair_coin.name(),
b"\x00" * 32 if last_xch_reserve_coin is None else last_xch_reserve_coin.name(),
b"\x00" * 32 if last_token_reserve_coin is None else last_token_reserve_coin.name(),
]),
0,
Program.to([
deposited_token_amount,
liquidity_cat_mint_coin_inner_puzzle_hash,
eph_xch_coin.name(), # parent of liquidity cat mint coin
deposited_xch_amount
])
])
lineage_proof = lineage_proof_for_coinsol(creation_spend)
pair_singleton_solution = solution_for_singleton(
lineage_proof, current_pair_coin.amount, pair_singleton_inner_solution
)
pair_singleton_spend = CoinSpend(current_pair_coin, pair_singleton_puzzle, pair_singleton_solution)
# 6. Spend liquidity cat mint coin
liquidity_cat_mint_coin = Coin(
eph_xch_coin.name(),
liquidity_cat_mint_coin_puzzle_hash,
new_liquidity_token_amount
)
output_conditions = announcement_asserts
output_conditions.append([ConditionOpcode.CREATE_COIN, OFFER_MOD_HASH, new_liquidity_token_amount])
liquidity_cat_mint_coin_tail_puzzle = pair_liquidity_tail_puzzle(pair_launcher_id)
output_conditions.append([
ConditionOpcode.CREATE_COIN,
0,
-113,
liquidity_cat_mint_coin_tail_puzzle,
liquidity_cat_mint_coin_tail_solution
])
# also assert the XCH ephemeral coin announcement
# specifically, for the payment made to mint liquidity tokens
eph_xch_settlement_announcement = eph_xch_coin_settlement_things[-1].get_tree_hash()
output_conditions.append([
ConditionOpcode.ASSERT_PUZZLE_ANNOUNCEMENT,
std_hash(eph_xch_coin.puzzle_hash + eph_xch_settlement_announcement)
])
liquidity_cat_mint_coin_solution = Program.to([
output_conditions,
[],
liquidity_cat_mint_coin.name(),
[liquidity_cat_mint_coin.parent_coin_info, liquidity_cat_mint_coin.puzzle_hash, new_liquidity_token_amount],
[liquidity_cat_mint_coin.parent_coin_info, liquidity_cat_mint_coin_inner_puzzle_hash, new_liquidity_token_amount],
[],
[]
])
liquidity_cat_mint_coin_spend = CoinSpend(
liquidity_cat_mint_coin,
liquidity_cat_mint_coin_puzzle,
liquidity_cat_mint_coin_solution
)
# 7. Ephemeral liquidity cat
ephemeral_liquidity_cat_coin_puzzle = construct_cat_puzzle(CAT_MOD, liquidity_cat_tail_hash, OFFER_MOD)
ephemeral_liquidity_cat_coin_puzzle_hash = ephemeral_liquidity_cat_coin_puzzle.get_tree_hash()
ephemeral_liquidity_cat = Coin(
liquidity_cat_mint_coin.name(),
ephemeral_liquidity_cat_coin_puzzle_hash,
new_liquidity_token_amount
)
notarized_payment = offer.get_requested_payments()[liquidity_cat_tail_hash][0]
nonce = notarized_payment.nonce
memos = notarized_payment.memos
ephemeral_liquidity_cat_inner_solution = Program.to([
Program.to([
nonce,
[memos[0], new_liquidity_token_amount, memos]
])
])
ephemeral_liquidity_cat_spend_bundle = unsigned_spend_bundle_for_spendable_cats(
CAT_MOD,
[
SpendableCAT(
ephemeral_liquidity_cat,
liquidity_cat_tail_hash,
OFFER_MOD,
ephemeral_liquidity_cat_inner_solution,
lineage_proof=LineageProof(
liquidity_cat_mint_coin.parent_coin_info,
liquidity_cat_mint_coin_inner_puzzle_hash,
new_liquidity_token_amount
)
)
],
)
ephemeral_liquidity_cat_spend = ephemeral_liquidity_cat_spend_bundle.coin_spends[0]
# 8. Right now, the tx is valid, but the fee is negative - spend the last (previous) xch reserve coin and we're done!
last_xch_reserve_spend_maybe = []
if last_xch_reserve_coin != None:
last_xch_reserve_coin_puzzle = p2_singleton_puzzle
last_xch_reserve_coin_solution = solution_for_p2_singleton_flashloan(
last_xch_reserve_coin, pair_singleton_inner_puzzle.get_tree_hash()
)
last_xch_reserve_spend_maybe.append(
CoinSpend(last_xch_reserve_coin, last_xch_reserve_coin_puzzle, last_xch_reserve_coin_solution)
)
sb = SpendBundle(
[
eph_xch_coin_spend,
pair_singleton_spend,
liquidity_cat_mint_coin_spend,
ephemeral_liquidity_cat_spend
] + token_reserve_creation_spends + cs_from_initial_offer + last_xch_reserve_spend_maybe,
offer_spend_bundle.aggregated_signature
)
return sb
async def respond_to_remove_liquidity_offer(
pair_launcher_id,
current_pair_coin,
creation_spend,
token_tail_hash,
pair_liquidity,
pair_xch_reserve,
pair_token_reserve,
offer_str,
last_xch_reserve_coin,
last_token_reserve_coin,
last_token_reserve_lineage_proof, # coin_parent_coin_info, inner_puzzle_hash, amount
return_address = DEFAULT_RETURN_ADDRESS
):
# 1. detect offered ephemeral coin (ephemeral liquidity coin, created the offer so we can use it)
offer = Offer.from_bech32(offer_str)
offer_spend_bundle = offer.to_spend_bundle()
offer_coin_spends = offer_spend_bundle.coin_spends
eph_liquidity_coin = None
eph_liquidity_coin_creation_spend = None # needed when spending eph_liquidity_coin since it's a CAT
announcement_asserts = [] # assert everything when the old XCH reserve is spent
liquidity_cat_tail = pair_liquidity_tail_puzzle(pair_launcher_id)
liquidity_cat_tail_hash = liquidity_cat_tail.get_tree_hash()
eph_liquidity_coin_puzzle = construct_cat_puzzle(CAT_MOD, liquidity_cat_tail_hash, OFFER_MOD)
eph_liquidity_coin_puzzle_hash = eph_liquidity_coin_puzzle.get_tree_hash()
cs_from_initial_offer = [] # all valid coin spends (i.e., not 'hints' for offered assets or coins)
for coin_spend in offer_coin_spends:
if coin_spend.coin.parent_coin_info == b"\x00" * 32: # 'hint' for offer requested coin