Skip to content
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.

Commit b9c10b2

Browse files
committedAug 9, 2024··
feat: filling orders
1 parent 74c3a70 commit b9c10b2

File tree

1 file changed

+84
-6
lines changed

1 file changed

+84
-6
lines changed
 

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

+84-6
Original file line numberDiff line numberDiff line change
@@ -257,7 +257,7 @@ async function initiateOrder() {
257257
const reactorAddress = order.order.settlementContract;
258258
const reactor = new ethers.Contract(reactorAddress, reactorAbi, signer);
259259

260-
// TODO: there needs to be set approvals for the reactorAddress for all inputs & collateral.
260+
// TODO: Set approvals for the reactorAddress for all inputs & collateral.
261261

262262
// The order arrives almost exactly ready to use, we just need to remove the type from the orderdata.
263263
const {type: _, ...cleanedOrderData} = order.order.orderData;
@@ -294,7 +294,7 @@ def initiate_order():
294294
reactor_address = order['order']['settlementContract']
295295
reactor = web3.eth.contract(address=reactor_address, abi=reactor_abi)
296296

297-
# TODO: there needs to be set approvals for the reactorAddress for all inputs & collateral.
297+
# TODO: Set approvals for the reactorAddress for all inputs & collateral.
298298
# This will depend on the specific ERC20 tokens you're using, you need to call approve() on the ERC20 token contracts
299299

300300
# Clean the order data by removing the type field
@@ -309,16 +309,12 @@ def initiate_order():
309309
'from': signer_address,
310310
'nonce': web3.eth.get_transaction_count(signer_address)
311311
})
312-
313312
# Sign the transaction
314313
signed_txn = web3.eth.account.sign_transaction(txn, private_key=signer_private_key)
315-
316314
# Send the transaction
317315
tx_hash = web3.eth.send_raw_transaction(signed_txn.rawTransaction)
318-
319316
# Wait for the transaction receipt
320317
receipt = web3.eth.wait_for_transaction_receipt(tx_hash)
321-
322318
return receipt
323319
```
324320

@@ -373,8 +369,90 @@ Asset delivery depends on whether the destination is a VM chain or Bitcoin. For
373369

374370
#### EVM deliveries
375371

372+
Call the destination chain oracle `getOrderData.order.orderData.remoteOracle`. You can get remote chain from the output `getOrderData.order.orderData.outputs[].chainId`.
373+
374+
<Tabs syncKey="lang">
375+
<TabItem label="Typescript">
376+
377+
```typescript
378+
// It is assumed you are continuing from the above steps.
379+
import {ethers} from 'ethers';
380+
381+
const oracleAbi = "...";
382+
383+
// The oracle allows filling multiple outputs from different orders in a single transaction.
384+
// They do have to go to the same chain.
385+
// For simplicity, this function assumes that all outputs goes to the same chain but it may not be the case.
386+
async function fillSingleChainOrder(order: CrossChainOrder) {
387+
const oracle = new ethers.Contract(order.orderData.remoteOracle, oracleAbi, signer);
388+
389+
let recordedChain;
390+
for (const output of order.orderData.outputs) {
391+
if (recordedChain === undefined) recordedChain = output.chainId;
392+
if (recordedChain !== output.chainId) throw Error(`Mixed ChainIds, seen ${recordedChain} and ${output.chainId}`);
393+
}
394+
395+
// TODO: Set approvals for the oracleAddress for the value of the output.
396+
397+
// We need to provide fill times. These have to be set to proofTime.
398+
// These are used to ensure you can't reuse fills.
399+
const fillTimes = order.orderData.outputs.map(_ => order.orderData.proofDeadline);
400+
401+
// Call the reactor to initiate the order.
402+
return oracle.fill(outputs, fillTimes);
403+
}
404+
```
405+
406+
</TabItem>
407+
<TabItem label="Python">
408+
409+
```python
410+
# It is assumed you are continuing from the above steps.
411+
412+
oracleAbi = "...";
413+
414+
# The oracle allows filling multiple outputs from different orders in a single transaction.
415+
# They do have to go to the same chain.
416+
# For simplicity, this function assumes that all outputs goes to the same chain but it may not be the case.
417+
def fill_single_chain_order(order):
418+
oracle_address = order['orderData']['remoteOracle']
419+
oracle = web3.eth.contract(address=oracle_address, abi=oracle_abi)
420+
421+
recordedChain = "";
422+
for (output in order['orderData']['outputs']):
423+
if (recordedChain == ""):
424+
recordedChain = output.chainId
425+
if (recordedChain != output.chainId):
426+
raise Exception(f"Mixed ChainIds, seen {recordedChain} and {output.chainId}");
427+
428+
# TODO: Set approvals for the oracleAddress for the value of the output.
429+
430+
431+
# We need to provide fill times. These have to be set to proofTime.
432+
# These are used to ensure you can't reuse fills.
433+
fillTimes = [order.orderData.proofDeadline for _ in order.orderData.outputs]
434+
435+
# Build the transaction
436+
txn = oracle.functions.fill(order.orderData.outputs, fillTimes).build_transaction({
437+
'from': signer_address,
438+
'nonce': web3.eth.get_transaction_count(signer_address)
439+
})
440+
# Sign the transaction
441+
signed_txn = web3.eth.account.sign_transaction(txn, private_key=signer_private_key)
442+
# Send the transaction
443+
tx_hash = web3.eth.send_raw_transaction(signed_txn.rawTransaction)
444+
# Wait for the transaction receipt
445+
receipt = web3.eth.wait_for_transaction_receipt(tx_hash)
446+
return receipt
447+
```
448+
449+
</TabItem>
450+
</Tabs>
451+
376452
#### Bitcoin Deliveries
377453

454+
The order contains a low level encoded version of the Bitcoin address. There exist 5 different types of Bitcoin scripts, P2PKH, P2SH, P2WPKH, P2WSH, and P2TR addresses using 3 different ways to encode the script into an address: P2PKH and P2SH uses `RIPEMD-160(SHA-256(pubkey/script))` with 1 and 3 prepended respectively, P2WPKH and P2WSH uses SHA256 encoded with Bech32 with either 20 or 32 bytes encoded respectively prepended with bc1 and lastly P2TR Bech32m encodes the output key directly.
455+
378456
## From Bitcoin
379457

380458
### Delivering quotes

0 commit comments

Comments
 (0)
Please sign in to comment.