Skip to content

Commit 70e7391

Browse files
committed
add ordering module
1 parent 2ab9836 commit 70e7391

File tree

11 files changed

+1580
-0
lines changed

11 files changed

+1580
-0
lines changed

tinyman/ordering/__init__.py

Whitespace-only changes.

tinyman/ordering/base_client.py

Lines changed: 118 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,118 @@
1+
from base64 import b64decode, b64encode
2+
import time
3+
from tinyman.utils import TransactionGroup
4+
from algosdk import transaction
5+
from algosdk.logic import get_application_address
6+
7+
from .struct import get_struct, get_box_costs
8+
9+
10+
class BaseClient():
11+
def __init__(self, algod, app_id, user_address, user_sk) -> None:
12+
self.algod = algod
13+
self.app_id = app_id
14+
self.application_address = get_application_address(self.app_id)
15+
self.user_address = user_address
16+
self.keys = {}
17+
self.add_key(user_address, user_sk)
18+
self.current_timestamp = None
19+
self.simulate = False
20+
21+
def get_suggested_params(self):
22+
return self.algod.suggested_params()
23+
24+
def get_current_timestamp(self):
25+
return self.current_timestamp or time.time()
26+
27+
def _submit(self, transactions, additional_fees=0):
28+
transactions = self.flatten_transactions(transactions)
29+
fee = transactions[0].fee
30+
n = 0
31+
for txn in transactions:
32+
if txn.fee == fee:
33+
txn.fee = 0
34+
n += 1
35+
transactions[0].fee = (n + additional_fees) * fee
36+
txn_group = TransactionGroup(transactions)
37+
for address, key in self.keys.items():
38+
if isinstance(key, transaction.LogicSigAccount):
39+
txn_group.sign_with_logicsig(key, address=address)
40+
else:
41+
txn_group.sign_with_private_key(address, key)
42+
if self.simulate:
43+
txn_info = self.algod.simulate_raw_transactions(txn_group.signed_transactions)
44+
else:
45+
txn_info = txn_group.submit(self.algod, wait=True)
46+
return txn_info
47+
48+
def flatten_transactions(self, txns):
49+
result = []
50+
if isinstance(txns, transaction.Transaction):
51+
result = [txns]
52+
elif type(txns) == list:
53+
for txn in txns:
54+
result += self.flatten_transactions(txn)
55+
return result
56+
57+
def calculate_min_balance(self, accounts=0, assets=0, boxes=None):
58+
cost = 0
59+
cost += accounts * 100_000
60+
cost += assets * 100_000
61+
cost += get_box_costs(boxes or {})
62+
return cost
63+
64+
def add_key(self, address, key):
65+
self.keys[address] = key
66+
67+
def get_global(self, key, default=None, app_id=None):
68+
app_id = app_id or self.app_id
69+
global_state = {s["key"]: s["value"] for s in self.algod.application_info(app_id)["params"]["global-state"]}
70+
key = b64encode(key).decode()
71+
if key in global_state:
72+
value = global_state[key]
73+
if value["type"] == 2:
74+
return value["uint"]
75+
else:
76+
return b64decode(value["bytes"])
77+
else:
78+
return default
79+
80+
def get_box(self, box_name, struct_name, app_id=None):
81+
app_id = app_id or self.app_id
82+
box_value = b64decode(self.algod.application_box_by_name(app_id, box_name)["value"])
83+
struct_class = get_struct(struct_name)
84+
struct = struct_class(box_value)
85+
return struct
86+
87+
def box_exists(self, box_name, app_id=None):
88+
app_id = app_id or self.app_id
89+
try:
90+
self.algod.application_box_by_name(app_id, box_name)
91+
return True
92+
except Exception:
93+
return False
94+
95+
def get_reward_slot(self, staking_asset_id, reward_asset_id):
96+
asset_box = self.get_asset_box(staking_asset_id)
97+
for i in range(8):
98+
if asset_box.reward_slots[i].asset_id == reward_asset_id:
99+
return i
100+
101+
def is_opted_in(self, address, asset_id):
102+
if asset_id == 0:
103+
return True
104+
105+
try:
106+
self.algod.account_asset_info(address, asset_id)
107+
return True
108+
except Exception:
109+
return False
110+
111+
def get_optin_if_needed_txn(self, sender, asset_id):
112+
if not self.is_opted_in(sender, asset_id):
113+
txn = transaction.AssetOptInTxn(
114+
sender=sender,
115+
sp=self.get_suggested_params(),
116+
index=asset_id,
117+
)
118+
return txn

0 commit comments

Comments
 (0)