-
Notifications
You must be signed in to change notification settings - Fork 43
/
Copy pathtypes.ts
431 lines (384 loc) · 12.3 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
/* eslint-disable no-unused-vars */
import { types } from "@algo-builder/web";
import algosdk, {
Account as AccountSDK,
EncodedTransaction,
modelsv2,
Transaction,
} from "algosdk";
import { TxnFields } from "./lib/constants";
import type { IStack } from "./lib/stack";
export type AppArgs = Array<string | number>;
export type StackElem = bigint | Uint8Array;
export type TEALStack = IStack<bigint | Uint8Array>;
export interface EncTx extends EncodedTransaction {
txID: string;
// user should push raw string TEAL code - not compiled code
// for approvalProgram and clearProgram
metaType?: types.MetaType;
approvalProgram?: string;
clearProgram?: string;
}
export type TxField = keyof typeof TxnFields[2];
export enum TxnType {
unknown = "0", // Unknown type. Invalid transaction
pay = "1", // Payment
keyreg = "2", // KeyRegistration
acfg = "3", // AssetConfig
axfer = "4", // AssetTransfer
afrz = "5", // AssetFreeze
appl = "6", // ApplicationCall
}
export enum GlobalField {
MinTxnFee, // micro Algos
MinBalance, // micro Algos
MaxTxnLife, // rounds
ZeroAddress, // 32 byte address of all zero bytes
GroupSize, // Number of transactions in this atomic transaction group. At least 1
LogicSigVersion, // Maximum supported TEAL version.
Round, // Current round number
LatestTimestamp, // Last confirmed block UNIX timestamp. Fails if negative.
CurrentApplicationID, // ID of current application executing. Fails if no such application is executing.
}
export enum EncodingType {
BASE64,
BASE32,
HEX,
UTF8,
}
export type AccountAddress = string;
export type ID = number; // Asset or Application index
export interface AccountsMap {
[addr: string]: AccountStoreI;
}
/**
* RuntimeAccountMap is for AccountStore used in runtime
* (where we use maps instead of arrays in sdk structures). */
export type RuntimeAccountMap = Map<string, AccountAddress>;
export interface BaseTxReceipt {
txn: EncTx;
txID: string;
gas?: number;
logs?: Uint8Array[];
}
export type TxReceipt = BaseTxReceipt | AppInfo | ASAInfo;
export interface State {
accounts: Map<string, AccountStoreI>;
accountNameAddress: Map<string, AccountAddress>;
globalApps: Map<number, string>;
assetDefs: Map<number, AccountAddress>;
assetNameInfo: Map<string, ASAInfo>;
appNameMap: Map<string, AppInfo>;
appCounter: number;
assetCounter: number;
txReceipts: Map<string, TxReceipt>; // map of {txID: txReceipt}
blocks: Map<number, Block>; // map of{round, block}
}
export interface DeployedAssetInfo {
creator: AccountAddress;
txID: string;
confirmedRound: number;
deleted: boolean;
}
// ASA deployment information (log)
export interface ASAInfo extends DeployedAssetInfo {
assetIndex: number;
assetDef: types.ASADef;
logs?: Uint8Array[];
}
// Stateful smart contract deployment information (log)
export interface AppInfo extends DeployedAssetInfo {
appID: number;
applicationAccount: string;
timestamp: number;
approvalFile: string;
clearFile: string;
logs?: Uint8Array[];
gas?: number; // used in runtime
}
export interface AppDeploymentFlags extends AppOptionalFlags {
sender: AccountSDK;
localInts: number;
localBytes: number;
globalInts: number;
globalBytes: number;
extraPages?: number;
}
// describes interpreter's local context (state + txns)
export interface Context {
state: State;
sharedScratchSpace: Map<number, StackElem[]>;
knowableID: Map<number, ID>;
tx: EncTx; // current txn
gtxs: EncTx[]; // all transactions
args?: Uint8Array[];
debugStack?: number; // max number of top elements from the stack to print after each opcode execution.
lastLog: Uint8Array;
budget: number;
pooledApplCost: number; // total opcode cost for each application call for single/group tx
// inner transaction props
isInnerTx: boolean; // true if "ctx" is switched to an inner transaction
// save appID use inner tx call stack
innerTxAppIDCallStack: number[];
remainingTxns: number; // number txn can execute on current call, include inner txn and normal txn.
// remaining fee from pool
remainingFee: number;
getAccount: (address: string) => AccountStoreI;
getAssetAccount: (assetId: number) => AccountStoreI;
getApp: (appID: number, line?: number) => SSCAttributesM;
getCallerApplicationID: () => number;
getCallerApplicationAddress: () => AccountAddress;
transferAlgo: (transaction: Transaction) => void;
verifyMinimumFees: () => void;
deductFee: (sender: AccountAddress, index: number, params: types.TxParams) => void;
transferAsset: (transaction: Transaction) => void;
modifyAsset: (assetId: number, fields: types.AssetModFields) => void;
freezeAsset: (assetId: number, freezeTarget: string, freezeState: boolean) => void;
revokeAsset: (
recipient: string,
assetID: number,
revocationTarget: string,
amount: bigint
) => void;
destroyAsset: (assetId: number) => void;
deleteApp: (appID: number) => void;
closeApp: (sender: AccountAddress, appID: number) => void;
processTransactions: (
signedTransactions: algosdk.SignedTransaction[],
appDefMap?: Map<number, types.AppDefinition | types.SmartContract>,
lsigMap?: Map<number, types.Lsig>
) => TxReceipt[];
deployASA: (
name: string,
fromAccountAddr: AccountAddress,
flags: ASADeploymentFlags
) => ASAInfo;
deployASADef: (
name: string,
asaDef: types.ASADef,
fromAccountAddr: AccountAddress,
flags: ASADeploymentFlags
) => ASAInfo;
optInToASA: (assetIndex: number, address: AccountAddress, flags: types.TxParams) => TxReceipt;
deployApp: (
fromAccountAddr: string,
appDefinition: types.AppDefinition,
idx: number,
scTmplParams?: SCParams
) => AppInfo;
optInToApp: (accountAddr: string, appID: number, idx: number) => TxReceipt;
updateApp: (
appID: number,
appSourceCode: types.SmartContract,
idx: number,
scTmplParams?: SCParams
) => TxReceipt;
assertProgramMaxLen: (
approvalProgramBytes: Uint8Array,
clearProgramBytes: Uint8Array,
extraPages: number
) => void;
}
// custom AssetHolding for AccountStore (using bigint in amount instead of number)
export interface AssetHoldingM {
amount: bigint;
"asset-id": number;
creator: string;
"is-frozen": boolean;
}
// custom AppsLocalState for AccountStore (using maps instead of array in 'key-value')
export interface AppLocalStateM {
id: number;
"key-value": Map<string, StackElem>; // string represents bytes as string eg. 11,22,34
schema: modelsv2.ApplicationStateSchema;
}
// custom SSCAttributes for AccountStore (using maps instead of array in 'global-state')
export interface SSCAttributesM {
"approval-program": string;
"clear-state-program": string;
creator: string;
"global-state": Map<string, StackElem>;
"global-state-schema": modelsv2.ApplicationStateSchema;
"local-state-schema": modelsv2.ApplicationStateSchema;
}
// custom CreatedApp for AccountStore
export interface CreatedAppM {
id: number;
attributes: SSCAttributesM;
}
export interface RuntimeAccountI extends AccountSDK {
name?: string;
spend: types.AccountAddress;
rekeyTo: (authAccountAddress: types.AccountAddress) => void;
getSpendAddress: () => types.AccountAddress;
}
// represent account used in tests and by the context
// NOTE: custom notations are used rather than SDK AccountState notations
export interface AccountStoreI {
address: string;
assets: Map<number, AssetHoldingM>;
amount: bigint;
minBalance: number;
appsLocalState: Map<number, AppLocalStateM>;
appsTotalSchema: modelsv2.ApplicationStateSchema;
createdApps: Map<number, SSCAttributesM>;
createdAssets: Map<number, modelsv2.AssetParams>;
account: RuntimeAccountI;
balance: () => bigint;
rekeyTo: (authAccountAddress: types.AccountAddress) => void;
getSpendAddress: () => AccountAddress;
getApp: (appID: number) => SSCAttributesM | undefined;
getAppFromLocal: (appID: number) => AppLocalStateM | undefined;
addApp: (appID: number, appDefinition: types.AppDefinitionFromSource) => CreatedAppM;
getAssetDef: (assetId: number) => modelsv2.AssetParams | undefined;
getAssetHolding: (assetId: number) => AssetHoldingM | undefined;
addAsset: (assetId: number, name: string, asadef: types.ASADef) => modelsv2.AssetParams;
modifyAsset: (assetId: number, fields: types.AssetModFields) => void;
closeAsset: (assetId: number) => void;
setFreezeState: (assetId: number, state: boolean) => void;
destroyAsset: (assetId: number) => void;
optInToApp: (appID: number, appParams: SSCAttributesM) => void;
optInToASA: (assetIndex: number, assetHolding: AssetHoldingM) => void;
deleteApp: (appID: number) => void;
closeApp: (appID: number) => void;
getLocalState: (appID: number, key: Uint8Array | string) => StackElem | undefined;
setLocalState: (
appID: number,
key: Uint8Array | string,
value: StackElem,
line?: number
) => AppLocalStateM;
getGlobalState: (appID: number, key: Uint8Array | string) => StackElem | undefined;
setGlobalState: (
appID: number,
key: Uint8Array | string,
value: StackElem,
line?: number
) => void;
}
/**
* https://developer.algorand.org/docs/reference/teal/specification/#oncomplete */
export enum TxOnComplete {
NoOp = "0",
OptIn = "1",
CloseOut = "2",
ClearState = "3",
UpdateApplication = "4",
DeleteApplication = "5",
}
/**
* https://developer.algorand.org/docs/reference/teal/specification/#execution-modes */
export enum ExecutionMode {
SIGNATURE, // stateless TEAL
APPLICATION, // application call (NoOp, CloseOut..)
}
/**
* Stateful Smart contract flags for specifying sender and schema */
export interface types 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)
}
/**
* 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;
}
export interface AnyMap {
[key: string]: any; // eslint-disable-line @typescript-eslint/no-explicit-any
}
export interface Account extends AccountSDK {
// from AccountSDK: addr: string;
// sk: Uint8Array
name: string;
}
export interface ASADeploymentFlags extends types.TxParams {
creator: Account;
}
/**
* SDK account type, used in algob */
export type AccountMap = Map<string, Account>;
/**
* SDK decoding types (Configure how the integer will be decoded)
* https://github.com/algorand/js-algorand-sdk/blob/develop/src/encoding/uint64.ts#L29
*/
export enum DecodingMode {
SAFE = "safe",
MIXED = "mixed",
BIGINT = "bigint",
}
// used for pyTEALOps
export interface SCParams {
[key: string]: string | bigint;
}
export interface ReplaceParams {
[key: string]: string;
}
export enum Base64Encoding {
URL = 0,
STD = 1,
}
export declare type TxnReceipt = BaseTxReceipt | AppInfoReceipt | ASAInfoReceipt;
export interface DeployedAssetInfoReceipt {
creator: AccountAddress;
txID: string;
"confirmed-round": number;
deleted: boolean;
}
export interface ASAInfoReceipt extends DeployedAssetInfoReceipt {
"asset-index": number;
"asset-def": types.ASADef;
logs?: Uint8Array[];
}
export interface AppInfoReceipt extends DeployedAssetInfoReceipt {
"application-id": number;
"application-account": string;
timestamp: number;
"approval-file": string;
"clear-file": string;
logs?: Uint8Array[];
gas?: number;
}
export interface Block {
timestamp: bigint; //uint64
seed: Uint8Array; //[]byte
}