Skip to content

Commit

Permalink
feat: allow bytes substitution for uint64 template vars (#306)
Browse files Browse the repository at this point in the history
* feat: allow bytes substitution for uint64 template vars

* docs
  • Loading branch information
joe-p authored Aug 9, 2024
1 parent 3830657 commit 138ae68
Show file tree
Hide file tree
Showing 3 changed files with 32 additions and 2 deletions.
4 changes: 2 additions & 2 deletions docs/code/modules/index.md
Original file line number Diff line number Diff line change
Expand Up @@ -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)

___

Expand Down Expand Up @@ -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)

___

Expand Down
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 138ae68

Please sign in to comment.