@ar.io/sdk 3.8.1 → 3.8.2
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 +55 -50
- package/bundles/web.bundle.min.js +55 -55
- package/lib/cjs/common/contracts/ao-process.js +86 -79
- package/lib/cjs/version.js +1 -1
- package/lib/esm/common/contracts/ao-process.js +86 -79
- package/lib/esm/version.js +1 -1
- package/lib/types/types/common.d.ts +12 -0
- package/lib/types/version.d.ts +1 -1
- package/package.json +1 -1
|
@@ -39,46 +39,25 @@ class AOProcess {
|
|
|
39
39
|
messageData === null);
|
|
40
40
|
}
|
|
41
41
|
async read({ tags, retries = 3, fromAddress, }) {
|
|
42
|
+
this.logger.debug(`Evaluating read interaction on process`, {
|
|
43
|
+
tags,
|
|
44
|
+
processId: this.processId,
|
|
45
|
+
});
|
|
46
|
+
// map tags to inputs
|
|
47
|
+
const dryRunInput = {
|
|
48
|
+
process: this.processId,
|
|
49
|
+
tags,
|
|
50
|
+
};
|
|
51
|
+
if (fromAddress !== undefined) {
|
|
52
|
+
dryRunInput['Owner'] = fromAddress;
|
|
53
|
+
}
|
|
42
54
|
let attempts = 0;
|
|
43
|
-
let
|
|
55
|
+
let result = undefined;
|
|
44
56
|
while (attempts < retries) {
|
|
45
57
|
try {
|
|
46
|
-
this.
|
|
47
|
-
|
|
48
|
-
|
|
49
|
-
});
|
|
50
|
-
// map tags to inputs
|
|
51
|
-
const dryRunInput = {
|
|
52
|
-
process: this.processId,
|
|
53
|
-
tags,
|
|
54
|
-
};
|
|
55
|
-
if (fromAddress !== undefined) {
|
|
56
|
-
dryRunInput['Owner'] = fromAddress;
|
|
57
|
-
}
|
|
58
|
-
const result = await this.ao.dryrun(dryRunInput);
|
|
59
|
-
this.logger.debug(`Read interaction result`, {
|
|
60
|
-
result,
|
|
61
|
-
processId: this.processId,
|
|
62
|
-
});
|
|
63
|
-
const error = (0, index_js_1.errorMessageFromOutput)(result);
|
|
64
|
-
if (error !== undefined) {
|
|
65
|
-
throw new Error(error);
|
|
66
|
-
}
|
|
67
|
-
if (result.Messages === undefined || result.Messages.length === 0) {
|
|
68
|
-
this.logger.debug(`Process ${this.processId} does not support provided action.`, {
|
|
69
|
-
result,
|
|
70
|
-
tags,
|
|
71
|
-
processId: this.processId,
|
|
72
|
-
});
|
|
73
|
-
throw new Error(`Process ${this.processId} does not support provided action.`);
|
|
74
|
-
}
|
|
75
|
-
const messageData = result.Messages?.[0]?.Data;
|
|
76
|
-
// return undefined if no data is returned
|
|
77
|
-
if (this.isMessageDataEmpty(messageData)) {
|
|
78
|
-
return undefined;
|
|
79
|
-
}
|
|
80
|
-
const response = (0, json_js_1.safeDecode)(messageData);
|
|
81
|
-
return response;
|
|
58
|
+
result = await this.ao.dryrun(dryRunInput);
|
|
59
|
+
// break on successful return of result
|
|
60
|
+
break;
|
|
82
61
|
}
|
|
83
62
|
catch (error) {
|
|
84
63
|
attempts++;
|
|
@@ -88,17 +67,45 @@ class AOProcess {
|
|
|
88
67
|
tags,
|
|
89
68
|
processId: this.processId,
|
|
90
69
|
});
|
|
91
|
-
|
|
70
|
+
if (attempts >= retries) {
|
|
71
|
+
throw error;
|
|
72
|
+
}
|
|
92
73
|
// exponential backoff
|
|
93
74
|
await new Promise((resolve) => setTimeout(resolve, 2 ** attempts * 1000));
|
|
94
75
|
}
|
|
95
76
|
}
|
|
96
|
-
|
|
77
|
+
if (result === undefined) {
|
|
78
|
+
throw new Error('Unexpected error when evaluating read interaction');
|
|
79
|
+
}
|
|
80
|
+
this.logger.debug(`Read interaction result`, {
|
|
81
|
+
result,
|
|
82
|
+
processId: this.processId,
|
|
83
|
+
});
|
|
84
|
+
const error = (0, index_js_1.errorMessageFromOutput)(result);
|
|
85
|
+
if (error !== undefined) {
|
|
86
|
+
throw new Error(error);
|
|
87
|
+
}
|
|
88
|
+
if (result.Messages === undefined || result.Messages.length === 0) {
|
|
89
|
+
this.logger.debug(`Process ${this.processId} does not support provided action.`, {
|
|
90
|
+
result,
|
|
91
|
+
tags,
|
|
92
|
+
processId: this.processId,
|
|
93
|
+
});
|
|
94
|
+
throw new Error(`Process ${this.processId} does not support provided action.`);
|
|
95
|
+
}
|
|
96
|
+
const messageData = result.Messages?.[0]?.Data;
|
|
97
|
+
// return undefined if no data is returned
|
|
98
|
+
if (this.isMessageDataEmpty(messageData)) {
|
|
99
|
+
return undefined;
|
|
100
|
+
}
|
|
101
|
+
const response = (0, json_js_1.safeDecode)(messageData);
|
|
102
|
+
return response;
|
|
97
103
|
}
|
|
98
104
|
async send({ tags, data, signer, retries = 3, }) {
|
|
99
105
|
// main purpose of retries is to handle network errors/new process delays
|
|
100
106
|
let attempts = 0;
|
|
101
|
-
let
|
|
107
|
+
let messageId;
|
|
108
|
+
let result = undefined;
|
|
102
109
|
while (attempts < retries) {
|
|
103
110
|
try {
|
|
104
111
|
this.logger.debug(`Evaluating send interaction on contract`, {
|
|
@@ -109,7 +116,7 @@ class AOProcess {
|
|
|
109
116
|
// TODO: do a read as a dry run to check if the process supports the action
|
|
110
117
|
// anchor is a random text produce non-deterministic messages IDs when deterministic signers are provided (ETH)
|
|
111
118
|
const anchor = (0, base64_js_1.getRandomText)(32);
|
|
112
|
-
|
|
119
|
+
messageId = await this.ao.message({
|
|
113
120
|
process: this.processId,
|
|
114
121
|
// TODO: any other default tags we want to add?
|
|
115
122
|
tags: [...tags, { name: 'AR-IO-SDK', value: version_js_1.version }],
|
|
@@ -123,36 +130,16 @@ class AOProcess {
|
|
|
123
130
|
anchor,
|
|
124
131
|
});
|
|
125
132
|
// check the result of the send interaction
|
|
126
|
-
|
|
133
|
+
result = await this.ao.result({
|
|
127
134
|
message: messageId,
|
|
128
135
|
process: this.processId,
|
|
129
136
|
});
|
|
130
137
|
this.logger.debug('Message result', {
|
|
131
|
-
|
|
132
|
-
messageId,
|
|
133
|
-
processId: this.processId,
|
|
134
|
-
});
|
|
135
|
-
const error = (0, index_js_1.errorMessageFromOutput)(output);
|
|
136
|
-
if (error !== undefined) {
|
|
137
|
-
throw new error_js_1.WriteInteractionError(error);
|
|
138
|
-
}
|
|
139
|
-
// check if there are any Messages in the output
|
|
140
|
-
if (output.Messages?.length === 0 || output.Messages === undefined) {
|
|
141
|
-
return { id: messageId };
|
|
142
|
-
}
|
|
143
|
-
if (output.Messages.length === 0) {
|
|
144
|
-
throw new Error(`Process ${this.processId} does not support provided action.`);
|
|
145
|
-
}
|
|
146
|
-
if (this.isMessageDataEmpty(output.Messages[0].Data)) {
|
|
147
|
-
return { id: messageId };
|
|
148
|
-
}
|
|
149
|
-
const resultData = (0, json_js_1.safeDecode)(output.Messages[0].Data);
|
|
150
|
-
this.logger.debug('Message result data', {
|
|
151
|
-
resultData,
|
|
138
|
+
result,
|
|
152
139
|
messageId,
|
|
153
140
|
processId: this.processId,
|
|
154
141
|
});
|
|
155
|
-
|
|
142
|
+
break;
|
|
156
143
|
}
|
|
157
144
|
catch (error) {
|
|
158
145
|
this.logger.error('Error sending message to process', {
|
|
@@ -161,24 +148,44 @@ class AOProcess {
|
|
|
161
148
|
processId: this.processId,
|
|
162
149
|
tags,
|
|
163
150
|
});
|
|
164
|
-
|
|
165
|
-
|
|
166
|
-
|
|
167
|
-
|
|
168
|
-
|
|
169
|
-
|
|
170
|
-
|
|
171
|
-
|
|
172
|
-
// exponential backoff
|
|
173
|
-
await new Promise((resolve) => setTimeout(resolve, 2 ** attempts * 2000));
|
|
174
|
-
attempts++;
|
|
175
|
-
lastError = error;
|
|
176
|
-
}
|
|
177
|
-
else
|
|
151
|
+
attempts++;
|
|
152
|
+
this.logger.debug('Retrying send interaction', {
|
|
153
|
+
attempts,
|
|
154
|
+
retries,
|
|
155
|
+
error: error?.message,
|
|
156
|
+
processId: this.processId,
|
|
157
|
+
});
|
|
158
|
+
if (attempts >= retries) {
|
|
178
159
|
throw error;
|
|
160
|
+
}
|
|
161
|
+
// exponential backoff
|
|
162
|
+
await new Promise((resolve) => setTimeout(resolve, 2 ** attempts * 2000));
|
|
179
163
|
}
|
|
180
164
|
}
|
|
181
|
-
|
|
165
|
+
if (result === undefined || messageId === undefined) {
|
|
166
|
+
throw new Error('Unexpected error when evaluating write interaction');
|
|
167
|
+
}
|
|
168
|
+
const error = (0, index_js_1.errorMessageFromOutput)(result);
|
|
169
|
+
if (error !== undefined) {
|
|
170
|
+
throw new error_js_1.WriteInteractionError(error);
|
|
171
|
+
}
|
|
172
|
+
// check if there are any Messages in the output
|
|
173
|
+
if (result.Messages?.length === 0 || result.Messages === undefined) {
|
|
174
|
+
return { id: messageId };
|
|
175
|
+
}
|
|
176
|
+
if (result.Messages.length === 0) {
|
|
177
|
+
throw new Error(`Process ${this.processId} does not support provided action.`);
|
|
178
|
+
}
|
|
179
|
+
if (this.isMessageDataEmpty(result.Messages[0].Data)) {
|
|
180
|
+
return { id: messageId };
|
|
181
|
+
}
|
|
182
|
+
const resultData = (0, json_js_1.safeDecode)(result.Messages[0].Data);
|
|
183
|
+
this.logger.debug('Message result data', {
|
|
184
|
+
resultData,
|
|
185
|
+
messageId,
|
|
186
|
+
processId: this.processId,
|
|
187
|
+
});
|
|
188
|
+
return { id: messageId, result: resultData };
|
|
182
189
|
}
|
|
183
190
|
}
|
|
184
191
|
exports.AOProcess = AOProcess;
|
package/lib/cjs/version.js
CHANGED
|
@@ -36,46 +36,25 @@ export class AOProcess {
|
|
|
36
36
|
messageData === null);
|
|
37
37
|
}
|
|
38
38
|
async read({ tags, retries = 3, fromAddress, }) {
|
|
39
|
+
this.logger.debug(`Evaluating read interaction on process`, {
|
|
40
|
+
tags,
|
|
41
|
+
processId: this.processId,
|
|
42
|
+
});
|
|
43
|
+
// map tags to inputs
|
|
44
|
+
const dryRunInput = {
|
|
45
|
+
process: this.processId,
|
|
46
|
+
tags,
|
|
47
|
+
};
|
|
48
|
+
if (fromAddress !== undefined) {
|
|
49
|
+
dryRunInput['Owner'] = fromAddress;
|
|
50
|
+
}
|
|
39
51
|
let attempts = 0;
|
|
40
|
-
let
|
|
52
|
+
let result = undefined;
|
|
41
53
|
while (attempts < retries) {
|
|
42
54
|
try {
|
|
43
|
-
this.
|
|
44
|
-
|
|
45
|
-
|
|
46
|
-
});
|
|
47
|
-
// map tags to inputs
|
|
48
|
-
const dryRunInput = {
|
|
49
|
-
process: this.processId,
|
|
50
|
-
tags,
|
|
51
|
-
};
|
|
52
|
-
if (fromAddress !== undefined) {
|
|
53
|
-
dryRunInput['Owner'] = fromAddress;
|
|
54
|
-
}
|
|
55
|
-
const result = await this.ao.dryrun(dryRunInput);
|
|
56
|
-
this.logger.debug(`Read interaction result`, {
|
|
57
|
-
result,
|
|
58
|
-
processId: this.processId,
|
|
59
|
-
});
|
|
60
|
-
const error = errorMessageFromOutput(result);
|
|
61
|
-
if (error !== undefined) {
|
|
62
|
-
throw new Error(error);
|
|
63
|
-
}
|
|
64
|
-
if (result.Messages === undefined || result.Messages.length === 0) {
|
|
65
|
-
this.logger.debug(`Process ${this.processId} does not support provided action.`, {
|
|
66
|
-
result,
|
|
67
|
-
tags,
|
|
68
|
-
processId: this.processId,
|
|
69
|
-
});
|
|
70
|
-
throw new Error(`Process ${this.processId} does not support provided action.`);
|
|
71
|
-
}
|
|
72
|
-
const messageData = result.Messages?.[0]?.Data;
|
|
73
|
-
// return undefined if no data is returned
|
|
74
|
-
if (this.isMessageDataEmpty(messageData)) {
|
|
75
|
-
return undefined;
|
|
76
|
-
}
|
|
77
|
-
const response = safeDecode(messageData);
|
|
78
|
-
return response;
|
|
55
|
+
result = await this.ao.dryrun(dryRunInput);
|
|
56
|
+
// break on successful return of result
|
|
57
|
+
break;
|
|
79
58
|
}
|
|
80
59
|
catch (error) {
|
|
81
60
|
attempts++;
|
|
@@ -85,17 +64,45 @@ export class AOProcess {
|
|
|
85
64
|
tags,
|
|
86
65
|
processId: this.processId,
|
|
87
66
|
});
|
|
88
|
-
|
|
67
|
+
if (attempts >= retries) {
|
|
68
|
+
throw error;
|
|
69
|
+
}
|
|
89
70
|
// exponential backoff
|
|
90
71
|
await new Promise((resolve) => setTimeout(resolve, 2 ** attempts * 1000));
|
|
91
72
|
}
|
|
92
73
|
}
|
|
93
|
-
|
|
74
|
+
if (result === undefined) {
|
|
75
|
+
throw new Error('Unexpected error when evaluating read interaction');
|
|
76
|
+
}
|
|
77
|
+
this.logger.debug(`Read interaction result`, {
|
|
78
|
+
result,
|
|
79
|
+
processId: this.processId,
|
|
80
|
+
});
|
|
81
|
+
const error = errorMessageFromOutput(result);
|
|
82
|
+
if (error !== undefined) {
|
|
83
|
+
throw new Error(error);
|
|
84
|
+
}
|
|
85
|
+
if (result.Messages === undefined || result.Messages.length === 0) {
|
|
86
|
+
this.logger.debug(`Process ${this.processId} does not support provided action.`, {
|
|
87
|
+
result,
|
|
88
|
+
tags,
|
|
89
|
+
processId: this.processId,
|
|
90
|
+
});
|
|
91
|
+
throw new Error(`Process ${this.processId} does not support provided action.`);
|
|
92
|
+
}
|
|
93
|
+
const messageData = result.Messages?.[0]?.Data;
|
|
94
|
+
// return undefined if no data is returned
|
|
95
|
+
if (this.isMessageDataEmpty(messageData)) {
|
|
96
|
+
return undefined;
|
|
97
|
+
}
|
|
98
|
+
const response = safeDecode(messageData);
|
|
99
|
+
return response;
|
|
94
100
|
}
|
|
95
101
|
async send({ tags, data, signer, retries = 3, }) {
|
|
96
102
|
// main purpose of retries is to handle network errors/new process delays
|
|
97
103
|
let attempts = 0;
|
|
98
|
-
let
|
|
104
|
+
let messageId;
|
|
105
|
+
let result = undefined;
|
|
99
106
|
while (attempts < retries) {
|
|
100
107
|
try {
|
|
101
108
|
this.logger.debug(`Evaluating send interaction on contract`, {
|
|
@@ -106,7 +113,7 @@ export class AOProcess {
|
|
|
106
113
|
// TODO: do a read as a dry run to check if the process supports the action
|
|
107
114
|
// anchor is a random text produce non-deterministic messages IDs when deterministic signers are provided (ETH)
|
|
108
115
|
const anchor = getRandomText(32);
|
|
109
|
-
|
|
116
|
+
messageId = await this.ao.message({
|
|
110
117
|
process: this.processId,
|
|
111
118
|
// TODO: any other default tags we want to add?
|
|
112
119
|
tags: [...tags, { name: 'AR-IO-SDK', value: version }],
|
|
@@ -120,36 +127,16 @@ export class AOProcess {
|
|
|
120
127
|
anchor,
|
|
121
128
|
});
|
|
122
129
|
// check the result of the send interaction
|
|
123
|
-
|
|
130
|
+
result = await this.ao.result({
|
|
124
131
|
message: messageId,
|
|
125
132
|
process: this.processId,
|
|
126
133
|
});
|
|
127
134
|
this.logger.debug('Message result', {
|
|
128
|
-
|
|
129
|
-
messageId,
|
|
130
|
-
processId: this.processId,
|
|
131
|
-
});
|
|
132
|
-
const error = errorMessageFromOutput(output);
|
|
133
|
-
if (error !== undefined) {
|
|
134
|
-
throw new WriteInteractionError(error);
|
|
135
|
-
}
|
|
136
|
-
// check if there are any Messages in the output
|
|
137
|
-
if (output.Messages?.length === 0 || output.Messages === undefined) {
|
|
138
|
-
return { id: messageId };
|
|
139
|
-
}
|
|
140
|
-
if (output.Messages.length === 0) {
|
|
141
|
-
throw new Error(`Process ${this.processId} does not support provided action.`);
|
|
142
|
-
}
|
|
143
|
-
if (this.isMessageDataEmpty(output.Messages[0].Data)) {
|
|
144
|
-
return { id: messageId };
|
|
145
|
-
}
|
|
146
|
-
const resultData = safeDecode(output.Messages[0].Data);
|
|
147
|
-
this.logger.debug('Message result data', {
|
|
148
|
-
resultData,
|
|
135
|
+
result,
|
|
149
136
|
messageId,
|
|
150
137
|
processId: this.processId,
|
|
151
138
|
});
|
|
152
|
-
|
|
139
|
+
break;
|
|
153
140
|
}
|
|
154
141
|
catch (error) {
|
|
155
142
|
this.logger.error('Error sending message to process', {
|
|
@@ -158,23 +145,43 @@ export class AOProcess {
|
|
|
158
145
|
processId: this.processId,
|
|
159
146
|
tags,
|
|
160
147
|
});
|
|
161
|
-
|
|
162
|
-
|
|
163
|
-
|
|
164
|
-
|
|
165
|
-
|
|
166
|
-
|
|
167
|
-
|
|
168
|
-
|
|
169
|
-
// exponential backoff
|
|
170
|
-
await new Promise((resolve) => setTimeout(resolve, 2 ** attempts * 2000));
|
|
171
|
-
attempts++;
|
|
172
|
-
lastError = error;
|
|
173
|
-
}
|
|
174
|
-
else
|
|
148
|
+
attempts++;
|
|
149
|
+
this.logger.debug('Retrying send interaction', {
|
|
150
|
+
attempts,
|
|
151
|
+
retries,
|
|
152
|
+
error: error?.message,
|
|
153
|
+
processId: this.processId,
|
|
154
|
+
});
|
|
155
|
+
if (attempts >= retries) {
|
|
175
156
|
throw error;
|
|
157
|
+
}
|
|
158
|
+
// exponential backoff
|
|
159
|
+
await new Promise((resolve) => setTimeout(resolve, 2 ** attempts * 2000));
|
|
176
160
|
}
|
|
177
161
|
}
|
|
178
|
-
|
|
162
|
+
if (result === undefined || messageId === undefined) {
|
|
163
|
+
throw new Error('Unexpected error when evaluating write interaction');
|
|
164
|
+
}
|
|
165
|
+
const error = errorMessageFromOutput(result);
|
|
166
|
+
if (error !== undefined) {
|
|
167
|
+
throw new WriteInteractionError(error);
|
|
168
|
+
}
|
|
169
|
+
// check if there are any Messages in the output
|
|
170
|
+
if (result.Messages?.length === 0 || result.Messages === undefined) {
|
|
171
|
+
return { id: messageId };
|
|
172
|
+
}
|
|
173
|
+
if (result.Messages.length === 0) {
|
|
174
|
+
throw new Error(`Process ${this.processId} does not support provided action.`);
|
|
175
|
+
}
|
|
176
|
+
if (this.isMessageDataEmpty(result.Messages[0].Data)) {
|
|
177
|
+
return { id: messageId };
|
|
178
|
+
}
|
|
179
|
+
const resultData = safeDecode(result.Messages[0].Data);
|
|
180
|
+
this.logger.debug('Message result data', {
|
|
181
|
+
resultData,
|
|
182
|
+
messageId,
|
|
183
|
+
processId: this.processId,
|
|
184
|
+
});
|
|
185
|
+
return { id: messageId, result: resultData };
|
|
179
186
|
}
|
|
180
187
|
}
|
package/lib/esm/version.js
CHANGED
|
@@ -113,3 +113,15 @@ export interface AOContract {
|
|
|
113
113
|
}
|
|
114
114
|
/** utility type to ensure WriteOptions are appended to each parameter set */
|
|
115
115
|
export type AoWriteAction<P, R = AoMessageResult> = (params: P, options?: WriteOptions) => Promise<R>;
|
|
116
|
+
export type DryRunResult = {
|
|
117
|
+
Output: any;
|
|
118
|
+
Messages: any[];
|
|
119
|
+
Spawns: any[];
|
|
120
|
+
Error?: any;
|
|
121
|
+
};
|
|
122
|
+
export type MessageResult = {
|
|
123
|
+
Output: any;
|
|
124
|
+
Messages: any[];
|
|
125
|
+
Spawns: any[];
|
|
126
|
+
Error?: any;
|
|
127
|
+
};
|
package/lib/types/version.d.ts
CHANGED