Skip to content

Commit

Permalink
fix: correct program encoding in application transactions (#96)
Browse files Browse the repository at this point in the history
  • Loading branch information
neilcampbell authored Aug 2, 2024
1 parent dce6b8a commit b014493
Show file tree
Hide file tree
Showing 2 changed files with 22 additions and 3 deletions.
11 changes: 8 additions & 3 deletions src/transform.ts
Original file line number Diff line number Diff line change
Expand Up @@ -280,7 +280,6 @@ export function getIndexerTransactionFromAlgodTransaction(
const getChildOffset = t.getChildOffset ? t.getChildOffset : () => ++childOffset

const encoder = new TextEncoder()
const decoder = new TextDecoder()

// The types in algosdk for state proofs are incorrect, so override them
const stateProof = transaction.stateProof as unknown as StateProof | undefined
Expand Down Expand Up @@ -365,8 +364,14 @@ export function getIndexerTransactionFromAlgodTransaction(
? {
'application-transaction': {
'application-id': transaction.appIndex,
'approval-program': decoder.decode(transaction.appApprovalProgram),
'clear-state-program': decoder.decode(transaction.appClearProgram),
'approval-program':
transaction.appApprovalProgram && transaction.appApprovalProgram.length > 0
? Buffer.from(transaction.appApprovalProgram).toString('base64')
: '',
'clear-state-program':
transaction.appClearProgram && transaction.appClearProgram.length > 0
? Buffer.from(transaction.appClearProgram).toString('base64')
: '',
'on-completion': algodOnCompleteToIndexerOnComplete(transaction.appOnComplete),
'application-args': transaction.appArgs?.map((a) => Buffer.from(a).toString('base64')),
'extra-program-pages': transaction.extraPages,
Expand Down
14 changes: 14 additions & 0 deletions tests/scenarios/transform-complex-txn.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -705,6 +705,7 @@ describe('Complex transaction with many nested inner transactions', () => {
expect(transaction.id).toBe('HHQHASIF2YLCSUYIPE6LIMLSNLCVMQBQHF3X46SKTX6F7ZSFKFCQ')
expect(transaction.id).toBe(blockTransactions[0].transaction.txID())
})

it('Produces the correct state deltas in an app call transaction', async () => {
const blocks = await getBlocksBulk({ startRound: 39430981, maxRound: 39430981 }, algod)
const blockTransactions = blocks.flatMap((b) => getBlockTransactions(b.block))
Expand Down Expand Up @@ -755,4 +756,17 @@ describe('Complex transaction with many nested inner transactions', () => {
},
]`)
})

it('Produces base64 encoded programs for an application create transaction', async () => {
const blocks = await getBlocksBulk({ startRound: 34632059, maxRound: 34632059 }, algod) // Contains a appl create transaction with approval and clear state programs
const blockTransactions = blocks.flatMap((b) => getBlockTransactions(b.block))

expect(blockTransactions.length).toBe(14)

const transaction = getIndexerTransactionFromAlgodTransaction(blockTransactions[5])
expect(transaction['application-transaction']!['approval-program']).toBe(
'CSAFAAGAgKSPxPlaEAImDwpsYXN0X21pbmVyDmhhbHZpbmdfc3VwcGx5EWxhc3RfbWluZXJfZWZmb3J0BmVmZm9ydAV0b2tlbgdoYWx2aW5nDG1pbmVkX3N1cHBseQxtaW5lcl9yZXdhcmQUY3VycmVudF9taW5lcl9lZmZvcnQFYmxvY2sMdG90YWxfZWZmb3J0EnRvdGFsX3RyYW5zYWN0aW9ucw1jdXJyZW50X21pbmVyD3N0YXJ0X3RpbWVzdGFtcAAxGCINgQYLMRkIjQgDCgMrAAAAAAAAAAADHQMsAIgAAiNDigAAJwQiZycJImcnCiJnJwsiZycFImcpgYCA0ofivC1nJwYiZycHgYCAgDJnKDIDZyoiZycMMgNnJwgiZycNgYCByKwGZ4mKAAAnBLGBA7IQgAZPcmFuZ2WyJoADT1JBsiUyCrIpMgqyKjIDsisyA7IsJLIigQiyI4A6aXBmczovL1FtVWl0eEp1UEpKcmN1QWRBaVZkRUVwdXpHbXNFTEdnQXZoTGQ1RmlYUlNoRXUjYXJjM7IngCDT/VG+LujCsXp66CbTScDfIP6rik1oAwNAHHQVYMMkNrIoIrIBgKgBSm9obiBBbGFuIFdvb2RzIDAxL0RlYy8yMDIzIFlvdSBrbm93LCBJIGNhbiBwdWxsIG1ldHJpY3Mgb3V0IG9mIHRoZSBhaXIgdG9vLCB3aGF0ZXZlciwgOCBtaWxsaW9uIHRyYW5zYWN0aW9ucyBvdmVyIHRoZSBsYXN0IHdlZWssIEkgZG9uJ3Qga25vdywgbXkgbW9tIGhhcyBmb3VyIG9yYW5nZXMusgWztDxniYgAAiNDigAAJwRkIhJBAAOI/qQxACsiZomKAwAyBjIGgQUYCYz/Jwlki/8TQQC3KGQnBGRwAExIQQB0KWQnB2QNQQAGJwdkQgACKWSM/rGBBLIQJwRkshEoZLIUi/6yEiKyAbMoZCpkFlCwJwYnBmSL/ghnKSlki/4JZylkIhJBAC0nBScFZCMIZycFZCUPQQAKKSQnBmQJZ0IAEykkJwZkCSEECmcnBycHZCEECmcoZDYyAGFBABsoZCtijP0oZCuL/SpkDUEACIv9KmQJQgABImYnCYv/ZygnDGRnKicIZGcnCCJniScOSTYaAUkVgSASRIgAAiNDigMAi/82MgArY0xIRDIHJw1kD0QnBWQlDkQxAYGgnAEORCcORwKI/vgnCicKZDEBCGcnCycLZCMIZ4v/K2IxAQiM/ov/K4v+Zov+jP0oZIv/EkEAE4v9KmQNQQAIi/0qZAlCAAEijP2L/ScIZA1BAAonDIv/ZycIi/1niTEbQfzygAS4RHs2NhoAjgH85QCABKsjcMw2GgCOAf9TAAAxG0H+ZQA=',
)
expect(transaction['application-transaction']!['clear-state-program']).toBe('CQ==')
})
})

0 comments on commit b014493

Please sign in to comment.