Skip to content

Commit

Permalink
Merge pull request #30 from algorand-devrel/dev
Browse files Browse the repository at this point in the history
0.9.0
  • Loading branch information
joe-p authored Apr 4, 2023
2 parents dce9dba + 1f73f2f commit e8777f7
Show file tree
Hide file tree
Showing 13 changed files with 515 additions and 21 deletions.
26 changes: 26 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -16,3 +16,29 @@ TEALScript is still very much a work in progress. The current version is `0.x.x`
* ABI ufixed encoding/decoding
* More testing
* More documentation

## Example Contract

The artifacts for this contract can be seen in [examples/calculator/artifacts](./examples/calculator/artifacts)

```ts
import { Contract } from '../../src/lib/index';

class Calculator extends Contract {
private getSum(a: number, b: number): number {
return a + b;
}

private getDifference(a: number, b: number): number {
return a >= b ? a - b : b - a;
}

doMath(a: number, b: number, operation: string): number {
if (operation === 'sum') {
return this.getSum(a, b);
} if (operation === 'difference') {
return this.getDifference(a, b);
} throw Error('Invalid operation');
}
}
```
31 changes: 31 additions & 0 deletions examples/calculator/artifacts/Calculator.abi.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
{
"name": "Calculator",
"desc": "",
"methods": [
{
"name": "doMath",
"args": [
{
"name": "a",
"type": "uint64",
"desc": ""
},
{
"name": "b",
"type": "uint64",
"desc": ""
},
{
"name": "operation",
"type": "string",
"desc": ""
}
],
"desc": "",
"returns": {
"type": "uint64",
"desc": ""
}
}
]
}
126 changes: 126 additions & 0 deletions examples/calculator/artifacts/Calculator.approval.teal
Original file line number Diff line number Diff line change
@@ -0,0 +1,126 @@
#pragma version 8
b main

getSum:
proto 3 1

// examples/calculator/calculator.algo.ts:6
// return a + b;
frame_dig -1 // a: uint64
frame_dig -2 // b: uint64
+
itob
byte 0x151f7c75
swap
concat
log
retsub

getDifference:
proto 3 1

// examples/calculator/calculator.algo.ts:10
// return a >= b ? a - b : b - a;
frame_dig -1 // a: uint64
frame_dig -2 // b: uint64
>=
bz ternary0_false
frame_dig -1 // a: uint64
frame_dig -2 // b: uint64
-
b ternary0_end

ternary0_false:
frame_dig -2 // b: uint64
frame_dig -1 // a: uint64
-

ternary0_end:
itob
byte 0x151f7c75
swap
concat
log
retsub

abi_route_doMath:
txn OnCompletion
int NoOp
==
txn ApplicationID
int 0
!=
&&
assert
byte 0x
dupn 1
txna ApplicationArgs 3
extract 2 0
txna ApplicationArgs 2
btoi
txna ApplicationArgs 1
btoi
callsub doMath
int 1
return

doMath:
proto 4 0

// if0_condition
// examples/calculator/calculator.algo.ts:14
// operation === 'sum'
frame_dig -3 // operation: bytes
byte "sum"
==
bz if0_end

// if0_consequent
// examples/calculator/calculator.algo.ts:15
// return this.getSum(a, b);
byte 0x
dupn 1
frame_dig -2 // b: uint64
frame_dig -1 // a: uint64
callsub getSum
itob
byte 0x151f7c75
swap
concat
log

if0_end:
// if1_condition
// examples/calculator/calculator.algo.ts:16
// operation === 'difference'
frame_dig -3 // operation: bytes
byte "difference"
==
bz if1_end

// if1_consequent
// examples/calculator/calculator.algo.ts:17
// return this.getDifference(a, b);
byte 0x
dupn 1
frame_dig -2 // b: uint64
frame_dig -1 // a: uint64
callsub getDifference
itob
byte 0x151f7c75
swap
concat
log

if1_end:
err // 'Invalid operation'
retsub

main:
txn NumAppArgs
bnz route_abi

route_abi:
method "doMath(uint64,uint64,string)uint64"
txna ApplicationArgs 0
match abi_route_doMath
3 changes: 3 additions & 0 deletions examples/calculator/artifacts/Calculator.clear.teal
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
#pragma version 8
int 1
return
1 change: 1 addition & 0 deletions examples/calculator/artifacts/Calculator.json
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
{"hints":{},"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":"I3ByYWdtYSB2ZXJzaW9uIDgKCWIgbWFpbgoKZ2V0U3VtOgoJcHJvdG8gMyAxCgoJLy8gZXhhbXBsZ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Y29uY2F0Cglsb2cKCmlmMV9lbmQ6CgllcnIgLy8gJ0ludmFsaWQgb3BlcmF0aW9uJwoJcmV0c3ViCgptYWluOgoJdHhuIE51bUFwcEFyZ3MKCWJueiByb3V0ZV9hYmkKCnJvdXRlX2FiaToKCW1ldGhvZCAiZG9NYXRoKHVpbnQ2NCx1aW50NjQsc3RyaW5nKXVpbnQ2NCIKCXR4bmEgQXBwbGljYXRpb25BcmdzIDAKCW1hdGNoIGFiaV9yb3V0ZV9kb01hdGg=","clear":"I3ByYWdtYSB2ZXJzaW9uIDgKaW50IDEKcmV0dXJu"},"contract":{"name":"Calculator","desc":"","methods":[{"name":"doMath","args":[{"name":"a","type":"uint64","desc":""},{"name":"b","type":"uint64","desc":""},{"name":"operation","type":"string","desc":""}],"desc":"","returns":{"type":"uint64","desc":""}}]}}
173 changes: 173 additions & 0 deletions examples/calculator/artifacts/Calculator.src_map.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,173 @@
{
"0": 0,
"1": 0,
"2": 0,
"3": 0,
"4": 0,
"5": 0,
"6": 0,
"7": 0,
"8": 0,
"9": 0,
"10": 0,
"11": 0,
"12": 1,
"13": 1,
"14": 1,
"15": 4,
"16": 4,
"17": 4,
"18": 8,
"19": 8,
"20": 9,
"21": 9,
"22": 10,
"23": 11,
"24": 12,
"25": 13,
"26": 14,
"27": 15,
"28": 16,
"29": 19,
"30": 19,
"31": 19,
"32": 23,
"33": 23,
"34": 24,
"35": 24,
"36": 25,
"37": 26,
"38": 26,
"39": 26,
"40": 27,
"41": 27,
"42": 28,
"43": 28,
"44": 29,
"45": 30,
"46": 30,
"47": 30,
"48": 33,
"49": 33,
"50": 34,
"51": 34,
"52": 35,
"53": 38,
"54": 39,
"55": 40,
"56": 41,
"57": 42,
"58": 43,
"59": 46,
"60": 46,
"61": 47,
"62": 48,
"63": 49,
"64": 49,
"65": 50,
"66": 51,
"67": 52,
"68": 53,
"69": 54,
"70": 55,
"71": 55,
"72": 56,
"73": 56,
"74": 56,
"75": 57,
"76": 57,
"77": 57,
"78": 58,
"79": 58,
"80": 58,
"81": 59,
"82": 60,
"83": 60,
"84": 60,
"85": 61,
"86": 62,
"87": 62,
"88": 62,
"89": 63,
"90": 63,
"91": 64,
"92": 67,
"93": 67,
"94": 67,
"95": 72,
"96": 72,
"97": 73,
"98": 73,
"99": 73,
"100": 73,
"101": 73,
"102": 74,
"103": 75,
"104": 75,
"105": 75,
"106": 80,
"107": 81,
"108": 81,
"109": 82,
"110": 82,
"111": 83,
"112": 83,
"113": 84,
"114": 84,
"115": 84,
"116": 85,
"117": 86,
"118": 87,
"119": 88,
"120": 89,
"121": 95,
"122": 95,
"123": 96,
"124": 96,
"125": 96,
"126": 96,
"127": 96,
"128": 96,
"129": 96,
"130": 96,
"131": 96,
"132": 96,
"133": 96,
"134": 96,
"135": 97,
"136": 98,
"137": 98,
"138": 98,
"139": 103,
"140": 104,
"141": 104,
"142": 105,
"143": 105,
"144": 106,
"145": 106,
"146": 107,
"147": 107,
"148": 107,
"149": 108,
"150": 109,
"151": 110,
"152": 111,
"153": 112,
"154": 115,
"155": 116,
"156": 119,
"157": 119,
"158": 120,
"159": 120,
"160": 120,
"161": 123,
"162": 123,
"163": 123,
"164": 123,
"165": 123,
"166": 123,
"167": 124,
"168": 124,
"169": 124,
"170": 125
}
20 changes: 20 additions & 0 deletions examples/calculator/calculator.algo.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
import { Contract } from '../../src/lib/index';

// eslint-disable-next-line no-unused-vars
class Calculator extends Contract {
private getSum(a: number, b: number): number {
return a + b;
}

private getDifference(a: number, b: number): number {
return a >= b ? a - b : b - a;
}

doMath(a: number, b: number, operation: string): number {
if (operation === 'sum') {
return this.getSum(a, b);
} if (operation === 'difference') {
return this.getDifference(a, b);
} throw Error('Invalid operation');
}
}
Loading

0 comments on commit e8777f7

Please sign in to comment.