Skip to content

Commit 74c3a70

Browse files
committed
feat: python order processing
1 parent c2f40ff commit 74c3a70

File tree

1 file changed

+59
-9
lines changed

1 file changed

+59
-9
lines changed

src/content/docs/protocol/v2/solver.mdx

+59-9
Original file line numberDiff line numberDiff line change
@@ -143,7 +143,7 @@ async function submitOrder(): bool {
143143
});
144144
const parsedResponse = await response.json() as { success: bool };
145145

146-
return fetchedOrders;
146+
return parsedResponse.success;
147147
}
148148
```
149149

@@ -152,13 +152,20 @@ async function submitOrder(): bool {
152152

153153
```python
154154
import requests
155+
import json
155156

156157
API_URL = "https://orders.catalyst.exchange/"
157158

158-
def get_orders():
159-
response = requests.post(API_URL+ "submitOrder/", data)
159+
def submit_order(): bool:
160+
order = {}
161+
signature = "0x..."
162+
data = {
163+
"order": order,
164+
"signature": signature
165+
}
166+
response = requests.post(API_URL+ "submitOrder/", json=data)
160167
fetched_orders = response.json()
161-
return fetched_orders
168+
return fetched_orders.get("success")
162169
```
163170

164171
</TabItem>
@@ -212,10 +219,8 @@ async function getOrders(): getOrdersFetch {
212219
```python
213220
import requests
214221

215-
API_URL = "https://orders.catalyst.exchange/getOrders"
216-
217222
def get_orders():
218-
response = requests.get(API_URL)
223+
response = requests.get(API_URL + "orders/")
219224
fetched_orders = response.json()
220225
return fetched_orders
221226
```
@@ -268,8 +273,53 @@ async function initiateOrder() {
268273
</TabItem>
269274
<TabItem label="Python">
270275

271-
```sh
272-
pnpm create astro --template starlight
276+
```python
277+
from web3 import Web3
278+
279+
rpc_url = ""
280+
web3 = Web3(Web3.HTTPProvider(eth_node_url))
281+
282+
# Your ABI and signer details
283+
reactor_abi = "..."
284+
signer_private_key = "your_private_key_here"
285+
signer_address = web3.eth.account.from_key(signer_private_key).address
286+
287+
288+
def initiate_order():
289+
# Get an order
290+
orders = get_orders()
291+
order = orders['orders'][0]
292+
293+
# Define the reactor we will call. You can get the reactor address from the order
294+
reactor_address = order['order']['settlementContract']
295+
reactor = web3.eth.contract(address=reactor_address, abi=reactor_abi)
296+
297+
# TODO: there needs to be set approvals for the reactorAddress for all inputs & collateral.
298+
# This will depend on the specific ERC20 tokens you're using, you need to call approve() on the ERC20 token contracts
299+
300+
# Clean the order data by removing the type field
301+
cleaned_order_data = order['order']['orderData'].copy()
302+
cleaned_order_data.pop('type')
303+
cleaned_order = {**order['order'], 'orderData': cleaned_order_data}
304+
signature = order['signature']
305+
filler_data = "0x" # We will touch on the later.
306+
307+
# Build the transaction
308+
txn = reactor.functions.initiate(cleaned_order, signature, filler_data).build_transaction({
309+
'from': signer_address,
310+
'nonce': web3.eth.get_transaction_count(signer_address)
311+
})
312+
313+
# Sign the transaction
314+
signed_txn = web3.eth.account.sign_transaction(txn, private_key=signer_private_key)
315+
316+
# Send the transaction
317+
tx_hash = web3.eth.send_raw_transaction(signed_txn.rawTransaction)
318+
319+
# Wait for the transaction receipt
320+
receipt = web3.eth.wait_for_transaction_receipt(tx_hash)
321+
322+
return receipt
273323
```
274324

275325
</TabItem>

0 commit comments

Comments
 (0)