Skip to content

Change app state methods, add loadScratch method #297

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 3 commits into from
Dec 28, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
32 changes: 20 additions & 12 deletions src/lib/compiler.ts
Original file line number Diff line number Diff line change
Expand Up @@ -727,33 +727,41 @@ export default class Compiler {
private readonly OP_PARAMS: {
[type: string]: { name: string; type?: string; args: number; fn: (node: ts.Node) => void }[];
} = {
account: [
...this.getOpParamObjects('acct_params_get'),
...this.getOpParamObjects('asset_holding_get'),
account: [...this.getOpParamObjects('acct_params_get'), ...this.getOpParamObjects('asset_holding_get')],
application: [
...this.getOpParamObjects('app_params_get'),
{
name: 'State',
name: 'GlobalState',
type: 'any',
args: 3,
args: 1,
fn: (node: ts.Node) => {
this.maybeValue(node, 'app_local_get_ex', StackType.any);
this.maybeValue(node, 'app_global_get_ex', StackType.any);
},
},
],
application: [
...this.getOpParamObjects('app_params_get'),
{
name: 'Global',
name: 'LocalState',
type: 'any',
args: 2,
fn: (node: ts.Node) => {
this.maybeValue(node, 'app_global_get_ex', StackType.any);
this.pushLines(node, 'swap', 'cover 2');
this.maybeValue(node, 'app_local_get_ex', StackType.any);
},
},
],
txn: this.getOpParamObjects('txn'),
global: this.getOpParamObjects('global'),
itxn: this.getOpParamObjects('itxn'),
gtxns: this.getOpParamObjects('gtxns'),
gtxns: [
...this.getOpParamObjects('gtxns'),
{
name: 'LoadScratch',
type: 'any',
args: 1,
fn: (node: ts.Node) => {
this.push(node, 'gloadss', StackType.any);
},
},
],
asset: this.getOpParamObjects('asset_params_get'),
};

Expand Down
18 changes: 17 additions & 1 deletion src/lib/optimize.ts
Original file line number Diff line number Diff line change
Expand Up @@ -108,7 +108,23 @@ export function optimizeOpcodes(inputTeal: NodeAndTEAL[]): NodeAndTEAL[] {
const teal = nodeAndTeal.teal.trim();
const { node } = nodeAndTeal;

if (teal.startsWith('cover ')) {
if (teal.startsWith('gloadss')) {
if (outputTeal.at(-1)?.teal.startsWith('int ')) {
const scratchSlot = Number(outputTeal.at(-1)?.teal.split(' ')[1]);
popTeal();
pushTeal(`gloads ${scratchSlot}`, node);
optimized = true;
}
} else if (teal.startsWith('gloads')) {
if (outputTeal.at(-1)?.teal.startsWith('int ')) {
const scratchSlot = Number(teal.split(' ')[1]);
const txnIndex = Number(outputTeal.at(-1)?.teal.split(' ')[1]);

popTeal();
pushTeal(`gload ${txnIndex} ${scratchSlot}`, node);
optimized = true;
}
} else if (teal.startsWith('cover ')) {
const n = Number(teal.split(' ')[1]);
const movedTeal = outputTeal.slice(-n - 1, -1);

Expand Down
19 changes: 18 additions & 1 deletion tests/contracts/artifacts/GeneralTest.approval.teal
Original file line number Diff line number Diff line change
Expand Up @@ -1035,6 +1035,22 @@ callInternalPublicMethod:
assert
retsub

abi_route_appLoadScratch:
// execute appLoadScratch()void
callsub appLoadScratch
int 1
return

// appLoadScratch()void
appLoadScratch:
proto 0 0

// tests/contracts/general.algo.ts:216
// log(this.txnGroup[1].loadScratch(2) as bytes)
gload 1 2
log
retsub

abi_route_createApplication:
int 1
return
Expand Down Expand Up @@ -1075,8 +1091,9 @@ call_NoOp:
method "callPrivateDefinedLater()void"
method "interalPublicMethod(uint64,uint64)uint64"
method "callInternalPublicMethod()void"
method "appLoadScratch()void"
txna ApplicationArgs 0
match abi_route_txnTypeEnum abi_route_txnGroupLength abi_route_asserts abi_route_verifyTxnFromArg abi_route_verifyTxnFromTxnGroup abi_route_verifyTxnCondition abi_route_verifyTxnIncludedIn abi_route_verifyTxnNotIncludedIn abi_route_submitPendingGroup abi_route_methodWithTxnArgs abi_route_shift abi_route_fromBytes abi_route_fromID abi_route_bzeroFunction abi_route_events abi_route_letOptimization abi_route_staticContractProperties abi_route_numberToString abi_route_methodOnParens abi_route_stringSubstring abi_route_idProperty abi_route_scratchSlot abi_route_ecdsa abi_route_verifyTxnTypes abi_route_stringPlusEquals abi_route_importedProgram abi_route_callPrivateDefinedLater abi_route_interalPublicMethod abi_route_callInternalPublicMethod
match abi_route_txnTypeEnum abi_route_txnGroupLength abi_route_asserts abi_route_verifyTxnFromArg abi_route_verifyTxnFromTxnGroup abi_route_verifyTxnCondition abi_route_verifyTxnIncludedIn abi_route_verifyTxnNotIncludedIn abi_route_submitPendingGroup abi_route_methodWithTxnArgs abi_route_shift abi_route_fromBytes abi_route_fromID abi_route_bzeroFunction abi_route_events abi_route_letOptimization abi_route_staticContractProperties abi_route_numberToString abi_route_methodOnParens abi_route_stringSubstring abi_route_idProperty abi_route_scratchSlot abi_route_ecdsa abi_route_verifyTxnTypes abi_route_stringPlusEquals abi_route_importedProgram abi_route_callPrivateDefinedLater abi_route_interalPublicMethod abi_route_callInternalPublicMethod abi_route_appLoadScratch
err

intToAscii:
Expand Down
16 changes: 15 additions & 1 deletion tests/contracts/artifacts/GeneralTest.arc32.json

Large diffs are not rendered by default.

9 changes: 9 additions & 0 deletions tests/contracts/artifacts/GeneralTest.arc4.json
Original file line number Diff line number Diff line change
Expand Up @@ -297,6 +297,15 @@
"desc": ""
}
},
{
"name": "appLoadScratch",
"args": [],
"desc": "",
"returns": {
"type": "void",
"desc": ""
}
},
{
"name": "createApplication",
"args": [],
Expand Down
4 changes: 2 additions & 2 deletions tests/contracts/artifacts/StorageTest.approval.teal
Original file line number Diff line number Diff line change
Expand Up @@ -746,7 +746,7 @@ exGlobal:
proto 0 0

// tests/contracts/storage.algo.ts:162
// log(this.app.global('foo') as bytes)
// log(this.app.globalState('foo') as bytes)
txna Applications 0
byte 0x666f6f // "foo"
app_global_get_ex
Expand All @@ -765,7 +765,7 @@ exLocal:
proto 0 0

// tests/contracts/storage.algo.ts:166
// log(this.txn.sender.state(this.app, 'foo') as bytes)
// log(this.app.localState(this.txn.sender, 'foo') as bytes)
txn Sender
txna Applications 0
byte 0x666f6f // "foo"
Expand Down
2 changes: 1 addition & 1 deletion tests/contracts/artifacts/StorageTest.arc32.json

Large diffs are not rendered by default.

4 changes: 4 additions & 0 deletions tests/contracts/general.algo.ts
Original file line number Diff line number Diff line change
Expand Up @@ -211,4 +211,8 @@ class GeneralTest extends Contract {
callInternalPublicMethod(): void {
assert(this.interalPublicMethod(1, 2) === 3);
}

appLoadScratch(): void {
log(this.txnGroup[1].loadScratch(2) as bytes);
}
}
4 changes: 2 additions & 2 deletions tests/contracts/storage.algo.ts
Original file line number Diff line number Diff line change
Expand Up @@ -159,10 +159,10 @@ class StorageTest extends Contract {
}

exGlobal(): void {
log(this.app.global('foo') as bytes);
log(this.app.globalState('foo') as bytes);
}

exLocal(): void {
log(this.txn.sender.state(this.app, 'foo') as bytes);
log(this.app.localState(this.txn.sender, 'foo') as bytes);
}
}
9 changes: 4 additions & 5 deletions types/global.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -419,9 +419,6 @@ declare class Address {
assetFrozen(asa: Asset): uint64;

isOptedInToApp(app: Application): boolean;

// eslint-disable-next-line @typescript-eslint/no-explicit-any
state(app: Application, key: BytesLike): any;
}

class Account extends Address {}
Expand Down Expand Up @@ -453,8 +450,9 @@ declare class Application {

readonly address: Address;

// eslint-disable-next-line @typescript-eslint/no-explicit-any
global(key: BytesLike): any;
globalState(key: BytesLike): unknown;

localState(account: Address, key: BytesLike): unknown;
}

// eslint-disable-next-line @typescript-eslint/no-explicit-any
Expand Down Expand Up @@ -535,6 +533,7 @@ interface AppOnChainTransactionParams extends CommonOnChainTransactionParams {
numLogs: uint64;
numApprovalProgrammPages: uint64;
numClearStateProgramPages: uint64;
loadScratch: (slot: uint64) => unknown;
}

interface AssetTransferParams extends CommonTransactionParams {
Expand Down