Project initialised with AlgoKit CLI using template: https://github.com/algorandfoundation/algokit-fullstack-template.git
This commit is contained in:
@ -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')
|
||||
})
|
||||
})
|
||||
@ -0,0 +1,7 @@
|
||||
import { Contract } from '@algorandfoundation/algorand-typescript'
|
||||
|
||||
export class HelloWorld extends Contract {
|
||||
public hello(name: string): string {
|
||||
return `Hello, ${name}`
|
||||
}
|
||||
}
|
||||
@ -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)
|
||||
})
|
||||
})
|
||||
@ -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}`,
|
||||
)
|
||||
}
|
||||
Reference in New Issue
Block a user