@alpha-arcade/sdk 0.3.0 → 0.3.1
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/dist/index.cjs +135 -2
- package/dist/index.cjs.map +1 -1
- package/dist/index.d.cts +37 -1
- package/dist/index.d.ts +37 -1
- package/dist/index.js +135 -2
- package/dist/index.js.map +1 -1
- package/package.json +1 -1
package/dist/index.cjs
CHANGED
|
@@ -2514,6 +2514,89 @@ var proposeMatch = async (config, params) => {
|
|
|
2514
2514
|
confirmedRound: Number(result.confirmedRound)
|
|
2515
2515
|
};
|
|
2516
2516
|
};
|
|
2517
|
+
var processMatch = async (config, params) => {
|
|
2518
|
+
const { algodClient, signer, activeAddress, usdcAssetId } = config;
|
|
2519
|
+
const { marketAppId, makerEscrowAppId, takerEscrowAppId } = params;
|
|
2520
|
+
const [marketState, makerAppInfo, takerAppInfo] = await Promise.all([
|
|
2521
|
+
getMarketGlobalState(algodClient, marketAppId),
|
|
2522
|
+
algodClient.getApplicationByID(makerEscrowAppId).do(),
|
|
2523
|
+
algodClient.getApplicationByID(takerEscrowAppId).do()
|
|
2524
|
+
]);
|
|
2525
|
+
const marketFeeAddress = marketState.fee_address;
|
|
2526
|
+
const yesAssetId = marketState.yes_asset_id;
|
|
2527
|
+
const noAssetId = marketState.no_asset_id;
|
|
2528
|
+
const makerState = decodeGlobalState(
|
|
2529
|
+
makerAppInfo.params?.globalState ?? makerAppInfo.params?.["global-state"] ?? []
|
|
2530
|
+
);
|
|
2531
|
+
const takerState = decodeGlobalState(
|
|
2532
|
+
takerAppInfo.params?.globalState ?? takerAppInfo.params?.["global-state"] ?? []
|
|
2533
|
+
);
|
|
2534
|
+
const makerOwner = makerState.owner ?? "";
|
|
2535
|
+
const takerOwner = takerState.owner ?? "";
|
|
2536
|
+
const makerRemaining = (makerState.quantity ?? 0) - (makerState.quantity_filled ?? 0);
|
|
2537
|
+
const takerRemaining = (takerState.quantity ?? 0) - (takerState.quantity_filled ?? 0);
|
|
2538
|
+
const matchedQuantity = Math.min(makerRemaining, takerRemaining);
|
|
2539
|
+
if (matchedQuantity <= 0) {
|
|
2540
|
+
throw new Error("processMatch: no quantity left to match on maker or taker escrow.");
|
|
2541
|
+
}
|
|
2542
|
+
const signerAccount = { signer, addr: activeAddress };
|
|
2543
|
+
const makerEscrowClient = new EscrowAppClient(
|
|
2544
|
+
{ resolveBy: "id", id: makerEscrowAppId, sender: signerAccount },
|
|
2545
|
+
algodClient
|
|
2546
|
+
);
|
|
2547
|
+
const takerEscrowClient = new EscrowAppClient(
|
|
2548
|
+
{ resolveBy: "id", id: takerEscrowAppId, sender: signerAccount },
|
|
2549
|
+
algodClient
|
|
2550
|
+
);
|
|
2551
|
+
const marketClient = new MarketAppClient(
|
|
2552
|
+
{ resolveBy: "id", id: marketAppId, sender: signerAccount },
|
|
2553
|
+
algodClient
|
|
2554
|
+
);
|
|
2555
|
+
const atc = new algosdk2.AtomicTransactionComposer();
|
|
2556
|
+
const assets = [usdcAssetId, yesAssetId, noAssetId];
|
|
2557
|
+
const sendParamsWithFee = { skipSending: true, fee: algokit4__namespace.microAlgos(3e3) };
|
|
2558
|
+
const sendParamsNoop = { skipSending: true, fee: algokit4__namespace.microAlgos(1e3) };
|
|
2559
|
+
const matchMakerTxn = await makerEscrowClient.matchMaker(
|
|
2560
|
+
{ taker: takerEscrowAppId, matchQuantity: matchedQuantity },
|
|
2561
|
+
{
|
|
2562
|
+
assets,
|
|
2563
|
+
apps: [marketAppId],
|
|
2564
|
+
accounts: [takerOwner, marketFeeAddress],
|
|
2565
|
+
sendParams: sendParamsWithFee
|
|
2566
|
+
}
|
|
2567
|
+
);
|
|
2568
|
+
atc.addTransaction({ txn: matchMakerTxn.transaction, signer });
|
|
2569
|
+
const matchTakerTxn = await takerEscrowClient.matchTaker(
|
|
2570
|
+
{ maker: makerEscrowAppId },
|
|
2571
|
+
{
|
|
2572
|
+
assets,
|
|
2573
|
+
apps: [marketAppId],
|
|
2574
|
+
accounts: [makerOwner, marketFeeAddress],
|
|
2575
|
+
sendParams: sendParamsWithFee
|
|
2576
|
+
}
|
|
2577
|
+
);
|
|
2578
|
+
atc.addTransaction({ txn: matchTakerTxn.transaction, signer });
|
|
2579
|
+
const processMatchTxn = await marketClient.processPotentialMatch(
|
|
2580
|
+
{ maker: makerEscrowAppId, taker: takerEscrowAppId },
|
|
2581
|
+
{
|
|
2582
|
+
assets,
|
|
2583
|
+
accounts: [makerOwner, takerOwner, marketFeeAddress],
|
|
2584
|
+
sendParams: sendParamsNoop
|
|
2585
|
+
}
|
|
2586
|
+
);
|
|
2587
|
+
atc.addTransaction({ txn: processMatchTxn.transaction, signer });
|
|
2588
|
+
const doNoopTxn = await marketClient.doNoop(
|
|
2589
|
+
{ callNumber: 1 },
|
|
2590
|
+
{ sendParams: sendParamsNoop }
|
|
2591
|
+
);
|
|
2592
|
+
atc.addTransaction({ txn: doNoopTxn.transaction, signer });
|
|
2593
|
+
const result = await atc.execute(algodClient, 4);
|
|
2594
|
+
return {
|
|
2595
|
+
success: true,
|
|
2596
|
+
txIds: result.txIDs,
|
|
2597
|
+
confirmedRound: Number(result.confirmedRound)
|
|
2598
|
+
};
|
|
2599
|
+
};
|
|
2517
2600
|
var amendOrder = async (config, params) => {
|
|
2518
2601
|
const { algodClient, signer, activeAddress, usdcAssetId } = config;
|
|
2519
2602
|
const { marketAppId, escrowAppId, price, quantity, slippage = 0 } = params;
|
|
@@ -2570,10 +2653,47 @@ var amendOrder = async (config, params) => {
|
|
|
2570
2653
|
);
|
|
2571
2654
|
atc.addTransaction({ txn: amendCall.transaction, signer });
|
|
2572
2655
|
const result = await atc.execute(algodClient, 4);
|
|
2656
|
+
const allTxIds = [...result.txIDs];
|
|
2657
|
+
let lastConfirmedRound = Number(result.confirmedRound);
|
|
2658
|
+
const orderbook = await getOrderbook(config, marketAppId);
|
|
2659
|
+
let matchingOrders = calculateMatchingOrders(orderbook, isBuy, position === 1, quantity, price, slippage);
|
|
2660
|
+
matchingOrders = matchingOrders.filter((m) => m.escrowAppId !== escrowAppId);
|
|
2661
|
+
if (matchingOrders.length > 0) {
|
|
2662
|
+
let quantityLeft = quantity;
|
|
2663
|
+
const matchedQuantities = [];
|
|
2664
|
+
const matchedPrices = [];
|
|
2665
|
+
for (const m of matchingOrders) {
|
|
2666
|
+
if (quantityLeft <= 0) break;
|
|
2667
|
+
try {
|
|
2668
|
+
const matchResult = await processMatch(config, {
|
|
2669
|
+
marketAppId,
|
|
2670
|
+
makerEscrowAppId: m.escrowAppId,
|
|
2671
|
+
takerEscrowAppId: escrowAppId
|
|
2672
|
+
});
|
|
2673
|
+
allTxIds.push(...matchResult.txIds);
|
|
2674
|
+
lastConfirmedRound = matchResult.confirmedRound;
|
|
2675
|
+
const q = Math.min(m.quantity, quantityLeft);
|
|
2676
|
+
matchedQuantities.push(q);
|
|
2677
|
+
matchedPrices.push((m.price ?? price) * q);
|
|
2678
|
+
quantityLeft -= q;
|
|
2679
|
+
} catch (err) {
|
|
2680
|
+
console.log(`Error matching order: ${JSON.stringify(err)}`);
|
|
2681
|
+
break;
|
|
2682
|
+
}
|
|
2683
|
+
}
|
|
2684
|
+
const totalMatchedQuantity = matchedQuantities.reduce((a, b) => a + b, 0);
|
|
2685
|
+
const matchedPrice = totalMatchedQuantity > 0 ? Math.round(matchedPrices.reduce((a, b) => a + b, 0) / totalMatchedQuantity) : void 0;
|
|
2686
|
+
return {
|
|
2687
|
+
success: true,
|
|
2688
|
+
txIds: allTxIds,
|
|
2689
|
+
confirmedRound: lastConfirmedRound,
|
|
2690
|
+
...totalMatchedQuantity > 0 && { matchedQuantity: totalMatchedQuantity, matchedPrice }
|
|
2691
|
+
};
|
|
2692
|
+
}
|
|
2573
2693
|
return {
|
|
2574
2694
|
success: true,
|
|
2575
|
-
txIds:
|
|
2576
|
-
confirmedRound:
|
|
2695
|
+
txIds: allTxIds,
|
|
2696
|
+
confirmedRound: lastConfirmedRound
|
|
2577
2697
|
};
|
|
2578
2698
|
};
|
|
2579
2699
|
var splitShares = async (config, params) => {
|
|
@@ -3056,6 +3176,19 @@ var AlphaClient = class {
|
|
|
3056
3176
|
async proposeMatch(params) {
|
|
3057
3177
|
return proposeMatch(this.config, params);
|
|
3058
3178
|
}
|
|
3179
|
+
/**
|
|
3180
|
+
* Matches two existing limit orders (no create-escrow in the group).
|
|
3181
|
+
*
|
|
3182
|
+
* Calls the market app's process_potential_match(maker, taker). Use this
|
|
3183
|
+
* after amending an order: the amended order is the taker (pays the fee),
|
|
3184
|
+
* the counterparty is the maker.
|
|
3185
|
+
*
|
|
3186
|
+
* @param params - Process match params (marketAppId, makerEscrowAppId, takerEscrowAppId)
|
|
3187
|
+
* @returns Whether the match succeeded
|
|
3188
|
+
*/
|
|
3189
|
+
async processMatch(params) {
|
|
3190
|
+
return processMatch(this.config, params);
|
|
3191
|
+
}
|
|
3059
3192
|
/**
|
|
3060
3193
|
* Amends (edits) an existing unfilled order in-place.
|
|
3061
3194
|
*
|