Skip to content

Commit

Permalink
feat: allow bytes substitution for uint64 template vars
Browse files Browse the repository at this point in the history
  • Loading branch information
joe-p committed Aug 9, 2024
1 parent 3830657 commit 915cb1e
Show file tree
Hide file tree
Showing 2 changed files with 30 additions and 0 deletions.
21 changes: 21 additions & 0 deletions src/app-deploy.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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>): AppDeployMetadata {
return {
name: 'test',
Expand Down
9 changes: 9 additions & 0 deletions src/app-deploy.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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'
Expand Down

0 comments on commit 915cb1e

Please sign in to comment.