-
Notifications
You must be signed in to change notification settings - Fork 4
/
close_cdp_frame.py
152 lines (117 loc) · 4.29 KB
/
close_cdp_frame.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
from shadowlands.sl_dapp import SLFrame
from decimal import Decimal, DivisionByZero, InvalidOperation, DivisionUndefined
from cached_property import cached_property
from shadowlands.tui.debug import debug
from cdp_manager.lock_eth_frame import LockEthFrame
#from cdp_manager.cdp_status_frame import CDPStatusFrame
import pdb
class CloseCDPFrame(SLFrame):
def initialize(self):
self.add_label_row([
("Outstanding debt:", 0),
("{:f}".format( self.debt_value())[0:16] + " DAI", 1)
],
add_divider=False,
layout=[4, 4]
)
self.add_label_with_button(
lambda: self.your_dai,
"Get DAI",
self.dai_uniswap_frame
)
self.add_label_row([
("Stability Fee:", 0),
(self.stability_fee, 1)
],
add_divider=False,
layout=[4, 4]
)
self.add_label_with_button(
lambda: self.your_mkr,
"Get MKR",
self.mkr_uniswap_frame
)
self.add_button_row([
("Close CDP", self.close_cdp_choice, 0),
("Cancel", self.close, 3)
])
@cached_property
def your_dai(self):
return "Your DAI: {:f}".format(self.dapp.dai.my_balance() / 10 ** 18)[:22]
@cached_property
def your_mkr(self):
return "Your MKR: {:f}".format(self.dapp.mkr.my_balance() / 10 ** 18)[:22]
def debt_value(self):
try:
debt = self.dapp.debt_value / self.dapp.WAD
except (InvalidOperation, DivisionUndefined):
return Decimal(0)
return debt
def dai_uniswap_frame(self):
self.dapp.add_uniswap_frame(self.dapp.dai.address, action='buy', buy_amount=self.debt_value())
def mkr_uniswap_frame(self):
self.dapp.add_uniswap_frame(self.dapp.mkr.address, action='buy', buy_amount=self.uniswap_to_buy_mkr_value())
def uniswap_to_buy_mkr_value(self):
sfee = self.dapp.cdp_stability_fee / self.dapp.WAD
return sfee + sfee * Decimal(0.01)
def stability_fee(self):
try:
fee = self.uniswap_to_buy_mkr_value()
if fee == Decimal('0'):
return "0"
return str(fee)[0:10]
except DivisionByZero:
return ""
def close_cdp_choice(self):
# check for mkr or dai stability fee choice
# check to see if we need to unlock
fee_denomination = 'MKR'
fee_erc20_contract = self.dapp.mkr
allowance = fee_erc20_contract.allowance(
self.dapp.node.credstick.address,
self.dapp.ds_proxy.address
)
if allowance < self.dapp.mkr_cdp_stability_fee:
self.dapp.add_transaction_dialog(
fee_erc20_contract.approve(
self.dapp.ds_proxy.address,
self.dapp.MAX_WEI
),
title="Allow cdp proxy to send {}".format(fee_denomination),
gas_limit=50000,
)
self.dapp.add_message_dialog(
"We must allow the CDP proxy to send {}".format(fee_denomination)
)
return
# Check DAI allowance
fee_denomination = 'DAI'
fee_erc20_contract = self.dapp.dai
allowance = fee_erc20_contract.allowance(
self.dapp.node.credstick.address,
self.dapp.ds_proxy.address
)
if allowance < self.dapp.debt_value:
self.dapp.add_transaction_dialog(
fee_erc20_contract.approve(
self.dapp.ds_proxy.address,
self.dapp.MAX_WEI
),
title="Allow cdp proxy to send {}".format(fee_denomination),
gas_limit=50000,
)
self.dapp.add_message_dialog(
"We must allow the CDP proxy to send {}".format(fee_denomination)
)
return
# unlock if needed - and then back to this frame
self.dapp.add_transaction_dialog(
self.dapp.ds_proxy.shut(
self.dapp.sai_proxy.address,
self.dapp.tub.address,
self.dapp.cup_id
),
title="Close CDP",
gas_limit=700000,
)
self.close()