This commit is contained in:
SaraJane
2025-12-30 13:07:28 +00:00
commit 93e3f09667
95 changed files with 20233 additions and 0 deletions

View File

@ -0,0 +1,14 @@
import { TestExecutionContext } from '@algorandfoundation/algorand-typescript-testing'
import { describe, expect, it } from 'vitest'
import { HelloWorld } from './contract.algo'
describe('HelloWorld contract', () => {
const ctx = new TestExecutionContext()
it('Logs the returned value when sayHello is called', () => {
const contract = ctx.contract.create(HelloWorld)
const result = contract.hello('Sally')
expect(result).toBe('Hello, Sally')
})
})

View File

@ -0,0 +1,7 @@
import { Contract } from '@algorandfoundation/algorand-typescript'
export class HelloWorld extends Contract {
public hello(name: string): string {
return `Hello, ${name}`
}
}

View File

@ -0,0 +1,53 @@
import { Config } from '@algorandfoundation/algokit-utils'
import { registerDebugEventHandlers } from '@algorandfoundation/algokit-utils-debug'
import { algorandFixture } from '@algorandfoundation/algokit-utils/testing'
import { Address } from 'algosdk'
import { beforeAll, beforeEach, describe, expect, test } from 'vitest'
import { HelloWorldFactory } from '../artifacts/hello_world/HelloWorldClient'
describe('HelloWorld contract', () => {
const localnet = algorandFixture()
beforeAll(() => {
Config.configure({
debug: true,
// traceAll: true,
})
registerDebugEventHandlers()
})
beforeEach(localnet.newScope)
const deploy = async (account: Address) => {
const factory = localnet.algorand.client.getTypedAppFactory(HelloWorldFactory, {
defaultSender: account,
})
const { appClient } = await factory.deploy({
onUpdate: 'append',
onSchemaBreak: 'append',
})
return { client: appClient }
}
test('says hello', async () => {
const { testAccount } = localnet.context
const { client } = await deploy(testAccount)
const result = await client.send.hello({ args: { name: 'World' } })
expect(result.return).toBe('Hello, World')
})
test('simulate says hello with correct budget consumed', async () => {
const { testAccount } = localnet.context
const { client } = await deploy(testAccount)
const result = await client
.newGroup()
.hello({ args: { name: 'World' } })
.hello({ args: { name: 'Jane' } })
.simulate()
expect(result.returns[0]).toBe('Hello, World')
expect(result.returns[1]).toBe('Hello, Jane')
expect(result.simulateResponse.txnGroups[0].appBudgetConsumed).toBeLessThan(100)
})
})

View File

@ -0,0 +1,33 @@
import { AlgorandClient } from '@algorandfoundation/algokit-utils'
import { HelloWorldFactory } from '../artifacts/hello_world/HelloWorldClient'
// Below is a showcase of various deployment options you can use in TypeScript Client
export async function deploy() {
console.log('=== Deploying HelloWorld ===')
const algorand = AlgorandClient.fromEnvironment()
const deployer = await algorand.account.fromEnvironment('DEPLOYER')
const factory = algorand.client.getTypedAppFactory(HelloWorldFactory, {
defaultSender: deployer.addr,
})
const { appClient, result } = await factory.deploy({ onUpdate: 'append', onSchemaBreak: 'append' })
// If app was just created fund the app account
if (['create', 'replace'].includes(result.operationPerformed)) {
await algorand.send.payment({
amount: (1).algo(),
sender: deployer.addr,
receiver: appClient.appAddress,
})
}
const method = 'hello'
const response = await appClient.send.hello({
args: { name: 'world' },
})
console.log(
`Called ${method} on ${appClient.appClient.appName} (${appClient.appClient.appId}) with name = world, received: ${response.return}`,
)
}

View File

@ -0,0 +1,62 @@
import { Config } from '@algorandfoundation/algokit-utils'
import { registerDebugEventHandlers } from '@algorandfoundation/algokit-utils-debug'
import { consoleLogger } from '@algorandfoundation/algokit-utils/types/logging'
import * as fs from 'node:fs'
import * as path from 'node:path'
// Uncomment the traceAll option to enable auto generation of AVM Debugger compliant sourceMap and simulation trace file for all AVM calls.
// Learn more about using AlgoKit AVM Debugger to debug your TEAL source codes and inspect various kinds of Algorand transactions in atomic groups -> https://github.com/algorandfoundation/algokit-avm-vscode-Debugger
Config.configure({
logger: consoleLogger,
debug: true,
// traceAll: true,
})
registerDebugEventHandlers()
// base directory
const baseDir = path.resolve(__dirname)
// function to validate and dynamically import a module
async function importDeployerIfExists(dir: string) {
const deployerPath = path.resolve(dir, 'deploy-config')
if (fs.existsSync(deployerPath + '.ts') || fs.existsSync(deployerPath + '.js')) {
const deployer = await import(deployerPath)
return { ...deployer, name: path.basename(dir) }
}
return null
}
// get a list of all deployers from the subdirectories
async function getDeployers() {
const directories = fs
.readdirSync(baseDir, { withFileTypes: true })
.filter((dirent) => dirent.isDirectory())
.map((dirent) => path.resolve(baseDir, dirent.name))
const deployers = await Promise.all(directories.map(importDeployerIfExists))
return deployers.filter((deployer) => deployer !== null) // Filter out null values
}
// execute all the deployers
(async () => {
const contractName = process.argv.length > 2 ? process.argv[2] : undefined
const contractDeployers = await getDeployers()
const filteredDeployers = contractName
? contractDeployers.filter(deployer => deployer.name === contractName)
: contractDeployers
if (contractName && filteredDeployers.length === 0) {
console.warn(`No deployer found for contract name: ${contractName}`)
return
}
for (const deployer of filteredDeployers) {
try {
await deployer.deploy()
} catch (e) {
console.error(`Error deploying ${deployer.name}:`, e)
}
}
})()