@@ -149,13 +149,19 @@ def close_debt_position(self, symbol, account=None):
149
149
})
150
150
return self .blockchain .finalizeOp (op , account ["name" ], "active" )
151
151
152
- def adjust_debt (self , delta , new_collateral_ratio = None , account = None ):
152
+ def adjust_debt (self , delta , new_collateral_ratio = None , account = None , target_collateral_ratio = None ):
153
153
""" Adjust the amount of debt for an asset
154
154
155
- :param Amount delta: Delta amount of the debt (-10 means reduce debt by 10, +10 means borrow another 10)
156
- :param float new_collateral_ratio: collateral ratio to maintain (optional, by default tries to maintain old ratio)
155
+ :param Amount delta: Delta amount of the debt (-10 means reduce
156
+ debt by 10, +10 means borrow another 10)
157
+ :param float new_collateral_ratio: collateral ratio to maintain
158
+ (optional, by default tries to maintain old ratio)
159
+ :param float target_collateral_ratio: Tag the call order so that in
160
+ case of margin call, only enough debt is covered to get back to
161
+ this ratio
157
162
:raises ValueError: if symbol is not a bitasset
158
- :raises ValueError: if collateral ratio is smaller than maintenance collateral ratio
163
+ :raises ValueError: if collateral ratio is smaller than maintenance
164
+ collateral ratio
159
165
:raises ValueError: if required amounts of collateral are not available
160
166
"""
161
167
if not account :
@@ -178,14 +184,11 @@ def adjust_debt(self, delta, new_collateral_ratio=None, account=None):
178
184
179
185
# Check minimum collateral ratio
180
186
backing_asset_id = bitasset ["options" ]["short_backing_asset" ]
181
- # maintenance_col_ratio = bitasset["current_feed"]["maintenance_collateral_ratio"] / 1000
182
187
current_debts = self .list_debt_positions (account )
183
188
if not new_collateral_ratio and symbol not in current_debts :
184
- new_collateral_ratio = maintenance_col_ratio
189
+ new_collateral_ratio = bitasset [ "current_feed" ][ "maintenance_collateral_ratio" ] / 1000
185
190
elif not new_collateral_ratio and symbol in current_debts :
186
191
new_collateral_ratio = current_debts [symbol ]["ratio" ]
187
- # if maintenance_col_ratio > new_collateral_ratio:
188
- # raise ValueError("Collateral Ratio has to be higher than %5.2f" % maintenance_col_ratio)
189
192
190
193
# Derive Amount of Collateral
191
194
collateral_asset = Asset (
@@ -212,7 +215,7 @@ def adjust_debt(self, delta, new_collateral_ratio=None, account=None):
212
215
raise ValueError ("Not enough funds available. Need %f %s, but only %f %s are available" %
213
216
(fundsNeeded , collateral_asset ["symbol" ], fundsHave , collateral_asset ["symbol" ]))
214
217
215
- op = operations . Call_order_update ( ** {
218
+ payload = {
216
219
'fee' : {'amount' : 0 , 'asset_id' : '1.3.0' },
217
220
'delta_debt' : {
218
221
'amount' : int (float (delta ) * 10 ** asset ["precision" ]),
@@ -221,15 +224,25 @@ def adjust_debt(self, delta, new_collateral_ratio=None, account=None):
221
224
'amount' : int (float (amount_of_collateral ) * 10 ** collateral_asset ["precision" ]),
222
225
'asset_id' : collateral_asset ["id" ]},
223
226
'funding_account' : account ["id" ],
224
- 'extensions' : []
225
- })
227
+ 'extensions' : {}
228
+ }
229
+ # Extension
230
+ if target_collateral_ratio :
231
+ payload ["extensions" ].update (dict (
232
+ target_collateral_ratio = int (target_collateral_ratio * 100 )
233
+ ))
234
+
235
+ op = operations .Call_order_update (** payload )
226
236
return self .blockchain .finalizeOp (op , account ["name" ], "active" )
227
237
228
- def adjust_collateral_ratio (self , symbol , target_collateral_ratio , account = None ):
238
+ def adjust_collateral_ratio (self , symbol , new_collateral_ratio , account = None , target_collateral_ratio = None ):
229
239
""" Adjust the collataral ratio of a debt position
230
240
231
241
:param Asset amount: Amount to borrow (denoted in 'asset')
232
- :param float target_collateral_ratio: desired collateral ratio
242
+ :param float new_collateral_ratio: desired collateral ratio
243
+ :param float target_collateral_ratio: Tag the call order so that in
244
+ case of margin call, only enough debt is covered to get back to
245
+ this ratio
233
246
:raises ValueError: if symbol is not a bitasset
234
247
:raises ValueError: if collateral ratio is smaller than maintenance collateral ratio
235
248
:raises ValueError: if required amounts of collateral are not available
@@ -243,17 +256,20 @@ def adjust_collateral_ratio(self, symbol, target_collateral_ratio, account=None)
243
256
current_debts = self .list_debt_positions (account )
244
257
if symbol not in current_debts :
245
258
raise ValueError ("No Call position available to adjust! Please borrow first!" )
246
- return self .adjust_debt (Amount (0 , symbol ), target_collateral_ratio , account )
259
+ return self .adjust_debt (Amount (0 , symbol ), new_collateral_ratio , account , target_collateral_ratio = target_collateral_ratio )
247
260
248
- def borrow (self , amount , collateral_ratio = None , account = None ):
261
+ def borrow (self , amount , collateral_ratio = None , account = None , target_collateral_ratio = None ):
249
262
""" Borrow bitassets/smartcoins from the network by putting up
250
263
collateral in a CFD at a given collateral ratio.
251
264
252
265
:param float amount: Amount to borrow (denoted in 'asset')
253
266
:param float collateral_ratio: Collateral ratio to borrow at
267
+ :param float target_collateral_ratio: Tag the call order so that in
268
+ case of margin call, only enough debt is covered to get back to
269
+ this ratio
254
270
:raises ValueError: if symbol is not a bitasset
255
271
:raises ValueError: if collateral ratio is smaller than maintenance collateral ratio
256
272
:raises ValueError: if required amounts of collateral are not available
257
273
258
274
"""
259
- return self .adjust_debt (amount , collateral_ratio , account )
275
+ return self .adjust_debt (amount , collateral_ratio , account , target_collateral_ratio = target_collateral_ratio )
0 commit comments