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

feat: allow bytes substitution for uint64 template vars #306

Merged
merged 2 commits into from
Aug 9, 2024
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
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
Loading