Skip to content

Commit

Permalink
Merge pull request #543 from algorandfoundation/fix/arc56_struct_names
Browse files Browse the repository at this point in the history
fix: ARC56 struct names in methods
  • Loading branch information
joe-p authored Sep 26, 2024
2 parents fc17ea8 + dc69068 commit 2fa2b61
Show file tree
Hide file tree
Showing 15 changed files with 1,436 additions and 413 deletions.
12 changes: 6 additions & 6 deletions examples/reti/artifacts/StakingPool.arc56_draft.json
Original file line number Diff line number Diff line change
Expand Up @@ -162,7 +162,7 @@
"returns": {
"type": "(address,uint64,uint64,uint64,uint64)",
"desc": "StakedInfo - The staked information for the given staker.",
"struct": "import(\"/Users/joe/git/algorandfoundation/tealscript/examples/reti/stakingPool.algo\").StakedInfo"
"struct": "StakedInfo"
},
"actions": {
"create": [],
Expand Down Expand Up @@ -329,12 +329,12 @@
"name": "poolKey",
"type": "(uint64,uint64,uint64)",
"desc": "ValidatorPoolKey tuple",
"struct": "import(\"/Users/joe/git/algorandfoundation/tealscript/examples/reti/validatorRegistry.algo\").ValidatorPoolKey"
"struct": "ValidatorPoolKey"
}
],
"returns": {
"type": "(uint64[24],uint64)",
"struct": "import(\"/Users/joe/git/algorandfoundation/tealscript/examples/reti/validatorRegistry.algo\").PoolTokenPayoutRatio"
"struct": "PoolTokenPayoutRatio"
},
"actions": {
"create": [],
Expand All @@ -349,7 +349,7 @@
56
],
"structs": {
"import(\"/Users/joe/git/algorandfoundation/tealscript/examples/reti/stakingPool.algo\").StakedInfo": [
"StakedInfo": [
{
"name": "account",
"type": "address"
Expand All @@ -371,7 +371,7 @@
"type": "uint64"
}
],
"import(\"/Users/joe/git/algorandfoundation/tealscript/examples/reti/validatorRegistry.algo\").PoolTokenPayoutRatio": [
"PoolTokenPayoutRatio": [
{
"name": "poolPctOfWhole",
"type": "uint64[24]"
Expand All @@ -381,7 +381,7 @@
"type": "uint64"
}
],
"import(\"/Users/joe/git/algorandfoundation/tealscript/examples/reti/validatorRegistry.algo\").ValidatorPoolKey": [
"ValidatorPoolKey": [
{
"name": "id",
"type": "uint64"
Expand Down
24 changes: 12 additions & 12 deletions examples/reti/artifacts/ValidatorRegistry.arc56_draft.json
Original file line number Diff line number Diff line change
Expand Up @@ -149,7 +149,7 @@
],
"returns": {
"type": "(uint64,address,address,uint64,uint8,address,uint64[4],uint64,uint64,uint64,uint32,uint32,address,uint64,uint64,uint8,uint64,uint64)",
"struct": "import(\"/Users/joe/git/algorandfoundation/tealscript/examples/reti/validatorRegistry.algo\").ValidatorConfig"
"struct": "ValidatorConfig"
},
"actions": {
"create": [],
Expand Down Expand Up @@ -243,7 +243,7 @@
{
"name": "poolKey",
"type": "(uint64,uint64,uint64)",
"struct": "import(\"/Users/joe/git/algorandfoundation/tealscript/examples/reti/validatorRegistry.algo\").ValidatorPoolKey"
"struct": "ValidatorPoolKey"
}
],
"returns": {
Expand Down Expand Up @@ -328,7 +328,7 @@
],
"returns": {
"type": "(uint64[24],uint64)",
"struct": "import(\"/Users/joe/git/algorandfoundation/tealscript/examples/reti/validatorRegistry.algo\").PoolTokenPayoutRatio"
"struct": "PoolTokenPayoutRatio"
},
"actions": {
"create": [],
Expand Down Expand Up @@ -387,7 +387,7 @@
"name": "config",
"type": "(uint64,address,address,uint64,uint8,address,uint64[4],uint64,uint64,uint64,uint32,uint32,address,uint64,uint64,uint8,uint64,uint64)",
"desc": "ValidatorConfig struct",
"struct": "import(\"/Users/joe/git/algorandfoundation/tealscript/examples/reti/validatorRegistry.algo\").ValidatorConfig"
"struct": "ValidatorConfig"
}
],
"returns": {
Expand Down Expand Up @@ -571,7 +571,7 @@
"returns": {
"type": "(uint64,uint64,uint64)",
"desc": "ValidatorPoolKey pool key to created pool",
"struct": "import(\"/Users/joe/git/algorandfoundation/tealscript/examples/reti/validatorRegistry.algo\").ValidatorPoolKey"
"struct": "ValidatorPoolKey"
},
"actions": {
"create": [],
Expand Down Expand Up @@ -603,7 +603,7 @@
"returns": {
"type": "(uint64,uint64,uint64)",
"desc": "ValidatorPoolKey - The key of the validator pool.",
"struct": "import(\"/Users/joe/git/algorandfoundation/tealscript/examples/reti/validatorRegistry.algo\").ValidatorPoolKey"
"struct": "ValidatorPoolKey"
},
"actions": {
"create": [],
Expand All @@ -625,7 +625,7 @@
"returns": {
"type": "(uint64[24],uint64)",
"desc": "PoolTokenPayoutRatio - the finished ratio data",
"struct": "import(\"/Users/joe/git/algorandfoundation/tealscript/examples/reti/validatorRegistry.algo\").PoolTokenPayoutRatio"
"struct": "PoolTokenPayoutRatio"
},
"actions": {
"create": [],
Expand All @@ -642,7 +642,7 @@
"name": "poolKey",
"type": "(uint64,uint64,uint64)",
"desc": "ValidatorPoolKey type",
"struct": "import(\"/Users/joe/git/algorandfoundation/tealscript/examples/reti/validatorRegistry.algo\").ValidatorPoolKey"
"struct": "ValidatorPoolKey"
},
{
"name": "algoToAdd",
Expand Down Expand Up @@ -683,7 +683,7 @@
"name": "poolKey",
"type": "(uint64,uint64,uint64)",
"desc": "calling us from which stake was removed",
"struct": "import(\"/Users/joe/git/algorandfoundation/tealscript/examples/reti/validatorRegistry.algo\").ValidatorPoolKey"
"struct": "ValidatorPoolKey"
},
{
"name": "staker",
Expand Down Expand Up @@ -995,7 +995,7 @@
"type": "uint64"
}
],
"import(\"/Users/joe/git/algorandfoundation/tealscript/examples/reti/validatorRegistry.algo\").ValidatorConfig": [
"ValidatorConfig": [
{
"name": "id",
"type": "uint64"
Expand Down Expand Up @@ -1101,7 +1101,7 @@
"type": "uint64"
}
],
"import(\"/Users/joe/git/algorandfoundation/tealscript/examples/reti/validatorRegistry.algo\").ValidatorPoolKey": [
"ValidatorPoolKey": [
{
"name": "id",
"type": "uint64"
Expand All @@ -1115,7 +1115,7 @@
"type": "uint64"
}
],
"import(\"/Users/joe/git/algorandfoundation/tealscript/examples/reti/validatorRegistry.algo\").PoolTokenPayoutRatio": [
"PoolTokenPayoutRatio": [
{
"name": "poolPctOfWhole",
"type": "uint64[24]"
Expand Down
7 changes: 5 additions & 2 deletions src/lib/compiler.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7764,7 +7764,10 @@ declare type AssetFreezeTxn = Required<AssetFreezeParams>;
const returnTypeInfo = this.getTypeInfo(subroutine.node.getReturnType());

if (returnTypeInfo.kind === 'object') {
const structName = subroutine.node.getReturnType().getText();
const returnTypeNode =
subroutine.node.getChildrenOfKind(ts.SyntaxKind.TypeReference)?.[0] || subroutine.node.getReturnType();
const structName = returnTypeNode?.getText();

// eslint-disable-next-line no-param-reassign
m.returns.struct = structName;
if (!arc56.structs[structName]) {
Expand All @@ -7778,7 +7781,7 @@ declare type AssetFreezeTxn = Required<AssetFreezeParams>;
const typeInfo = this.getTypeInfo(p.getType());

if (typeInfo.kind === 'object') {
const structName = p.getType().getText();
const structName = (p.getChildrenOfKind(ts.SyntaxKind.TypeReference)?.[0] || p.getType()).getText();
arg.struct = structName;
if (!arc56.structs[structName]) {
arc56.structs[structName] = objectToStructFields(typeInfo);
Expand Down
7 changes: 6 additions & 1 deletion tests/contracts/arc56.algo.ts
Original file line number Diff line number Diff line change
@@ -1,9 +1,10 @@
import { Contract } from '../../src/lib';
import { CustomStruct as CustomStructAsName, ContractWithCustomStruct } from './inheritance-external.algo';

type Inputs = { add: { a: uint64; b: uint64 }; subtract: { a: uint64; b: uint64 } };
type Outputs = { sum: uint64; difference: uint64 };

export class ARC56Test extends Contract {
export class ARC56Test extends ContractWithCustomStruct {
globalKey = GlobalStateKey<uint64>();

globalMap = GlobalStateMap<string, { foo: uint16; bar: uint16 }>({ maxKeys: 37, prefix: 'p' });
Expand Down Expand Up @@ -38,6 +39,10 @@ export class ARC56Test extends Contract {
this.boxKey.value = 'baz';
this.boxMap({ add: { a: 1, b: 2 }, subtract: { a: 4, b: 3 } }).value = { sum: 3, difference: 1 };
}

customType(input: CustomStructAsName): CustomStructAsName {
return input;
}
}

export class ARC56TestNoTemplateVars extends Contract {}
82 changes: 73 additions & 9 deletions tests/contracts/artifacts/ARC56Test.approval.teal
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,37 @@ switch *call_NoOp *call_OptIn *NOT_IMPLEMENTED *NOT_IMPLEMENTED *NOT_IMPLEMENTED
// The requested action is not implemented in this contract. Are you using the correct OnComplete? Did you set your app ID?
err

// customStructMethod((uint64))(uint64)
*abi_route_customStructMethod:
// The ABI return prefix
byte 0x151f7c75

// input: (uint64)
txna ApplicationArgs 1
dup
len
int 8
==

// argument 0 (input) for customStructMethod must be a (uint64)
assert

// execute customStructMethod((uint64))(uint64)
callsub customStructMethod
concat
log
int 1
return

// customStructMethod(input: AnotherCustomStruct): AnotherCustomStruct
customStructMethod:
proto 1 1

// tests/contracts/inheritance-external.algo.ts:21
// return input;
frame_dig -1 // input: AnotherCustomStruct
retsub

// foo(((uint64,uint64),(uint64,uint64)))(uint64,uint64)
*abi_route_foo:
// The ABI return prefix
Expand Down Expand Up @@ -54,7 +85,7 @@ foo:
proto 1 1

// *if0_condition
// tests/contracts/arc56.algo.ts:24
// tests/contracts/arc56.algo.ts:25
// inputs.subtract.a < inputs.subtract.b
frame_dig -1 // inputs: Inputs
extract 16 8
Expand All @@ -70,19 +101,19 @@ foo:
err

*if0_end:
// tests/contracts/arc56.algo.ts:26
// tests/contracts/arc56.algo.ts:27
// this.globalKey.value = this.someNumber
byte 0x676c6f62616c4b6579 // "globalKey"
load 200 // TMPL_someNumber
app_global_put

// tests/contracts/arc56.algo.ts:27
// tests/contracts/arc56.algo.ts:28
// this.globalMap('foo').value = { foo: 13, bar: 37 }
byte 0x700003666f6f
byte 0x000d0025
app_global_put

// tests/contracts/arc56.algo.ts:29
// tests/contracts/arc56.algo.ts:30
// return {
// sum: inputs.add.a + inputs.add.b,
// difference: inputs.subtract.a - inputs.subtract.b,
Expand Down Expand Up @@ -117,21 +148,21 @@ foo:
optInToApplication:
proto 0 0

// tests/contracts/arc56.algo.ts:36
// tests/contracts/arc56.algo.ts:37
// this.localKey(this.txn.sender).value = this.someNumber
txn Sender
byte 0x6c6f63616c4b6579 // "localKey"
load 200 // TMPL_someNumber
app_local_put

// tests/contracts/arc56.algo.ts:37
// tests/contracts/arc56.algo.ts:38
// this.localMap(this.txn.sender, 'foo').value = 'bar'
txn Sender
byte 0x70666f6f
byte 0x0003626172
app_local_put

// tests/contracts/arc56.algo.ts:38
// tests/contracts/arc56.algo.ts:39
// this.boxKey.value = 'baz'
byte 0x626f784b6579 // "boxKey"
dup
Expand All @@ -140,13 +171,44 @@ optInToApplication:
byte 0x000362617a
box_put

// tests/contracts/arc56.algo.ts:39
// tests/contracts/arc56.algo.ts:40
// this.boxMap({ add: { a: 1, b: 2 }, subtract: { a: 4, b: 3 } }).value = { sum: 3, difference: 1 }
byte 0x700000000000000001000000000000000200000000000000040000000000000003
byte 0x00000000000000030000000000000001
box_put
retsub

// customType((uint64))(uint64)
*abi_route_customType:
// The ABI return prefix
byte 0x151f7c75

// input: (uint64)
txna ApplicationArgs 1
dup
len
int 8
==

// argument 0 (input) for customType must be a (uint64)
assert

// execute customType((uint64))(uint64)
callsub customType
concat
log
int 1
return

// customType(input: CustomStructAsName): CustomStructAsName
customType:
proto 1 1

// tests/contracts/arc56.algo.ts:44
// return input;
frame_dig -1 // input: CustomStructAsName
retsub

*abi_route_createApplication:
int 1
return
Expand All @@ -160,9 +222,11 @@ optInToApplication:
err

*call_NoOp:
method "customStructMethod((uint64))(uint64)"
method "foo(((uint64,uint64),(uint64,uint64)))(uint64,uint64)"
method "customType((uint64))(uint64)"
txna ApplicationArgs 0
match *abi_route_foo
match *abi_route_customStructMethod *abi_route_foo *abi_route_customType

// this contract does not implement the given ABI method for call NoOp
err
Expand Down
Loading

0 comments on commit 2fa2b61

Please sign in to comment.