-
Notifications
You must be signed in to change notification settings - Fork 43
/
Copy pathtypes.ts
470 lines (407 loc) · 12.6 KB
/
types.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
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
/* eslint-disable no-unused-vars */
import { IClientMeta } from "@walletconnect/types";
import algosdk, { Account as AccountSDK, LogicSigAccount, Transaction } from "algosdk";
import * as z from "zod";
import { WalletMultisigMetadata, WalletTransaction } from "./algo-signer-types";
import { WAIT_ROUNDS } from "./lib/constants";
import type { ASADefSchema, ASADefsSchema } from "./types-input";
export type AccountAddress = string;
export interface AnyMap {
[key: string]: any; // eslint-disable-line @typescript-eslint/no-explicit-any
}
/**
* After an asset has been created only the manager,
* reserve, freeze and reserve accounts can be changed.
* All other parameters are locked for the life of the asset.
*/
export interface AssetModFields {
manager?: string;
reserve?: string;
freeze?: string;
clawback?: string;
}
/**
* Common transaction parameters (fees, note..) */
export interface TxParams {
/**
* feePerByte or totalFee is used to set the appropriate transaction fee parameter.
* If both are set then totalFee takes precedence.
* NOTE: SDK expects`fee: number` and boolean `flatFee`. But the API expects only one
* on parameter: `fee`. Here, we define feePerByte and totalFee - both as numberic
* parameters. We think that this is more explicit. */
feePerByte?: number;
totalFee?: number;
flatFee?: boolean;
// The first round for when the transaction is valid.
firstValid?: number;
// firstValid + validRounds will give us the ending round for which the transaction is valid.
validRounds?: number;
// A lease enforces mutual exclusion of transactions.
lease?: Uint8Array;
// Any data up to 1000 bytes.
note?: string | Uint8Array;
// base64 encoded string
noteb64?: string;
// When set, it indicates that the transaction is requesting
// that the Sender account should be closed, and all remaining
// funds, after the fee and amount are paid, be transferred to this address.
closeRemainderTo?: AccountAddress;
// Specifies the authorized address.
rekeyTo?: AccountAddress;
// you can learn more about these parameters here.(https://developer.algorand.org/docs/reference/transactions/#common-fields-header-and-type)
}
/**
* Stateful Smart contract flags for specifying sender and schema */
export interface AppDeploymentFlags extends AppOptionalFlags {
sender: AccountSDK;
localInts: number;
localBytes: number;
globalInts: number;
globalBytes: number;
extraPages?: number;
}
/**
* Stateful smart contract transaction optional parameters (accounts, args..). */
export interface AppOptionalFlags {
/**
* Transaction specific arguments accessed from
* the application's approval-program and clear-state-program.
*/
appArgs?: Array<Uint8Array | string>;
/**
* List of accounts in addition to the sender that may
* be accessed from the application's approval-program and clear-state-program.
*/
accounts?: string[];
/**
* Lists the applications in addition to the application-id
* whose global states may be accessed by this
* application's approval-program and clear-state-program. The access is read-only.
*/
foreignApps?: number[];
/**
* Lists the assets whose AssetParams may be accessed by
* this application's approval-program and clear-state-program.
* The access is read-only.
*/
foreignAssets?: number[];
// Any data up to 1000 bytes.
note?: Uint8Array;
// A lease enforces mutual exclusion of transactions.
lease?: Uint8Array;
// you can learn more about these parameters from here.(https://developer.algorand.org/docs/reference/transactions/#application-call-transaction)
}
/**
* Transaction execution parameters (on blockchain OR runtime) */
export type ExecParams =
| AlgoTransferParam
| AssetTransferParam
| KeyRegistrationParam
| AppCallsParam
| ModifyAssetParam
| FreezeAssetParam
| RevokeAssetParam
| DestroyAssetParam
| DeployASAParam
| DeployAppParam
| OptInASAParam
| UpdateAppParam;
export enum SignType {
SecretKey,
LogicSignature,
MultiSignature,
}
export enum TransactionType {
TransferAlgo,
TransferAsset,
KeyRegistration,
ModifyAsset,
FreezeAsset,
RevokeAsset,
DestroyAsset,
CallApp,
ClearApp,
CloseApp,
DeleteApp,
DeployASA,
DeployApp,
OptInASA,
OptInToApp,
UpdateApp,
}
interface SignWithSk {
sign: SignType.SecretKey;
fromAccount: AccountSDK;
/**
* if passed then it will be used as the from account address, but tx will be signed
* by fromAcount's sk. This is used if an account address is rekeyed to another account. */
fromAccountAddr?: AccountAddress;
}
interface SignWithLsig {
sign: SignType.LogicSignature;
fromAccount?: AccountSDK;
fromAccountAddr: AccountAddress;
lsig: LogicSigAccount;
/** logic signature args */
args?: Uint8Array[];
}
export interface SignWithMultisig {
sign: SignType.MultiSignature;
mparams: WalletMultisigMetadata;
fromAccount?: AccountSDK;
fromAccountAddr: AccountAddress;
}
export type Lsig = SignWithLsig;
export type Sign = SignWithSk | SignWithLsig | SignWithMultisig;
export type BasicParams = Sign & {
payFlags: TxParams;
};
export type DeployASAParam = BasicParams & {
type: TransactionType.DeployASA;
asaName: string;
asaDef?: ASADef;
overrideAsaDef?: Partial<ASADef>;
};
export enum MetaType {
FILE,
SOURCE_CODE,
BYTES,
}
export type StorageConfig = {
localInts: number;
localBytes: number;
globalInts: number;
globalBytes: number;
extraPages?: number;
appName: string; // name of app to store info against in checkpoint, now it's required
};
export type SourceFile = {
metaType: MetaType.FILE;
approvalProgramFilename: string;
clearProgramFilename: string;
};
export type SourceCode = {
metaType: MetaType.SOURCE_CODE;
approvalProgramCode: string;
clearProgramCode: string;
};
// Compiled bytes of a TEAL program.
export type SourceCompiled = {
metaType: MetaType.BYTES;
approvalProgramBytes: Uint8Array;
clearProgramBytes: Uint8Array;
};
export type SmartContract = (SourceFile | SourceCode | SourceCompiled) & {
extraPages?: number;
};
export type AppDefinitionFromFile = StorageConfig & AppOptionalFlags & SourceFile;
export type AppDefinitionFromSource = StorageConfig & AppOptionalFlags & SourceCode;
export type AppDefinitionFromSourceCompiled = StorageConfig & AppOptionalFlags & SourceCompiled;
export type AppDefinition =
| AppDefinitionFromFile
| AppDefinitionFromSource
| AppDefinitionFromSourceCompiled;
export type DeployAppParam = BasicParams & {
type: TransactionType.DeployApp;
appDefinition: AppDefinition;
};
export type UpdateAppParam = BasicParams &
AppOptionalFlags & {
type: TransactionType.UpdateApp;
appID: number;
newAppCode: SmartContract;
appName: string; // name of app to store info against in checkpoint
};
export type AppCallsParam = BasicParams &
AppOptionalFlags & {
type:
| TransactionType.CallApp
| TransactionType.ClearApp
| TransactionType.CloseApp
| TransactionType.DeleteApp
| TransactionType.OptInToApp;
appID: number;
};
export type OptInASAParam = BasicParams & {
type: TransactionType.OptInASA;
assetID: number | string;
};
export type ModifyAssetParam = BasicParams & {
type: TransactionType.ModifyAsset;
assetID: number | string;
fields: AssetModFields;
};
export type FreezeAssetParam = BasicParams & {
type: TransactionType.FreezeAsset;
assetID: number | string;
freezeTarget: AccountAddress;
freezeState: boolean;
};
export type RevokeAssetParam = BasicParams & {
type: TransactionType.RevokeAsset;
/**
* Revoked assets are sent to this address
*/
recipient: AccountAddress;
assetID: number | string;
/** Revocation target is the account from which the clawback revokes asset. */
revocationTarget: AccountAddress;
amount: number | bigint;
};
export type DestroyAssetParam = BasicParams & {
type: TransactionType.DestroyAsset;
assetID: number | string;
};
export type AlgoTransferParam = BasicParams & {
type: TransactionType.TransferAlgo;
toAccountAddr: AccountAddress;
amountMicroAlgos: number | bigint;
};
export type AssetTransferParam = BasicParams & {
type: TransactionType.TransferAsset;
toAccountAddr: AccountAddress;
amount: number | bigint;
assetID: number | string;
};
export type KeyRegistrationParam = BasicParams & {
type: TransactionType.KeyRegistration;
/// voteKey must be a 32 byte Uint8Array or Buffer or base64 string.
voteKey: string | Uint8Array;
/// voteKey must be a 32 byte Uint8Array or Buffer or base64 string.
selectionKey: string | Uint8Array;
voteFirst: number;
voteLast: number;
voteKeyDilution: number;
nonParticipation?: false;
};
export interface TransactionAndSign {
transaction: Transaction;
sign: Sign;
}
export type ASADef = z.infer<typeof ASADefSchema>;
export type ASADefs = z.infer<typeof ASADefsSchema>;
export interface RequestError extends Error {
response?: {
statusCode: number;
text: string;
body: {
message: string;
};
error?: Error;
};
error?: Error;
}
export interface FileError extends Error {
errno: number;
}
// This function is used to check if given objects implements `FileError` interface
export function isFileError(object: unknown): object is FileError {
return Object.prototype.hasOwnProperty.call(object, "errno");
}
// This function is used to check if given objects implements `RequestError` interface
// https://www.technicalfeeder.com/2021/02/how-to-check-if-a-object-implements-an-interface-in-typescript/
export function isRequestError(object: unknown): object is RequestError {
const res =
Object.prototype.hasOwnProperty.call(object, "response.statusCode") &&
Object.prototype.hasOwnProperty.call(object, "response.text") &&
Object.prototype.hasOwnProperty.call(object, "response.body.message") &&
Object.prototype.hasOwnProperty.call(object, "response.error");
return res && Object.prototype.hasOwnProperty.call(object, "error");
}
// This function checks if given object implements `Transaction` class
export function isSDKTransaction(object: unknown): object is Transaction {
if (object === undefined || object === null) {
return false;
}
const props = ["tag", "from", "fee", "firstRound", "lastRound", "genesisID", "genesisHash"];
let res = Object.prototype.hasOwnProperty.call(object, "name");
for (const prop of props) {
res = res && Object.prototype.hasOwnProperty.call(object, prop);
}
return res;
}
// This function checks if given object implements `Transaction` class and has Sign
export function isSDKTransactionAndSign(object: unknown): object is TransactionAndSign {
if (object === undefined || object === null) {
return false;
}
const res = isSDKTransaction((object as TransactionAndSign).transaction);
return Object.prototype.hasOwnProperty.call(object, "sign") && res;
}
// This function checks if given object implements `ExecParams` class
export function isExecParams(object: unknown): object is ExecParams {
if (object === undefined || object === null) {
return false;
}
const props = ["payFlags", "sign"];
let res = Object.prototype.hasOwnProperty.call(object, "type");
for (const prop of props) {
res = res && Object.prototype.hasOwnProperty.call(object, prop);
}
return res;
}
/* Wallet Connect types */
export enum ChainType {
MainNet = "MainNet",
TestNet = "TestNet",
BetaNet = "BetaNet",
}
export interface SessionConnectResponse {
peerId: string;
peerMeta?: IClientMeta;
accounts: string[];
}
export interface SessionUpdateResponse {
accounts: string[];
}
export interface SessionDisconnectResponse {
message?: string;
}
export interface SignTxnOpts {
/**
* Optional message explaining the reason of the group of
* transactions.
*/
message?: string;
// other options may be present, but are not standard
}
export type SignTxnParams = [WalletTransaction[], SignTxnOpts?];
export interface TransactionInGroup {
txn: Transaction;
shouldSign?: boolean;
signers?: string | string[];
msig?: WalletMultisigMetadata;
message?: string;
}
export interface AlgodTokenHeader {
"X-Algo-API-Token": string;
}
export interface CustomTokenHeader {
[headerName: string]: string;
}
export interface HttpNetworkConfig {
server: string; // with optional http o https prefix
port: string | number;
token: string | AlgodTokenHeader | CustomTokenHeader;
httpHeaders?: { [name: string]: string };
}
export { WAIT_ROUNDS };
export interface ConfirmedTxInfo {
"confirmed-round": number;
"asset-index": number;
"application-index": number;
"global-state-delta"?: algosdk.modelsv2.EvalDeltaKeyValue;
"local-state-delta"?: algosdk.modelsv2.AccountStateDelta;
"inner-txns"?: ConfirmedTxInfo;
txn: algosdk.EncodedSignedTransaction;
}
export interface TxnReceipt extends ConfirmedTxInfo {
txID: string;
}
/**
* contractABI extends the algosdk type by appID which is helpful in algob
* since in every mode we know what is the network currently in use
*/
export interface ABIContract extends algosdk.ABIContract {
appID?: number;
}