-
Notifications
You must be signed in to change notification settings - Fork 89
Expand file tree
/
Copy pathmessage.py
More file actions
297 lines (243 loc) · 8.3 KB
/
message.py
File metadata and controls
297 lines (243 loc) · 8.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
from decimal import Decimal
from typing import List, Optional
from v4_proto.cosmos.bank.v1beta1.tx_pb2 import MsgSend
from v4_proto.cosmos.base.v1beta1.coin_pb2 import Coin
from v4_proto.cosmos.distribution.v1beta1.tx_pb2 import MsgWithdrawDelegatorReward
from v4_proto.cosmos.staking.v1beta1.tx_pb2 import MsgUndelegate, MsgDelegate
from v4_proto.dydxprotocol.affiliates.tx_pb2 import MsgRegisterAffiliate
from v4_proto.dydxprotocol.clob.order_pb2 import (
Order,
OrderId,
BuilderCodeParameters,
TwapParameters,
)
from v4_proto.dydxprotocol.clob.tx_pb2 import (
MsgCancelOrder,
MsgPlaceOrder,
MsgUpdateLeverage,
)
from v4_proto.dydxprotocol.listing.tx_pb2 import MsgCreateMarketPermissionless
from v4_proto.dydxprotocol.sending.transfer_pb2 import (
MsgDepositToSubaccount,
MsgWithdrawFromSubaccount,
Transfer,
)
from v4_proto.dydxprotocol.sending.tx_pb2 import MsgCreateTransfer
from v4_proto.dydxprotocol.subaccounts.subaccount_pb2 import SubaccountId
from v4_proto.dydxprotocol.clob.tx_pb2 import MsgBatchCancel, OrderBatch
from v4_proto.dydxprotocol.accountplus.tx_pb2 import (
MsgAddAuthenticator,
MsgRemoveAuthenticator,
)
from v4_proto.dydxprotocol.revshare import tx_pb2 as revshare_tx_types
from v4_proto.dydxprotocol.revshare.revshare_pb2 import OrderRouterRevShare
import json
import base64
from v4_proto.dydxprotocol.vault.share_pb2 import NumShares
from v4_proto.dydxprotocol.vault.tx_pb2 import (
MsgDepositToMegavault,
MsgWithdrawFromMegavault,
)
PY_V2_CLIENT_ID = 2
def order(
order_id: OrderId,
side: Order.Side,
quantums: int,
subticks: int,
time_in_force: Order.TimeInForce,
reduce_only: bool,
builder_code_parameters: Optional[BuilderCodeParameters],
twap_parameters: Optional[TwapParameters],
order_router_address: Optional[str],
good_til_block: int = None,
good_til_block_time: int = None,
client_metadata: int = 0,
condition_type: Order.ConditionType = Order.ConditionType.CONDITION_TYPE_UNSPECIFIED,
conditional_order_trigger_subticks: int = 0,
):
return Order(
order_id=order_id,
side=side,
quantums=quantums,
subticks=subticks,
good_til_block=good_til_block,
good_til_block_time=good_til_block_time,
time_in_force=time_in_force,
reduce_only=reduce_only,
client_metadata=PY_V2_CLIENT_ID,
condition_type=condition_type,
conditional_order_trigger_subticks=conditional_order_trigger_subticks,
builder_code_parameters=builder_code_parameters,
twap_parameters=twap_parameters,
order_router_address=order_router_address,
)
def order_id(
address: str,
subaccount_number: int,
client_id: int,
clob_pair_id: int,
order_flags: int,
) -> OrderId:
return OrderId(
subaccount_id=subaccount(owner=address, number=subaccount_number),
client_id=client_id,
order_flags=order_flags,
clob_pair_id=clob_pair_id,
)
def subaccount(owner: str, number: int):
return SubaccountId(owner=owner, number=number)
def place_order(order: Order):
return MsgPlaceOrder(order=order)
def cancel_order(
order_id: OrderId,
good_til_block: int = None,
good_til_block_time: int = None,
):
message = MsgCancelOrder(
order_id=order_id,
good_til_block=good_til_block,
good_til_block_time=good_til_block_time,
)
return message
def batch_cancel(
subaccount_id: SubaccountId,
short_term_cancels: List[OrderBatch],
good_til_block: int,
):
message = MsgBatchCancel(
subaccount_id=subaccount_id,
short_term_cancels=short_term_cancels,
good_til_block=good_til_block,
)
return message
def transfer(
sender_subaccount: SubaccountId,
recipient_subaccount: SubaccountId,
asset_id: int,
amount: int,
):
msg = Transfer(
sender=sender_subaccount,
recipient=recipient_subaccount,
asset_id=asset_id,
amount=amount,
)
return MsgCreateTransfer(transfer=msg)
def deposit(
sender: str,
recipient_subaccount: SubaccountId,
asset_id: int,
quantums: int,
):
message = MsgDepositToSubaccount(
sender=sender,
recipient=recipient_subaccount,
asset_id=asset_id,
quantums=quantums,
)
return message
def withdraw(
sender_subaccount: SubaccountId,
recipient: str,
asset_id: int,
quantums: int,
):
message = MsgWithdrawFromSubaccount(
sender=sender_subaccount,
recipient=recipient,
asset_id=asset_id,
quantums=quantums,
)
return message
def send_token(sender: str, recipient: str, quantums: int, denomination: str):
message = MsgSend(
from_address=sender,
to_address=recipient,
amount=[Coin(amount=str(quantums), denom=denomination)],
)
return message
def add_authenticator(sender: str, auth_type: str, config: bytes):
message = MsgAddAuthenticator(
sender=sender,
authenticator_type=auth_type,
data=convert_nested_config_to_base64(config),
)
return message
def remove_authenticator(sender: str, authenticator_id: int):
message = MsgRemoveAuthenticator(
sender=sender,
id=authenticator_id,
)
return message
def convert_nested_config_to_base64(config: bytes):
try:
config_json_array = json.loads(config.decode())
for config_json in config_json_array:
if config_json["type"] == "AnyOf" or config_json["type"] == "AllOf":
config_json["config"] = convert_nested_config_to_base64(
bytes(config_json["config"])
)
config_json["config"] = base64.b64encode(
bytes(config_json["config"])
).decode()
config_modified = json.dumps(config_json_array)
return config_modified.encode()
except:
return config
def create_market_permissionless(ticker: str, address: str, subaccount_id: int):
return MsgCreateMarketPermissionless(
ticker=ticker, subaccount_id=SubaccountId(owner=address, number=subaccount_id)
)
def register_affiliate(referee: str, affiliate: str):
return MsgRegisterAffiliate(referee=referee, affiliate=affiliate)
def withdraw_delegator_reward(delegator: str, validator: str):
return MsgWithdrawDelegatorReward(
delegator_address=delegator, validator_address=validator
)
def undelegate(delegator: str, validator: str, quantums: int, denomination: str):
return MsgUndelegate(
delegator_address=delegator,
validator_address=validator,
amount=Coin(amount=str(quantums), denom=denomination),
)
def delegate(delegator: str, validator: str, quantums: int, denomination: str):
return MsgDelegate(
delegator_address=delegator,
validator_address=validator,
amount=Coin(amount=str(quantums), denom=denomination),
)
def deposit_to_megavault(address: str, subaccount_number: int, quantums: bytes):
return MsgDepositToMegavault(
subaccount_id=SubaccountId(owner=address, number=subaccount_number),
quote_quantums=quantums,
)
def withdraw_from_megavault(
address: str, subaccount_number: int, min_quantums: bytes, num_shares: bytes
):
return MsgWithdrawFromMegavault(
subaccount_id=SubaccountId(owner=address, number=subaccount_number),
min_quote_quantums=min_quantums,
shares=NumShares(num_shares=num_shares),
)
def builder_code_parameters(builder_address: str, fee_ppm: int):
if builder_address is None:
return None
return BuilderCodeParameters(builder_address=builder_address, fee_ppm=fee_ppm)
def twap_parameters(duration: int, interval: int, price_tolerance: int):
return TwapParameters(
duration=duration, interval=interval, price_tolerance=price_tolerance
)
def update_leverage(address: str, subaccount_number: int, entries: List):
"""
Create a MsgUpdateLeverage message.
Args:
address (str): The subaccount owner address.
subaccount_number (int): The subaccount number.
entries (List): List of LeverageEntry objects.
Returns:
MsgUpdateLeverage: The update leverage message.
"""
return MsgUpdateLeverage(
subaccount_id=SubaccountId(owner=address, number=subaccount_number),
clob_pair_leverage=entries,
)