Skip to content

Commit

Permalink
Merge pull request #80 from algorand-devrel/feature/booleans
Browse files Browse the repository at this point in the history
Initial boolean
  • Loading branch information
joe-p authored Jul 23, 2023
2 parents 2e2926d + a27b701 commit 16a4f0f
Show file tree
Hide file tree
Showing 44 changed files with 1,986 additions and 63 deletions.
246 changes: 191 additions & 55 deletions src/lib/compiler.ts

Large diffs are not rendered by default.

80 changes: 73 additions & 7 deletions tests/abi.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -143,14 +143,19 @@ async function runMethod(
sendParams: { suppressLog: true },
};

if (name.includes('Storage')) {
await appClient.fundAppAccount({
amount: algokit.microAlgos(127400),
sendParams: { suppressLog: true },
});
return (await appClient.optIn(params)).return?.returnValue;
try {
if (name.includes('Storage')) {
await appClient.fundAppAccount({
amount: algokit.microAlgos(127400),
sendParams: { suppressLog: true },
});
return (await appClient.optIn(params)).return?.returnValue;
}
return (await appClient.call(params)).return?.returnValue;
} catch (e) {
console.warn(e);
throw e;
}
return (await appClient.call(params)).return?.returnValue;
}

describe('ABI', function () {
Expand Down Expand Up @@ -607,4 +612,65 @@ describe('ABI', function () {

expect(await runMethod(appClient, 'emptyDynamicArray')).toEqual([]);
});
test.concurrent('booleanArgAndReturn', async () => {
const { appClient } = await compileAndCreate('booleanArgAndReturn');

expect(await runMethod(appClient, 'booleanArgAndReturn', [true])).toEqual(true);

expect(await runMethod(appClient, 'booleanArgAndReturn', [false])).toEqual(false);
});

test.concurrent('boolTuple', async () => {
const { appClient } = await compileAndCreate('boolTuple');

expect(await runMethod(appClient, 'boolTuple')).toEqual([true, false, true, true, false, false, true, false, false]);
});

test.concurrent('staticBoolArray', async () => {
const { appClient } = await compileAndCreate('staticBoolArray');

expect(await runMethod(appClient, 'staticBoolArray')).toEqual([true, false, true, true, false, false, true, false, false]);
});

test.concurrent('boolTupleAccess', async () => {
const { appClient } = await compileAndCreate('boolTupleAccess');

expect(await runMethod(appClient, 'boolTupleAccess')).toEqual(true);
});

test.concurrent('staticBoolArrayAccess', async () => {
const { appClient } = await compileAndCreate('staticBoolArrayAccess');

expect(await runMethod(appClient, 'staticBoolArrayAccess')).toEqual(false);
});

test.concurrent('dynamicBoolArray', async () => {
const { appClient } = await compileAndCreate('dynamicBoolArray');

expect(await runMethod(appClient, 'dynamicBoolArray')).toEqual([true, false, true, true, false, false, true, false, false]);
});

test.concurrent('dynamicBoolArrayAccess', async () => {
const { appClient } = await compileAndCreate('dynamicBoolArrayAccess');

expect(await runMethod(appClient, 'dynamicBoolArrayAccess')).toEqual(false);
});

test.concurrent('staticBoolArrayUpdate', async () => {
const { appClient } = await compileAndCreate('staticBoolArrayUpdate');

expect(await runMethod(appClient, 'staticBoolArrayUpdate')).toEqual([true, false, true, true, false, false, true, false, true]);
});

test.concurrent('dynamicBoolArrayUpdate', async () => {
const { appClient } = await compileAndCreate('dynamicBoolArrayUpdate');

expect(await runMethod(appClient, 'dynamicBoolArrayUpdate')).toEqual([true, false, true, true, false, false, true, false, true]);
});

test.concurrent('boolTupleUpdate', async () => {
const { appClient } = await compileAndCreate('boolTupleUpdate');

expect(await runMethod(appClient, 'boolTupleUpdate')).toEqual([true, false, true, true, false, false, true, false, true]);
});
});
92 changes: 92 additions & 0 deletions tests/contracts/abi.algo.ts
Original file line number Diff line number Diff line change
Expand Up @@ -835,3 +835,95 @@ class ABITestEmptyDynamicArray extends Contract {
return [];
}
}
class ABITestBooleanArgAndReturn extends Contract {
booleanArgAndReturn(a: boolean): boolean {
return a;
}
}

class ABITestBoolTuple extends Contract {
boolTuple(): [boolean, boolean, boolean, boolean, boolean, boolean, boolean, boolean, boolean] {
const a: [boolean, boolean, boolean, boolean, boolean, boolean, boolean, boolean, boolean] = [
true, false, true, true, false, false, true, false, false,
];

return a;
}
}

class ABITestStaticBoolArray extends Contract {
staticBoolArray(): StaticArray<boolean, 9> {
const a: StaticArray<boolean, 9> = [true, false, true, true, false, false, true, false, false];

return a;
}
}

class ABITestBoolTupleAccess extends Contract {
boolTupleAccess(): boolean {
const a: [
boolean, boolean, boolean, boolean, boolean, boolean, boolean, boolean, boolean
] = [
false, false, false, false, false, false, false, false, true,
];

return a[8];
}
}

class ABITestStaticBoolArrayAccess extends Contract {
staticBoolArrayAccess(): boolean {
const a: StaticArray<boolean, 9> = [true, false, true, true, false, false, true, false, false];

return a[8];
}
}

class ABITestDynamicBoolArray extends Contract {
dynamicBoolArray(): boolean[] {
const a: boolean[] = [true, false, true, true, false, false, true, false, false];

return a;
}
}

class ABITestDynamicBoolArrayAccess extends Contract {
dynamicBoolArrayAccess(): boolean {
const a: boolean[] = [true, false, true, true, false, false, true, false, false];

return a[8];
}
}

class ABITestStaticBoolArrayUpdate extends Contract {
staticBoolArrayUpdate(): StaticArray<boolean, 9> {
const a: StaticArray<boolean, 9> = [true, false, true, true, false, false, true, false, false];

a[8] = true;

return a;
}
}

class ABITestDynamicBoolArrayUpdate extends Contract {
dynamicBoolArrayUpdate(): boolean[] {
const a: boolean[] = [true, false, true, true, false, false, true, false, false];

a[8] = true;

return a;
}
}

class ABITestBoolTupleUpdate extends Contract {
boolTupleUpdate(): [
boolean, boolean, boolean, boolean, boolean, boolean, boolean, boolean, boolean
] {
const a: [
boolean, boolean, boolean, boolean, boolean, boolean, boolean, boolean, boolean
] = [true, false, true, true, false, false, true, false, false];

a[8] = true;
return a;
}
}
15 changes: 15 additions & 0 deletions tests/contracts/artifacts/ABITestBoolTuple.abi.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
{
"name": "ABITestBoolTuple",
"desc": "",
"methods": [
{
"name": "boolTuple",
"args": [],
"desc": "",
"returns": {
"type": "(bool,bool,bool,bool,bool,bool,bool,bool,bool)",
"desc": ""
}
}
]
}
95 changes: 95 additions & 0 deletions tests/contracts/artifacts/ABITestBoolTuple.approval.teal
Original file line number Diff line number Diff line change
@@ -0,0 +1,95 @@
#pragma version 8
b main

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

boolTuple:
proto 1 0

// tests/contracts/abi.algo.ts:846
// a: [boolean, boolean, boolean, boolean, boolean, boolean, boolean, boolean, boolean] = [
byte 0x // initial head
byte 0x // initial tail
byte 0x0002 // initial head offset
byte 0x0000
int 0
int 1
setbit
int 1
int 0
setbit
int 2
int 1
setbit
int 3
int 1
setbit
int 4
int 0
setbit
int 5
int 0
setbit
int 6
int 1
setbit
int 7
int 0
setbit
int 8
int 0
setbit
callsub process_static_tuple_element
pop // pop head offset
concat // concat head and tail
frame_bury -1 // a: [bool,bool,bool,bool,bool,bool,bool,bool,bool]

// tests/contracts/abi.algo.ts:850
// return a;
frame_dig -1 // a: [bool,bool,bool,bool,bool,bool,bool,bool,bool]
byte 0x151f7c75
swap
concat
log
retsub

main:
txn NumAppArgs
bnz route_abi

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

route_abi:
method "boolTuple()(bool,bool,bool,bool,bool,bool,bool,bool,bool)"
txna ApplicationArgs 0
match abi_route_boolTuple
err

process_static_tuple_element:
proto 4 3
frame_dig -4 // tuple head
frame_dig -1 // element
concat
frame_dig -3 // tuple tail
frame_dig -2 // head offset
retsub
3 changes: 3 additions & 0 deletions tests/contracts/artifacts/ABITestBoolTuple.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/ABITestBoolTuple.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,51 @@
{
"hints": {
"boolTuple()(bool,bool,bool,bool,bool,bool,bool,bool,bool)": {
"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": "I3ByYWdtYSB2ZXJzaW9uIDgKCWIgbWFp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ib29sLGJvb2wsYm9vbCxib29sLGJvb2wsYm9vbCxib29sLGJvb2xdCglieXRlIDB4MTUxZjdjNzUKCXN3YXAKCWNvbmNhdAoJbG9nCglyZXRzdWIKCm1haW46Cgl0eG4gTnVtQXBwQXJncwoJYm56IHJvdXRlX2FiaQoKCS8vIGRlZmF1bHQgY3JlYXRlQXBwbGljYXRpb24KCXR4biBBcHBsaWNhdGlvbklECglpbnQgMAoJPT0KCXR4biBPbkNvbXBsZXRpb24KCWludCBOb09wCgk9PQoJJiYKCXJldHVybgoKcm91dGVfYWJpOgoJbWV0aG9kICJib29sVHVwbGUoKShib29sLGJvb2wsYm9vbCxib29sLGJvb2wsYm9vbCxib29sLGJvb2wsYm9vbCkiCgl0eG5hIEFwcGxpY2F0aW9uQXJncyAwCgltYXRjaCBhYmlfcm91dGVfYm9vbFR1cGxlCgllcnIKCnByb2Nlc3Nfc3RhdGljX3R1cGxlX2VsZW1lbnQ6Cglwcm90byA0IDMKCWZyYW1lX2RpZyAtNCAvLyB0dXBsZSBoZWFkCglmcmFtZV9kaWcgLTEgLy8gZWxlbWVudAoJY29uY2F0CglmcmFtZV9kaWcgLTMgLy8gdHVwbGUgdGFpbAoJZnJhbWVfZGlnIC0yIC8vIGhlYWQgb2Zmc2V0CglyZXRzdWI=",
"clear": "I3ByYWdtYSB2ZXJzaW9uIDgKaW50IDEKcmV0dXJu"
},
"contract": {
"name": "ABITestBoolTuple",
"desc": "",
"methods": [
{
"name": "boolTuple",
"args": [],
"desc": "",
"returns": {
"type": "(bool,bool,bool,bool,bool,bool,bool,bool,bool)",
"desc": ""
}
}
]
}
}
15 changes: 15 additions & 0 deletions tests/contracts/artifacts/ABITestBoolTupleAccess.abi.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
{
"name": "ABITestBoolTupleAccess",
"desc": "",
"methods": [
{
"name": "boolTupleAccess",
"args": [],
"desc": "",
"returns": {
"type": "bool",
"desc": ""
}
}
]
}
Loading

0 comments on commit 16a4f0f

Please sign in to comment.