@alpha-arcade/sdk 0.2.11 → 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/README.md +2 -2
- package/dist/index.cjs +212 -55
- package/dist/index.cjs.map +1 -1
- package/dist/index.d.cts +41 -3
- package/dist/index.d.ts +41 -3
- package/dist/index.js +212 -52
- package/dist/index.js.map +1 -1
- package/package.json +5 -5
package/dist/index.js
CHANGED
|
@@ -1,4 +1,5 @@
|
|
|
1
|
-
import
|
|
1
|
+
import * as algosdk2 from 'algosdk';
|
|
2
|
+
import { AtomicTransactionComposer, getApplicationAddress, modelsv2 } from 'algosdk';
|
|
2
3
|
import * as algokit4 from '@algorandfoundation/algokit-utils';
|
|
3
4
|
import { Decimal } from 'decimal.js';
|
|
4
5
|
|
|
@@ -1973,47 +1974,58 @@ var calculateFeeFromTotal = (totalAmount, price, feeBase) => {
|
|
|
1973
1974
|
var decodeGlobalState = (rawState) => {
|
|
1974
1975
|
const state = {};
|
|
1975
1976
|
for (const item of rawState) {
|
|
1976
|
-
const
|
|
1977
|
-
|
|
1977
|
+
const rawKey = item.key;
|
|
1978
|
+
const key = typeof rawKey === "string" ? Buffer.from(rawKey, "base64").toString() : rawKey instanceof Uint8Array ? Buffer.from(rawKey).toString() : String(rawKey);
|
|
1979
|
+
const val = item.value;
|
|
1980
|
+
const type = val.type;
|
|
1981
|
+
if (type === 1) {
|
|
1978
1982
|
if (key === "owner" || key === "oracle_address" || key === "fee_address" || key === "market_friend_addr" || key === "escrow_cancel_address") {
|
|
1979
1983
|
try {
|
|
1980
|
-
const
|
|
1984
|
+
const rawBytes = val.bytes;
|
|
1985
|
+
const addressBytes = typeof rawBytes === "string" ? Buffer.from(rawBytes, "base64") : rawBytes instanceof Uint8Array ? rawBytes : Buffer.from(String(rawBytes), "base64");
|
|
1981
1986
|
if (addressBytes.length === 32) {
|
|
1982
|
-
state[key] = algosdk2.encodeAddress(addressBytes);
|
|
1987
|
+
state[key] = algosdk2.encodeAddress(new Uint8Array(addressBytes));
|
|
1983
1988
|
} else {
|
|
1984
|
-
state[key] =
|
|
1989
|
+
state[key] = val.bytes;
|
|
1985
1990
|
}
|
|
1986
1991
|
} catch {
|
|
1987
|
-
state[key] =
|
|
1992
|
+
state[key] = val.bytes;
|
|
1988
1993
|
}
|
|
1989
1994
|
} else {
|
|
1990
1995
|
try {
|
|
1991
|
-
|
|
1996
|
+
const rawBytes = val.bytes;
|
|
1997
|
+
if (typeof rawBytes === "string") {
|
|
1998
|
+
state[key] = Buffer.from(rawBytes, "base64").toString();
|
|
1999
|
+
} else if (rawBytes instanceof Uint8Array) {
|
|
2000
|
+
state[key] = Buffer.from(rawBytes).toString();
|
|
2001
|
+
} else {
|
|
2002
|
+
state[key] = rawBytes;
|
|
2003
|
+
}
|
|
1992
2004
|
} catch {
|
|
1993
|
-
state[key] =
|
|
2005
|
+
state[key] = val.bytes;
|
|
1994
2006
|
}
|
|
1995
2007
|
}
|
|
1996
2008
|
} else {
|
|
1997
|
-
state[key] = Number(
|
|
2009
|
+
state[key] = Number(val.uint);
|
|
1998
2010
|
}
|
|
1999
2011
|
}
|
|
2000
2012
|
return state;
|
|
2001
2013
|
};
|
|
2002
2014
|
var getMarketGlobalState = async (algodClient, marketAppId) => {
|
|
2003
2015
|
const appInfo = await algodClient.getApplicationByID(marketAppId).do();
|
|
2004
|
-
const rawState = appInfo.params?.
|
|
2016
|
+
const rawState = appInfo.params?.globalState ?? appInfo.params?.["global-state"] ?? [];
|
|
2005
2017
|
return decodeGlobalState(rawState);
|
|
2006
2018
|
};
|
|
2007
2019
|
var getEscrowGlobalState = async (indexerClient, escrowAppId) => {
|
|
2008
2020
|
const appInfo = await indexerClient.lookupApplications(escrowAppId).do();
|
|
2009
|
-
const rawState = appInfo.application?.params?.["global-state"] ?? [];
|
|
2021
|
+
const rawState = appInfo.application?.params?.globalState ?? appInfo.application?.params?.["global-state"] ?? [];
|
|
2010
2022
|
return decodeGlobalState(rawState);
|
|
2011
2023
|
};
|
|
2012
2024
|
var checkAssetOptIn = async (algodClient, address, assetId) => {
|
|
2013
2025
|
try {
|
|
2014
2026
|
const accountInfo = await algodClient.accountInformation(address).do();
|
|
2015
|
-
const assets = accountInfo.assets ||
|
|
2016
|
-
return assets.some((a) => (a["asset-id"]
|
|
2027
|
+
const assets = accountInfo.assets || [];
|
|
2028
|
+
return assets.some((a) => Number(a.assetId ?? a["asset-id"]) === assetId);
|
|
2017
2029
|
} catch {
|
|
2018
2030
|
return false;
|
|
2019
2031
|
}
|
|
@@ -2107,8 +2119,9 @@ var getAllCreatedApplications = async (indexerClient, address, limit = 100) => {
|
|
|
2107
2119
|
if (response.applications?.length) {
|
|
2108
2120
|
applications = [...applications, ...response.applications];
|
|
2109
2121
|
}
|
|
2110
|
-
|
|
2111
|
-
|
|
2122
|
+
const token = response.nextToken ?? response["next-token"];
|
|
2123
|
+
if (token) {
|
|
2124
|
+
nextToken = token;
|
|
2112
2125
|
} else {
|
|
2113
2126
|
hasMore = false;
|
|
2114
2127
|
}
|
|
@@ -2122,7 +2135,7 @@ var fetchApplicationsGlobalState = async (indexerClient, applications) => {
|
|
|
2122
2135
|
try {
|
|
2123
2136
|
const appInfo = await indexerClient.lookupApplications(appId).do();
|
|
2124
2137
|
const globalState = {};
|
|
2125
|
-
const rawGlobalState = appInfo.application?.params?.["global-state"];
|
|
2138
|
+
const rawGlobalState = appInfo.application?.params?.globalState ?? appInfo.application?.params?.["global-state"];
|
|
2126
2139
|
if (rawGlobalState) {
|
|
2127
2140
|
const decoded = decodeGlobalState(rawGlobalState);
|
|
2128
2141
|
Object.assign(globalState, decoded);
|
|
@@ -2141,7 +2154,7 @@ var transformOrders = (orders) => orders.filter((o) => o.appId > 0).map((o) => (
|
|
|
2141
2154
|
owner: o.globalState.owner ?? ""
|
|
2142
2155
|
}));
|
|
2143
2156
|
var getOrderbook = async (config, marketAppId) => {
|
|
2144
|
-
const appAddress = algosdk2.getApplicationAddress(marketAppId);
|
|
2157
|
+
const appAddress = algosdk2.getApplicationAddress(marketAppId).toString();
|
|
2145
2158
|
const applications = await getAllCreatedApplications(config.indexerClient, appAddress);
|
|
2146
2159
|
const appsWithState = await fetchApplicationsGlobalState(config.indexerClient, applications);
|
|
2147
2160
|
const isOpenLimitOrder = (o) => (o.globalState.quantity ?? 0) > (o.globalState.quantity_filled ?? 0) && o.globalState.slippage === 0;
|
|
@@ -2170,7 +2183,7 @@ var getOrderbook = async (config, marketAppId) => {
|
|
|
2170
2183
|
};
|
|
2171
2184
|
var getOpenOrders = async (config, marketAppId, walletAddress) => {
|
|
2172
2185
|
const owner = walletAddress ?? config.activeAddress;
|
|
2173
|
-
const appAddress = algosdk2.getApplicationAddress(marketAppId);
|
|
2186
|
+
const appAddress = algosdk2.getApplicationAddress(marketAppId).toString();
|
|
2174
2187
|
const applications = await getAllCreatedApplications(config.indexerClient, appAddress);
|
|
2175
2188
|
const appsWithState = await fetchApplicationsGlobalState(config.indexerClient, applications);
|
|
2176
2189
|
return appsWithState.filter(
|
|
@@ -2235,8 +2248,10 @@ var getWalletOrdersFromApi = async (config, walletAddress) => {
|
|
|
2235
2248
|
var extractEscrowAppId = async (algodClient, indexerClient, targetTxId) => {
|
|
2236
2249
|
try {
|
|
2237
2250
|
const pendingInfo = await algodClient.pendingTransactionInformation(targetTxId).do();
|
|
2238
|
-
|
|
2239
|
-
|
|
2251
|
+
const innerTxns = pendingInfo.innerTxns ?? pendingInfo["inner-txns"];
|
|
2252
|
+
const createdAppId = innerTxns?.[0]?.applicationIndex ?? innerTxns?.[0]?.["created-application-index"];
|
|
2253
|
+
if (createdAppId) {
|
|
2254
|
+
return Number(createdAppId);
|
|
2240
2255
|
}
|
|
2241
2256
|
} catch {
|
|
2242
2257
|
}
|
|
@@ -2245,8 +2260,11 @@ var extractEscrowAppId = async (algodClient, indexerClient, targetTxId) => {
|
|
|
2245
2260
|
try {
|
|
2246
2261
|
await new Promise((resolve) => setTimeout(resolve, delayMs));
|
|
2247
2262
|
const txnLookup = await indexerClient.lookupTransactionByID(targetTxId).do();
|
|
2248
|
-
|
|
2249
|
-
|
|
2263
|
+
const txn = txnLookup.transaction;
|
|
2264
|
+
const innerTxns = txn?.innerTxns ?? txn?.["inner-txns"];
|
|
2265
|
+
const createdAppId = innerTxns?.[0]?.createdApplicationIndex ?? innerTxns?.[0]?.["created-application-index"];
|
|
2266
|
+
if (createdAppId) {
|
|
2267
|
+
return Number(createdAppId);
|
|
2250
2268
|
}
|
|
2251
2269
|
} catch {
|
|
2252
2270
|
}
|
|
@@ -2314,7 +2332,7 @@ var createOrder = async (config, params) => {
|
|
|
2314
2332
|
if (isBuying) {
|
|
2315
2333
|
fee = calculateFee(quantity, price + slippage, feeBase);
|
|
2316
2334
|
}
|
|
2317
|
-
const marketAddress = getApplicationAddress(marketAppId);
|
|
2335
|
+
const marketAddress = getApplicationAddress(marketAppId).toString();
|
|
2318
2336
|
const atc = new AtomicTransactionComposer();
|
|
2319
2337
|
let createEscrowTxnIndex = 0;
|
|
2320
2338
|
if (!isBuying) {
|
|
@@ -2363,7 +2381,7 @@ var createOrder = async (config, params) => {
|
|
|
2363
2381
|
const payCounterPartyTxn = await algokit4.transferAlgos(
|
|
2364
2382
|
{
|
|
2365
2383
|
from: signerAccount,
|
|
2366
|
-
to: getApplicationAddress(matchingOrder.escrowAppId),
|
|
2384
|
+
to: getApplicationAddress(matchingOrder.escrowAppId).toString(),
|
|
2367
2385
|
amount: algokit4.microAlgos(1e3 * (isBuying ? 1 : 2)),
|
|
2368
2386
|
skipSending: true
|
|
2369
2387
|
},
|
|
@@ -2395,7 +2413,7 @@ var createOrder = async (config, params) => {
|
|
|
2395
2413
|
return {
|
|
2396
2414
|
escrowAppId,
|
|
2397
2415
|
txIds: result.txIDs,
|
|
2398
|
-
confirmedRound: result.confirmedRound
|
|
2416
|
+
confirmedRound: Number(result.confirmedRound)
|
|
2399
2417
|
};
|
|
2400
2418
|
};
|
|
2401
2419
|
var cancelOrder = async (config, params) => {
|
|
@@ -2424,7 +2442,7 @@ var cancelOrder = async (config, params) => {
|
|
|
2424
2442
|
return {
|
|
2425
2443
|
success: true,
|
|
2426
2444
|
txIds: result.txIDs,
|
|
2427
|
-
confirmedRound: result.confirmedRound
|
|
2445
|
+
confirmedRound: Number(result.confirmedRound)
|
|
2428
2446
|
};
|
|
2429
2447
|
};
|
|
2430
2448
|
var proposeMatch = async (config, params) => {
|
|
@@ -2443,7 +2461,7 @@ var proposeMatch = async (config, params) => {
|
|
|
2443
2461
|
const payMakerTxn = await algokit4.transferAlgos(
|
|
2444
2462
|
{
|
|
2445
2463
|
from: signerAccount,
|
|
2446
|
-
to: getApplicationAddress(makerEscrowAppId),
|
|
2464
|
+
to: getApplicationAddress(makerEscrowAppId).toString(),
|
|
2447
2465
|
amount: algokit4.microAlgos(2e3),
|
|
2448
2466
|
skipSending: true
|
|
2449
2467
|
},
|
|
@@ -2471,7 +2489,90 @@ var proposeMatch = async (config, params) => {
|
|
|
2471
2489
|
return {
|
|
2472
2490
|
success: true,
|
|
2473
2491
|
txIds: result.txIDs,
|
|
2474
|
-
confirmedRound: result.confirmedRound
|
|
2492
|
+
confirmedRound: Number(result.confirmedRound)
|
|
2493
|
+
};
|
|
2494
|
+
};
|
|
2495
|
+
var processMatch = async (config, params) => {
|
|
2496
|
+
const { algodClient, signer, activeAddress, usdcAssetId } = config;
|
|
2497
|
+
const { marketAppId, makerEscrowAppId, takerEscrowAppId } = params;
|
|
2498
|
+
const [marketState, makerAppInfo, takerAppInfo] = await Promise.all([
|
|
2499
|
+
getMarketGlobalState(algodClient, marketAppId),
|
|
2500
|
+
algodClient.getApplicationByID(makerEscrowAppId).do(),
|
|
2501
|
+
algodClient.getApplicationByID(takerEscrowAppId).do()
|
|
2502
|
+
]);
|
|
2503
|
+
const marketFeeAddress = marketState.fee_address;
|
|
2504
|
+
const yesAssetId = marketState.yes_asset_id;
|
|
2505
|
+
const noAssetId = marketState.no_asset_id;
|
|
2506
|
+
const makerState = decodeGlobalState(
|
|
2507
|
+
makerAppInfo.params?.globalState ?? makerAppInfo.params?.["global-state"] ?? []
|
|
2508
|
+
);
|
|
2509
|
+
const takerState = decodeGlobalState(
|
|
2510
|
+
takerAppInfo.params?.globalState ?? takerAppInfo.params?.["global-state"] ?? []
|
|
2511
|
+
);
|
|
2512
|
+
const makerOwner = makerState.owner ?? "";
|
|
2513
|
+
const takerOwner = takerState.owner ?? "";
|
|
2514
|
+
const makerRemaining = (makerState.quantity ?? 0) - (makerState.quantity_filled ?? 0);
|
|
2515
|
+
const takerRemaining = (takerState.quantity ?? 0) - (takerState.quantity_filled ?? 0);
|
|
2516
|
+
const matchedQuantity = Math.min(makerRemaining, takerRemaining);
|
|
2517
|
+
if (matchedQuantity <= 0) {
|
|
2518
|
+
throw new Error("processMatch: no quantity left to match on maker or taker escrow.");
|
|
2519
|
+
}
|
|
2520
|
+
const signerAccount = { signer, addr: activeAddress };
|
|
2521
|
+
const makerEscrowClient = new EscrowAppClient(
|
|
2522
|
+
{ resolveBy: "id", id: makerEscrowAppId, sender: signerAccount },
|
|
2523
|
+
algodClient
|
|
2524
|
+
);
|
|
2525
|
+
const takerEscrowClient = new EscrowAppClient(
|
|
2526
|
+
{ resolveBy: "id", id: takerEscrowAppId, sender: signerAccount },
|
|
2527
|
+
algodClient
|
|
2528
|
+
);
|
|
2529
|
+
const marketClient = new MarketAppClient(
|
|
2530
|
+
{ resolveBy: "id", id: marketAppId, sender: signerAccount },
|
|
2531
|
+
algodClient
|
|
2532
|
+
);
|
|
2533
|
+
const atc = new AtomicTransactionComposer();
|
|
2534
|
+
const assets = [usdcAssetId, yesAssetId, noAssetId];
|
|
2535
|
+
const sendParamsWithFee = { skipSending: true, fee: algokit4.microAlgos(3e3) };
|
|
2536
|
+
const sendParamsNoop = { skipSending: true, fee: algokit4.microAlgos(1e3) };
|
|
2537
|
+
const matchMakerTxn = await makerEscrowClient.matchMaker(
|
|
2538
|
+
{ taker: takerEscrowAppId, matchQuantity: matchedQuantity },
|
|
2539
|
+
{
|
|
2540
|
+
assets,
|
|
2541
|
+
apps: [marketAppId],
|
|
2542
|
+
accounts: [takerOwner, marketFeeAddress],
|
|
2543
|
+
sendParams: sendParamsWithFee
|
|
2544
|
+
}
|
|
2545
|
+
);
|
|
2546
|
+
atc.addTransaction({ txn: matchMakerTxn.transaction, signer });
|
|
2547
|
+
const matchTakerTxn = await takerEscrowClient.matchTaker(
|
|
2548
|
+
{ maker: makerEscrowAppId },
|
|
2549
|
+
{
|
|
2550
|
+
assets,
|
|
2551
|
+
apps: [marketAppId],
|
|
2552
|
+
accounts: [makerOwner, marketFeeAddress],
|
|
2553
|
+
sendParams: sendParamsWithFee
|
|
2554
|
+
}
|
|
2555
|
+
);
|
|
2556
|
+
atc.addTransaction({ txn: matchTakerTxn.transaction, signer });
|
|
2557
|
+
const processMatchTxn = await marketClient.processPotentialMatch(
|
|
2558
|
+
{ maker: makerEscrowAppId, taker: takerEscrowAppId },
|
|
2559
|
+
{
|
|
2560
|
+
assets,
|
|
2561
|
+
accounts: [makerOwner, takerOwner, marketFeeAddress],
|
|
2562
|
+
sendParams: sendParamsNoop
|
|
2563
|
+
}
|
|
2564
|
+
);
|
|
2565
|
+
atc.addTransaction({ txn: processMatchTxn.transaction, signer });
|
|
2566
|
+
const doNoopTxn = await marketClient.doNoop(
|
|
2567
|
+
{ callNumber: 1 },
|
|
2568
|
+
{ sendParams: sendParamsNoop }
|
|
2569
|
+
);
|
|
2570
|
+
atc.addTransaction({ txn: doNoopTxn.transaction, signer });
|
|
2571
|
+
const result = await atc.execute(algodClient, 4);
|
|
2572
|
+
return {
|
|
2573
|
+
success: true,
|
|
2574
|
+
txIds: result.txIDs,
|
|
2575
|
+
confirmedRound: Number(result.confirmedRound)
|
|
2475
2576
|
};
|
|
2476
2577
|
};
|
|
2477
2578
|
var amendOrder = async (config, params) => {
|
|
@@ -2479,7 +2580,7 @@ var amendOrder = async (config, params) => {
|
|
|
2479
2580
|
const { marketAppId, escrowAppId, price, quantity, slippage = 0 } = params;
|
|
2480
2581
|
const escrowAppInfo = await algodClient.getApplicationByID(escrowAppId).do();
|
|
2481
2582
|
const escrowState = decodeGlobalState(
|
|
2482
|
-
escrowAppInfo.params?.
|
|
2583
|
+
escrowAppInfo.params?.globalState ?? escrowAppInfo.params?.["global-state"] ?? []
|
|
2483
2584
|
);
|
|
2484
2585
|
if ((escrowState.quantity_filled ?? 0) > 0) {
|
|
2485
2586
|
throw new Error("Cannot amend an order that has been partially or fully filled.");
|
|
@@ -2530,10 +2631,47 @@ var amendOrder = async (config, params) => {
|
|
|
2530
2631
|
);
|
|
2531
2632
|
atc.addTransaction({ txn: amendCall.transaction, signer });
|
|
2532
2633
|
const result = await atc.execute(algodClient, 4);
|
|
2634
|
+
const allTxIds = [...result.txIDs];
|
|
2635
|
+
let lastConfirmedRound = Number(result.confirmedRound);
|
|
2636
|
+
const orderbook = await getOrderbook(config, marketAppId);
|
|
2637
|
+
let matchingOrders = calculateMatchingOrders(orderbook, isBuy, position === 1, quantity, price, slippage);
|
|
2638
|
+
matchingOrders = matchingOrders.filter((m) => m.escrowAppId !== escrowAppId);
|
|
2639
|
+
if (matchingOrders.length > 0) {
|
|
2640
|
+
let quantityLeft = quantity;
|
|
2641
|
+
const matchedQuantities = [];
|
|
2642
|
+
const matchedPrices = [];
|
|
2643
|
+
for (const m of matchingOrders) {
|
|
2644
|
+
if (quantityLeft <= 0) break;
|
|
2645
|
+
try {
|
|
2646
|
+
const matchResult = await processMatch(config, {
|
|
2647
|
+
marketAppId,
|
|
2648
|
+
makerEscrowAppId: m.escrowAppId,
|
|
2649
|
+
takerEscrowAppId: escrowAppId
|
|
2650
|
+
});
|
|
2651
|
+
allTxIds.push(...matchResult.txIds);
|
|
2652
|
+
lastConfirmedRound = matchResult.confirmedRound;
|
|
2653
|
+
const q = Math.min(m.quantity, quantityLeft);
|
|
2654
|
+
matchedQuantities.push(q);
|
|
2655
|
+
matchedPrices.push((m.price ?? price) * q);
|
|
2656
|
+
quantityLeft -= q;
|
|
2657
|
+
} catch (err) {
|
|
2658
|
+
console.log(`Error matching order: ${JSON.stringify(err)}`);
|
|
2659
|
+
break;
|
|
2660
|
+
}
|
|
2661
|
+
}
|
|
2662
|
+
const totalMatchedQuantity = matchedQuantities.reduce((a, b) => a + b, 0);
|
|
2663
|
+
const matchedPrice = totalMatchedQuantity > 0 ? Math.round(matchedPrices.reduce((a, b) => a + b, 0) / totalMatchedQuantity) : void 0;
|
|
2664
|
+
return {
|
|
2665
|
+
success: true,
|
|
2666
|
+
txIds: allTxIds,
|
|
2667
|
+
confirmedRound: lastConfirmedRound,
|
|
2668
|
+
...totalMatchedQuantity > 0 && { matchedQuantity: totalMatchedQuantity, matchedPrice }
|
|
2669
|
+
};
|
|
2670
|
+
}
|
|
2533
2671
|
return {
|
|
2534
2672
|
success: true,
|
|
2535
|
-
txIds:
|
|
2536
|
-
confirmedRound:
|
|
2673
|
+
txIds: allTxIds,
|
|
2674
|
+
confirmedRound: lastConfirmedRound
|
|
2537
2675
|
};
|
|
2538
2676
|
};
|
|
2539
2677
|
var splitShares = async (config, params) => {
|
|
@@ -2547,7 +2685,7 @@ var splitShares = async (config, params) => {
|
|
|
2547
2685
|
{ resolveBy: "id", id: marketAppId, sender: signerAccount },
|
|
2548
2686
|
algodClient
|
|
2549
2687
|
);
|
|
2550
|
-
const marketAddress = getApplicationAddress(marketAppId);
|
|
2688
|
+
const marketAddress = getApplicationAddress(marketAppId).toString();
|
|
2551
2689
|
const atc = new AtomicTransactionComposer();
|
|
2552
2690
|
let optInCosts = 0;
|
|
2553
2691
|
const hasYesOptIn = await checkAssetOptIn(algodClient, activeAddress, yesAssetId);
|
|
@@ -2587,7 +2725,7 @@ var splitShares = async (config, params) => {
|
|
|
2587
2725
|
return {
|
|
2588
2726
|
success: true,
|
|
2589
2727
|
txIds: result.txIDs,
|
|
2590
|
-
confirmedRound: result.confirmedRound
|
|
2728
|
+
confirmedRound: Number(result.confirmedRound)
|
|
2591
2729
|
};
|
|
2592
2730
|
};
|
|
2593
2731
|
var mergeShares = async (config, params) => {
|
|
@@ -2601,7 +2739,7 @@ var mergeShares = async (config, params) => {
|
|
|
2601
2739
|
{ resolveBy: "id", id: marketAppId, sender: signerAccount },
|
|
2602
2740
|
algodClient
|
|
2603
2741
|
);
|
|
2604
|
-
const marketAddress = getApplicationAddress(marketAppId);
|
|
2742
|
+
const marketAddress = getApplicationAddress(marketAppId).toString();
|
|
2605
2743
|
const atc = new AtomicTransactionComposer();
|
|
2606
2744
|
let optInCosts = 0;
|
|
2607
2745
|
const hasUsdcOptIn = await checkAssetOptIn(algodClient, activeAddress, usdcAssetId);
|
|
@@ -2637,7 +2775,7 @@ var mergeShares = async (config, params) => {
|
|
|
2637
2775
|
return {
|
|
2638
2776
|
success: true,
|
|
2639
2777
|
txIds: result.txIDs,
|
|
2640
|
-
confirmedRound: result.confirmedRound
|
|
2778
|
+
confirmedRound: Number(result.confirmedRound)
|
|
2641
2779
|
};
|
|
2642
2780
|
};
|
|
2643
2781
|
var claim = async (config, params) => {
|
|
@@ -2648,12 +2786,12 @@ var claim = async (config, params) => {
|
|
|
2648
2786
|
{ resolveBy: "id", id: marketAppId, sender: signerAccount },
|
|
2649
2787
|
algodClient
|
|
2650
2788
|
);
|
|
2651
|
-
const marketAddress = getApplicationAddress(marketAppId);
|
|
2789
|
+
const marketAddress = getApplicationAddress(marketAppId).toString();
|
|
2652
2790
|
let tokenBalance = params.amount;
|
|
2653
2791
|
if (!tokenBalance) {
|
|
2654
2792
|
const accountInfo = await algodClient.accountInformation(activeAddress).do();
|
|
2655
|
-
const assets = accountInfo.assets ||
|
|
2656
|
-
const asset = assets.find((a) => (a["asset-id"]
|
|
2793
|
+
const assets = accountInfo.assets || [];
|
|
2794
|
+
const asset = assets.find((a) => Number(a.assetId ?? a["asset-id"]) === assetId);
|
|
2657
2795
|
tokenBalance = asset ? Number(asset.amount) : 0;
|
|
2658
2796
|
}
|
|
2659
2797
|
if (tokenBalance <= 0) {
|
|
@@ -2675,8 +2813,8 @@ var claim = async (config, params) => {
|
|
|
2675
2813
|
atc.addTransaction({ txn: claimTxn.transaction, signer });
|
|
2676
2814
|
const sp = await algodClient.getTransactionParams().do();
|
|
2677
2815
|
const closeOutTxn = algosdk2.makeAssetTransferTxnWithSuggestedParamsFromObject({
|
|
2678
|
-
|
|
2679
|
-
|
|
2816
|
+
sender: activeAddress,
|
|
2817
|
+
receiver: marketAddress,
|
|
2680
2818
|
amount: 0,
|
|
2681
2819
|
assetIndex: assetId,
|
|
2682
2820
|
closeRemainderTo: marketAddress,
|
|
@@ -2687,7 +2825,7 @@ var claim = async (config, params) => {
|
|
|
2687
2825
|
return {
|
|
2688
2826
|
success: true,
|
|
2689
2827
|
txIds: result.txIDs,
|
|
2690
|
-
confirmedRound: result.confirmedRound,
|
|
2828
|
+
confirmedRound: Number(result.confirmedRound),
|
|
2691
2829
|
amountClaimed: tokenBalance
|
|
2692
2830
|
};
|
|
2693
2831
|
};
|
|
@@ -2695,17 +2833,17 @@ var getPositions = async (config, walletAddress) => {
|
|
|
2695
2833
|
const { algodClient, indexerClient } = config;
|
|
2696
2834
|
const address = walletAddress ?? config.activeAddress;
|
|
2697
2835
|
const accountInfo = await algodClient.accountInformation(address).do();
|
|
2698
|
-
const assets = accountInfo.assets ||
|
|
2836
|
+
const assets = accountInfo.assets || [];
|
|
2699
2837
|
const nonZeroAssets = assets.filter((a) => Number(a.amount) > 0);
|
|
2700
2838
|
if (nonZeroAssets.length === 0) return [];
|
|
2701
2839
|
const positions = /* @__PURE__ */ new Map();
|
|
2702
2840
|
for (const asset of nonZeroAssets) {
|
|
2703
|
-
const assetId = asset["asset-id"]
|
|
2841
|
+
const assetId = Number(asset.assetId ?? asset["asset-id"]);
|
|
2704
2842
|
const amount = Number(asset.amount);
|
|
2705
2843
|
try {
|
|
2706
2844
|
const assetInfo = await indexerClient.lookupAssetByID(assetId).do();
|
|
2707
2845
|
const assetName = assetInfo.asset?.params?.name ?? "";
|
|
2708
|
-
const unitName = assetInfo.asset?.params?.["unit-name"] ?? "";
|
|
2846
|
+
const unitName = assetInfo.asset?.params?.unitName ?? assetInfo.asset?.params?.["unit-name"] ?? "";
|
|
2709
2847
|
if (!unitName.startsWith("ALPHA-")) continue;
|
|
2710
2848
|
const match = assetName.match(/^Alpha Market (\d+) (Yes|No)$/);
|
|
2711
2849
|
if (!match) continue;
|
|
@@ -2721,16 +2859,24 @@ var getPositions = async (config, walletAddress) => {
|
|
|
2721
2859
|
} else {
|
|
2722
2860
|
try {
|
|
2723
2861
|
const appInfo = await indexerClient.lookupApplications(marketAppId).do();
|
|
2724
|
-
const rawState = appInfo.application?.params?.["global-state"];
|
|
2862
|
+
const rawState = appInfo.application?.params?.globalState ?? appInfo.application?.params?.["global-state"];
|
|
2725
2863
|
if (!rawState) continue;
|
|
2726
2864
|
let yesAssetIdOnChain = 0;
|
|
2727
2865
|
let noAssetIdOnChain = 0;
|
|
2728
2866
|
let marketTitle = "";
|
|
2729
2867
|
for (const item of rawState) {
|
|
2730
|
-
const
|
|
2868
|
+
const rawKey = item.key;
|
|
2869
|
+
const key = typeof rawKey === "string" ? Buffer.from(rawKey, "base64").toString() : rawKey instanceof Uint8Array ? Buffer.from(rawKey).toString() : String(rawKey);
|
|
2731
2870
|
if (key === "yes_asset_id") yesAssetIdOnChain = Number(item.value.uint);
|
|
2732
2871
|
if (key === "no_asset_id") noAssetIdOnChain = Number(item.value.uint);
|
|
2733
|
-
if (key === "title")
|
|
2872
|
+
if (key === "title") {
|
|
2873
|
+
const rawBytes = item.value.bytes;
|
|
2874
|
+
if (typeof rawBytes === "string") {
|
|
2875
|
+
marketTitle = Buffer.from(rawBytes, "base64").toString();
|
|
2876
|
+
} else if (rawBytes instanceof Uint8Array) {
|
|
2877
|
+
marketTitle = Buffer.from(rawBytes).toString();
|
|
2878
|
+
}
|
|
2879
|
+
}
|
|
2734
2880
|
}
|
|
2735
2881
|
if (yesAssetIdOnChain === 0 && noAssetIdOnChain === 0) continue;
|
|
2736
2882
|
positions.set(marketAppId, {
|
|
@@ -2811,8 +2957,9 @@ var getMarketsOnChain = async (config, options) => {
|
|
|
2811
2957
|
if (response.applications?.length) {
|
|
2812
2958
|
allApps.push(...response.applications);
|
|
2813
2959
|
}
|
|
2814
|
-
|
|
2815
|
-
|
|
2960
|
+
const token = response.nextToken ?? response["next-token"];
|
|
2961
|
+
if (token) {
|
|
2962
|
+
nextToken = token;
|
|
2816
2963
|
} else {
|
|
2817
2964
|
hasMore = false;
|
|
2818
2965
|
}
|
|
@@ -2820,7 +2967,7 @@ var getMarketsOnChain = async (config, options) => {
|
|
|
2820
2967
|
const flatMarkets = [];
|
|
2821
2968
|
for (const app of allApps) {
|
|
2822
2969
|
if (app.deleted) continue;
|
|
2823
|
-
const rawState = app.params?.["global-state"];
|
|
2970
|
+
const rawState = app.params?.globalState ?? app.params?.["global-state"];
|
|
2824
2971
|
if (!rawState) continue;
|
|
2825
2972
|
const state = decodeGlobalState(rawState);
|
|
2826
2973
|
if (activeOnly && !state.is_activated) continue;
|
|
@@ -2846,7 +2993,7 @@ var getMarketOnChain = async (config, marketAppId) => {
|
|
|
2846
2993
|
try {
|
|
2847
2994
|
const appId = typeof marketAppId === "string" ? Number(marketAppId) : marketAppId;
|
|
2848
2995
|
const appInfo = await config.algodClient.getApplicationByID(appId).do();
|
|
2849
|
-
const rawState = appInfo.params?.
|
|
2996
|
+
const rawState = appInfo.params?.globalState ?? appInfo.params?.["global-state"] ?? [];
|
|
2850
2997
|
const state = decodeGlobalState(rawState);
|
|
2851
2998
|
return {
|
|
2852
2999
|
id: String(appId),
|
|
@@ -3007,6 +3154,19 @@ var AlphaClient = class {
|
|
|
3007
3154
|
async proposeMatch(params) {
|
|
3008
3155
|
return proposeMatch(this.config, params);
|
|
3009
3156
|
}
|
|
3157
|
+
/**
|
|
3158
|
+
* Matches two existing limit orders (no create-escrow in the group).
|
|
3159
|
+
*
|
|
3160
|
+
* Calls the market app's process_potential_match(maker, taker). Use this
|
|
3161
|
+
* after amending an order: the amended order is the taker (pays the fee),
|
|
3162
|
+
* the counterparty is the maker.
|
|
3163
|
+
*
|
|
3164
|
+
* @param params - Process match params (marketAppId, makerEscrowAppId, takerEscrowAppId)
|
|
3165
|
+
* @returns Whether the match succeeded
|
|
3166
|
+
*/
|
|
3167
|
+
async processMatch(params) {
|
|
3168
|
+
return processMatch(this.config, params);
|
|
3169
|
+
}
|
|
3010
3170
|
/**
|
|
3011
3171
|
* Amends (edits) an existing unfilled order in-place.
|
|
3012
3172
|
*
|