|
| 1 | +import type { bytes, uint64 } from '@algorandfoundation/algorand-typescript' |
| 2 | +import { assert, compile, Contract, itxn } from '@algorandfoundation/algorand-typescript' |
| 3 | +import { decodeArc4, encodeArc4, methodSelector, OnCompleteAction } from '@algorandfoundation/algorand-typescript/arc4' |
| 4 | +import { Hello, HelloTemplate, HelloTemplateCustomPrefix, LargeProgram, TerribleCustodialAccount } from './precompiled-apps.algo' |
| 5 | + |
| 6 | +export class HelloFactory extends Contract { |
| 7 | + test_compile_contract() { |
| 8 | + const compiled = compile(Hello) |
| 9 | + |
| 10 | + const helloApp = itxn |
| 11 | + .applicationCall({ |
| 12 | + appArgs: [methodSelector('create(string)void'), encodeArc4('hello')], |
| 13 | + approvalProgram: compiled.approvalProgram, |
| 14 | + clearStateProgram: compiled.clearStateProgram, |
| 15 | + globalNumBytes: 1, |
| 16 | + }) |
| 17 | + .submit().createdApp |
| 18 | + |
| 19 | + const txn = itxn |
| 20 | + .applicationCall({ |
| 21 | + appArgs: [methodSelector('greet(string)string'), encodeArc4('world')], |
| 22 | + appId: helloApp, |
| 23 | + }) |
| 24 | + .submit() |
| 25 | + const result = decodeArc4<string>(txn.lastLog, 'log') |
| 26 | + |
| 27 | + assert(result === 'hello world') |
| 28 | + |
| 29 | + itxn |
| 30 | + .applicationCall({ |
| 31 | + appId: helloApp, |
| 32 | + appArgs: [methodSelector('delete()void')], |
| 33 | + onCompletion: OnCompleteAction.DeleteApplication, |
| 34 | + }) |
| 35 | + .submit() |
| 36 | + } |
| 37 | + |
| 38 | + test_compile_contract_with_template() { |
| 39 | + const compiled = compile(HelloTemplate, { templateVars: { GREETING: 'hey' } }) |
| 40 | + |
| 41 | + const helloApp = itxn |
| 42 | + .applicationCall({ |
| 43 | + appArgs: [methodSelector('create()void')], |
| 44 | + approvalProgram: compiled.approvalProgram, |
| 45 | + clearStateProgram: compiled.clearStateProgram, |
| 46 | + globalNumBytes: 1, |
| 47 | + }) |
| 48 | + .submit().createdApp |
| 49 | + |
| 50 | + const txn = itxn |
| 51 | + .applicationCall({ |
| 52 | + appArgs: [methodSelector('greet(string)string'), encodeArc4('world')], |
| 53 | + appId: helloApp, |
| 54 | + }) |
| 55 | + .submit() |
| 56 | + const result = decodeArc4<string>(txn.lastLog, 'log') |
| 57 | + |
| 58 | + assert(result === 'hey world') |
| 59 | + |
| 60 | + itxn |
| 61 | + .applicationCall({ |
| 62 | + appId: helloApp, |
| 63 | + appArgs: [methodSelector('delete()void')], |
| 64 | + onCompletion: OnCompleteAction.DeleteApplication, |
| 65 | + }) |
| 66 | + .submit() |
| 67 | + } |
| 68 | + |
| 69 | + test_compile_contract_with_template_and_custom_prefix() { |
| 70 | + const compiled = compile(HelloTemplateCustomPrefix, { templateVars: { GREETING: 'bonjour' }, templateVarsPrefix: 'PRFX_' }) |
| 71 | + |
| 72 | + const helloApp = itxn |
| 73 | + .applicationCall({ |
| 74 | + appArgs: [methodSelector('create()void')], |
| 75 | + approvalProgram: compiled.approvalProgram, |
| 76 | + clearStateProgram: compiled.clearStateProgram, |
| 77 | + globalNumBytes: 1, |
| 78 | + }) |
| 79 | + .submit().createdApp |
| 80 | + |
| 81 | + const txn = itxn |
| 82 | + .applicationCall({ |
| 83 | + appArgs: [methodSelector('greet(string)string'), encodeArc4('world')], |
| 84 | + appId: helloApp, |
| 85 | + }) |
| 86 | + .submit() |
| 87 | + const result = decodeArc4<string>(txn.lastLog, 'log') |
| 88 | + |
| 89 | + assert(result === 'bonjour world') |
| 90 | + |
| 91 | + itxn |
| 92 | + .applicationCall({ |
| 93 | + appId: helloApp, |
| 94 | + appArgs: [methodSelector('delete()void')], |
| 95 | + onCompletion: OnCompleteAction.DeleteApplication, |
| 96 | + }) |
| 97 | + .submit() |
| 98 | + } |
| 99 | + |
| 100 | + test_compile_contract_large() { |
| 101 | + const compiled = compile(LargeProgram) |
| 102 | + |
| 103 | + const largeApp = itxn |
| 104 | + .applicationCall({ |
| 105 | + approvalProgram: compiled.approvalProgram, |
| 106 | + clearStateProgram: compiled.clearStateProgram, |
| 107 | + extraProgramPages: compiled.extraProgramPages, |
| 108 | + globalNumBytes: compiled.globalBytes, |
| 109 | + }) |
| 110 | + .submit().createdApp |
| 111 | + |
| 112 | + const txn = itxn |
| 113 | + .applicationCall({ |
| 114 | + appArgs: [methodSelector('getBigBytesLength()uint64')], |
| 115 | + appId: largeApp, |
| 116 | + }) |
| 117 | + .submit() |
| 118 | + const result = decodeArc4<uint64>(txn.lastLog, 'log') |
| 119 | + |
| 120 | + assert(result === 4096) |
| 121 | + |
| 122 | + itxn |
| 123 | + .applicationCall({ |
| 124 | + appId: largeApp, |
| 125 | + appArgs: [methodSelector('delete()void')], |
| 126 | + onCompletion: OnCompleteAction.DeleteApplication, |
| 127 | + }) |
| 128 | + .submit() |
| 129 | + } |
| 130 | + |
| 131 | + test_compile_logic_sig(account: bytes) { |
| 132 | + const compiled = compile(TerribleCustodialAccount) |
| 133 | + |
| 134 | + assert(compiled.account.bytes === account) |
| 135 | + } |
| 136 | +} |
0 commit comments