@ar.io/sdk 3.22.0 → 3.23.0-alpha.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 +0 -1
- package/lib/cjs/cli/cli.js +11 -2
- package/lib/cjs/cli/options.js +5 -0
- package/lib/cjs/cli/utils.js +2 -1
- package/lib/cjs/common/ant.js +10 -1
- package/lib/cjs/common/index.js +1 -0
- package/lib/cjs/common/marketplace.js +575 -0
- package/lib/cjs/constants.js +2 -1
- package/lib/cjs/version.js +1 -1
- package/lib/esm/cli/cli.js +11 -2
- package/lib/esm/cli/options.js +5 -0
- package/lib/esm/cli/utils.js +2 -1
- package/lib/esm/common/ant.js +10 -1
- package/lib/esm/common/index.js +1 -0
- package/lib/esm/common/marketplace.js +568 -0
- package/lib/esm/constants.js +1 -0
- package/lib/esm/version.js +1 -1
- package/lib/types/cli/options.d.ts +5 -0
- package/lib/types/common/ant.d.ts +7 -1
- package/lib/types/common/index.d.ts +1 -0
- package/lib/types/common/marketplace.d.ts +434 -0
- package/lib/types/constants.d.ts +1 -0
- package/lib/types/types/ant.d.ts +1 -0
- package/lib/types/version.d.ts +1 -1
- package/package.json +5 -4
package/README.md
CHANGED
|
@@ -361,7 +361,6 @@ To claim testnet tokens from the testnet token faucet, you can use one of the fo
|
|
|
361
361
|
1. Visit [faucet.ar.io](https://faucet.ar.io) - the easiest way to quickly get tokens for testing for a single address.
|
|
362
362
|
|
|
363
363
|
2. Programmatically via the SDK - useful if you need to claim tokens for multiple addresses or dynamically within your application.
|
|
364
|
-
|
|
365
364
|
- `ARIO.testnet().faucet.captchaUrl()` - returns the captcha URL for the testnet faucet. Open this URL in a new browser window and listen for the `ario-jwt-success` event to be emitted.
|
|
366
365
|
- `ARIO.testnet().faucet.claimWithAuthToken({ authToken, recipient, quantity })` - claims tokens for the specified recipient address using the provided auth token.
|
|
367
366
|
- `ARIO.testnet().faucet.verifyAuthToken({ authToken })` - verifies if the provided auth token is still valid.
|
package/lib/cjs/cli/cli.js
CHANGED
|
@@ -570,12 +570,21 @@ const utils_js_1 = require("./utils.js");
|
|
|
570
570
|
(0, utils_js_1.makeCommand)({
|
|
571
571
|
name: 'transfer-ant-ownership',
|
|
572
572
|
description: 'Transfer ownership of an ANT process',
|
|
573
|
-
options: [
|
|
573
|
+
options: [
|
|
574
|
+
options_js_1.optionMap.processId,
|
|
575
|
+
options_js_1.optionMap.target,
|
|
576
|
+
options_js_1.optionMap.removeControllers,
|
|
577
|
+
...options_js_1.writeActionOptions,
|
|
578
|
+
],
|
|
574
579
|
action: async (options) => {
|
|
575
580
|
const target = (0, utils_js_1.requiredStringFromOptions)(options, 'target');
|
|
576
|
-
|
|
581
|
+
const removeControllers = options.removeControllers !== undefined
|
|
582
|
+
? options.removeControllers
|
|
583
|
+
: true;
|
|
584
|
+
await (0, utils_js_1.assertConfirmationPrompt)(`Are you sure you want to transfer ANT ownership to ${target}${removeControllers ? ' (controllers will be removed)' : ' (controllers will be retained)'}?`, options);
|
|
577
585
|
return (0, utils_js_1.writeANTFromOptions)(options).transfer({
|
|
578
586
|
target,
|
|
587
|
+
removeControllers,
|
|
579
588
|
}, (0, utils_js_1.customTagsFromOptions)(options));
|
|
580
589
|
},
|
|
581
590
|
});
|
package/lib/cjs/cli/options.js
CHANGED
|
@@ -285,6 +285,11 @@ exports.optionMap = {
|
|
|
285
285
|
description: 'Whether the vaulted transfer is revokable by the sender. Defaults to false',
|
|
286
286
|
type: 'boolean',
|
|
287
287
|
},
|
|
288
|
+
removeControllers: {
|
|
289
|
+
alias: '--remove-controllers',
|
|
290
|
+
description: 'Whether to remove controllers when transferring ANT ownership. Defaults to true',
|
|
291
|
+
type: 'boolean',
|
|
292
|
+
},
|
|
288
293
|
lockLengthMs: {
|
|
289
294
|
alias: '--lock-length-ms <lockLengthMs>',
|
|
290
295
|
description: 'The length of time in milliseconds to lock the vault for',
|
package/lib/cjs/cli/utils.js
CHANGED
|
@@ -183,7 +183,8 @@ function readARIOFromOptions(options) {
|
|
|
183
183
|
return index_js_1.ARIO.init({
|
|
184
184
|
hyperbeamUrl: options.hyperbeamUrl,
|
|
185
185
|
process: aoProcessFromOptions({
|
|
186
|
-
cuUrl: '
|
|
186
|
+
cuUrl: 'http://localhost:6363', // default to ardrive cu for ARIO process
|
|
187
|
+
//cuUrl: 'https://cu.ardrive.io', // default to ardrive cu for ARIO process
|
|
187
188
|
...options,
|
|
188
189
|
}),
|
|
189
190
|
paymentUrl: options.paymentUrl,
|
package/lib/cjs/common/ant.js
CHANGED
|
@@ -709,17 +709,26 @@ class AoANTWriteable extends AoANTReadable {
|
|
|
709
709
|
}
|
|
710
710
|
/**
|
|
711
711
|
* @param target @type {string} The address of the account you want to transfer the ANT to.
|
|
712
|
+
* @param removeControllers @type {boolean} Whether to remove the controllers of the ANT. Default is true.
|
|
713
|
+
* @example
|
|
714
|
+
* ```ts
|
|
715
|
+
* ant.transfer({ target: "fGht8v4STuwPnTck1zFVkQqJh5K9q9Zik4Y5-5dV7nk", removeControllers: false }); // will not remove the controllers of the ANT and just transfer ownership
|
|
716
|
+
* ```
|
|
712
717
|
* @returns {Promise<AoMessageResult>} The result of the interaction.
|
|
713
718
|
* @example
|
|
714
719
|
* ```ts
|
|
715
720
|
* ant.transfer({ target: "fGht8v4STuwPnTck1zFVkQqJh5K9q9Zik4Y5-5dV7nk" });
|
|
716
721
|
* ```
|
|
717
722
|
*/
|
|
718
|
-
async transfer({ target }, options) {
|
|
723
|
+
async transfer({ target, removeControllers = true, }, options) {
|
|
719
724
|
const tags = [
|
|
720
725
|
...(options?.tags ?? []),
|
|
721
726
|
{ name: 'Action', value: 'Transfer' },
|
|
722
727
|
{ name: 'Recipient', value: target },
|
|
728
|
+
{
|
|
729
|
+
name: 'Remove-Controllers',
|
|
730
|
+
value: removeControllers ? 'true' : 'false',
|
|
731
|
+
},
|
|
723
732
|
];
|
|
724
733
|
return this.process.send({
|
|
725
734
|
tags,
|
package/lib/cjs/common/index.js
CHANGED
|
@@ -36,6 +36,7 @@ __exportStar(require("./ant.js"), exports);
|
|
|
36
36
|
__exportStar(require("./ant-registry.js"), exports);
|
|
37
37
|
__exportStar(require("./ant-versions.js"), exports);
|
|
38
38
|
__exportStar(require("./faucet.js"), exports);
|
|
39
|
+
__exportStar(require("./marketplace.js"), exports);
|
|
39
40
|
// ao
|
|
40
41
|
__exportStar(require("./io.js"), exports);
|
|
41
42
|
__exportStar(require("./contracts/ao-process.js"), exports);
|
|
@@ -0,0 +1,575 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
+
exports.ArNSMarketplaceWrite = exports.ArNSMarketplaceRead = void 0;
|
|
4
|
+
exports.calculateListingFee = calculateListingFee;
|
|
5
|
+
exports.calculateSaleTax = calculateSaleTax;
|
|
6
|
+
/**
|
|
7
|
+
* Copyright (C) 2022-2024 Permanent Data Solutions, Inc.
|
|
8
|
+
*
|
|
9
|
+
* Licensed under the Apache License, Version 2.0 (the "License");
|
|
10
|
+
* you may not use this file except in compliance with the License.
|
|
11
|
+
* You may obtain a copy of the License at
|
|
12
|
+
*
|
|
13
|
+
* http://www.apache.org/licenses/LICENSE-2.0
|
|
14
|
+
*
|
|
15
|
+
* Unless required by applicable law or agreed to in writing, software
|
|
16
|
+
* distributed under the License is distributed on an "AS IS" BASIS,
|
|
17
|
+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
|
18
|
+
* See the License for the specific language governing permissions and
|
|
19
|
+
* limitations under the License.
|
|
20
|
+
*/
|
|
21
|
+
const index_js_1 = require("../node/index.js");
|
|
22
|
+
class ArNSMarketplaceRead {
|
|
23
|
+
process;
|
|
24
|
+
logger;
|
|
25
|
+
constructor({ process = new index_js_1.AOProcess({
|
|
26
|
+
processId: index_js_1.MARKETPLACE_CONTRACT_ID,
|
|
27
|
+
}), logger = index_js_1.Logger.default, }) {
|
|
28
|
+
this.process = process;
|
|
29
|
+
this.logger = logger;
|
|
30
|
+
}
|
|
31
|
+
async getInfo() {
|
|
32
|
+
return this.process.read({
|
|
33
|
+
tags: [{ name: 'Action', value: 'Info' }],
|
|
34
|
+
});
|
|
35
|
+
}
|
|
36
|
+
async getPaginatedIntents({ cursor, limit, sortBy, sortOrder, filters, } = {}) {
|
|
37
|
+
const tags = [
|
|
38
|
+
{ name: 'Action', value: 'Get-Paginated-Intents' },
|
|
39
|
+
{ name: 'Cursor', value: cursor },
|
|
40
|
+
{ name: 'Limit', value: limit?.toString() },
|
|
41
|
+
{ name: 'Sort-By', value: sortBy },
|
|
42
|
+
{ name: 'Sort-Order', value: sortOrder },
|
|
43
|
+
{ name: 'Filters', value: JSON.stringify(filters) },
|
|
44
|
+
];
|
|
45
|
+
const filteredTags = tags.filter((tag) => tag.value !== undefined);
|
|
46
|
+
return this.process.read({
|
|
47
|
+
tags: filteredTags,
|
|
48
|
+
});
|
|
49
|
+
}
|
|
50
|
+
async getIntent(intentId) {
|
|
51
|
+
return this.process.read({
|
|
52
|
+
tags: [
|
|
53
|
+
{ name: 'Action', value: 'Get-Intent-By-Id' },
|
|
54
|
+
{ name: 'Intent-Id', value: intentId },
|
|
55
|
+
],
|
|
56
|
+
});
|
|
57
|
+
}
|
|
58
|
+
async getIntentByANTId(antId) {
|
|
59
|
+
const res = await this.getPaginatedIntents({
|
|
60
|
+
filters: {
|
|
61
|
+
antProcessId: antId,
|
|
62
|
+
},
|
|
63
|
+
});
|
|
64
|
+
if (res.items.length === 0) {
|
|
65
|
+
throw new Error(`No intent found for ANT ID: ${antId}`);
|
|
66
|
+
}
|
|
67
|
+
return res.items[0];
|
|
68
|
+
}
|
|
69
|
+
/**
|
|
70
|
+
* Get orders with flexible selectors
|
|
71
|
+
* @param params - Parameters for filtering and pagination
|
|
72
|
+
* @returns Orders matching the criteria
|
|
73
|
+
*/
|
|
74
|
+
async getPaginatedOrders(params) {
|
|
75
|
+
const tags = [
|
|
76
|
+
{ name: 'Action', value: 'Get-Orders' },
|
|
77
|
+
{ name: 'Status', value: params?.status },
|
|
78
|
+
{ name: 'Ids', value: params?.ids?.join(',') },
|
|
79
|
+
{ name: 'Dominant-Token', value: params?.dominantToken },
|
|
80
|
+
{ name: 'Swap-Token', value: params?.swapToken },
|
|
81
|
+
{ name: 'Cursor', value: params?.cursor },
|
|
82
|
+
{ name: 'Limit', value: params?.limit?.toString() },
|
|
83
|
+
{ name: 'Sort-By', value: params?.sortBy },
|
|
84
|
+
{ name: 'Sort-Order', value: params?.sortOrder },
|
|
85
|
+
{
|
|
86
|
+
name: 'Filters',
|
|
87
|
+
value: params?.filters ? JSON.stringify(params.filters) : undefined,
|
|
88
|
+
},
|
|
89
|
+
];
|
|
90
|
+
const filteredTags = tags.filter((tag) => tag.value !== undefined);
|
|
91
|
+
return this.process.read({ tags: filteredTags });
|
|
92
|
+
}
|
|
93
|
+
/**
|
|
94
|
+
* Get a single order by ID
|
|
95
|
+
* @param orderId - The order ID to fetch
|
|
96
|
+
* @returns The order if found
|
|
97
|
+
*/
|
|
98
|
+
async getOrder(orderId) {
|
|
99
|
+
return this.process.read({
|
|
100
|
+
tags: [
|
|
101
|
+
{ name: 'Action', value: 'Get-Order' },
|
|
102
|
+
{ name: 'Order-Id', value: orderId },
|
|
103
|
+
],
|
|
104
|
+
});
|
|
105
|
+
}
|
|
106
|
+
async getOrderByANTId(antId) {
|
|
107
|
+
const res = await this.getPaginatedOrders({
|
|
108
|
+
dominantToken: antId,
|
|
109
|
+
});
|
|
110
|
+
if (res.items.length === 0) {
|
|
111
|
+
throw new Error(`No order found for ANT ID: ${antId}`);
|
|
112
|
+
}
|
|
113
|
+
// Strange behaviour here. It seems that the filter logic will return an order with potentially different dominant token.
|
|
114
|
+
const order = res.items[0];
|
|
115
|
+
if (order.dominantToken !== antId) {
|
|
116
|
+
throw new Error(`No order for ANT ID: ${antId}`);
|
|
117
|
+
}
|
|
118
|
+
return res.items[0];
|
|
119
|
+
}
|
|
120
|
+
async getPaginatedMarketplaceBalances(params) {
|
|
121
|
+
return this.process.read({
|
|
122
|
+
tags: [
|
|
123
|
+
{ name: 'Action', value: 'Get-Paginated-Balances' },
|
|
124
|
+
...(0, index_js_1.paginationParamsToTags)(params),
|
|
125
|
+
],
|
|
126
|
+
});
|
|
127
|
+
}
|
|
128
|
+
/**
|
|
129
|
+
* Get ARIO balance for an address in the marketplace
|
|
130
|
+
*/
|
|
131
|
+
async getMarketplaceBalance({ address, }) {
|
|
132
|
+
return this.process.read({
|
|
133
|
+
tags: [
|
|
134
|
+
{ name: 'Action', value: 'Get-Balance' },
|
|
135
|
+
{ name: 'Address', value: address },
|
|
136
|
+
],
|
|
137
|
+
});
|
|
138
|
+
}
|
|
139
|
+
async getUserAssets({ address, arioProcessId, }) {
|
|
140
|
+
async function fetchIntents(context) {
|
|
141
|
+
const intents = [];
|
|
142
|
+
// fetch all intents for user, paginating as needed and adding each ant id to the set
|
|
143
|
+
let intentsCursor = undefined;
|
|
144
|
+
let intentsHasMore = true;
|
|
145
|
+
while (intentsHasMore) {
|
|
146
|
+
const intentsRes = await context.getPaginatedIntents({
|
|
147
|
+
cursor: intentsCursor,
|
|
148
|
+
limit: 1000,
|
|
149
|
+
filters: {
|
|
150
|
+
initiator: address,
|
|
151
|
+
},
|
|
152
|
+
});
|
|
153
|
+
intentsRes.items.forEach((intent) => {
|
|
154
|
+
intents.push(intent);
|
|
155
|
+
});
|
|
156
|
+
intentsCursor = intentsRes.nextCursor;
|
|
157
|
+
intentsHasMore = intentsRes.hasMore;
|
|
158
|
+
}
|
|
159
|
+
return intents;
|
|
160
|
+
}
|
|
161
|
+
async function fetchOrders(context) {
|
|
162
|
+
const orders = [];
|
|
163
|
+
// fetch all orders for user, paginating as needed
|
|
164
|
+
let ordersCursor = undefined;
|
|
165
|
+
let ordersHasMore = true;
|
|
166
|
+
while (ordersHasMore) {
|
|
167
|
+
const ordersRes = await context.getPaginatedOrders({
|
|
168
|
+
cursor: ordersCursor,
|
|
169
|
+
limit: 1000,
|
|
170
|
+
filters: {
|
|
171
|
+
creator: address,
|
|
172
|
+
},
|
|
173
|
+
});
|
|
174
|
+
ordersRes.items.forEach((order) => {
|
|
175
|
+
orders.push(order);
|
|
176
|
+
});
|
|
177
|
+
ordersCursor = ordersRes.nextCursor;
|
|
178
|
+
ordersHasMore = ordersRes.hasMore;
|
|
179
|
+
}
|
|
180
|
+
return orders;
|
|
181
|
+
}
|
|
182
|
+
// parallel fetch balances, intents, and orders
|
|
183
|
+
const [balances, intents, orders] = await Promise.all([
|
|
184
|
+
this.getMarketplaceBalance({ address }),
|
|
185
|
+
fetchIntents(this),
|
|
186
|
+
fetchOrders(this),
|
|
187
|
+
]);
|
|
188
|
+
const antIdsArray = Array.from(new Set([
|
|
189
|
+
...intents.map((intent) => intent.antProcessId),
|
|
190
|
+
...orders.map((order) => order.dominantToken),
|
|
191
|
+
...orders.map((order) => order.swapToken),
|
|
192
|
+
])).filter((antId) => antId !== arioProcessId); // since we add both swap and dominant token for simplicity, we need to filter out the ario token
|
|
193
|
+
return { intents, orders, balances, antIds: antIdsArray };
|
|
194
|
+
}
|
|
195
|
+
}
|
|
196
|
+
exports.ArNSMarketplaceRead = ArNSMarketplaceRead;
|
|
197
|
+
class ArNSMarketplaceWrite extends ArNSMarketplaceRead {
|
|
198
|
+
process;
|
|
199
|
+
signer;
|
|
200
|
+
ario;
|
|
201
|
+
constructor({ process = new index_js_1.AOProcess({
|
|
202
|
+
processId: index_js_1.MARKETPLACE_CONTRACT_ID,
|
|
203
|
+
}), signer, ario, logger = index_js_1.Logger.default, }) {
|
|
204
|
+
super({ process: process, logger: logger });
|
|
205
|
+
this.process = process;
|
|
206
|
+
this.signer = signer;
|
|
207
|
+
this.ario = ario;
|
|
208
|
+
this.logger = logger;
|
|
209
|
+
}
|
|
210
|
+
/**
|
|
211
|
+
* Deposit ARIO to the marketplace (simulates Credit-Notice from ARIO token process)
|
|
212
|
+
* Returns the message ID for verification
|
|
213
|
+
*/
|
|
214
|
+
async depositArIO(params) {
|
|
215
|
+
return this.ario.transfer({
|
|
216
|
+
// the marketplace process id is the target
|
|
217
|
+
target: this.process.processId,
|
|
218
|
+
qty: Number(params.amount),
|
|
219
|
+
}, {
|
|
220
|
+
tags: [{ name: 'X-Action', value: 'Deposit' }],
|
|
221
|
+
});
|
|
222
|
+
}
|
|
223
|
+
/**
|
|
224
|
+
* Withdraw ARIO from the marketplace back to user
|
|
225
|
+
*/
|
|
226
|
+
async withdrawArIO(params) {
|
|
227
|
+
return this.process.send({
|
|
228
|
+
tags: [
|
|
229
|
+
{ name: 'Action', value: 'Withdraw-Ario' },
|
|
230
|
+
{ name: 'Quantity', value: params.amount },
|
|
231
|
+
],
|
|
232
|
+
signer: this.signer,
|
|
233
|
+
});
|
|
234
|
+
}
|
|
235
|
+
async createIntent({ antId, orderType, quantity, price, expirationTime, minimumPrice, decreaseInterval, }) {
|
|
236
|
+
const tags = [
|
|
237
|
+
{ name: 'Action', value: 'Create-Intent' },
|
|
238
|
+
{ name: 'X-Intent-ANT-Id', value: antId },
|
|
239
|
+
{ name: 'X-Intent-Order-Type', value: orderType },
|
|
240
|
+
{ name: 'X-Intent-Quantity', value: quantity },
|
|
241
|
+
{ name: 'X-Intent-Price', value: price },
|
|
242
|
+
{ name: 'X-Intent-Expiration-Time', value: expirationTime },
|
|
243
|
+
{ name: 'X-Intent-Minimum-Price', value: minimumPrice },
|
|
244
|
+
{ name: 'X-Intent-Decrease-Interval', value: decreaseInterval },
|
|
245
|
+
];
|
|
246
|
+
const filteredTags = tags.filter((tag) => tag.value !== undefined);
|
|
247
|
+
return this.process.send({
|
|
248
|
+
tags: filteredTags,
|
|
249
|
+
signer: this.signer,
|
|
250
|
+
});
|
|
251
|
+
}
|
|
252
|
+
async pushANTIntentResolution(intentId) {
|
|
253
|
+
return this.process.send({
|
|
254
|
+
tags: [
|
|
255
|
+
{ name: 'Action', value: 'Push-ANT-Intent-Resolution' },
|
|
256
|
+
{ name: 'X-Intent-Id', value: intentId },
|
|
257
|
+
],
|
|
258
|
+
signer: this.signer,
|
|
259
|
+
});
|
|
260
|
+
}
|
|
261
|
+
async settleAuction(params) {
|
|
262
|
+
const tags = [
|
|
263
|
+
{ name: 'Action', value: 'Settle-Auction' },
|
|
264
|
+
{ name: 'Order-Id', value: params.orderId },
|
|
265
|
+
{ name: 'Dominant-Token', value: params.dominantToken },
|
|
266
|
+
{ name: 'Swap-Token', value: params.swapToken },
|
|
267
|
+
];
|
|
268
|
+
const filteredTags = tags.filter((tag) => tag.value !== undefined);
|
|
269
|
+
return this.process.send({
|
|
270
|
+
tags: filteredTags,
|
|
271
|
+
signer: this.signer,
|
|
272
|
+
});
|
|
273
|
+
}
|
|
274
|
+
async listNameForSale({ name, expirationTime, price, type, walletAddress, minimumPrice, decreaseInterval, }) {
|
|
275
|
+
// Get arns record for the current ant id associated with it
|
|
276
|
+
const record = await this.ario.getArNSRecord({ name: name });
|
|
277
|
+
this.logger.info(`Record ${name} found: ${JSON.stringify(record)}`);
|
|
278
|
+
if (record === undefined) {
|
|
279
|
+
throw new Error(`Record ${name} not found`);
|
|
280
|
+
}
|
|
281
|
+
const antId = record.processId;
|
|
282
|
+
this.logger.info(`ANT ID for ${name} found: ${antId}`);
|
|
283
|
+
const ant = index_js_1.ANT.init({
|
|
284
|
+
process: new index_js_1.AOProcess({
|
|
285
|
+
processId: antId,
|
|
286
|
+
ao: this.process.ao,
|
|
287
|
+
}),
|
|
288
|
+
signer: this.signer,
|
|
289
|
+
});
|
|
290
|
+
const antState = await ant.getState();
|
|
291
|
+
if (antState.Owner !== walletAddress) {
|
|
292
|
+
this.logger.error(`Wallet address ${walletAddress} does not match the owner of the ANT ${antId}. Only the owner can list the name for sale.`);
|
|
293
|
+
throw new Error('Wallet address does not match the owner of the ANT. Only the owner can list the name for sale.');
|
|
294
|
+
}
|
|
295
|
+
let intent;
|
|
296
|
+
try {
|
|
297
|
+
const intentResult = await this.createIntent({
|
|
298
|
+
antId,
|
|
299
|
+
orderType: type,
|
|
300
|
+
quantity: price,
|
|
301
|
+
expirationTime: expirationTime.toString(),
|
|
302
|
+
price: price,
|
|
303
|
+
minimumPrice: minimumPrice,
|
|
304
|
+
decreaseInterval: decreaseInterval,
|
|
305
|
+
});
|
|
306
|
+
if (intentResult.result === undefined) {
|
|
307
|
+
throw new Error('Failed to create intent: ' + intentResult.id);
|
|
308
|
+
}
|
|
309
|
+
this.logger.info(`Intent created: ${JSON.stringify(intentResult.result)}`);
|
|
310
|
+
intent = intentResult.result;
|
|
311
|
+
}
|
|
312
|
+
catch (error) {
|
|
313
|
+
this.logger.error(`Error creating intent: ${error.message}`);
|
|
314
|
+
// check error for existing intent. Will be a contract error message.
|
|
315
|
+
const isExistingIntentError = error.message.includes('An intent already exists for this ANT ID');
|
|
316
|
+
if (isExistingIntentError) {
|
|
317
|
+
intent = await this.getIntentByANTId(antId).catch((error) => {
|
|
318
|
+
this.logger.error(`Failed to get intent: ${error.message}`);
|
|
319
|
+
throw new Error('An intent already exists for this ANT ID but failed to get intent:\n\n' +
|
|
320
|
+
error.message);
|
|
321
|
+
});
|
|
322
|
+
}
|
|
323
|
+
else {
|
|
324
|
+
// Possible to get other errors, eg insufficient deposited ario balance. Rethrow them here.
|
|
325
|
+
throw error;
|
|
326
|
+
}
|
|
327
|
+
}
|
|
328
|
+
// Type guard to ensure intent is defined
|
|
329
|
+
if (intent === undefined) {
|
|
330
|
+
throw new Error('Failed to create intent');
|
|
331
|
+
}
|
|
332
|
+
let antTransferResult;
|
|
333
|
+
try {
|
|
334
|
+
antTransferResult = await ant.transfer({
|
|
335
|
+
target: this.process.processId,
|
|
336
|
+
removeControllers: false, // important: do not remove the controllers of the ANT to prevent loss of control
|
|
337
|
+
}, {
|
|
338
|
+
tags: [{ name: 'X-Intent-Id', value: intent.intentId }],
|
|
339
|
+
});
|
|
340
|
+
this.logger.info(`ANT transferred: ${JSON.stringify(antTransferResult)}`);
|
|
341
|
+
}
|
|
342
|
+
catch (error) {
|
|
343
|
+
this.logger.error(`Failed to transfer ANT: ${error.message}`);
|
|
344
|
+
return {
|
|
345
|
+
intent,
|
|
346
|
+
order: null,
|
|
347
|
+
antTransferResult: null,
|
|
348
|
+
error: error,
|
|
349
|
+
};
|
|
350
|
+
}
|
|
351
|
+
// poll for the order to be created
|
|
352
|
+
// This is to ensure the order is created before returning the result for ux purposes.
|
|
353
|
+
// This may still fail, in which case we return the intent and ant transfer result and handle the error in the client.
|
|
354
|
+
let order = null;
|
|
355
|
+
let tries = 0;
|
|
356
|
+
while (order === null && tries < 5) {
|
|
357
|
+
try {
|
|
358
|
+
order = await this.getOrderByANTId(antId).catch((error) => {
|
|
359
|
+
console.log(new Error('Failed to get order: ' + error.message));
|
|
360
|
+
return null;
|
|
361
|
+
});
|
|
362
|
+
if (order === null) {
|
|
363
|
+
await new Promise((resolve) => setTimeout(resolve, 1000));
|
|
364
|
+
this.logger.info(`Waiting for order to be created...`);
|
|
365
|
+
if (tries === 5) {
|
|
366
|
+
this.logger.error(`Failed to get order after ${tries} attempts`);
|
|
367
|
+
throw new Error(`Failed to get order after ${tries} attempts`);
|
|
368
|
+
}
|
|
369
|
+
tries++;
|
|
370
|
+
}
|
|
371
|
+
// if the order is found, break the loop
|
|
372
|
+
break;
|
|
373
|
+
}
|
|
374
|
+
catch (error) {
|
|
375
|
+
this.logger.error(`Failed to get order: ${error.message}`);
|
|
376
|
+
return {
|
|
377
|
+
intent,
|
|
378
|
+
order: null,
|
|
379
|
+
antTransferResult,
|
|
380
|
+
error: new Error('Failed to get order: ' + error.message),
|
|
381
|
+
};
|
|
382
|
+
}
|
|
383
|
+
}
|
|
384
|
+
if (order === null) {
|
|
385
|
+
this.logger.error(`Failed to get order`);
|
|
386
|
+
return {
|
|
387
|
+
intent,
|
|
388
|
+
order: null,
|
|
389
|
+
antTransferResult,
|
|
390
|
+
error: new Error('Failed to get order'),
|
|
391
|
+
};
|
|
392
|
+
}
|
|
393
|
+
return { intent, order, antTransferResult, error: null };
|
|
394
|
+
}
|
|
395
|
+
async cancelOrder(orderId) {
|
|
396
|
+
const tags = [
|
|
397
|
+
{ name: 'Action', value: 'Cancel-Order' },
|
|
398
|
+
{ name: 'Order-Id', value: orderId },
|
|
399
|
+
];
|
|
400
|
+
return this.process.send({
|
|
401
|
+
tags,
|
|
402
|
+
signer: this.signer,
|
|
403
|
+
});
|
|
404
|
+
}
|
|
405
|
+
async createOrder({ swapToken, quantity, orderType, price, expirationTime, minimumPrice, decreaseInterval, transferDenomination, }) {
|
|
406
|
+
const tags = [
|
|
407
|
+
{ name: 'Action', value: 'Create-Order' },
|
|
408
|
+
{ name: 'Swap-Token', value: swapToken },
|
|
409
|
+
{ name: 'Quantity', value: quantity },
|
|
410
|
+
{ name: 'Order-Type', value: orderType },
|
|
411
|
+
{ name: 'Price', value: price },
|
|
412
|
+
{ name: 'Expiration-Time', value: expirationTime },
|
|
413
|
+
{ name: 'Minimum-Price', value: minimumPrice },
|
|
414
|
+
{ name: 'Decrease-Interval', value: decreaseInterval },
|
|
415
|
+
{ name: 'Transfer-Denomination', value: transferDenomination },
|
|
416
|
+
];
|
|
417
|
+
const filteredTags = tags.filter((tag) => tag.value !== undefined);
|
|
418
|
+
// Send the message
|
|
419
|
+
return this.process.send({
|
|
420
|
+
tags: filteredTags,
|
|
421
|
+
signer: this.signer,
|
|
422
|
+
});
|
|
423
|
+
}
|
|
424
|
+
async buyFixedPriceANT(params) {
|
|
425
|
+
const order = await this.getOrderByANTId(params.antId);
|
|
426
|
+
if (order === undefined) {
|
|
427
|
+
throw new Error(`No active sell order found for ANT: ${params.antId}`);
|
|
428
|
+
}
|
|
429
|
+
if (order.status !== 'active') {
|
|
430
|
+
throw new Error(`Order status is ${order.status}, must be active`);
|
|
431
|
+
}
|
|
432
|
+
if (order.price === undefined) {
|
|
433
|
+
throw new Error(`Order price is undefined for ANT: ${params.antId}`);
|
|
434
|
+
}
|
|
435
|
+
// Create a buy order that matches the sell order
|
|
436
|
+
// This should trigger immediate execution if prices match
|
|
437
|
+
// For ArNS: buying 1 ANT, so quantity = price = total cost
|
|
438
|
+
return this.createOrder({
|
|
439
|
+
swapToken: params.antId,
|
|
440
|
+
quantity: order.price, // Total ARIO cost
|
|
441
|
+
orderType: 'fixed',
|
|
442
|
+
price: order.price, // Match their asking price
|
|
443
|
+
});
|
|
444
|
+
}
|
|
445
|
+
async buyDutchAuctionANT(params) {
|
|
446
|
+
const order = await this.getOrderByANTId(params.antId);
|
|
447
|
+
if (order === undefined) {
|
|
448
|
+
throw new Error(`No active sell order found for ANT: ${params.antId}`);
|
|
449
|
+
}
|
|
450
|
+
if (order.status !== 'active') {
|
|
451
|
+
throw new Error(`Order status is ${order.status}, must be active`);
|
|
452
|
+
}
|
|
453
|
+
if (order.orderType !== 'dutch') {
|
|
454
|
+
throw new Error(`Order is not a Dutch auction, it's: ${order.orderType}`);
|
|
455
|
+
}
|
|
456
|
+
if (order.price === undefined) {
|
|
457
|
+
throw new Error(`Order starting price is undefined for ANT: ${params.antId}`);
|
|
458
|
+
}
|
|
459
|
+
// For Dutch auction, the current price will be calculated on execution
|
|
460
|
+
return this.createOrder({
|
|
461
|
+
swapToken: params.antId,
|
|
462
|
+
quantity: order.price,
|
|
463
|
+
orderType: 'fixed',
|
|
464
|
+
price: order.price,
|
|
465
|
+
});
|
|
466
|
+
}
|
|
467
|
+
/**
|
|
468
|
+
* Place a bid on an English auction
|
|
469
|
+
* Note: This requires you to have sufficient ARIO balance in the marketplace
|
|
470
|
+
*/
|
|
471
|
+
async bidOnANTEnglishAuction(params) {
|
|
472
|
+
const order = await this.getOrderByANTId(params.antId);
|
|
473
|
+
if (order === undefined) {
|
|
474
|
+
throw new Error(`No active sell order found for ANT: ${params.antId}`);
|
|
475
|
+
}
|
|
476
|
+
if (order.status !== 'active') {
|
|
477
|
+
throw new Error(`Order status is ${order.status}, must be active`);
|
|
478
|
+
}
|
|
479
|
+
if (order.orderType !== 'english') {
|
|
480
|
+
throw new Error(`Order is not an English auction, it's: ${order.orderType}`);
|
|
481
|
+
}
|
|
482
|
+
// Check if auction has expired
|
|
483
|
+
if (order.expirationTime !== undefined &&
|
|
484
|
+
Date.now() >= order.expirationTime) {
|
|
485
|
+
throw new Error(`Auction has already expired for ANT: ${params.antId}`);
|
|
486
|
+
}
|
|
487
|
+
return this.process.send({
|
|
488
|
+
tags: [
|
|
489
|
+
{ name: 'Action', value: 'Bid-On-English-Auction' },
|
|
490
|
+
{ name: 'Order-Id', value: order.id },
|
|
491
|
+
{ name: 'Bid-Amount', value: params.bidAmount },
|
|
492
|
+
],
|
|
493
|
+
signer: this.signer,
|
|
494
|
+
});
|
|
495
|
+
}
|
|
496
|
+
/**
|
|
497
|
+
* Settle an expired English auction
|
|
498
|
+
* Can be called by anyone after the auction expires
|
|
499
|
+
* The highest bidder wins the ANT
|
|
500
|
+
*/
|
|
501
|
+
async settleANTEnglishAuction(params) {
|
|
502
|
+
const order = await this.getOrderByANTId(params.antId);
|
|
503
|
+
if (order === undefined) {
|
|
504
|
+
throw new Error(`No active sell order found for ANT: ${params.antId}`);
|
|
505
|
+
}
|
|
506
|
+
if (order.orderType !== 'english') {
|
|
507
|
+
throw new Error(`Order is not an English auction, it's: ${order.orderType}`);
|
|
508
|
+
}
|
|
509
|
+
// Check if auction has expired
|
|
510
|
+
if (order.expirationTime !== undefined &&
|
|
511
|
+
Date.now() < order.expirationTime) {
|
|
512
|
+
throw new Error(`Auction has not yet expired for ANT: ${params.antId}. Expires at: ${new Date(order.expirationTime).toISOString()}`);
|
|
513
|
+
}
|
|
514
|
+
return this.settleAuction({
|
|
515
|
+
orderId: order.id,
|
|
516
|
+
dominantToken: order.dominantToken,
|
|
517
|
+
swapToken: order.swapToken,
|
|
518
|
+
});
|
|
519
|
+
}
|
|
520
|
+
}
|
|
521
|
+
exports.ArNSMarketplaceWrite = ArNSMarketplaceWrite;
|
|
522
|
+
/**
|
|
523
|
+
* Calculates the listing fee for an order based on the end timestamp.
|
|
524
|
+
* The fee is calculated by rounding up the hours until the end timestamp to the nearest hour.
|
|
525
|
+
*
|
|
526
|
+
* @param params - Parameters for calculating the listing fee
|
|
527
|
+
* @param params.listingFeePerHour - The listing fee per hour in mARIO (as a string)
|
|
528
|
+
* @param params.endTimestamp - Unix timestamp when the order expires
|
|
529
|
+
* @returns The listing fee in mARIO as a BigInt
|
|
530
|
+
*
|
|
531
|
+
* @example
|
|
532
|
+
* ```typescript
|
|
533
|
+
* const info = await marketplace.getInfo();
|
|
534
|
+
* const endTimestamp = Date.now() + 24 * 60 * 60 * 1000; // 24 hours from now
|
|
535
|
+
* const listingFee = calculateListingFee({
|
|
536
|
+
* listingFeePerHour: info.fees.listingFeePerHour,
|
|
537
|
+
* endTimestamp,
|
|
538
|
+
* });
|
|
539
|
+
* ```
|
|
540
|
+
*/
|
|
541
|
+
function calculateListingFee({ listingFeePerHour, endTimestamp, }) {
|
|
542
|
+
const now = Date.now();
|
|
543
|
+
const millisecondsUntilEnd = endTimestamp - now;
|
|
544
|
+
if (millisecondsUntilEnd <= 0) {
|
|
545
|
+
return BigInt(0);
|
|
546
|
+
}
|
|
547
|
+
// Convert milliseconds to hours and round up to the nearest hour
|
|
548
|
+
const hoursUntilEnd = Math.ceil(millisecondsUntilEnd / (1000 * 60 * 60));
|
|
549
|
+
// Calculate listing fee: hours * fee per hour
|
|
550
|
+
return BigInt(hoursUntilEnd) * BigInt(listingFeePerHour);
|
|
551
|
+
}
|
|
552
|
+
/**
|
|
553
|
+
* Calculates the sale tax for an order based on the sale amount.
|
|
554
|
+
*
|
|
555
|
+
* @param params - Parameters for calculating the sale tax
|
|
556
|
+
* @param params.saleAmount - The sale amount in mARIO (as a string or number)
|
|
557
|
+
* @param params.saleTaxNumerator - The numerator for sale tax calculation
|
|
558
|
+
* @param params.saleTaxDenominator - The denominator for sale tax calculation
|
|
559
|
+
* @returns The sale tax in mARIO as a BigInt
|
|
560
|
+
*
|
|
561
|
+
* @example
|
|
562
|
+
* ```typescript
|
|
563
|
+
* const info = await marketplace.getInfo();
|
|
564
|
+
* const saleAmount = 100000000000; // 100 ARIO in mARIO
|
|
565
|
+
* const tax = calculateSaleTax({
|
|
566
|
+
* saleAmount,
|
|
567
|
+
* saleTaxNumerator: info.fees.saleTaxNumerator,
|
|
568
|
+
* saleTaxDenominator: info.fees.saleTaxDenominator,
|
|
569
|
+
* });
|
|
570
|
+
* ```
|
|
571
|
+
*/
|
|
572
|
+
function calculateSaleTax({ saleAmount, saleTaxNumerator, saleTaxDenominator, }) {
|
|
573
|
+
// Calculate tax: (saleAmount * numerator) / denominator
|
|
574
|
+
return ((BigInt(saleAmount) * BigInt(saleTaxNumerator)) / BigInt(saleTaxDenominator));
|
|
575
|
+
}
|
package/lib/cjs/constants.js
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
"use strict";
|
|
2
2
|
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
-
exports.DEFAULT_SCHEDULER_ID = exports.AO_AUTHORITY = exports.ANT_LUA_ID = exports.AOS_MODULE_ID = exports.MARIO_PER_ARIO = exports.ANT_REGISTRY_ID = exports.ANT_REGISTRY_TESTNET_ID = exports.ARIO_MAINNET_PROCESS_ID = exports.ARIO_TESTNET_PROCESS_ID = exports.arioDevnetProcessId = exports.ARIO_DEVNET_PROCESS_ID = exports.FQDN_REGEX = exports.ARWEAVE_TX_REGEX = void 0;
|
|
3
|
+
exports.DEFAULT_SCHEDULER_ID = exports.AO_AUTHORITY = exports.ANT_LUA_ID = exports.AOS_MODULE_ID = exports.MARIO_PER_ARIO = exports.MARKETPLACE_CONTRACT_ID = exports.ANT_REGISTRY_ID = exports.ANT_REGISTRY_TESTNET_ID = exports.ARIO_MAINNET_PROCESS_ID = exports.ARIO_TESTNET_PROCESS_ID = exports.arioDevnetProcessId = exports.ARIO_DEVNET_PROCESS_ID = exports.FQDN_REGEX = exports.ARWEAVE_TX_REGEX = void 0;
|
|
4
4
|
/**
|
|
5
5
|
* Copyright (C) 2022-2024 Permanent Data Solutions, Inc.
|
|
6
6
|
*
|
|
@@ -26,6 +26,7 @@ exports.ARIO_TESTNET_PROCESS_ID = 'agYcCFJtrMG6cqMuZfskIkFTGvUPddICmtQSBIoPdiA';
|
|
|
26
26
|
exports.ARIO_MAINNET_PROCESS_ID = 'qNvAoz0TgcH7DMg8BCVn8jF32QH5L6T29VjHxhHqqGE';
|
|
27
27
|
exports.ANT_REGISTRY_TESTNET_ID = 'RR0vheYqtsKuJCWh6xj0beE35tjaEug5cejMw9n2aa8';
|
|
28
28
|
exports.ANT_REGISTRY_ID = 'i_le_yKKPVstLTDSmkHRqf-wYphMnwB9OhleiTgMkWc';
|
|
29
|
+
exports.MARKETPLACE_CONTRACT_ID = 'wPh88ziUF4meLpHfffmgdA6IFtzmXHgdeg-epzBjkao';
|
|
29
30
|
exports.MARIO_PER_ARIO = 1_000_000;
|
|
30
31
|
/**
|
|
31
32
|
* @deprecated - use ANT.versions.getLatestANTVersion() to get latest ANT module
|