Skip to content

Commit 6c4e2a5

Browse files
committed
fix: uintN comparison
1 parent c0a301c commit 6c4e2a5

File tree

7 files changed

+203
-1
lines changed

7 files changed

+203
-1
lines changed

src/lib/compiler.ts

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4104,7 +4104,11 @@ export default class Compiler {
41044104
throw new Error(`Exponent operator only supported for uintN <= 64, got ${leftTypeStr} and ${rightTypeStr}`);
41054105
}
41064106

4107-
if (leftTypeStr.match(/\d+$/) && !isNumeric(leftType) && (operator === '==' || operator === '!=')) {
4107+
if (
4108+
leftTypeStr.match(/\d+$/) &&
4109+
!isNumeric(leftType) &&
4110+
(operator === '==' || operator === '!=' || operator.startsWith('<') || operator.startsWith('>'))
4111+
) {
41084112
this.push(node, `b${operator}`, { kind: 'base', type: 'bool' });
41094113
} else if (isMathOp && leftTypeStr.match(/\d+$/) && !isSmallNumber(leftType) && !isNumeric(leftType)) {
41104114
this.push(node.getOperatorToken(), `b${operator}`, { kind: 'base', type: `unsafe ${leftTypeStr}` });

tests/abi.test.ts

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -790,5 +790,13 @@ describe('ABI', function () {
790790

791791
expect(await runMethod(appClient, 'plusEqualsObjValueInBox')).toEqual([3n, 5n]);
792792
});
793+
794+
test('uintNComparison', async () => {
795+
const { appClient } = await compileAndCreate('uintNComparison');
796+
797+
expect(await runMethod(appClient, 'uintNComparison', [2n, 2n])).toEqual(true);
798+
expect(await runMethod(appClient, 'uintNComparison', [1n, 2n])).toEqual(false);
799+
expect(await runMethod(appClient, 'uintNComparison', [2n, 1n])).toEqual(true);
800+
});
793801
});
794802
});

tests/contracts/abi.algo.ts

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1262,3 +1262,9 @@ class ABITestPlusEqualsObjValueInBox extends Contract {
12621262
return this.bMap('bMap').value;
12631263
}
12641264
}
1265+
1266+
class ABITestUintNComparison extends Contract {
1267+
uintNComparison(x: uint<256>, y: uint<256>): boolean {
1268+
return x >= y;
1269+
}
1270+
}
Lines changed: 80 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,80 @@
1+
#pragma version 9
2+
3+
// This TEAL was generated by TEALScript v0.67.3
4+
// https://github.com/algorandfoundation/TEALScript
5+
6+
// This contract is compliant with and/or implements the following ARCs: [ ARC4 ]
7+
8+
// The following ten lines of TEAL handle initial program flow
9+
// This pattern is used to make it easy for anyone to parse the start of the program and determine if a specific action is allowed
10+
// Here, action refers to the OnComplete in combination with whether the app is being created or called
11+
// Every possible action for this contract is represented in the switch statement
12+
// If the action is not implmented in the contract, its respective branch will be "NOT_IMPLEMENTED" which just contains "err"
13+
txn ApplicationID
14+
!
15+
int 6
16+
*
17+
txn OnCompletion
18+
+
19+
switch call_NoOp NOT_IMPLEMENTED NOT_IMPLEMENTED NOT_IMPLEMENTED NOT_IMPLEMENTED NOT_IMPLEMENTED create_NoOp NOT_IMPLEMENTED NOT_IMPLEMENTED NOT_IMPLEMENTED NOT_IMPLEMENTED NOT_IMPLEMENTED
20+
21+
NOT_IMPLEMENTED:
22+
err
23+
24+
abi_route_uintNComparison:
25+
// The ABI return prefix
26+
byte 0x151f7c75
27+
28+
// y: uint256
29+
txna ApplicationArgs 2
30+
dup
31+
len
32+
int 32
33+
==
34+
assert
35+
36+
// x: uint256
37+
txna ApplicationArgs 1
38+
dup
39+
len
40+
int 32
41+
==
42+
assert
43+
44+
// execute uintNComparison(uint256,uint256)bool
45+
callsub uintNComparison
46+
byte 0x00
47+
int 0
48+
uncover 2
49+
setbit
50+
concat
51+
log
52+
int 1
53+
return
54+
55+
// uintNComparison(uint256,uint256)bool
56+
uintNComparison:
57+
proto 2 1
58+
59+
// tests/contracts/abi.algo.ts:1268
60+
// return x >= y;
61+
frame_dig -1 // x: uint<256>
62+
frame_dig -2 // y: uint<256>
63+
b>=
64+
retsub
65+
66+
abi_route_createApplication:
67+
int 1
68+
return
69+
70+
create_NoOp:
71+
method "createApplication()void"
72+
txna ApplicationArgs 0
73+
match abi_route_createApplication
74+
err
75+
76+
call_NoOp:
77+
method "uintNComparison(uint256,uint256)bool"
78+
txna ApplicationArgs 0
79+
match abi_route_uintNComparison
80+
err
Lines changed: 74 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,74 @@
1+
{
2+
"hints": {
3+
"uintNComparison(uint256,uint256)bool": {
4+
"call_config": {
5+
"no_op": "CALL"
6+
}
7+
},
8+
"createApplication()void": {
9+
"call_config": {
10+
"no_op": "CREATE"
11+
}
12+
}
13+
},
14+
"bare_call_config": {
15+
"no_op": "NEVER",
16+
"opt_in": "NEVER",
17+
"close_out": "NEVER",
18+
"update_application": "NEVER",
19+
"delete_application": "NEVER"
20+
},
21+
"schema": {
22+
"local": {
23+
"declared": {},
24+
"reserved": {}
25+
},
26+
"global": {
27+
"declared": {},
28+
"reserved": {}
29+
}
30+
},
31+
"state": {
32+
"global": {
33+
"num_byte_slices": 0,
34+
"num_uints": 0
35+
},
36+
"local": {
37+
"num_byte_slices": 0,
38+
"num_uints": 0
39+
}
40+
},
41+
"source": {
42+
"approval": "I3ByYWdtYSB2ZXJzaW9uIDkKCi8vIFRoaXMgVEVBTCB3YXMgZ2VuZXJhdGVkIGJ5IFRFQUxTY3JpcHQgdjAuNjcuMwovLyBodHRwczovL2dpdGh1Yi5jb20vYWxnb3JhbmRmb3VuZGF0aW9uL1RFQUxTY3JpcHQKCi8vIFRoaXMgY29udHJhY3QgaXMgY29tcGxpYW50IHdpdGggYW5kL29yIGltcGxlbWVudHMgdGhlIGZvbGxvd2luZyBBUkNzOiBbIEFSQzQgXQoKLy8gVGhlIGZvbGxvd2luZyB0ZW4gbGluZXMgb2YgVEVBTCBoYW5kbGUgaW5pdGlhbCBwcm9ncmFtIGZsb3cKLy8gVGhpcyBwYXR0ZXJuIGlzIHVzZWQgdG8gbWFrZSBpdCBlYXN5IGZvciBhbnlvbmUgdG8gcGFyc2UgdGhlIHN0YXJ0I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Y2F0aW9uQXJncyAxCglkdXAKCWxlbgoJaW50IDMyCgk9PQoJYXNzZXJ0CgoJLy8gZXhlY3V0ZSB1aW50TkNvbXBhcmlzb24odWludDI1Nix1aW50MjU2KWJvb2wKCWNhbGxzdWIgdWludE5Db21wYXJpc29uCglieXRlIDB4MDAKCWludCAwCgl1bmNvdmVyIDIKCXNldGJpdAoJY29uY2F0Cglsb2cKCWludCAxCglyZXR1cm4KCi8vIHVpbnROQ29tcGFyaXNvbih1aW50MjU2LHVpbnQyNTYpYm9vbAp1aW50TkNvbXBhcmlzb246Cglwcm90byAyIDEKCgkvLyB0ZXN0cy9jb250cmFjdHMvYWJpLmFsZ28udHM6MTI2OAoJLy8gcmV0dXJuIHggPj0geTsKCWZyYW1lX2RpZyAtMSAvLyB4OiB1aW50PDI1Nj4KCWZyYW1lX2RpZyAtMiAvLyB5OiB1aW50PDI1Nj4KCWI+PQoJcmV0c3ViCgphYmlfcm91dGVfY3JlYXRlQXBwbGljYXRpb246CglpbnQgMQoJcmV0dXJuCgpjcmVhdGVfTm9PcDoKCW1ldGhvZCAiY3JlYXRlQXBwbGljYXRpb24oKXZvaWQiCgl0eG5hIEFwcGxpY2F0aW9uQXJncyAwCgltYXRjaCBhYmlfcm91dGVfY3JlYXRlQXBwbGljYXRpb24KCWVycgoKY2FsbF9Ob09wOgoJbWV0aG9kICJ1aW50TkNvbXBhcmlzb24odWludDI1Nix1aW50MjU2KWJvb2wiCgl0eG5hIEFwcGxpY2F0aW9uQXJncyAwCgltYXRjaCBhYmlfcm91dGVfdWludE5Db21wYXJpc29uCgllcnI=",
43+
"clear": "I3ByYWdtYSB2ZXJzaW9uIDk="
44+
},
45+
"contract": {
46+
"name": "ABITestUintNComparison",
47+
"desc": "",
48+
"methods": [
49+
{
50+
"name": "uintNComparison",
51+
"args": [
52+
{
53+
"name": "x",
54+
"type": "uint256"
55+
},
56+
{
57+
"name": "y",
58+
"type": "uint256"
59+
}
60+
],
61+
"returns": {
62+
"type": "bool"
63+
}
64+
},
65+
{
66+
"name": "createApplication",
67+
"args": [],
68+
"returns": {
69+
"type": "void"
70+
}
71+
}
72+
]
73+
}
74+
}
Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
{
2+
"name": "ABITestUintNComparison",
3+
"desc": "",
4+
"methods": [
5+
{
6+
"name": "uintNComparison",
7+
"args": [
8+
{
9+
"name": "x",
10+
"type": "uint256"
11+
},
12+
{
13+
"name": "y",
14+
"type": "uint256"
15+
}
16+
],
17+
"returns": {
18+
"type": "bool"
19+
}
20+
},
21+
{
22+
"name": "createApplication",
23+
"args": [],
24+
"returns": {
25+
"type": "void"
26+
}
27+
}
28+
]
29+
}
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
#pragma version 9

0 commit comments

Comments
 (0)