Skip to content

Commit

Permalink
txn.typeEnum and TransactionType
Browse files Browse the repository at this point in the history
  • Loading branch information
joe-p committed Jul 13, 2023
1 parent 03af67f commit e546bc5
Show file tree
Hide file tree
Showing 8 changed files with 168 additions and 1 deletion.
16 changes: 16 additions & 0 deletions src/lib/compiler.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3006,6 +3006,22 @@ export default class Compiler {
}

private processMemberExpression(node: ts.PropertyAccessExpression) {
if (node.expression.getText() === 'TransactionType') {
const enums: {[key: string]: string} = {
Unknown: 'unknown',
Payment: 'pay',
KeyRegistration: 'keyreg',
AssetConfig: 'acfg',
AssetTransfer: 'axfer',
AssetFreeze: 'afrz',
ApplicationCall: 'appl',
};

if (!enums[node.name.getText()]) throw new Error(`Unknown transaction type ${node.name.getText()}`);
this.pushVoid(node, `int ${enums[node.name.getText()]}`);
return;
}

const chain = this.getChain(node).reverse();

chain.push(node);
Expand Down
15 changes: 15 additions & 0 deletions tests/contracts/artifacts/GeneralTest.abi.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
{
"name": "GeneralTest",
"desc": "",
"methods": [
{
"name": "txnTypeEnum",
"args": [],
"desc": "",
"returns": {
"type": "void",
"desc": ""
}
}
]
}
53 changes: 53 additions & 0 deletions tests/contracts/artifacts/GeneralTest.approval.teal
Original file line number Diff line number Diff line change
@@ -0,0 +1,53 @@
#pragma version 8
b main

abi_route_txnTypeEnum:
txn OnCompletion
int NoOp
==
txn ApplicationID
int 0
!=
&&
assert
byte 0x
callsub txnTypeEnum
int 1
return

txnTypeEnum:
proto 1 0

// tests/contracts/general.algo.ts:6
// t = this.txnGroup[0]
int 0
frame_bury -1 // t: txn

// tests/contracts/general.algo.ts:7
// assert(t.typeEnum === TransactionType.Payment)
frame_dig -1 // t: txn
gtxns TypeEnum
int pay
==
assert
retsub

main:
txn NumAppArgs
bnz route_abi

// default createApplication
txn ApplicationID
int 0
==
txn OnCompletion
int NoOp
==
&&
return

route_abi:
method "txnTypeEnum()void"
txna ApplicationArgs 0
match abi_route_txnTypeEnum
err
3 changes: 3 additions & 0 deletions tests/contracts/artifacts/GeneralTest.clear.teal
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
#pragma version 8
int 1
return
51 changes: 51 additions & 0 deletions tests/contracts/artifacts/GeneralTest.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,51 @@
{
"hints": {
"txnTypeEnum()void": {
"call_config": {
"no_op": "CALL"
}
}
},
"bare_call_config": {
"no_op": "CREATE"
},
"schema": {
"local": {
"declared": {},
"reserved": {}
},
"global": {
"declared": {},
"reserved": {}
}
},
"state": {
"global": {
"num_byte_slices": 0,
"num_uints": 0
},
"local": {
"num_byte_slices": 0,
"num_uints": 0
}
},
"source": {
"approval": "I3ByYWdtYSB2ZXJzaW9uIDgKCWIgbWFpbgoKYWJpX3JvdXRlX3R4blR5cGVFbnVtOgoJdHhuIE9uQ29tcGxldGlvbgoJaW50IE5vT3AKCT09Cgl0eG4gQXBwbGljYXRpb25JRAoJaW50IDAKCSE9CgkmJgoJYXNzZXJ0CglieXRlIDB4CgljYWxsc3ViIHR4blR5cGVFbnVtCglpbnQgMQoJcmV0dXJuCgp0eG5UeXBlRW51bToKCXByb3RvIDEgMAoKCS8vIHRlc3RzL2NvbnRyYWN0cy9nZW5lcmFsLmFsZ28udHM6NgoJLy8gdCA9IHRoaXMudHhuR3JvdXBbMF0KCWludCAwCglmcmFtZV9idXJ5IC0xIC8vIHQ6IHR4bgoKCS8vIHRlc3RzL2NvbnRyYWN0cy9nZW5lcmFsLmFsZ28udHM6NwoJLy8gYXNzZXJ0KHQudHlwZUVudW0gPT09IFRyYW5zYWN0aW9uVHlwZS5QYXltZW50KQoJZnJhbWVfZGlnIC0xIC8vIHQ6IHR4bgoJZ3R4bnMgVHlwZUVudW0KCWludCBwYXkKCT09Cglhc3NlcnQKCXJldHN1YgoKbWFpbjoKCXR4biBOdW1BcHBBcmdzCglibnogcm91dGVfYWJpCgoJLy8gZGVmYXVsdCBjcmVhdGVBcHBsaWNhdGlvbgoJdHhuIEFwcGxpY2F0aW9uSUQKCWludCAwCgk9PQoJdHhuIE9uQ29tcGxldGlvbgoJaW50IE5vT3AKCT09CgkmJgoJcmV0dXJuCgpyb3V0ZV9hYmk6CgltZXRob2QgInR4blR5cGVFbnVtKCl2b2lkIgoJdHhuYSBBcHBsaWNhdGlvbkFyZ3MgMAoJbWF0Y2ggYWJpX3JvdXRlX3R4blR5cGVFbnVtCgllcnI=",
"clear": "I3ByYWdtYSB2ZXJzaW9uIDgKaW50IDEKcmV0dXJu"
},
"contract": {
"name": "GeneralTest",
"desc": "",
"methods": [
{
"name": "txnTypeEnum",
"args": [],
"desc": "",
"returns": {
"type": "void",
"desc": ""
}
}
]
}
}
9 changes: 9 additions & 0 deletions tests/contracts/general.algo.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
import { Contract } from '../../src/lib/index';

// eslint-disable-next-line no-unused-vars
class GeneralTest extends Contract {
txnTypeEnum(): void {
const t = this.txnGroup[0];
assert(t.typeEnum === TransactionType.Payment);
}
}
3 changes: 3 additions & 0 deletions tests/general.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
import { artifactsTest } from './common';

artifactsTest('GeneralTest', 'tests/contracts/general.algo.ts', 'tests/contracts/artifacts/', 'GeneralTest');
19 changes: 18 additions & 1 deletion types/global.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -551,7 +551,13 @@ interface MethodCallParams<ArgsType> extends AppParams {

type ThisTxnParams = AppOnChainTransactionParams & AppParams

type Txn = PayTxn & AssetTransferTxn & AppCallTxn & KeyRegTxn & AssetConfigTxn & AssetFreezeTxn
type Txn = PayTxn &
AssetTransferTxn &
AppCallTxn &
KeyRegTxn &
AssetConfigTxn &
AssetFreezeTxn
& {typeEnum: TransactionType}

declare const globals: {
minTxnFee: uint64
Expand Down Expand Up @@ -669,3 +675,14 @@ type StaticArray<
T extends BytesLike | IntLike | StaticArray,
N extends number
> = (T extends byte ? string : (N extends 0 ? never[] : T[])) & {__type?: T, __length?: N}

// eslint-disable-next-line no-shadow
enum TransactionType {
Unknown, // unknown
Payment, // pay
KeyRegistration, // keyreg
AssetConfig, // acfg
AssetTransfer, // axfer
AssetFreeze, // afrz
ApplicationCall, // appl
}

0 comments on commit e546bc5

Please sign in to comment.