@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/README.md
CHANGED
|
@@ -49,7 +49,7 @@ const client = new AlphaClient({
|
|
|
49
49
|
algodClient,
|
|
50
50
|
indexerClient,
|
|
51
51
|
signer,
|
|
52
|
-
activeAddress: account.addr,
|
|
52
|
+
activeAddress: account.addr.toString(),
|
|
53
53
|
matcherAppId: 3078581851,
|
|
54
54
|
usdcAssetId: 31566704,
|
|
55
55
|
});
|
|
@@ -380,7 +380,7 @@ const setup = () => {
|
|
|
380
380
|
algodClient,
|
|
381
381
|
indexerClient,
|
|
382
382
|
signer: algosdk.makeBasicAccountTransactionSigner(account),
|
|
383
|
-
activeAddress: account.addr,
|
|
383
|
+
activeAddress: account.addr.toString(),
|
|
384
384
|
matcherAppId: 3078581851,
|
|
385
385
|
usdcAssetId: 31566704,
|
|
386
386
|
});
|
package/dist/index.cjs
CHANGED
|
@@ -4,8 +4,6 @@ var algosdk2 = require('algosdk');
|
|
|
4
4
|
var algokit4 = require('@algorandfoundation/algokit-utils');
|
|
5
5
|
var decimal_js = require('decimal.js');
|
|
6
6
|
|
|
7
|
-
function _interopDefault (e) { return e && e.__esModule ? e : { default: e }; }
|
|
8
|
-
|
|
9
7
|
function _interopNamespace(e) {
|
|
10
8
|
if (e && e.__esModule) return e;
|
|
11
9
|
var n = Object.create(null);
|
|
@@ -24,7 +22,7 @@ function _interopNamespace(e) {
|
|
|
24
22
|
return Object.freeze(n);
|
|
25
23
|
}
|
|
26
24
|
|
|
27
|
-
var
|
|
25
|
+
var algosdk2__namespace = /*#__PURE__*/_interopNamespace(algosdk2);
|
|
28
26
|
var algokit4__namespace = /*#__PURE__*/_interopNamespace(algokit4);
|
|
29
27
|
|
|
30
28
|
// src/modules/trading.ts
|
|
@@ -1998,47 +1996,58 @@ var calculateFeeFromTotal = (totalAmount, price, feeBase) => {
|
|
|
1998
1996
|
var decodeGlobalState = (rawState) => {
|
|
1999
1997
|
const state = {};
|
|
2000
1998
|
for (const item of rawState) {
|
|
2001
|
-
const
|
|
2002
|
-
|
|
1999
|
+
const rawKey = item.key;
|
|
2000
|
+
const key = typeof rawKey === "string" ? Buffer.from(rawKey, "base64").toString() : rawKey instanceof Uint8Array ? Buffer.from(rawKey).toString() : String(rawKey);
|
|
2001
|
+
const val = item.value;
|
|
2002
|
+
const type = val.type;
|
|
2003
|
+
if (type === 1) {
|
|
2003
2004
|
if (key === "owner" || key === "oracle_address" || key === "fee_address" || key === "market_friend_addr" || key === "escrow_cancel_address") {
|
|
2004
2005
|
try {
|
|
2005
|
-
const
|
|
2006
|
+
const rawBytes = val.bytes;
|
|
2007
|
+
const addressBytes = typeof rawBytes === "string" ? Buffer.from(rawBytes, "base64") : rawBytes instanceof Uint8Array ? rawBytes : Buffer.from(String(rawBytes), "base64");
|
|
2006
2008
|
if (addressBytes.length === 32) {
|
|
2007
|
-
state[key] =
|
|
2009
|
+
state[key] = algosdk2__namespace.encodeAddress(new Uint8Array(addressBytes));
|
|
2008
2010
|
} else {
|
|
2009
|
-
state[key] =
|
|
2011
|
+
state[key] = val.bytes;
|
|
2010
2012
|
}
|
|
2011
2013
|
} catch {
|
|
2012
|
-
state[key] =
|
|
2014
|
+
state[key] = val.bytes;
|
|
2013
2015
|
}
|
|
2014
2016
|
} else {
|
|
2015
2017
|
try {
|
|
2016
|
-
|
|
2018
|
+
const rawBytes = val.bytes;
|
|
2019
|
+
if (typeof rawBytes === "string") {
|
|
2020
|
+
state[key] = Buffer.from(rawBytes, "base64").toString();
|
|
2021
|
+
} else if (rawBytes instanceof Uint8Array) {
|
|
2022
|
+
state[key] = Buffer.from(rawBytes).toString();
|
|
2023
|
+
} else {
|
|
2024
|
+
state[key] = rawBytes;
|
|
2025
|
+
}
|
|
2017
2026
|
} catch {
|
|
2018
|
-
state[key] =
|
|
2027
|
+
state[key] = val.bytes;
|
|
2019
2028
|
}
|
|
2020
2029
|
}
|
|
2021
2030
|
} else {
|
|
2022
|
-
state[key] = Number(
|
|
2031
|
+
state[key] = Number(val.uint);
|
|
2023
2032
|
}
|
|
2024
2033
|
}
|
|
2025
2034
|
return state;
|
|
2026
2035
|
};
|
|
2027
2036
|
var getMarketGlobalState = async (algodClient, marketAppId) => {
|
|
2028
2037
|
const appInfo = await algodClient.getApplicationByID(marketAppId).do();
|
|
2029
|
-
const rawState = appInfo.params?.
|
|
2038
|
+
const rawState = appInfo.params?.globalState ?? appInfo.params?.["global-state"] ?? [];
|
|
2030
2039
|
return decodeGlobalState(rawState);
|
|
2031
2040
|
};
|
|
2032
2041
|
var getEscrowGlobalState = async (indexerClient, escrowAppId) => {
|
|
2033
2042
|
const appInfo = await indexerClient.lookupApplications(escrowAppId).do();
|
|
2034
|
-
const rawState = appInfo.application?.params?.["global-state"] ?? [];
|
|
2043
|
+
const rawState = appInfo.application?.params?.globalState ?? appInfo.application?.params?.["global-state"] ?? [];
|
|
2035
2044
|
return decodeGlobalState(rawState);
|
|
2036
2045
|
};
|
|
2037
2046
|
var checkAssetOptIn = async (algodClient, address, assetId) => {
|
|
2038
2047
|
try {
|
|
2039
2048
|
const accountInfo = await algodClient.accountInformation(address).do();
|
|
2040
|
-
const assets = accountInfo.assets ||
|
|
2041
|
-
return assets.some((a) => (a["asset-id"]
|
|
2049
|
+
const assets = accountInfo.assets || [];
|
|
2050
|
+
return assets.some((a) => Number(a.assetId ?? a["asset-id"]) === assetId);
|
|
2042
2051
|
} catch {
|
|
2043
2052
|
return false;
|
|
2044
2053
|
}
|
|
@@ -2132,8 +2141,9 @@ var getAllCreatedApplications = async (indexerClient, address, limit = 100) => {
|
|
|
2132
2141
|
if (response.applications?.length) {
|
|
2133
2142
|
applications = [...applications, ...response.applications];
|
|
2134
2143
|
}
|
|
2135
|
-
|
|
2136
|
-
|
|
2144
|
+
const token = response.nextToken ?? response["next-token"];
|
|
2145
|
+
if (token) {
|
|
2146
|
+
nextToken = token;
|
|
2137
2147
|
} else {
|
|
2138
2148
|
hasMore = false;
|
|
2139
2149
|
}
|
|
@@ -2147,7 +2157,7 @@ var fetchApplicationsGlobalState = async (indexerClient, applications) => {
|
|
|
2147
2157
|
try {
|
|
2148
2158
|
const appInfo = await indexerClient.lookupApplications(appId).do();
|
|
2149
2159
|
const globalState = {};
|
|
2150
|
-
const rawGlobalState = appInfo.application?.params?.["global-state"];
|
|
2160
|
+
const rawGlobalState = appInfo.application?.params?.globalState ?? appInfo.application?.params?.["global-state"];
|
|
2151
2161
|
if (rawGlobalState) {
|
|
2152
2162
|
const decoded = decodeGlobalState(rawGlobalState);
|
|
2153
2163
|
Object.assign(globalState, decoded);
|
|
@@ -2166,7 +2176,7 @@ var transformOrders = (orders) => orders.filter((o) => o.appId > 0).map((o) => (
|
|
|
2166
2176
|
owner: o.globalState.owner ?? ""
|
|
2167
2177
|
}));
|
|
2168
2178
|
var getOrderbook = async (config, marketAppId) => {
|
|
2169
|
-
const appAddress =
|
|
2179
|
+
const appAddress = algosdk2__namespace.getApplicationAddress(marketAppId).toString();
|
|
2170
2180
|
const applications = await getAllCreatedApplications(config.indexerClient, appAddress);
|
|
2171
2181
|
const appsWithState = await fetchApplicationsGlobalState(config.indexerClient, applications);
|
|
2172
2182
|
const isOpenLimitOrder = (o) => (o.globalState.quantity ?? 0) > (o.globalState.quantity_filled ?? 0) && o.globalState.slippage === 0;
|
|
@@ -2195,7 +2205,7 @@ var getOrderbook = async (config, marketAppId) => {
|
|
|
2195
2205
|
};
|
|
2196
2206
|
var getOpenOrders = async (config, marketAppId, walletAddress) => {
|
|
2197
2207
|
const owner = walletAddress ?? config.activeAddress;
|
|
2198
|
-
const appAddress =
|
|
2208
|
+
const appAddress = algosdk2__namespace.getApplicationAddress(marketAppId).toString();
|
|
2199
2209
|
const applications = await getAllCreatedApplications(config.indexerClient, appAddress);
|
|
2200
2210
|
const appsWithState = await fetchApplicationsGlobalState(config.indexerClient, applications);
|
|
2201
2211
|
return appsWithState.filter(
|
|
@@ -2260,8 +2270,10 @@ var getWalletOrdersFromApi = async (config, walletAddress) => {
|
|
|
2260
2270
|
var extractEscrowAppId = async (algodClient, indexerClient, targetTxId) => {
|
|
2261
2271
|
try {
|
|
2262
2272
|
const pendingInfo = await algodClient.pendingTransactionInformation(targetTxId).do();
|
|
2263
|
-
|
|
2264
|
-
|
|
2273
|
+
const innerTxns = pendingInfo.innerTxns ?? pendingInfo["inner-txns"];
|
|
2274
|
+
const createdAppId = innerTxns?.[0]?.applicationIndex ?? innerTxns?.[0]?.["created-application-index"];
|
|
2275
|
+
if (createdAppId) {
|
|
2276
|
+
return Number(createdAppId);
|
|
2265
2277
|
}
|
|
2266
2278
|
} catch {
|
|
2267
2279
|
}
|
|
@@ -2270,8 +2282,11 @@ var extractEscrowAppId = async (algodClient, indexerClient, targetTxId) => {
|
|
|
2270
2282
|
try {
|
|
2271
2283
|
await new Promise((resolve) => setTimeout(resolve, delayMs));
|
|
2272
2284
|
const txnLookup = await indexerClient.lookupTransactionByID(targetTxId).do();
|
|
2273
|
-
|
|
2274
|
-
|
|
2285
|
+
const txn = txnLookup.transaction;
|
|
2286
|
+
const innerTxns = txn?.innerTxns ?? txn?.["inner-txns"];
|
|
2287
|
+
const createdAppId = innerTxns?.[0]?.createdApplicationIndex ?? innerTxns?.[0]?.["created-application-index"];
|
|
2288
|
+
if (createdAppId) {
|
|
2289
|
+
return Number(createdAppId);
|
|
2275
2290
|
}
|
|
2276
2291
|
} catch {
|
|
2277
2292
|
}
|
|
@@ -2339,7 +2354,7 @@ var createOrder = async (config, params) => {
|
|
|
2339
2354
|
if (isBuying) {
|
|
2340
2355
|
fee = calculateFee(quantity, price + slippage, feeBase);
|
|
2341
2356
|
}
|
|
2342
|
-
const marketAddress = algosdk2.getApplicationAddress(marketAppId);
|
|
2357
|
+
const marketAddress = algosdk2.getApplicationAddress(marketAppId).toString();
|
|
2343
2358
|
const atc = new algosdk2.AtomicTransactionComposer();
|
|
2344
2359
|
let createEscrowTxnIndex = 0;
|
|
2345
2360
|
if (!isBuying) {
|
|
@@ -2388,7 +2403,7 @@ var createOrder = async (config, params) => {
|
|
|
2388
2403
|
const payCounterPartyTxn = await algokit4__namespace.transferAlgos(
|
|
2389
2404
|
{
|
|
2390
2405
|
from: signerAccount,
|
|
2391
|
-
to: algosdk2.getApplicationAddress(matchingOrder.escrowAppId),
|
|
2406
|
+
to: algosdk2.getApplicationAddress(matchingOrder.escrowAppId).toString(),
|
|
2392
2407
|
amount: algokit4__namespace.microAlgos(1e3 * (isBuying ? 1 : 2)),
|
|
2393
2408
|
skipSending: true
|
|
2394
2409
|
},
|
|
@@ -2420,7 +2435,7 @@ var createOrder = async (config, params) => {
|
|
|
2420
2435
|
return {
|
|
2421
2436
|
escrowAppId,
|
|
2422
2437
|
txIds: result.txIDs,
|
|
2423
|
-
confirmedRound: result.confirmedRound
|
|
2438
|
+
confirmedRound: Number(result.confirmedRound)
|
|
2424
2439
|
};
|
|
2425
2440
|
};
|
|
2426
2441
|
var cancelOrder = async (config, params) => {
|
|
@@ -2449,7 +2464,7 @@ var cancelOrder = async (config, params) => {
|
|
|
2449
2464
|
return {
|
|
2450
2465
|
success: true,
|
|
2451
2466
|
txIds: result.txIDs,
|
|
2452
|
-
confirmedRound: result.confirmedRound
|
|
2467
|
+
confirmedRound: Number(result.confirmedRound)
|
|
2453
2468
|
};
|
|
2454
2469
|
};
|
|
2455
2470
|
var proposeMatch = async (config, params) => {
|
|
@@ -2468,7 +2483,7 @@ var proposeMatch = async (config, params) => {
|
|
|
2468
2483
|
const payMakerTxn = await algokit4__namespace.transferAlgos(
|
|
2469
2484
|
{
|
|
2470
2485
|
from: signerAccount,
|
|
2471
|
-
to: algosdk2.getApplicationAddress(makerEscrowAppId),
|
|
2486
|
+
to: algosdk2.getApplicationAddress(makerEscrowAppId).toString(),
|
|
2472
2487
|
amount: algokit4__namespace.microAlgos(2e3),
|
|
2473
2488
|
skipSending: true
|
|
2474
2489
|
},
|
|
@@ -2496,7 +2511,90 @@ var proposeMatch = async (config, params) => {
|
|
|
2496
2511
|
return {
|
|
2497
2512
|
success: true,
|
|
2498
2513
|
txIds: result.txIDs,
|
|
2499
|
-
confirmedRound: result.confirmedRound
|
|
2514
|
+
confirmedRound: Number(result.confirmedRound)
|
|
2515
|
+
};
|
|
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)
|
|
2500
2598
|
};
|
|
2501
2599
|
};
|
|
2502
2600
|
var amendOrder = async (config, params) => {
|
|
@@ -2504,7 +2602,7 @@ var amendOrder = async (config, params) => {
|
|
|
2504
2602
|
const { marketAppId, escrowAppId, price, quantity, slippage = 0 } = params;
|
|
2505
2603
|
const escrowAppInfo = await algodClient.getApplicationByID(escrowAppId).do();
|
|
2506
2604
|
const escrowState = decodeGlobalState(
|
|
2507
|
-
escrowAppInfo.params?.
|
|
2605
|
+
escrowAppInfo.params?.globalState ?? escrowAppInfo.params?.["global-state"] ?? []
|
|
2508
2606
|
);
|
|
2509
2607
|
if ((escrowState.quantity_filled ?? 0) > 0) {
|
|
2510
2608
|
throw new Error("Cannot amend an order that has been partially or fully filled.");
|
|
@@ -2555,10 +2653,47 @@ var amendOrder = async (config, params) => {
|
|
|
2555
2653
|
);
|
|
2556
2654
|
atc.addTransaction({ txn: amendCall.transaction, signer });
|
|
2557
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
|
+
}
|
|
2558
2693
|
return {
|
|
2559
2694
|
success: true,
|
|
2560
|
-
txIds:
|
|
2561
|
-
confirmedRound:
|
|
2695
|
+
txIds: allTxIds,
|
|
2696
|
+
confirmedRound: lastConfirmedRound
|
|
2562
2697
|
};
|
|
2563
2698
|
};
|
|
2564
2699
|
var splitShares = async (config, params) => {
|
|
@@ -2572,7 +2707,7 @@ var splitShares = async (config, params) => {
|
|
|
2572
2707
|
{ resolveBy: "id", id: marketAppId, sender: signerAccount },
|
|
2573
2708
|
algodClient
|
|
2574
2709
|
);
|
|
2575
|
-
const marketAddress = algosdk2.getApplicationAddress(marketAppId);
|
|
2710
|
+
const marketAddress = algosdk2.getApplicationAddress(marketAppId).toString();
|
|
2576
2711
|
const atc = new algosdk2.AtomicTransactionComposer();
|
|
2577
2712
|
let optInCosts = 0;
|
|
2578
2713
|
const hasYesOptIn = await checkAssetOptIn(algodClient, activeAddress, yesAssetId);
|
|
@@ -2612,7 +2747,7 @@ var splitShares = async (config, params) => {
|
|
|
2612
2747
|
return {
|
|
2613
2748
|
success: true,
|
|
2614
2749
|
txIds: result.txIDs,
|
|
2615
|
-
confirmedRound: result.confirmedRound
|
|
2750
|
+
confirmedRound: Number(result.confirmedRound)
|
|
2616
2751
|
};
|
|
2617
2752
|
};
|
|
2618
2753
|
var mergeShares = async (config, params) => {
|
|
@@ -2626,7 +2761,7 @@ var mergeShares = async (config, params) => {
|
|
|
2626
2761
|
{ resolveBy: "id", id: marketAppId, sender: signerAccount },
|
|
2627
2762
|
algodClient
|
|
2628
2763
|
);
|
|
2629
|
-
const marketAddress = algosdk2.getApplicationAddress(marketAppId);
|
|
2764
|
+
const marketAddress = algosdk2.getApplicationAddress(marketAppId).toString();
|
|
2630
2765
|
const atc = new algosdk2.AtomicTransactionComposer();
|
|
2631
2766
|
let optInCosts = 0;
|
|
2632
2767
|
const hasUsdcOptIn = await checkAssetOptIn(algodClient, activeAddress, usdcAssetId);
|
|
@@ -2662,7 +2797,7 @@ var mergeShares = async (config, params) => {
|
|
|
2662
2797
|
return {
|
|
2663
2798
|
success: true,
|
|
2664
2799
|
txIds: result.txIDs,
|
|
2665
|
-
confirmedRound: result.confirmedRound
|
|
2800
|
+
confirmedRound: Number(result.confirmedRound)
|
|
2666
2801
|
};
|
|
2667
2802
|
};
|
|
2668
2803
|
var claim = async (config, params) => {
|
|
@@ -2673,12 +2808,12 @@ var claim = async (config, params) => {
|
|
|
2673
2808
|
{ resolveBy: "id", id: marketAppId, sender: signerAccount },
|
|
2674
2809
|
algodClient
|
|
2675
2810
|
);
|
|
2676
|
-
const marketAddress = algosdk2.getApplicationAddress(marketAppId);
|
|
2811
|
+
const marketAddress = algosdk2.getApplicationAddress(marketAppId).toString();
|
|
2677
2812
|
let tokenBalance = params.amount;
|
|
2678
2813
|
if (!tokenBalance) {
|
|
2679
2814
|
const accountInfo = await algodClient.accountInformation(activeAddress).do();
|
|
2680
|
-
const assets = accountInfo.assets ||
|
|
2681
|
-
const asset = assets.find((a) => (a["asset-id"]
|
|
2815
|
+
const assets = accountInfo.assets || [];
|
|
2816
|
+
const asset = assets.find((a) => Number(a.assetId ?? a["asset-id"]) === assetId);
|
|
2682
2817
|
tokenBalance = asset ? Number(asset.amount) : 0;
|
|
2683
2818
|
}
|
|
2684
2819
|
if (tokenBalance <= 0) {
|
|
@@ -2699,9 +2834,9 @@ var claim = async (config, params) => {
|
|
|
2699
2834
|
);
|
|
2700
2835
|
atc.addTransaction({ txn: claimTxn.transaction, signer });
|
|
2701
2836
|
const sp = await algodClient.getTransactionParams().do();
|
|
2702
|
-
const closeOutTxn =
|
|
2703
|
-
|
|
2704
|
-
|
|
2837
|
+
const closeOutTxn = algosdk2__namespace.makeAssetTransferTxnWithSuggestedParamsFromObject({
|
|
2838
|
+
sender: activeAddress,
|
|
2839
|
+
receiver: marketAddress,
|
|
2705
2840
|
amount: 0,
|
|
2706
2841
|
assetIndex: assetId,
|
|
2707
2842
|
closeRemainderTo: marketAddress,
|
|
@@ -2712,7 +2847,7 @@ var claim = async (config, params) => {
|
|
|
2712
2847
|
return {
|
|
2713
2848
|
success: true,
|
|
2714
2849
|
txIds: result.txIDs,
|
|
2715
|
-
confirmedRound: result.confirmedRound,
|
|
2850
|
+
confirmedRound: Number(result.confirmedRound),
|
|
2716
2851
|
amountClaimed: tokenBalance
|
|
2717
2852
|
};
|
|
2718
2853
|
};
|
|
@@ -2720,17 +2855,17 @@ var getPositions = async (config, walletAddress) => {
|
|
|
2720
2855
|
const { algodClient, indexerClient } = config;
|
|
2721
2856
|
const address = walletAddress ?? config.activeAddress;
|
|
2722
2857
|
const accountInfo = await algodClient.accountInformation(address).do();
|
|
2723
|
-
const assets = accountInfo.assets ||
|
|
2858
|
+
const assets = accountInfo.assets || [];
|
|
2724
2859
|
const nonZeroAssets = assets.filter((a) => Number(a.amount) > 0);
|
|
2725
2860
|
if (nonZeroAssets.length === 0) return [];
|
|
2726
2861
|
const positions = /* @__PURE__ */ new Map();
|
|
2727
2862
|
for (const asset of nonZeroAssets) {
|
|
2728
|
-
const assetId = asset["asset-id"]
|
|
2863
|
+
const assetId = Number(asset.assetId ?? asset["asset-id"]);
|
|
2729
2864
|
const amount = Number(asset.amount);
|
|
2730
2865
|
try {
|
|
2731
2866
|
const assetInfo = await indexerClient.lookupAssetByID(assetId).do();
|
|
2732
2867
|
const assetName = assetInfo.asset?.params?.name ?? "";
|
|
2733
|
-
const unitName = assetInfo.asset?.params?.["unit-name"] ?? "";
|
|
2868
|
+
const unitName = assetInfo.asset?.params?.unitName ?? assetInfo.asset?.params?.["unit-name"] ?? "";
|
|
2734
2869
|
if (!unitName.startsWith("ALPHA-")) continue;
|
|
2735
2870
|
const match = assetName.match(/^Alpha Market (\d+) (Yes|No)$/);
|
|
2736
2871
|
if (!match) continue;
|
|
@@ -2746,16 +2881,24 @@ var getPositions = async (config, walletAddress) => {
|
|
|
2746
2881
|
} else {
|
|
2747
2882
|
try {
|
|
2748
2883
|
const appInfo = await indexerClient.lookupApplications(marketAppId).do();
|
|
2749
|
-
const rawState = appInfo.application?.params?.["global-state"];
|
|
2884
|
+
const rawState = appInfo.application?.params?.globalState ?? appInfo.application?.params?.["global-state"];
|
|
2750
2885
|
if (!rawState) continue;
|
|
2751
2886
|
let yesAssetIdOnChain = 0;
|
|
2752
2887
|
let noAssetIdOnChain = 0;
|
|
2753
2888
|
let marketTitle = "";
|
|
2754
2889
|
for (const item of rawState) {
|
|
2755
|
-
const
|
|
2890
|
+
const rawKey = item.key;
|
|
2891
|
+
const key = typeof rawKey === "string" ? Buffer.from(rawKey, "base64").toString() : rawKey instanceof Uint8Array ? Buffer.from(rawKey).toString() : String(rawKey);
|
|
2756
2892
|
if (key === "yes_asset_id") yesAssetIdOnChain = Number(item.value.uint);
|
|
2757
2893
|
if (key === "no_asset_id") noAssetIdOnChain = Number(item.value.uint);
|
|
2758
|
-
if (key === "title")
|
|
2894
|
+
if (key === "title") {
|
|
2895
|
+
const rawBytes = item.value.bytes;
|
|
2896
|
+
if (typeof rawBytes === "string") {
|
|
2897
|
+
marketTitle = Buffer.from(rawBytes, "base64").toString();
|
|
2898
|
+
} else if (rawBytes instanceof Uint8Array) {
|
|
2899
|
+
marketTitle = Buffer.from(rawBytes).toString();
|
|
2900
|
+
}
|
|
2901
|
+
}
|
|
2759
2902
|
}
|
|
2760
2903
|
if (yesAssetIdOnChain === 0 && noAssetIdOnChain === 0) continue;
|
|
2761
2904
|
positions.set(marketAppId, {
|
|
@@ -2836,8 +2979,9 @@ var getMarketsOnChain = async (config, options) => {
|
|
|
2836
2979
|
if (response.applications?.length) {
|
|
2837
2980
|
allApps.push(...response.applications);
|
|
2838
2981
|
}
|
|
2839
|
-
|
|
2840
|
-
|
|
2982
|
+
const token = response.nextToken ?? response["next-token"];
|
|
2983
|
+
if (token) {
|
|
2984
|
+
nextToken = token;
|
|
2841
2985
|
} else {
|
|
2842
2986
|
hasMore = false;
|
|
2843
2987
|
}
|
|
@@ -2845,7 +2989,7 @@ var getMarketsOnChain = async (config, options) => {
|
|
|
2845
2989
|
const flatMarkets = [];
|
|
2846
2990
|
for (const app of allApps) {
|
|
2847
2991
|
if (app.deleted) continue;
|
|
2848
|
-
const rawState = app.params?.["global-state"];
|
|
2992
|
+
const rawState = app.params?.globalState ?? app.params?.["global-state"];
|
|
2849
2993
|
if (!rawState) continue;
|
|
2850
2994
|
const state = decodeGlobalState(rawState);
|
|
2851
2995
|
if (activeOnly && !state.is_activated) continue;
|
|
@@ -2871,7 +3015,7 @@ var getMarketOnChain = async (config, marketAppId) => {
|
|
|
2871
3015
|
try {
|
|
2872
3016
|
const appId = typeof marketAppId === "string" ? Number(marketAppId) : marketAppId;
|
|
2873
3017
|
const appInfo = await config.algodClient.getApplicationByID(appId).do();
|
|
2874
|
-
const rawState = appInfo.params?.
|
|
3018
|
+
const rawState = appInfo.params?.globalState ?? appInfo.params?.["global-state"] ?? [];
|
|
2875
3019
|
const state = decodeGlobalState(rawState);
|
|
2876
3020
|
return {
|
|
2877
3021
|
id: String(appId),
|
|
@@ -3032,6 +3176,19 @@ var AlphaClient = class {
|
|
|
3032
3176
|
async proposeMatch(params) {
|
|
3033
3177
|
return proposeMatch(this.config, params);
|
|
3034
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
|
+
}
|
|
3035
3192
|
/**
|
|
3036
3193
|
* Amends (edits) an existing unfilled order in-place.
|
|
3037
3194
|
*
|