@ar.io/sdk 3.12.0-alpha.1 → 3.12.0

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.
@@ -1,148 +0,0 @@
1
- import { strict as assert } from 'node:assert';
2
- import { describe, it } from 'node:test';
3
- import { z } from 'zod';
4
- import { AntHandlerNames, AntInfoSchema, AntStateSchema, isAoANTState, } from './ant.js';
5
- const stub_arweave_address = 'valid-address'.padEnd(43, '1');
6
- const stub_eth_address = '0x8B5f221c8837d862b818799e6b19cc23CE23B0E5';
7
- describe('ANT Schemas', () => {
8
- it('should validate AntStateSchema', () => {
9
- const validState = {
10
- Name: 'TestToken',
11
- Ticker: 'TST',
12
- Description: 'Test description',
13
- Keywords: ['keyword1', 'keyword2', 'keyword3'],
14
- Denomination: 0,
15
- Owner: stub_arweave_address,
16
- Controllers: [stub_arweave_address],
17
- Records: {
18
- record1: {
19
- transactionId: stub_arweave_address,
20
- ttlSeconds: 3600,
21
- },
22
- },
23
- Balances: {
24
- [stub_arweave_address]: 1,
25
- },
26
- Logo: stub_arweave_address,
27
- TotalSupply: 1,
28
- Initialized: true,
29
- };
30
- const invalidState = {
31
- Name: 'TestToken',
32
- Ticker: 'TST',
33
- Description: 'Test description',
34
- Keywords: ['keyword1', 'keyword2', 'keyword3'],
35
- Denomination: 0,
36
- Owner: stub_arweave_address,
37
- Controllers: [stub_arweave_address],
38
- Records: {
39
- record1: {
40
- transactionId: 'invalid-id',
41
- ttlSeconds: '3600',
42
- },
43
- },
44
- Balances: {
45
- [stub_arweave_address]: 1,
46
- },
47
- Logo: stub_arweave_address,
48
- TotalSupply: -1,
49
- Initialized: true,
50
- };
51
- assert.doesNotThrow(() => AntStateSchema.parse(validState));
52
- assert.throws(() => AntStateSchema.parse(invalidState), z.ZodError);
53
- });
54
- it('should validate AntStateSchema with ETH address', () => {
55
- const validState = {
56
- Name: 'TestToken',
57
- Ticker: 'TST',
58
- Description: 'Test description',
59
- Keywords: ['keyword1', 'keyword2', 'keyword3'],
60
- Denomination: 0,
61
- Owner: stub_eth_address,
62
- Controllers: [stub_eth_address],
63
- Records: {
64
- record1: {
65
- transactionId: stub_arweave_address,
66
- ttlSeconds: 3600,
67
- },
68
- },
69
- Balances: {
70
- [stub_eth_address]: 1,
71
- },
72
- Logo: stub_arweave_address,
73
- TotalSupply: 1,
74
- Initialized: true,
75
- };
76
- assert.doesNotThrow(() => AntStateSchema.parse(validState));
77
- });
78
- it('should validate AntInfoSchema', () => {
79
- const validInfo = {
80
- Name: 'TestToken',
81
- Owner: stub_arweave_address,
82
- Ticker: 'TST',
83
- Description: 'Test description',
84
- Keywords: ['keyword1', 'keyword2', 'keyword3'],
85
- ['Total-Supply']: '1',
86
- Logo: stub_arweave_address,
87
- Denomination: '0',
88
- Handlers: AntHandlerNames,
89
- };
90
- const invalidInfo = {
91
- Name: 'TestToken',
92
- Owner: stub_arweave_address,
93
- Ticker: 'TST',
94
- ['Total-Supply']: 1000,
95
- Logo: stub_arweave_address,
96
- Denomination: '1',
97
- Handlers: AntHandlerNames,
98
- };
99
- assert.doesNotThrow(() => AntInfoSchema.parse(validInfo));
100
- assert.throws(() => AntInfoSchema.parse(invalidInfo), z.ZodError);
101
- });
102
- it('should validate isAoANTState', () => {
103
- const validState = {
104
- Name: 'TestToken',
105
- Ticker: 'TST',
106
- Description: 'Test description',
107
- Keywords: ['keyword1', 'keyword2', 'keyword3'],
108
- Denomination: 0,
109
- Owner: stub_arweave_address,
110
- Controllers: [stub_arweave_address],
111
- Records: {
112
- record1: {
113
- transactionId: stub_arweave_address,
114
- ttlSeconds: 3600,
115
- },
116
- },
117
- Balances: {
118
- [stub_arweave_address]: 1,
119
- },
120
- Logo: stub_arweave_address,
121
- TotalSupply: 0,
122
- Initialized: true,
123
- };
124
- const invalidState = {
125
- Name: 'TestToken',
126
- Ticker: 'TST',
127
- Description: 'Test description',
128
- Keywords: ['keyword1', 'keyword2', 'keyword3'],
129
- Denomination: 0,
130
- Owner: stub_arweave_address,
131
- Controllers: [stub_arweave_address],
132
- Records: {
133
- record1: {
134
- transactionId: 'invalid-id',
135
- ttlSeconds: '3600',
136
- },
137
- },
138
- Balances: {
139
- [stub_arweave_address]: 1,
140
- },
141
- Logo: stub_arweave_address,
142
- TotalSupply: -1,
143
- Initialized: true,
144
- };
145
- assert.strictEqual(isAoANTState(validState), true);
146
- assert.strictEqual(isAoANTState(invalidState), false);
147
- });
148
- });
@@ -1,81 +0,0 @@
1
- import { strict as assert } from 'node:assert';
2
- import { describe, it } from 'node:test';
3
- import { ARIOToken, mARIOToken } from './token.js';
4
- describe('ARIOToken', () => {
5
- it('should throw an error on invalid input', () => {
6
- assert.throws(() => new ARIOToken(-1));
7
- });
8
- it('should round at 6 decimal places', () => {
9
- const token = new ARIOToken(1.123456789);
10
- assert.strictEqual(token.valueOf(), 1.123457);
11
- });
12
- it('should convert to mARIOToken', () => {
13
- const token = new ARIOToken(1);
14
- const mToken = token.toMARIO();
15
- assert.strictEqual(mToken.valueOf(), 1000000);
16
- });
17
- it('should print as a string', () => {
18
- const token = new ARIOToken(1);
19
- assert.strictEqual(`${token}`, '1');
20
- });
21
- });
22
- describe('mARIOToken', () => {
23
- it('should multiply by a number', () => {
24
- const token = new mARIOToken(1);
25
- const result = token.multiply(2);
26
- assert.strictEqual(result.valueOf(), 2);
27
- });
28
- it('should multiply by another mARIOToken', () => {
29
- const token = new mARIOToken(1);
30
- const result = token.multiply(new mARIOToken(2));
31
- assert.strictEqual(result.valueOf(), 2);
32
- });
33
- it('should divide by a number', () => {
34
- const token = new mARIOToken(2);
35
- const result = token.divide(2);
36
- assert.strictEqual(result.valueOf(), 1);
37
- });
38
- it('should divide by another mARIOToken', () => {
39
- const token = new mARIOToken(2);
40
- const result = token.divide(new mARIOToken(2));
41
- assert.strictEqual(result.valueOf(), 1);
42
- });
43
- it('should throw an error on division by zero', () => {
44
- const token = new mARIOToken(2);
45
- assert.throws(() => token.divide(0));
46
- });
47
- it('should round down on multiplication of a number', () => {
48
- const token = new mARIOToken(1);
49
- const result = token.multiply(1.5);
50
- assert.strictEqual(result.valueOf(), 1);
51
- });
52
- it('should round down on division with a number', () => {
53
- const token = new mARIOToken(2);
54
- const result = token.divide(3);
55
- assert.strictEqual(result.valueOf(), 0);
56
- });
57
- it('should round down on division with another mARIOToken', () => {
58
- const token = new mARIOToken(2);
59
- const result = token.divide(new mARIOToken(3));
60
- assert.strictEqual(result.valueOf(), 0);
61
- });
62
- it('should add', () => {
63
- const token = new mARIOToken(1);
64
- const result = token.plus(new mARIOToken(1));
65
- assert.strictEqual(result.valueOf(), 2);
66
- });
67
- it('should subtract', () => {
68
- const token = new mARIOToken(2);
69
- const result = token.minus(new mARIOToken(1));
70
- assert.strictEqual(result.valueOf(), 1);
71
- });
72
- it('should convert to ARIO', () => {
73
- const token = new mARIOToken(1000000);
74
- const result = token.toARIO();
75
- assert.strictEqual(result.valueOf(), 1);
76
- });
77
- it('should print as a string', () => {
78
- const token = new mARIOToken(1);
79
- assert.strictEqual(`${token}`, '1');
80
- });
81
- });
@@ -1,109 +0,0 @@
1
- import { strict as assert } from 'node:assert';
2
- import { describe, it } from 'node:test';
3
- import { sortANTRecords } from './ant.js';
4
- describe('sortANTRecordsByPriority', () => {
5
- it('should sort records by priority and then lexicographically', () => {
6
- const records = {
7
- undername01: { priority: 1, transactionId: 'test', ttlSeconds: 1 }, // same priority, lexicographic sorting applied to the name
8
- undername1: { priority: 1, transactionId: 'test', ttlSeconds: 1 },
9
- undername11: { priority: 1, transactionId: 'test', ttlSeconds: 1 }, // same priority, lexicographic sorting applied to the name
10
- undername2: { priority: 2, transactionId: 'test', ttlSeconds: 1 },
11
- undername3: { priority: 3, transactionId: 'test', ttlSeconds: 1 }, // colliding priorities default to lexicographic sorting
12
- undername4: { priority: 3, transactionId: 'test', ttlSeconds: 1 },
13
- undername5: { priority: 100, transactionId: 'test', ttlSeconds: 1 }, // priority does not represent the index or position of the record, just the order of resolution relative to other records
14
- noPriority: { transactionId: 'test', ttlSeconds: 1 },
15
- noPriority01: { transactionId: 'test', ttlSeconds: 1 },
16
- noPriority1: { transactionId: 'test', ttlSeconds: 1 },
17
- noPriority11: { transactionId: 'test', ttlSeconds: 1 },
18
- '@': { transactionId: 'test', ttlSeconds: 1 }, // always first, even if no priority
19
- };
20
- const sorted = sortANTRecords(records);
21
- assert.deepStrictEqual(sorted, {
22
- '@': { transactionId: 'test', ttlSeconds: 1, index: 0 }, // always first, even if no priority
23
- undername01: {
24
- priority: 1,
25
- transactionId: 'test',
26
- ttlSeconds: 1,
27
- index: 1,
28
- },
29
- undername1: {
30
- priority: 1,
31
- transactionId: 'test',
32
- ttlSeconds: 1,
33
- index: 2,
34
- },
35
- undername11: {
36
- priority: 1,
37
- transactionId: 'test',
38
- ttlSeconds: 1,
39
- index: 3,
40
- },
41
- undername2: {
42
- priority: 2,
43
- transactionId: 'test',
44
- ttlSeconds: 1,
45
- index: 4,
46
- },
47
- undername3: {
48
- priority: 3,
49
- transactionId: 'test',
50
- ttlSeconds: 1,
51
- index: 5,
52
- },
53
- undername4: {
54
- priority: 3,
55
- transactionId: 'test',
56
- ttlSeconds: 1,
57
- index: 6,
58
- },
59
- undername5: {
60
- priority: 100,
61
- transactionId: 'test',
62
- ttlSeconds: 1,
63
- index: 7,
64
- },
65
- noPriority: { transactionId: 'test', ttlSeconds: 1, index: 8 },
66
- noPriority01: { transactionId: 'test', ttlSeconds: 1, index: 9 },
67
- noPriority1: { transactionId: 'test', ttlSeconds: 1, index: 10 },
68
- noPriority11: { transactionId: 'test', ttlSeconds: 1, index: 11 },
69
- });
70
- });
71
- it('should always return @ as the first, regardless of priority', () => {
72
- const scenarios = [
73
- {
74
- records: {
75
- '@': { priority: 5, transactionId: 'test', ttlSeconds: 1 }, // priorities set on '@' are ignored, they are always first
76
- undername1: { priority: 2, transactionId: 'test', ttlSeconds: 1 },
77
- },
78
- expected: {
79
- '@': { priority: 5, transactionId: 'test', ttlSeconds: 1, index: 0 },
80
- undername1: {
81
- priority: 2,
82
- transactionId: 'test',
83
- ttlSeconds: 1,
84
- index: 1,
85
- },
86
- },
87
- },
88
- {
89
- records: {
90
- '@': { transactionId: 'test', ttlSeconds: 1 }, // priority 0 is missing, but '@' is always first
91
- undername1: { priority: 2, transactionId: 'test', ttlSeconds: 1 },
92
- },
93
- expected: {
94
- '@': { transactionId: 'test', ttlSeconds: 1, index: 0 },
95
- undername1: {
96
- priority: 2,
97
- transactionId: 'test',
98
- ttlSeconds: 1,
99
- index: 1,
100
- },
101
- },
102
- },
103
- ];
104
- for (const scenario of scenarios) {
105
- const sorted = sortANTRecords(scenario.records);
106
- assert.deepStrictEqual(sorted, scenario.expected);
107
- }
108
- });
109
- });
@@ -1,70 +0,0 @@
1
- import { strict as assert } from 'node:assert';
2
- import { describe, it } from 'node:test';
3
- import { fromB64Url, getRandomText, toB64Url } from './base64.js';
4
- describe('b64utils', () => {
5
- it('should convert various strings to base64url and back', () => {
6
- const testStrings = [
7
- 'Hello, World!',
8
- 'Test123!@#',
9
- 'Base64URLEncoding',
10
- 'Special_Chars+/',
11
- '',
12
- 'A',
13
- '1234567890',
14
- ];
15
- for (const str of testStrings) {
16
- const encoded = toB64Url(Buffer.from(str));
17
- const decoded = fromB64Url(encoded);
18
- assert.deepStrictEqual(decoded, Buffer.from(str), `Failed for string: ${str}`);
19
- }
20
- });
21
- it('should convert various buffers to base64url and back', () => {
22
- const testBuffers = [
23
- Buffer.from('Hello, World!'),
24
- Buffer.from([0, 1, 2, 3, 4, 5]),
25
- Buffer.from('Test123!@#'),
26
- Buffer.from('Base64URLEncoding'),
27
- Buffer.from('Special_Chars+/'),
28
- Buffer.alloc(0),
29
- Buffer.from('A'),
30
- Buffer.from('1234567890'),
31
- ];
32
- for (const buf of testBuffers) {
33
- const encoded = toB64Url(buf);
34
- const decoded = fromB64Url(encoded);
35
- assert.deepStrictEqual(decoded, buf, `Failed for buffer: ${buf.toString()}`);
36
- }
37
- });
38
- it('should handle edge cases for base64url conversion', () => {
39
- const edgeCases = [
40
- '',
41
- 'A',
42
- 'AA',
43
- 'AAA',
44
- '====',
45
- '===',
46
- '==',
47
- '=',
48
- 'A===',
49
- 'AA==',
50
- 'AAA=',
51
- ];
52
- for (const testCase of edgeCases) {
53
- const encoded = toB64Url(Buffer.from(testCase));
54
- const decoded = Buffer.from(fromB64Url(encoded)).toString();
55
- assert.strictEqual(decoded, testCase, `Failed for edge case: ${testCase}`);
56
- }
57
- });
58
- it('should generate random text', () => {
59
- const randomText = getRandomText();
60
- const randomText2 = getRandomText();
61
- assert.strictEqual(randomText.length, 32);
62
- assert.strictEqual(randomText2.length, 32);
63
- assert.notStrictEqual(randomText, randomText2);
64
- const smallRandomText = getRandomText(16);
65
- const smallRandomText2 = getRandomText(16);
66
- assert.strictEqual(smallRandomText.length, 16);
67
- assert.strictEqual(smallRandomText2.length, 16);
68
- assert.notStrictEqual(smallRandomText, smallRandomText2);
69
- });
70
- });
@@ -1,19 +0,0 @@
1
- import assert from 'node:assert';
2
- import { test } from 'node:test';
3
- import { urlWithSearchParams } from './url.js';
4
- test('urlWithSearchParams prunes undefined values but keeps other falsey values', () => {
5
- const result = urlWithSearchParams({
6
- baseUrl: 'https://example.com',
7
- params: {
8
- number: 1,
9
- string: 'string',
10
- boolean: true,
11
- empty: '',
12
- zero: 0,
13
- false: false,
14
- null: null,
15
- undef: undefined,
16
- },
17
- });
18
- assert.strictEqual(result, 'https://example.com/?number=1&string=string&boolean=true&empty=&zero=0&false=false');
19
- });
@@ -1,192 +0,0 @@
1
- import { strict as assert } from 'node:assert';
2
- import { describe, it } from 'node:test';
3
- import { errorMessageFromOutput } from './ao.js';
4
- import { pruneTags, sortAndPaginateEpochDataIntoEligibleDistributions, } from './arweave.js';
5
- describe('pruneTags', () => {
6
- it('should remove tags with undefined values', () => {
7
- const tags = [
8
- { name: 'Tag1', value: 'value1' },
9
- { name: 'Tag2', value: undefined },
10
- { name: 'Tag3', value: 'value3' },
11
- { name: 'Tag4', value: undefined },
12
- ];
13
- const prunedTags = pruneTags(tags);
14
- assert.deepEqual(prunedTags, [
15
- { name: 'Tag1', value: 'value1' },
16
- { name: 'Tag3', value: 'value3' },
17
- ]);
18
- });
19
- it('should return empty array when all tags have undefined values', () => {
20
- const tags = [
21
- { name: 'Tag1', value: undefined },
22
- { name: 'Tag2', value: undefined },
23
- ];
24
- const prunedTags = pruneTags(tags);
25
- assert.deepEqual(prunedTags, []);
26
- });
27
- it('should return same array when no tags have undefined values', () => {
28
- const tags = [
29
- { name: 'Tag1', value: 'value1' },
30
- { name: 'Tag2', value: 'value2' },
31
- ];
32
- const prunedTags = pruneTags(tags);
33
- assert.deepEqual(prunedTags, tags);
34
- });
35
- it('should return empty array with no tags', () => {
36
- const tags = [];
37
- const prunedTags = pruneTags(tags);
38
- assert.deepEqual(prunedTags, []);
39
- });
40
- });
41
- describe('errorMessageFromOutput', () => {
42
- it('should return error message from Error field', () => {
43
- const output = {
44
- Error: 'Error message',
45
- };
46
- const errorMessage = errorMessageFromOutput(output);
47
- assert.equal(errorMessage, 'Error message');
48
- });
49
- it('should return error message from Error tag', () => {
50
- const output = {
51
- Messages: [
52
- {
53
- Tags: [{ name: 'Error', value: 'Error message' }],
54
- },
55
- ],
56
- };
57
- const errorMessage = errorMessageFromOutput(output);
58
- assert.equal(errorMessage, 'Error message');
59
- });
60
- it('should return error message from Error tag if Error field is undefined', () => {
61
- const output = {
62
- Messages: [
63
- {
64
- Tags: [{ name: 'Error', value: 'Error message' }],
65
- },
66
- ],
67
- };
68
- const errorMessage = errorMessageFromOutput(output);
69
- assert.equal(errorMessage, 'Error message');
70
- });
71
- it('should return undefined if no error message is present', () => {
72
- const output = {
73
- Messages: [
74
- {
75
- Tags: [{ name: 'Tag1', value: 'value1' }],
76
- },
77
- ],
78
- };
79
- const errorMessage = errorMessageFromOutput(output);
80
- assert.equal(errorMessage, undefined);
81
- });
82
- it('should return error message with line number', () => {
83
- const output = {
84
- Error: '[string "aos"]:123: Error message',
85
- };
86
- const errorMessage = errorMessageFromOutput(output);
87
- assert.equal(errorMessage, 'Error message (line 123)');
88
- });
89
- it('should return error message with line number and remove unicode', () => {
90
- const output = {
91
- Error: '[string "aos"]:123: Error message\u001b[0m',
92
- };
93
- const errorMessage = errorMessageFromOutput(output);
94
- assert.equal(errorMessage, 'Error message (line 123)');
95
- });
96
- const knownErrorMessages = '\u001b[31mError\u001b[90m handling message with Action = Register\u001b[0m\n\u001b[32m[string ".handlers"]:723: [string "aos"]:128: Already registered\u001b[0m\n\n\u001b[90mstack traceback:\n\t[string ".process"]:871: in function \'.process.handle\'\u001b[0m\n\n\u001b[31merror:\n\u001b[0m[string ".handlers"]:723: [string "aos"]:128: Already registered';
97
- it('should display a clean error for a known error message', () => {
98
- const output = {
99
- Error: knownErrorMessages,
100
- };
101
- const errorMessage = errorMessageFromOutput(output);
102
- assert.equal(errorMessage, 'Already registered (line 128)');
103
- });
104
- });
105
- describe('sortAndPaginateEpochDataIntoEligibleDistributions', () => {
106
- const mockEpochData = {
107
- distributions: {
108
- distributedTimestamp: 1234567890,
109
- rewards: {
110
- eligible: {
111
- gateway1: {
112
- operatorReward: 50,
113
- delegateRewards: {
114
- delegate1: 20,
115
- delegate2: 30,
116
- },
117
- },
118
- gateway2: {
119
- operatorReward: 70,
120
- delegateRewards: {
121
- delegate3: 40,
122
- },
123
- },
124
- },
125
- },
126
- },
127
- };
128
- it('sorts rewards in descending order by eligibleReward', () => {
129
- const result = sortAndPaginateEpochDataIntoEligibleDistributions(mockEpochData);
130
- assert.deepEqual(result.items, [
131
- {
132
- type: 'operatorReward',
133
- recipient: 'gateway2',
134
- eligibleReward: 70,
135
- cursorId: 'gateway2_gateway2',
136
- gatewayAddress: 'gateway2',
137
- },
138
- {
139
- type: 'operatorReward',
140
- recipient: 'gateway1',
141
- eligibleReward: 50,
142
- cursorId: 'gateway1_gateway1',
143
- gatewayAddress: 'gateway1',
144
- },
145
- {
146
- type: 'delegateReward',
147
- recipient: 'delegate3',
148
- eligibleReward: 40,
149
- cursorId: 'gateway2_delegate3',
150
- gatewayAddress: 'gateway2',
151
- },
152
- {
153
- type: 'delegateReward',
154
- recipient: 'delegate2',
155
- eligibleReward: 30,
156
- cursorId: 'gateway1_delegate2',
157
- gatewayAddress: 'gateway1',
158
- },
159
- {
160
- type: 'delegateReward',
161
- recipient: 'delegate1',
162
- eligibleReward: 20,
163
- cursorId: 'gateway1_delegate1',
164
- gatewayAddress: 'gateway1',
165
- },
166
- ]);
167
- });
168
- it('supports sorting in ascending order', () => {
169
- const result = sortAndPaginateEpochDataIntoEligibleDistributions(mockEpochData, {
170
- sortOrder: 'asc',
171
- });
172
- assert.equal(result.items[0].eligibleReward, 20);
173
- assert.equal(result.items[result.items.length - 1].eligibleReward, 70);
174
- });
175
- it('paginates results correctly', () => {
176
- const result = sortAndPaginateEpochDataIntoEligibleDistributions(mockEpochData, {
177
- limit: 2,
178
- });
179
- assert.equal(result.items.length, 2);
180
- assert.equal(result.hasMore, true);
181
- assert.equal(result.nextCursor, 'gateway2_delegate3');
182
- });
183
- it('resumes pagination from cursor', () => {
184
- const firstPage = sortAndPaginateEpochDataIntoEligibleDistributions(mockEpochData, { limit: 2 });
185
- const secondPage = sortAndPaginateEpochDataIntoEligibleDistributions(mockEpochData, {
186
- limit: 2,
187
- cursor: firstPage.nextCursor,
188
- });
189
- assert.equal(secondPage.items.length, 2);
190
- assert.equal(secondPage.items[0].cursorId, 'gateway1_delegate2');
191
- });
192
- });
@@ -1 +0,0 @@
1
- export {};
@@ -1 +0,0 @@
1
- export {};
@@ -1 +0,0 @@
1
- export {};
@@ -1 +0,0 @@
1
- export {};
@@ -1 +0,0 @@
1
- export {};
@@ -1 +0,0 @@
1
- export {};