From 915cb1ebf697ff0ad057711de6ac7e985b278998 Mon Sep 17 00:00:00 2001 From: Joe Polny Date: Fri, 9 Aug 2024 07:07:09 -0400 Subject: [PATCH 1/2] feat: allow bytes substitution for uint64 template vars --- src/app-deploy.spec.ts | 21 +++++++++++++++++++++ src/app-deploy.ts | 9 +++++++++ 2 files changed, 30 insertions(+) diff --git a/src/app-deploy.spec.ts b/src/app-deploy.spec.ts index 28278255..2b6f94c0 100644 --- a/src/app-deploy.spec.ts +++ b/src/app-deploy.spec.ts @@ -588,6 +588,27 @@ test('Can substitute template variable with multiple underscores', async () => { `) }) +test('Can substitue both bytes and int uint64', async () => { + const test_teal = ` + int TMPL_SOME_VALUE + pushint TMPL_SOME_VALUE + bytes TMPL_SOME_VALUE + pushbytes TMPL_SOME_VALUE + return + ` + const test_params = { + SOME_VALUE: 123, + } + const substituted = algokit.performTemplateSubstitution(test_teal, test_params) + expect(substituted).toBe(` + int 123 + pushint 123 + bytes 0x000000000000007b + pushbytes 0x000000000000007b + return + `) +}) + function getMetadata(overrides?: Partial): AppDeployMetadata { return { name: 'test', diff --git a/src/app-deploy.ts b/src/app-deploy.ts index fa4059d7..74967de3 100644 --- a/src/app-deploy.ts +++ b/src/app-deploy.ts @@ -588,6 +588,15 @@ export function performTemplateSubstitution(tealCode: string, templateParams?: T for (const key in templateParams) { const value = templateParams[key] const token = `TMPL_${key.replace(/^TMPL_/, '')}` + + // If this is a number, first replace any byte representations of the number + // These may appear in the TEAL in order to circumvent int compression and preserve PC values + if (typeof value === 'number' || typeof value === 'boolean') { + tealCode = tealCode.replace(new RegExp(`(?<=bytes )${token}`, 'g'), `0x${value.toString(16).padStart(16, '0')}`) + + // We could probably return here since mixing pushint and pushbytes is likely not going to happen, but might as well do both + } + tealCode = tealCode.replace( new RegExp(token, 'g'), typeof value === 'string' From d1576e892cd120e6e828d7ee10d0cd66ee58b1b8 Mon Sep 17 00:00:00 2001 From: Joe Polny Date: Fri, 9 Aug 2024 07:09:21 -0400 Subject: [PATCH 2/2] docs --- docs/code/modules/index.md | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/docs/code/modules/index.md b/docs/code/modules/index.md index 072e6a3e..758ce060 100644 --- a/docs/code/modules/index.md +++ b/docs/code/modules/index.md @@ -2466,7 +2466,7 @@ The information about the compiled code #### Defined in -[src/app-deploy.ts:616](https://github.com/algorandfoundation/algokit-utils-ts/blob/main/src/app-deploy.ts#L616) +[src/app-deploy.ts:625](https://github.com/algorandfoundation/algokit-utils-ts/blob/main/src/app-deploy.ts#L625) ___ @@ -2823,7 +2823,7 @@ The TEAL without comments #### Defined in -[src/app-deploy.ts:639](https://github.com/algorandfoundation/algokit-utils-ts/blob/main/src/app-deploy.ts#L639) +[src/app-deploy.ts:648](https://github.com/algorandfoundation/algokit-utils-ts/blob/main/src/app-deploy.ts#L648) ___