-
Notifications
You must be signed in to change notification settings - Fork 59
/
oracle.ts
311 lines (292 loc) · 10.4 KB
/
oracle.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
/**
* Oracle methods - routines to interact with the æternity oracle system
*
* The high-level description of the oracle system is
* https://github.com/aeternity/protocol/blob/master/ORACLE.md in the protocol
* repository.
*/
import { mapObject, pause } from './utils/other';
import { oracleQueryId } from './tx/builder/helpers';
import { unpackTx, buildTxAsync, BuildTxOptions } from './tx/builder';
import { Tag } from './tx/builder/constants';
import { RequestTimedOutError } from './utils/errors';
import {
decode, encode, Encoded, Encoding,
} from './utils/encoder';
import { _getPollInterval, getHeight } from './chain';
import { sendTransaction, SendTransactionOptions } from './send-transaction';
import Node from './Node';
import AccountBase from './account/Base';
type OracleQueries = Awaited<ReturnType<Node['getOracleQueriesByPubkey']>>['oracleQueries'];
/**
* Poll for oracle queries
* @category oracle
* @param oracleId - Oracle public key
* @param onQuery - OnQuery callback
* @param options - Options object
* @param options.interval - Poll interval(default: 5000)
* @param options.onNode - Node to use
* @returns Callback to stop polling function
*/
export function pollForQueries(
oracleId: Encoded.OracleAddress,
onQuery: (query: OracleQueries[number]) => void,
{ interval, ...options }: { interval?: number; onNode: Node }
& Parameters<typeof _getPollInterval>[1],
): () => void {
const knownQueryIds = new Set();
const checkNewQueries = async (): Promise<void> => {
const queries = ((await options.onNode.getOracleQueriesByPubkey(oracleId)).oracleQueries ?? [])
.filter(({ id }) => !knownQueryIds.has(id));
queries.forEach((query) => {
knownQueryIds.add(query.id);
onQuery(query);
});
};
let stopped = false;
// eslint-disable-next-line @typescript-eslint/no-floating-promises
(async () => {
interval ??= await _getPollInterval('micro-block', options);
while (!stopped) { // eslint-disable-line no-unmodified-loop-condition
// TODO: allow to handle this error somehow
await checkNewQueries().catch(console.error);
await pause(interval);
}
})();
return () => { stopped = true; };
}
/**
* Poll for oracle query response
* @category oracle
* @param oracleId - Oracle public key
* @param queryId - Oracle Query id
* @param options - Options object
* @param options.interval - Poll interval
* @param options.onNode - Node to use
* @returns OracleQuery object
*/
export async function pollForQueryResponse(
oracleId: Encoded.OracleAddress,
queryId: Encoded.OracleQueryId,
{ interval, ...options }:
{ interval?: number; onNode: Node } & Parameters<typeof _getPollInterval>[1],
): Promise<string> {
interval ??= await _getPollInterval('micro-block', options);
let height;
let ttl;
let response;
do {
({ response, ttl } = await options.onNode.getOracleQueryByPubkeyAndQueryId(oracleId, queryId));
const responseBuffer = decode(response as Encoded.OracleResponse);
if (responseBuffer.length > 0) return responseBuffer.toString();
await pause(interval);
height = await getHeight({ ...options, cached: true });
} while (ttl >= height);
throw new RequestTimedOutError(height);
}
/**
* Constructor for OracleQuery Object (helper object for using OracleQuery)
* @category oracle
* @param oracleId - Oracle public key
* @param queryId - Oracle Query id
* @param options - Options
* @returns OracleQuery object
*/
export async function getQueryObject(
oracleId: Encoded.OracleAddress,
queryId: Encoded.OracleQueryId,
options: RespondToQueryOptions & Parameters<typeof pollForQueryResponse>[2],
): Promise<GetQueryObjectReturnType> {
const record = await options.onNode.getOracleQueryByPubkeyAndQueryId(oracleId, queryId);
return {
...record,
decodedQuery: decode(record.query as Encoded.OracleQueryId).toString(),
decodedResponse: decode(record.response as Encoded.OracleResponse).toString(),
respond: async (response, opt) => (
// eslint-disable-next-line @typescript-eslint/no-use-before-define
respondToQuery(queryId, response, { ...options, ...opt })
),
pollForResponse: async (opt) => pollForQueryResponse(oracleId, queryId, { ...options, ...opt }),
};
}
interface GetQueryObjectReturnType extends Awaited<ReturnType<Node['getOracleQueryByPubkeyAndQueryId']>> {
decodedQuery: string;
decodedResponse: string;
respond: (response: string, options?: Parameters<typeof respondToQuery>[2]) =>
ReturnType<typeof respondToQuery>;
pollForResponse: (options?: Parameters<typeof pollForQueryResponse>[2]) =>
ReturnType<typeof pollForQueryResponse>;
}
/**
* Post query to oracle
* @category oracle
* @param oracleId - Oracle public key
* @param query - Oracle query object
* @param options - Options object
* @returns Query object
*/
export async function postQueryToOracle(
oracleId: Encoded.OracleAddress,
query: string,
options: PostQueryToOracleOptions,
): Promise<
Awaited<ReturnType<typeof sendTransaction>> & Awaited<ReturnType<typeof getQueryObject>>
> {
const senderId = options.onAccount.address;
const oracleQueryTx = await buildTxAsync({
_isInternalBuild: true,
...options,
tag: Tag.OracleQueryTx,
oracleId,
senderId,
query,
});
const { nonce } = unpackTx(oracleQueryTx, Tag.OracleQueryTx);
const queryId = oracleQueryId(senderId, nonce, oracleId);
return {
...await sendTransaction(oracleQueryTx, options),
...await getQueryObject(oracleId, queryId, options),
};
}
type PostQueryToOracleOptionsType = Parameters<typeof sendTransaction>[1]
& Parameters<typeof getQueryObject>[2]
& BuildTxOptions<Tag.OracleQueryTx, 'oracleId' | 'senderId' | 'query'>;
interface PostQueryToOracleOptions extends PostQueryToOracleOptionsType {}
/**
* Extend oracle ttl
* @category oracle
* @param options - Options object
* @returns Oracle object
*/
export async function extendOracleTtl(options: ExtendOracleTtlOptions): Promise<
Awaited<ReturnType<typeof sendTransaction>> & Awaited<ReturnType<typeof getOracleObject>>
> {
const oracleId = encode(decode(options.onAccount.address), Encoding.OracleAddress);
const oracleExtendTx = await buildTxAsync({
_isInternalBuild: true,
...options,
tag: Tag.OracleExtendTx,
oracleId,
});
return {
...await sendTransaction(oracleExtendTx, options),
// eslint-disable-next-line @typescript-eslint/no-use-before-define
...await getOracleObject(oracleId, options),
};
}
type ExtendOracleTtlOptionsType = SendTransactionOptions & Parameters<typeof getOracleObject>[1]
& BuildTxOptions<Tag.OracleExtendTx, 'callerId' | 'oracleId'>;
interface ExtendOracleTtlOptions extends ExtendOracleTtlOptionsType {}
/**
* Extend oracle ttl
* @category oracle
* @param queryId - Oracle query id
* @param response - Oracle query response
* @param options - Options object
* @returns Oracle object
*/
export async function respondToQuery(
queryId: Encoded.OracleQueryId,
response: string,
options: RespondToQueryOptions,
): Promise<
Awaited<ReturnType<typeof sendTransaction>> & Awaited<ReturnType<typeof getOracleObject>>
> {
const oracleId = encode(decode(options.onAccount.address), Encoding.OracleAddress);
const oracleRespondTx = await buildTxAsync({
_isInternalBuild: true,
...options,
tag: Tag.OracleResponseTx,
oracleId,
queryId,
response,
});
return {
...await sendTransaction(oracleRespondTx, options),
// eslint-disable-next-line @typescript-eslint/no-use-before-define
...await getOracleObject(oracleId, options),
};
}
type RespondToQueryOptionsType = SendTransactionOptions & Parameters<typeof getOracleObject>[1]
& BuildTxOptions<Tag.OracleResponseTx, 'callerId' | 'oracleId' | 'queryId' | 'response'>;
interface RespondToQueryOptions extends RespondToQueryOptionsType {}
/**
* Constructor for Oracle Object (helper object for using Oracle)
* @category oracle
* @param oracleId - Oracle public key
* @param options - Options
* @returns Oracle object
*/
export async function getOracleObject(
oracleId: Encoded.OracleAddress,
options: { onNode: Node; onAccount: AccountBase },
): Promise<GetOracleObjectReturnType> {
return {
...await options.onNode.getOracleByPubkey(oracleId),
queries: (await options.onNode.getOracleQueriesByPubkey(oracleId)).oracleQueries,
...mapObject<Function, Function>(
{
pollQueries: pollForQueries,
postQuery: postQueryToOracle,
respondToQuery,
extendOracle: extendOracleTtl,
getQuery: getQueryObject,
},
([name, handler]) => [
name,
(...args: any) => {
const lastArg = args[args.length - 1];
if (lastArg != null && typeof lastArg === 'object' && lastArg.constructor === Object) {
Object.assign(lastArg, { ...options, ...lastArg });
} else args.push(options);
return handler(
...['extendOracle', 'respondToQuery'].includes(name) ? [] : [oracleId],
...args,
);
},
],
),
} as any;
}
interface GetOracleObjectReturnType extends Awaited<ReturnType<Node['getOracleByPubkey']>> {
id: Encoded.OracleAddress;
queries: OracleQueries;
// TODO: replace getOracleObject with a class
pollQueries: (cb: Parameters<typeof pollForQueries>[1]) => ReturnType<typeof pollForQueries>;
postQuery: Function;
respondToQuery: Function;
extendOracle: Function;
getQuery: Function;
}
/**
* Register oracle
* @category oracle
* @param queryFormat - Format of query
* @param responseFormat - Format of query response
* @param options - Options
* @returns Oracle object
*/
export async function registerOracle(
queryFormat: string,
responseFormat: string,
options: RegisterOracleOptions,
): Promise<
Awaited<ReturnType<typeof sendTransaction>> & Awaited<ReturnType<typeof getOracleObject>>
> {
const accountId = options.onAccount.address;
const oracleRegisterTx = await buildTxAsync({
_isInternalBuild: true,
...options,
tag: Tag.OracleRegisterTx,
accountId,
queryFormat,
responseFormat,
});
return {
...await sendTransaction(oracleRegisterTx, options),
...await getOracleObject(encode(decode(accountId), Encoding.OracleAddress), options),
};
}
type RegisterOracleOptionsType = SendTransactionOptions & Parameters<typeof getOracleObject>[1]
& BuildTxOptions<Tag.OracleRegisterTx, 'accountId' | 'queryFormat' | 'responseFormat'>;
interface RegisterOracleOptions extends RegisterOracleOptionsType {}