Skip to content
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

Add approvalProgramPages to get chunked approvalProgram #531

Open
wants to merge 2 commits into
base: dev
Choose a base branch
from
Open
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
31 changes: 30 additions & 1 deletion src/lib/compiler.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2570,8 +2570,24 @@ export default class Compiler {
}

const target = tealLine.split(':')[0].split('_').at(-1)!.toLowerCase() as 'approval' | 'clear' | 'lsig';
const pages = (tealLine.split(':')[0].split('_').at(-2)!.toLowerCase() as 'pages') === 'pages';
const compiledProgram = await c.algodCompileProgram(target);
return { teal: `byte b64 ${compiledProgram.result}`, node: t.node };
if (!pages) {
return { teal: `byte b64 ${compiledProgram.result}`, node: t.node };
}
const bin = Uint8Array.from(Buffer.from(compiledProgram.result, 'base64'));
const chunks: Uint8Array[] = [];
for (let i = 0; i < bin.length; i += 2048) {
chunks.push(bin.slice(i, i + 2048));
}
const nodes: { teal: string; node: typeof t.node }[] = [];
// eslint-disable-next-line no-restricted-syntax
for (const chunk of chunks) {
const b64 = Buffer.from(chunk).toString('base64');
nodes.push({ teal: `byte b64 ${b64}`, node: t.node });
nodes.push({ teal: `itxn_field ${capitalizeFirstChar(`${target}Program`)}Pages`, node: t.node });
}
return nodes;
}

const method = tealLine.split(' ')[1];
Expand Down Expand Up @@ -6310,6 +6326,12 @@ export default class Compiler {
this.push(chain[1], `PENDING_COMPILE_CLEAR: ${base.getText()}`, StackType.bytes);
chain.splice(0, 2);
break;
case 'approvalProgramPages':
if (!chain[1].isKind(ts.SyntaxKind.CallExpression))
throw Error(`approvralProgram must be a function call`);
this.push(chain[1], `PENDING_COMPILE_PAGES_APPROVAL: ${base.getText()}`, StackType.bytes);
chain.splice(0, 2);
break;
case 'schema':
if (!chain[1].isKind(ts.SyntaxKind.PropertyAccessExpression)) throw Error();
if (!chain[2].isKind(ts.SyntaxKind.PropertyAccessExpression)) throw Error();
Expand Down Expand Up @@ -7237,6 +7259,12 @@ declare type AssetFreezeTxn = Required<AssetFreezeParams>;
this.processNode(e);
this.pushVoid(e, `itxn_field ${capitalizeFirstChar(key)}Pages`);
});
} else if (
key === 'approvalProgram' &&
init?.getText().endsWith('.approvalProgramPages()') &&
init?.isKind(ts.SyntaxKind.CallExpression)
) {
this.processNode(init);
} else if (key === 'onCompletion') {
if (!p.isKind(ts.SyntaxKind.PropertyAssignment) || !init?.isKind(ts.SyntaxKind.PropertyAccessExpression)) {
throw new Error('Must use OnCompletion enum');
Expand Down Expand Up @@ -7430,6 +7458,7 @@ declare type AssetFreezeTxn = Required<AssetFreezeParams>;

if (this.isDynamicType(tVar.type)) {
if (program === 'lsig' || program === 'approval') {
// eslint-disable-next-line no-console
console.warn(
`WARNING: Due to dynamic template variable type for ${tVar.name} (${typeInfoToABIString(
tVar.type
Expand Down
2 changes: 2 additions & 0 deletions src/lib/contract.ts
Original file line number Diff line number Diff line change
Expand Up @@ -54,6 +54,8 @@ export default abstract class Contract {

static clearProgram: () => bytes;

static approvalProgramPages: () => bytes[];

static schema: {
global: {
numUint: number;
Expand Down