@ar.io/sdk 3.2.0 → 3.3.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/bundles/web.bundle.min.js +111 -90
- package/lib/cjs/cli/cli.js +6 -2
- package/lib/cjs/common/contracts/ao-process.js +1 -1
- package/lib/cjs/common/io.js +63 -6
- package/lib/cjs/constants.js +1 -1
- package/lib/cjs/utils/ao.js +22 -22
- package/lib/cjs/utils/arweave.js +51 -16
- package/lib/cjs/version.js +1 -1
- package/lib/esm/cli/cli.js +6 -2
- package/lib/esm/common/contracts/ao-process.js +1 -1
- package/lib/esm/common/io.js +61 -7
- package/lib/esm/constants.js +1 -1
- package/lib/esm/utils/ao.js +20 -21
- package/lib/esm/utils/arweave.js +49 -16
- package/lib/esm/version.js +1 -1
- package/lib/types/common/contracts/ao-process.d.ts +1 -1
- package/lib/types/common/io.d.ts +28 -6
- package/lib/types/constants.d.ts +1 -1
- package/lib/types/types/io.d.ts +2 -2
- package/lib/types/utils/ao.d.ts +13 -3
- package/lib/types/utils/arweave.d.ts +31 -1
- package/lib/types/version.d.ts +1 -1
- package/package.json +1 -1
package/lib/cjs/cli/cli.js
CHANGED
|
@@ -145,13 +145,17 @@ const utils_js_1 = require("./utils.js");
|
|
|
145
145
|
name: 'get-observations',
|
|
146
146
|
description: 'Get observations for an epoch',
|
|
147
147
|
options: options_js_1.epochOptions,
|
|
148
|
-
action: (o) => (0, utils_js_1.readARIOFromOptions)(o)
|
|
148
|
+
action: (o) => (0, utils_js_1.readARIOFromOptions)(o)
|
|
149
|
+
.getObservations((0, utils_js_1.epochInputFromOptions)(o))
|
|
150
|
+
.then((result) => result ?? { message: 'No observations found for epoch' }),
|
|
149
151
|
});
|
|
150
152
|
(0, utils_js_1.makeCommand)({
|
|
151
153
|
name: 'get-distributions',
|
|
152
154
|
description: 'Get distributions for an epoch',
|
|
153
155
|
options: options_js_1.epochOptions,
|
|
154
|
-
action: (o) => (0, utils_js_1.readARIOFromOptions)(o)
|
|
156
|
+
action: (o) => (0, utils_js_1.readARIOFromOptions)(o)
|
|
157
|
+
.getDistributions((0, utils_js_1.epochInputFromOptions)(o))
|
|
158
|
+
.then((result) => result ?? { message: 'No distributions found for epoch' }),
|
|
155
159
|
});
|
|
156
160
|
(0, utils_js_1.makeCommand)({
|
|
157
161
|
name: 'get-token-cost',
|
|
@@ -24,8 +24,8 @@ const error_js_1 = require("../error.js");
|
|
|
24
24
|
const logger_js_1 = require("../logger.js");
|
|
25
25
|
class AOProcess {
|
|
26
26
|
logger;
|
|
27
|
-
processId;
|
|
28
27
|
ao;
|
|
28
|
+
processId;
|
|
29
29
|
constructor({ processId, ao = (0, aoconnect_1.connect)(), logger = logger_js_1.Logger.default, }) {
|
|
30
30
|
this.processId = processId;
|
|
31
31
|
this.logger = logger;
|
package/lib/cjs/common/io.js
CHANGED
|
@@ -1,4 +1,7 @@
|
|
|
1
1
|
"use strict";
|
|
2
|
+
var __importDefault = (this && this.__importDefault) || function (mod) {
|
|
3
|
+
return (mod && mod.__esModule) ? mod : { "default": mod };
|
|
4
|
+
};
|
|
2
5
|
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
6
|
exports.ARIOWriteable = exports.ARIOReadable = exports.ARIO = void 0;
|
|
4
7
|
/**
|
|
@@ -16,6 +19,7 @@ exports.ARIOWriteable = exports.ARIOReadable = exports.ARIO = void 0;
|
|
|
16
19
|
* See the License for the specific language governing permissions and
|
|
17
20
|
* limitations under the License.
|
|
18
21
|
*/
|
|
22
|
+
const arweave_1 = __importDefault(require("arweave"));
|
|
19
23
|
const constants_js_1 = require("../constants.js");
|
|
20
24
|
const io_js_1 = require("../types/io.js");
|
|
21
25
|
const ao_js_1 = require("../utils/ao.js");
|
|
@@ -23,23 +27,32 @@ const arweave_js_1 = require("../utils/arweave.js");
|
|
|
23
27
|
const ao_process_js_1 = require("./contracts/ao-process.js");
|
|
24
28
|
const error_js_1 = require("./error.js");
|
|
25
29
|
class ARIO {
|
|
26
|
-
static init(config) {
|
|
27
|
-
if (config && config.signer) {
|
|
30
|
+
static init({ arweave, ...config } = {}) {
|
|
31
|
+
if (config !== undefined && config.signer) {
|
|
28
32
|
const { signer, ...rest } = config;
|
|
29
33
|
return new ARIOWriteable({
|
|
30
34
|
...rest,
|
|
31
35
|
signer,
|
|
36
|
+
arweave,
|
|
32
37
|
});
|
|
33
38
|
}
|
|
34
|
-
return new ARIOReadable(
|
|
39
|
+
return new ARIOReadable({
|
|
40
|
+
arweave,
|
|
41
|
+
...config,
|
|
42
|
+
});
|
|
35
43
|
}
|
|
36
44
|
}
|
|
37
45
|
exports.ARIO = ARIO;
|
|
38
46
|
class ARIOReadable {
|
|
39
47
|
process;
|
|
40
48
|
epochSettings;
|
|
41
|
-
|
|
42
|
-
|
|
49
|
+
arweave;
|
|
50
|
+
constructor({ arweave = arweave_1.default.init({
|
|
51
|
+
host: 'arweave.net',
|
|
52
|
+
port: 443,
|
|
53
|
+
protocol: 'https',
|
|
54
|
+
}), ...config }) {
|
|
55
|
+
if (config === undefined) {
|
|
43
56
|
this.process = new ao_process_js_1.AOProcess({
|
|
44
57
|
processId: constants_js_1.ARIO_TESTNET_PROCESS_ID,
|
|
45
58
|
});
|
|
@@ -55,6 +68,7 @@ class ARIOReadable {
|
|
|
55
68
|
else {
|
|
56
69
|
throw new error_js_1.InvalidContractConfigurationError();
|
|
57
70
|
}
|
|
71
|
+
this.arweave = arweave;
|
|
58
72
|
}
|
|
59
73
|
async getInfo() {
|
|
60
74
|
return this.process.read({
|
|
@@ -72,6 +86,13 @@ class ARIOReadable {
|
|
|
72
86
|
const epochLengthMs = epochSettings.durationMs;
|
|
73
87
|
return Math.floor((timestamp - epochZeroStartTimestamp) / epochLengthMs);
|
|
74
88
|
}
|
|
89
|
+
async computeCurrentEpochIndex() {
|
|
90
|
+
const epochSettings = await this.getEpochSettings();
|
|
91
|
+
const epochZeroStartTimestamp = epochSettings.epochZeroStartTimestamp;
|
|
92
|
+
const epochLengthMs = epochSettings.durationMs;
|
|
93
|
+
const currentTimestamp = Date.now();
|
|
94
|
+
return Math.floor((currentTimestamp - epochZeroStartTimestamp) / epochLengthMs);
|
|
95
|
+
}
|
|
75
96
|
async computeEpochIndex(params) {
|
|
76
97
|
const epochIndex = params?.epochIndex;
|
|
77
98
|
if (epochIndex !== undefined) {
|
|
@@ -89,6 +110,17 @@ class ARIOReadable {
|
|
|
89
110
|
}));
|
|
90
111
|
}
|
|
91
112
|
async getEpoch(epoch) {
|
|
113
|
+
const epochIndex = await this.computeEpochIndex(epoch);
|
|
114
|
+
const currentIndex = await this.computeCurrentEpochIndex();
|
|
115
|
+
if (epochIndex !== undefined && +epochIndex < currentIndex) {
|
|
116
|
+
const epochData = await (0, arweave_js_1.getEpochDataFromGql)({
|
|
117
|
+
arweave: this.arweave,
|
|
118
|
+
epochIndex: +epochIndex,
|
|
119
|
+
processId: this.process.processId,
|
|
120
|
+
});
|
|
121
|
+
return epochData;
|
|
122
|
+
}
|
|
123
|
+
// go to the process epoch and fetch the epoch data
|
|
92
124
|
const allTags = [
|
|
93
125
|
{ name: 'Action', value: 'Epoch' },
|
|
94
126
|
{ name: 'Epoch-Index', value: await this.computeEpochIndex(epoch) },
|
|
@@ -219,7 +251,19 @@ class ARIOReadable {
|
|
|
219
251
|
tags: (0, arweave_js_1.pruneTags)(allTags),
|
|
220
252
|
});
|
|
221
253
|
}
|
|
254
|
+
// we need to find the epoch index for the epoch that is currently being distributed and fetch it from gql
|
|
222
255
|
async getObservations(epoch) {
|
|
256
|
+
const epochIndex = await this.computeEpochIndex(epoch);
|
|
257
|
+
const currentIndex = await this.computeCurrentEpochIndex();
|
|
258
|
+
if (epochIndex !== undefined && +epochIndex < currentIndex) {
|
|
259
|
+
const epochData = await (0, arweave_js_1.getEpochDataFromGql)({
|
|
260
|
+
arweave: this.arweave,
|
|
261
|
+
epochIndex: +epochIndex,
|
|
262
|
+
processId: this.process.processId,
|
|
263
|
+
});
|
|
264
|
+
return epochData?.observations;
|
|
265
|
+
}
|
|
266
|
+
// go to the process epoch and fetch the observations
|
|
223
267
|
const allTags = [
|
|
224
268
|
{ name: 'Action', value: 'Epoch-Observations' },
|
|
225
269
|
{ name: 'Epoch-Index', value: await this.computeEpochIndex(epoch) },
|
|
@@ -229,6 +273,17 @@ class ARIOReadable {
|
|
|
229
273
|
});
|
|
230
274
|
}
|
|
231
275
|
async getDistributions(epoch) {
|
|
276
|
+
const epochIndex = await this.computeEpochIndex(epoch);
|
|
277
|
+
const currentIndex = await this.computeCurrentEpochIndex();
|
|
278
|
+
if (epochIndex !== undefined && +epochIndex < currentIndex) {
|
|
279
|
+
const epochData = await (0, arweave_js_1.getEpochDataFromGql)({
|
|
280
|
+
arweave: this.arweave,
|
|
281
|
+
epochIndex: +epochIndex,
|
|
282
|
+
processId: this.process.processId,
|
|
283
|
+
});
|
|
284
|
+
return epochData?.distributions;
|
|
285
|
+
}
|
|
286
|
+
// go to the process epoch and fetch the distributions
|
|
232
287
|
const allTags = [
|
|
233
288
|
{ name: 'Action', value: 'Epoch-Distributions' },
|
|
234
289
|
{ name: 'Epoch-Index', value: await this.computeEpochIndex(epoch) },
|
|
@@ -419,12 +474,13 @@ class ARIOReadable {
|
|
|
419
474
|
exports.ARIOReadable = ARIOReadable;
|
|
420
475
|
class ARIOWriteable extends ARIOReadable {
|
|
421
476
|
signer;
|
|
422
|
-
constructor({ signer, ...config }) {
|
|
477
|
+
constructor({ signer, arweave, ...config }) {
|
|
423
478
|
if (Object.keys(config).length === 0) {
|
|
424
479
|
super({
|
|
425
480
|
process: new ao_process_js_1.AOProcess({
|
|
426
481
|
processId: constants_js_1.ARIO_TESTNET_PROCESS_ID,
|
|
427
482
|
}),
|
|
483
|
+
arweave: arweave,
|
|
428
484
|
});
|
|
429
485
|
this.signer = (0, ao_js_1.createAoSigner)(signer);
|
|
430
486
|
}
|
|
@@ -437,6 +493,7 @@ class ARIOWriteable extends ARIOReadable {
|
|
|
437
493
|
process: new ao_process_js_1.AOProcess({
|
|
438
494
|
processId: config.processId,
|
|
439
495
|
}),
|
|
496
|
+
arweave: arweave,
|
|
440
497
|
});
|
|
441
498
|
this.signer = (0, ao_js_1.createAoSigner)(signer);
|
|
442
499
|
}
|
package/lib/cjs/constants.js
CHANGED
|
@@ -29,6 +29,6 @@ exports.arioDevnetProcessId = exports.ARIO_DEVNET_PROCESS_ID;
|
|
|
29
29
|
exports.ARIO_TESTNET_PROCESS_ID = 'agYcCFJtrMG6cqMuZfskIkFTGvUPddICmtQSBIoPdiA';
|
|
30
30
|
exports.ANT_REGISTRY_ID = 'i_le_yKKPVstLTDSmkHRqf-wYphMnwB9OhleiTgMkWc';
|
|
31
31
|
exports.MARIO_PER_ARIO = 1_000_000;
|
|
32
|
-
exports.AOS_MODULE_ID = '
|
|
32
|
+
exports.AOS_MODULE_ID = 'tyojTpJ9fIva_7BjTA9ruGM4uk2vyTfhVUulnbVxR8I';
|
|
33
33
|
exports.ANT_LUA_ID = '16_FyX-V2QU0RPSh1GIaEETSaUjNb0oVjCFpVbAfQq4';
|
|
34
34
|
exports.DEFAULT_SCHEDULER_ID = '_GQ33BkPtZrqxA84vM8Zk-N2aO0toNNu_C-l-rawrBA';
|
package/lib/cjs/utils/ao.js
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
"use strict";
|
|
2
2
|
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
-
exports.initANTStateForAddress = exports.defaultTargetManifestId = exports.createAoSigner = exports.isAoSigner = exports.evolveANT = exports.spawnANT = void 0;
|
|
3
|
+
exports.parseAoEpochData = exports.initANTStateForAddress = exports.defaultTargetManifestId = exports.createAoSigner = exports.isAoSigner = exports.evolveANT = exports.spawnANT = void 0;
|
|
4
4
|
/**
|
|
5
5
|
* Copyright (C) 2022-2024 Permanent Data Solutions, Inc.
|
|
6
6
|
*
|
|
@@ -22,12 +22,8 @@ const zod_1 = require("zod");
|
|
|
22
22
|
const arweave_js_1 = require("../common/arweave.js");
|
|
23
23
|
const index_js_1 = require("../common/index.js");
|
|
24
24
|
const constants_js_1 = require("../constants.js");
|
|
25
|
-
async function spawnANT({ signer, module = constants_js_1.AOS_MODULE_ID,
|
|
26
|
-
//TODO:
|
|
27
|
-
const luaString = (await arweave.transactions.getData(luaCodeTxId, {
|
|
28
|
-
decode: true,
|
|
29
|
-
string: true,
|
|
30
|
-
}));
|
|
25
|
+
async function spawnANT({ signer, module = constants_js_1.AOS_MODULE_ID, ao = (0, aoconnect_1.connect)(), scheduler = constants_js_1.DEFAULT_SCHEDULER_ID, state, stateContractTxId, antRegistryId = constants_js_1.ANT_REGISTRY_ID, logger = index_js_1.Logger.default, }) {
|
|
26
|
+
// TODO: use On-Boot data handler for bootstrapping state instead of initialize-state
|
|
31
27
|
const processId = await ao.spawn({
|
|
32
28
|
module,
|
|
33
29
|
scheduler,
|
|
@@ -37,10 +33,6 @@ async function spawnANT({ signer, module = constants_js_1.AOS_MODULE_ID, luaCode
|
|
|
37
33
|
name: 'ANT-Registry-Id',
|
|
38
34
|
value: antRegistryId,
|
|
39
35
|
},
|
|
40
|
-
{
|
|
41
|
-
name: 'Source-Code-TX-ID', // utility for understanding what the original source id of the lua code was
|
|
42
|
-
value: luaCodeTxId,
|
|
43
|
-
},
|
|
44
36
|
],
|
|
45
37
|
});
|
|
46
38
|
const aosClient = new index_js_1.AOProcess({
|
|
@@ -48,21 +40,10 @@ async function spawnANT({ signer, module = constants_js_1.AOS_MODULE_ID, luaCode
|
|
|
48
40
|
ao,
|
|
49
41
|
logger,
|
|
50
42
|
});
|
|
51
|
-
const { id: evalId } = await aosClient.send({
|
|
52
|
-
tags: [
|
|
53
|
-
{ name: 'Action', value: 'Eval' },
|
|
54
|
-
{ name: 'App-Name', value: 'ArNS-ANT' },
|
|
55
|
-
{ name: 'Source-Code-TX-ID', value: luaCodeTxId },
|
|
56
|
-
],
|
|
57
|
-
data: luaString,
|
|
58
|
-
signer,
|
|
59
|
-
});
|
|
60
43
|
logger.debug(`Spawned ANT`, {
|
|
61
44
|
processId,
|
|
62
45
|
module,
|
|
63
46
|
scheduler,
|
|
64
|
-
luaCodeTxId,
|
|
65
|
-
evalId,
|
|
66
47
|
});
|
|
67
48
|
if (state) {
|
|
68
49
|
const { id: initializeMsgId } = await aosClient.send({
|
|
@@ -82,6 +63,7 @@ async function spawnANT({ signer, module = constants_js_1.AOS_MODULE_ID, luaCode
|
|
|
82
63
|
initializeMsgId,
|
|
83
64
|
});
|
|
84
65
|
}
|
|
66
|
+
// This could be done by the ANT in On-Boot to self-register with its tagged ANT registry
|
|
85
67
|
const registryClient = index_js_1.ANTRegistry.init({
|
|
86
68
|
process: new index_js_1.AOProcess({
|
|
87
69
|
processId: antRegistryId,
|
|
@@ -200,3 +182,21 @@ function initANTStateForAddress({ owner, targetId, ttlSeconds = 3600, keywords =
|
|
|
200
182
|
};
|
|
201
183
|
}
|
|
202
184
|
exports.initANTStateForAddress = initANTStateForAddress;
|
|
185
|
+
/**
|
|
186
|
+
* Uses zod schema to parse the epoch data
|
|
187
|
+
*/
|
|
188
|
+
function parseAoEpochData(value) {
|
|
189
|
+
const epochDataSchema = zod_1.z.object({
|
|
190
|
+
startTimestamp: zod_1.z.number(),
|
|
191
|
+
startHeight: zod_1.z.number(),
|
|
192
|
+
distributions: zod_1.z.any(),
|
|
193
|
+
endTimestamp: zod_1.z.number(),
|
|
194
|
+
prescribedObservers: zod_1.z.any(),
|
|
195
|
+
prescribedNames: zod_1.z.array(zod_1.z.string()),
|
|
196
|
+
observations: zod_1.z.any(),
|
|
197
|
+
distributionTimestamp: zod_1.z.number(),
|
|
198
|
+
epochIndex: zod_1.z.number(),
|
|
199
|
+
});
|
|
200
|
+
return epochDataSchema.parse(value);
|
|
201
|
+
}
|
|
202
|
+
exports.parseAoEpochData = parseAoEpochData;
|
package/lib/cjs/utils/arweave.js
CHANGED
|
@@ -1,22 +1,8 @@
|
|
|
1
1
|
"use strict";
|
|
2
2
|
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
-
exports.paginationParamsToTags = exports.pruneTags = exports.isBlockHeight = exports.validateArweaveId = void 0;
|
|
4
|
-
/**
|
|
5
|
-
* Copyright (C) 2022-2024 Permanent Data Solutions, Inc.
|
|
6
|
-
*
|
|
7
|
-
* Licensed under the Apache License, Version 2.0 (the "License");
|
|
8
|
-
* you may not use this file except in compliance with the License.
|
|
9
|
-
* You may obtain a copy of the License at
|
|
10
|
-
*
|
|
11
|
-
* http://www.apache.org/licenses/LICENSE-2.0
|
|
12
|
-
*
|
|
13
|
-
* Unless required by applicable law or agreed to in writing, software
|
|
14
|
-
* distributed under the License is distributed on an "AS IS" BASIS,
|
|
15
|
-
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
|
16
|
-
* See the License for the specific language governing permissions and
|
|
17
|
-
* limitations under the License.
|
|
18
|
-
*/
|
|
3
|
+
exports.epochDistributionNoticeGqlQuery = exports.getEpochDataFromGql = exports.paginationParamsToTags = exports.pruneTags = exports.isBlockHeight = exports.validateArweaveId = void 0;
|
|
19
4
|
const constants_js_1 = require("../constants.js");
|
|
5
|
+
const ao_js_1 = require("./ao.js");
|
|
20
6
|
const validateArweaveId = (id) => {
|
|
21
7
|
return constants_js_1.ARWEAVE_TX_REGEX.test(id);
|
|
22
8
|
};
|
|
@@ -44,3 +30,52 @@ const paginationParamsToTags = (params) => {
|
|
|
44
30
|
return (0, exports.pruneTags)(tags);
|
|
45
31
|
};
|
|
46
32
|
exports.paginationParamsToTags = paginationParamsToTags;
|
|
33
|
+
/**
|
|
34
|
+
* Get the epoch with distribution data for the current epoch
|
|
35
|
+
* @param arweave - The Arweave instance
|
|
36
|
+
* @returns The epoch with distribution data
|
|
37
|
+
*/
|
|
38
|
+
const getEpochDataFromGql = async ({ arweave, epochIndex, processId = constants_js_1.ARIO_TESTNET_PROCESS_ID, }) => {
|
|
39
|
+
// fetch from gql
|
|
40
|
+
const query = (0, exports.epochDistributionNoticeGqlQuery)({ epochIndex, processId });
|
|
41
|
+
const response = await arweave.api.post('graphql', query);
|
|
42
|
+
// parse the nodes to get the id
|
|
43
|
+
if (response.data.data.transactions?.edges?.length === 0) {
|
|
44
|
+
return undefined;
|
|
45
|
+
}
|
|
46
|
+
const id = response.data.data.transactions.edges[0].node.id;
|
|
47
|
+
// fetch the transaction from arweave
|
|
48
|
+
const transaction = await arweave.api.get(id);
|
|
49
|
+
const data = transaction.data;
|
|
50
|
+
// assert it is the correct type
|
|
51
|
+
return (0, ao_js_1.parseAoEpochData)(data);
|
|
52
|
+
};
|
|
53
|
+
exports.getEpochDataFromGql = getEpochDataFromGql;
|
|
54
|
+
const epochDistributionNoticeGqlQuery = ({ epochIndex, processId = constants_js_1.ARIO_TESTNET_PROCESS_ID, }) => {
|
|
55
|
+
// write the query
|
|
56
|
+
const gqlQuery = JSON.stringify({
|
|
57
|
+
query: `
|
|
58
|
+
query {
|
|
59
|
+
transactions(
|
|
60
|
+
tags: [
|
|
61
|
+
{ name: "From-Process", values: ["${processId}"] }
|
|
62
|
+
{ name: "Action", values: ["Epoch-Distribution-Notice"] }
|
|
63
|
+
{ name: "Epoch-Index", values: ["${epochIndex}"] }
|
|
64
|
+
{ name: "Data-Protocol", values: ["ao"] }
|
|
65
|
+
],
|
|
66
|
+
owners: ["fcoN_xJeisVsPXA-trzVAuIiqO3ydLQxM-L4XbrQKzY"],
|
|
67
|
+
first: 1,
|
|
68
|
+
sort: HEIGHT_DESC
|
|
69
|
+
) {
|
|
70
|
+
edges {
|
|
71
|
+
node {
|
|
72
|
+
id
|
|
73
|
+
}
|
|
74
|
+
}
|
|
75
|
+
}
|
|
76
|
+
}
|
|
77
|
+
`,
|
|
78
|
+
});
|
|
79
|
+
return gqlQuery;
|
|
80
|
+
};
|
|
81
|
+
exports.epochDistributionNoticeGqlQuery = epochDistributionNoticeGqlQuery;
|
package/lib/cjs/version.js
CHANGED
package/lib/esm/cli/cli.js
CHANGED
|
@@ -143,13 +143,17 @@ makeCommand({
|
|
|
143
143
|
name: 'get-observations',
|
|
144
144
|
description: 'Get observations for an epoch',
|
|
145
145
|
options: epochOptions,
|
|
146
|
-
action: (o) => readARIOFromOptions(o)
|
|
146
|
+
action: (o) => readARIOFromOptions(o)
|
|
147
|
+
.getObservations(epochInputFromOptions(o))
|
|
148
|
+
.then((result) => result ?? { message: 'No observations found for epoch' }),
|
|
147
149
|
});
|
|
148
150
|
makeCommand({
|
|
149
151
|
name: 'get-distributions',
|
|
150
152
|
description: 'Get distributions for an epoch',
|
|
151
153
|
options: epochOptions,
|
|
152
|
-
action: (o) => readARIOFromOptions(o)
|
|
154
|
+
action: (o) => readARIOFromOptions(o)
|
|
155
|
+
.getDistributions(epochInputFromOptions(o))
|
|
156
|
+
.then((result) => result ?? { message: 'No distributions found for epoch' }),
|
|
153
157
|
});
|
|
154
158
|
makeCommand({
|
|
155
159
|
name: 'get-token-cost',
|
|
@@ -21,8 +21,8 @@ import { WriteInteractionError } from '../error.js';
|
|
|
21
21
|
import { Logger } from '../logger.js';
|
|
22
22
|
export class AOProcess {
|
|
23
23
|
logger;
|
|
24
|
-
processId;
|
|
25
24
|
ao;
|
|
25
|
+
processId;
|
|
26
26
|
constructor({ processId, ao = connect(), logger = Logger.default, }) {
|
|
27
27
|
this.processId = processId;
|
|
28
28
|
this.logger = logger;
|
package/lib/esm/common/io.js
CHANGED
|
@@ -13,29 +13,39 @@
|
|
|
13
13
|
* See the License for the specific language governing permissions and
|
|
14
14
|
* limitations under the License.
|
|
15
15
|
*/
|
|
16
|
+
import Arweave from 'arweave';
|
|
16
17
|
import { ARIO_TESTNET_PROCESS_ID } from '../constants.js';
|
|
17
18
|
import { isProcessConfiguration, isProcessIdConfiguration, } from '../types/io.js';
|
|
18
19
|
import { createAoSigner } from '../utils/ao.js';
|
|
19
|
-
import { paginationParamsToTags, pruneTags } from '../utils/arweave.js';
|
|
20
|
+
import { getEpochDataFromGql, paginationParamsToTags, pruneTags, } from '../utils/arweave.js';
|
|
20
21
|
import { AOProcess } from './contracts/ao-process.js';
|
|
21
22
|
import { InvalidContractConfigurationError } from './error.js';
|
|
22
23
|
export class ARIO {
|
|
23
|
-
static init(config) {
|
|
24
|
-
if (config && config.signer) {
|
|
24
|
+
static init({ arweave, ...config } = {}) {
|
|
25
|
+
if (config !== undefined && config.signer) {
|
|
25
26
|
const { signer, ...rest } = config;
|
|
26
27
|
return new ARIOWriteable({
|
|
27
28
|
...rest,
|
|
28
29
|
signer,
|
|
30
|
+
arweave,
|
|
29
31
|
});
|
|
30
32
|
}
|
|
31
|
-
return new ARIOReadable(
|
|
33
|
+
return new ARIOReadable({
|
|
34
|
+
arweave,
|
|
35
|
+
...config,
|
|
36
|
+
});
|
|
32
37
|
}
|
|
33
38
|
}
|
|
34
39
|
export class ARIOReadable {
|
|
35
40
|
process;
|
|
36
41
|
epochSettings;
|
|
37
|
-
|
|
38
|
-
|
|
42
|
+
arweave;
|
|
43
|
+
constructor({ arweave = Arweave.init({
|
|
44
|
+
host: 'arweave.net',
|
|
45
|
+
port: 443,
|
|
46
|
+
protocol: 'https',
|
|
47
|
+
}), ...config }) {
|
|
48
|
+
if (config === undefined) {
|
|
39
49
|
this.process = new AOProcess({
|
|
40
50
|
processId: ARIO_TESTNET_PROCESS_ID,
|
|
41
51
|
});
|
|
@@ -51,6 +61,7 @@ export class ARIOReadable {
|
|
|
51
61
|
else {
|
|
52
62
|
throw new InvalidContractConfigurationError();
|
|
53
63
|
}
|
|
64
|
+
this.arweave = arweave;
|
|
54
65
|
}
|
|
55
66
|
async getInfo() {
|
|
56
67
|
return this.process.read({
|
|
@@ -68,6 +79,13 @@ export class ARIOReadable {
|
|
|
68
79
|
const epochLengthMs = epochSettings.durationMs;
|
|
69
80
|
return Math.floor((timestamp - epochZeroStartTimestamp) / epochLengthMs);
|
|
70
81
|
}
|
|
82
|
+
async computeCurrentEpochIndex() {
|
|
83
|
+
const epochSettings = await this.getEpochSettings();
|
|
84
|
+
const epochZeroStartTimestamp = epochSettings.epochZeroStartTimestamp;
|
|
85
|
+
const epochLengthMs = epochSettings.durationMs;
|
|
86
|
+
const currentTimestamp = Date.now();
|
|
87
|
+
return Math.floor((currentTimestamp - epochZeroStartTimestamp) / epochLengthMs);
|
|
88
|
+
}
|
|
71
89
|
async computeEpochIndex(params) {
|
|
72
90
|
const epochIndex = params?.epochIndex;
|
|
73
91
|
if (epochIndex !== undefined) {
|
|
@@ -85,6 +103,17 @@ export class ARIOReadable {
|
|
|
85
103
|
}));
|
|
86
104
|
}
|
|
87
105
|
async getEpoch(epoch) {
|
|
106
|
+
const epochIndex = await this.computeEpochIndex(epoch);
|
|
107
|
+
const currentIndex = await this.computeCurrentEpochIndex();
|
|
108
|
+
if (epochIndex !== undefined && +epochIndex < currentIndex) {
|
|
109
|
+
const epochData = await getEpochDataFromGql({
|
|
110
|
+
arweave: this.arweave,
|
|
111
|
+
epochIndex: +epochIndex,
|
|
112
|
+
processId: this.process.processId,
|
|
113
|
+
});
|
|
114
|
+
return epochData;
|
|
115
|
+
}
|
|
116
|
+
// go to the process epoch and fetch the epoch data
|
|
88
117
|
const allTags = [
|
|
89
118
|
{ name: 'Action', value: 'Epoch' },
|
|
90
119
|
{ name: 'Epoch-Index', value: await this.computeEpochIndex(epoch) },
|
|
@@ -215,7 +244,19 @@ export class ARIOReadable {
|
|
|
215
244
|
tags: pruneTags(allTags),
|
|
216
245
|
});
|
|
217
246
|
}
|
|
247
|
+
// we need to find the epoch index for the epoch that is currently being distributed and fetch it from gql
|
|
218
248
|
async getObservations(epoch) {
|
|
249
|
+
const epochIndex = await this.computeEpochIndex(epoch);
|
|
250
|
+
const currentIndex = await this.computeCurrentEpochIndex();
|
|
251
|
+
if (epochIndex !== undefined && +epochIndex < currentIndex) {
|
|
252
|
+
const epochData = await getEpochDataFromGql({
|
|
253
|
+
arweave: this.arweave,
|
|
254
|
+
epochIndex: +epochIndex,
|
|
255
|
+
processId: this.process.processId,
|
|
256
|
+
});
|
|
257
|
+
return epochData?.observations;
|
|
258
|
+
}
|
|
259
|
+
// go to the process epoch and fetch the observations
|
|
219
260
|
const allTags = [
|
|
220
261
|
{ name: 'Action', value: 'Epoch-Observations' },
|
|
221
262
|
{ name: 'Epoch-Index', value: await this.computeEpochIndex(epoch) },
|
|
@@ -225,6 +266,17 @@ export class ARIOReadable {
|
|
|
225
266
|
});
|
|
226
267
|
}
|
|
227
268
|
async getDistributions(epoch) {
|
|
269
|
+
const epochIndex = await this.computeEpochIndex(epoch);
|
|
270
|
+
const currentIndex = await this.computeCurrentEpochIndex();
|
|
271
|
+
if (epochIndex !== undefined && +epochIndex < currentIndex) {
|
|
272
|
+
const epochData = await getEpochDataFromGql({
|
|
273
|
+
arweave: this.arweave,
|
|
274
|
+
epochIndex: +epochIndex,
|
|
275
|
+
processId: this.process.processId,
|
|
276
|
+
});
|
|
277
|
+
return epochData?.distributions;
|
|
278
|
+
}
|
|
279
|
+
// go to the process epoch and fetch the distributions
|
|
228
280
|
const allTags = [
|
|
229
281
|
{ name: 'Action', value: 'Epoch-Distributions' },
|
|
230
282
|
{ name: 'Epoch-Index', value: await this.computeEpochIndex(epoch) },
|
|
@@ -414,12 +466,13 @@ export class ARIOReadable {
|
|
|
414
466
|
}
|
|
415
467
|
export class ARIOWriteable extends ARIOReadable {
|
|
416
468
|
signer;
|
|
417
|
-
constructor({ signer, ...config }) {
|
|
469
|
+
constructor({ signer, arweave, ...config }) {
|
|
418
470
|
if (Object.keys(config).length === 0) {
|
|
419
471
|
super({
|
|
420
472
|
process: new AOProcess({
|
|
421
473
|
processId: ARIO_TESTNET_PROCESS_ID,
|
|
422
474
|
}),
|
|
475
|
+
arweave: arweave,
|
|
423
476
|
});
|
|
424
477
|
this.signer = createAoSigner(signer);
|
|
425
478
|
}
|
|
@@ -432,6 +485,7 @@ export class ARIOWriteable extends ARIOReadable {
|
|
|
432
485
|
process: new AOProcess({
|
|
433
486
|
processId: config.processId,
|
|
434
487
|
}),
|
|
488
|
+
arweave: arweave,
|
|
435
489
|
});
|
|
436
490
|
this.signer = createAoSigner(signer);
|
|
437
491
|
}
|
package/lib/esm/constants.js
CHANGED
|
@@ -26,6 +26,6 @@ export const arioDevnetProcessId = ARIO_DEVNET_PROCESS_ID;
|
|
|
26
26
|
export const ARIO_TESTNET_PROCESS_ID = 'agYcCFJtrMG6cqMuZfskIkFTGvUPddICmtQSBIoPdiA';
|
|
27
27
|
export const ANT_REGISTRY_ID = 'i_le_yKKPVstLTDSmkHRqf-wYphMnwB9OhleiTgMkWc';
|
|
28
28
|
export const MARIO_PER_ARIO = 1_000_000;
|
|
29
|
-
export const AOS_MODULE_ID = '
|
|
29
|
+
export const AOS_MODULE_ID = 'tyojTpJ9fIva_7BjTA9ruGM4uk2vyTfhVUulnbVxR8I';
|
|
30
30
|
export const ANT_LUA_ID = '16_FyX-V2QU0RPSh1GIaEETSaUjNb0oVjCFpVbAfQq4';
|
|
31
31
|
export const DEFAULT_SCHEDULER_ID = '_GQ33BkPtZrqxA84vM8Zk-N2aO0toNNu_C-l-rawrBA';
|
package/lib/esm/utils/ao.js
CHANGED
|
@@ -19,12 +19,8 @@ import { z } from 'zod';
|
|
|
19
19
|
import { defaultArweave } from '../common/arweave.js';
|
|
20
20
|
import { ANTRegistry, AOProcess, Logger } from '../common/index.js';
|
|
21
21
|
import { ANT_LUA_ID, ANT_REGISTRY_ID, AOS_MODULE_ID, DEFAULT_SCHEDULER_ID, } from '../constants.js';
|
|
22
|
-
export async function spawnANT({ signer, module = AOS_MODULE_ID,
|
|
23
|
-
//TODO:
|
|
24
|
-
const luaString = (await arweave.transactions.getData(luaCodeTxId, {
|
|
25
|
-
decode: true,
|
|
26
|
-
string: true,
|
|
27
|
-
}));
|
|
22
|
+
export async function spawnANT({ signer, module = AOS_MODULE_ID, ao = connect(), scheduler = DEFAULT_SCHEDULER_ID, state, stateContractTxId, antRegistryId = ANT_REGISTRY_ID, logger = Logger.default, }) {
|
|
23
|
+
// TODO: use On-Boot data handler for bootstrapping state instead of initialize-state
|
|
28
24
|
const processId = await ao.spawn({
|
|
29
25
|
module,
|
|
30
26
|
scheduler,
|
|
@@ -34,10 +30,6 @@ export async function spawnANT({ signer, module = AOS_MODULE_ID, luaCodeTxId = A
|
|
|
34
30
|
name: 'ANT-Registry-Id',
|
|
35
31
|
value: antRegistryId,
|
|
36
32
|
},
|
|
37
|
-
{
|
|
38
|
-
name: 'Source-Code-TX-ID', // utility for understanding what the original source id of the lua code was
|
|
39
|
-
value: luaCodeTxId,
|
|
40
|
-
},
|
|
41
33
|
],
|
|
42
34
|
});
|
|
43
35
|
const aosClient = new AOProcess({
|
|
@@ -45,21 +37,10 @@ export async function spawnANT({ signer, module = AOS_MODULE_ID, luaCodeTxId = A
|
|
|
45
37
|
ao,
|
|
46
38
|
logger,
|
|
47
39
|
});
|
|
48
|
-
const { id: evalId } = await aosClient.send({
|
|
49
|
-
tags: [
|
|
50
|
-
{ name: 'Action', value: 'Eval' },
|
|
51
|
-
{ name: 'App-Name', value: 'ArNS-ANT' },
|
|
52
|
-
{ name: 'Source-Code-TX-ID', value: luaCodeTxId },
|
|
53
|
-
],
|
|
54
|
-
data: luaString,
|
|
55
|
-
signer,
|
|
56
|
-
});
|
|
57
40
|
logger.debug(`Spawned ANT`, {
|
|
58
41
|
processId,
|
|
59
42
|
module,
|
|
60
43
|
scheduler,
|
|
61
|
-
luaCodeTxId,
|
|
62
|
-
evalId,
|
|
63
44
|
});
|
|
64
45
|
if (state) {
|
|
65
46
|
const { id: initializeMsgId } = await aosClient.send({
|
|
@@ -79,6 +60,7 @@ export async function spawnANT({ signer, module = AOS_MODULE_ID, luaCodeTxId = A
|
|
|
79
60
|
initializeMsgId,
|
|
80
61
|
});
|
|
81
62
|
}
|
|
63
|
+
// This could be done by the ANT in On-Boot to self-register with its tagged ANT registry
|
|
82
64
|
const registryClient = ANTRegistry.init({
|
|
83
65
|
process: new AOProcess({
|
|
84
66
|
processId: antRegistryId,
|
|
@@ -192,3 +174,20 @@ export function initANTStateForAddress({ owner, targetId, ttlSeconds = 3600, key
|
|
|
192
174
|
},
|
|
193
175
|
};
|
|
194
176
|
}
|
|
177
|
+
/**
|
|
178
|
+
* Uses zod schema to parse the epoch data
|
|
179
|
+
*/
|
|
180
|
+
export function parseAoEpochData(value) {
|
|
181
|
+
const epochDataSchema = z.object({
|
|
182
|
+
startTimestamp: z.number(),
|
|
183
|
+
startHeight: z.number(),
|
|
184
|
+
distributions: z.any(),
|
|
185
|
+
endTimestamp: z.number(),
|
|
186
|
+
prescribedObservers: z.any(),
|
|
187
|
+
prescribedNames: z.array(z.string()),
|
|
188
|
+
observations: z.any(),
|
|
189
|
+
distributionTimestamp: z.number(),
|
|
190
|
+
epochIndex: z.number(),
|
|
191
|
+
});
|
|
192
|
+
return epochDataSchema.parse(value);
|
|
193
|
+
}
|