Skip to content

Commit

Permalink
feat: add support for simulating an atc (#86)
Browse files Browse the repository at this point in the history
* feat: add support for simulating an atc
  • Loading branch information
neilcampbell authored Dec 14, 2023
1 parent 9a727c9 commit 460b11f
Show file tree
Hide file tree
Showing 9 changed files with 101 additions and 11 deletions.
19 changes: 19 additions & 0 deletions examples/helloworld/client.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -71,4 +71,23 @@ describe('hello world typed client', () => {
expect(result.returns[1]).toBe('Hello, World!')
expect(result.txIds.length).toBe(5)
})

test('Simulates hello', async () => {
const { algod, indexer, testAccount } = localnet.context
const client = new HelloWorldAppClient(
{
resolveBy: 'creatorAndName',
sender: testAccount,
creatorAddress: testAccount.addr,
findExistingUsing: indexer,
},
algod,
)
await client.deploy()

const response = await client.compose().hello({ name: 'mate' }).simulate()

expect(response.methodResults[0].returnValue).toBe('Hello, mate')
expect(response.simulateResponse.txnGroups[0].appBudgetConsumed).toBeLessThan(50)
})
})
17 changes: 15 additions & 2 deletions examples/helloworld/client.ts
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ import type {
} from '@algorandfoundation/algokit-utils/types/app-client'
import type { AppSpec } from '@algorandfoundation/algokit-utils/types/app-spec'
import type { SendTransactionResult, TransactionToSign, SendTransactionFrom } from '@algorandfoundation/algokit-utils/types/transaction'
import type { TransactionWithSigner } from 'algosdk'
import type { ABIResult, TransactionWithSigner, modelsv2 } from 'algosdk'
import { Algodv2, OnApplicationComplete, Transaction, AtomicTransactionComposer } from 'algosdk'
export const APP_SPEC: AppSpec = {
"hints": {
Expand Down Expand Up @@ -554,6 +554,11 @@ export class HelloWorldAppClient {
await promiseChain
return atc
},
async simulate() {
await promiseChain
const result = await atc.simulate(client.algod)
return result
},
async execute() {
await promiseChain
const result = await algokit.sendAtomicTransactionComposer({ atc, sendParams: {} }, client.algod)
Expand Down Expand Up @@ -634,10 +639,18 @@ export type HelloWorldAppComposer<TReturns extends [...any[]] = []> = {
*/
atc(): Promise<AtomicTransactionComposer>
/**
* Executes the transaction group and returns an array of results
* Simulates the transaction group and returns the result
*/
simulate(): Promise<HelloWorldAppComposerSimulateResult>
/**
* Executes the transaction group and returns the results
*/
execute(): Promise<HelloWorldAppComposerResults<TReturns>>
}
export type HelloWorldAppComposerSimulateResult = {
methodResults: ABIResult[]
simulateResponse: modelsv2.SimulateResponse
}
export type HelloWorldAppComposerResults<TReturns extends [...any[]]> = {
returns: TReturns
groupId: string
Expand Down
17 changes: 15 additions & 2 deletions examples/lifecycle/client.ts
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ import type {
} from '@algorandfoundation/algokit-utils/types/app-client'
import type { AppSpec } from '@algorandfoundation/algokit-utils/types/app-spec'
import type { SendTransactionResult, TransactionToSign, SendTransactionFrom } from '@algorandfoundation/algokit-utils/types/transaction'
import type { TransactionWithSigner } from 'algosdk'
import type { ABIResult, TransactionWithSigner, modelsv2 } from 'algosdk'
import { Algodv2, OnApplicationComplete, Transaction, AtomicTransactionComposer } from 'algosdk'
export const APP_SPEC: AppSpec = {
"hints": {
Expand Down Expand Up @@ -666,6 +666,11 @@ export class LifeCycleAppClient {
await promiseChain
return atc
},
async simulate() {
await promiseChain
const result = await atc.simulate(client.algod)
return result
},
async execute() {
await promiseChain
const result = await algokit.sendAtomicTransactionComposer({ atc, sendParams: {} }, client.algod)
Expand Down Expand Up @@ -729,10 +734,18 @@ export type LifeCycleAppComposer<TReturns extends [...any[]] = []> = {
*/
atc(): Promise<AtomicTransactionComposer>
/**
* Executes the transaction group and returns an array of results
* Simulates the transaction group and returns the result
*/
simulate(): Promise<LifeCycleAppComposerSimulateResult>
/**
* Executes the transaction group and returns the results
*/
execute(): Promise<LifeCycleAppComposerResults<TReturns>>
}
export type LifeCycleAppComposerSimulateResult = {
methodResults: ABIResult[]
simulateResponse: modelsv2.SimulateResponse
}
export type LifeCycleAppComposerResults<TReturns extends [...any[]]> = {
returns: TReturns
groupId: string
Expand Down
17 changes: 15 additions & 2 deletions examples/state/client.ts
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ import type {
} from '@algorandfoundation/algokit-utils/types/app-client'
import type { AppSpec } from '@algorandfoundation/algokit-utils/types/app-spec'
import type { SendTransactionResult, TransactionToSign, SendTransactionFrom } from '@algorandfoundation/algokit-utils/types/transaction'
import type { TransactionWithSigner } from 'algosdk'
import type { ABIResult, TransactionWithSigner, modelsv2 } from 'algosdk'
import { Algodv2, OnApplicationComplete, Transaction, AtomicTransactionComposer } from 'algosdk'
export const APP_SPEC: AppSpec = {
"hints": {
Expand Down Expand Up @@ -1572,6 +1572,11 @@ export class StateAppClient {
await promiseChain
return atc
},
async simulate() {
await promiseChain
const result = await atc.simulate(client.algod)
return result
},
async execute() {
await promiseChain
const result = await algokit.sendAtomicTransactionComposer({ atc, sendParams: {} }, client.algod)
Expand Down Expand Up @@ -1768,10 +1773,18 @@ export type StateAppComposer<TReturns extends [...any[]] = []> = {
*/
atc(): Promise<AtomicTransactionComposer>
/**
* Executes the transaction group and returns an array of results
* Simulates the transaction group and returns the result
*/
simulate(): Promise<StateAppComposerSimulateResult>
/**
* Executes the transaction group and returns the results
*/
execute(): Promise<StateAppComposerResults<TReturns>>
}
export type StateAppComposerSimulateResult = {
methodResults: ABIResult[]
simulateResponse: modelsv2.SimulateResponse
}
export type StateAppComposerResults<TReturns extends [...any[]]> = {
returns: TReturns
groupId: string
Expand Down
17 changes: 15 additions & 2 deletions examples/voting/client.ts
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ import type {
} from '@algorandfoundation/algokit-utils/types/app-client'
import type { AppSpec } from '@algorandfoundation/algokit-utils/types/app-spec'
import type { SendTransactionResult, TransactionToSign, SendTransactionFrom } from '@algorandfoundation/algokit-utils/types/transaction'
import type { TransactionWithSigner } from 'algosdk'
import type { ABIResult, TransactionWithSigner, modelsv2 } from 'algosdk'
import { Algodv2, OnApplicationComplete, Transaction, AtomicTransactionComposer } from 'algosdk'
export const APP_SPEC: AppSpec = {
"hints": {
Expand Down Expand Up @@ -925,6 +925,11 @@ export class VotingRoundAppClient {
await promiseChain
return atc
},
async simulate() {
await promiseChain
const result = await atc.simulate(client.algod)
return result
},
async execute() {
await promiseChain
const result = await algokit.sendAtomicTransactionComposer({ atc, sendParams: {} }, client.algod)
Expand Down Expand Up @@ -1008,10 +1013,18 @@ export type VotingRoundAppComposer<TReturns extends [...any[]] = []> = {
*/
atc(): Promise<AtomicTransactionComposer>
/**
* Executes the transaction group and returns an array of results
* Simulates the transaction group and returns the result
*/
simulate(): Promise<VotingRoundAppComposerSimulateResult>
/**
* Executes the transaction group and returns the results
*/
execute(): Promise<VotingRoundAppComposerResults<TReturns>>
}
export type VotingRoundAppComposerSimulateResult = {
methodResults: ABIResult[]
simulateResponse: modelsv2.SimulateResponse
}
export type VotingRoundAppComposerResults<TReturns extends [...any[]]> = {
returns: TReturns
groupId: string
Expand Down
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -134,4 +134,4 @@
"@semantic-release/github"
]
}
}
}
13 changes: 12 additions & 1 deletion src/client/call-composer-types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -56,12 +56,23 @@ export function* callComposerType(ctx: GeneratorContext): DocumentParts {
yield `atc(): Promise<AtomicTransactionComposer>`

yield* jsDoc({
description: 'Executes the transaction group and returns an array of results',
description: 'Simulates the transaction group and returns the result',
})
yield `simulate(): Promise<${name}ComposerSimulateResult>`

yield* jsDoc({
description: 'Executes the transaction group and returns the results',
})
yield `execute(): Promise<${name}ComposerResults<TReturns>>`

yield DecIndentAndCloseBlock

yield `export type ${name}ComposerSimulateResult = {`
yield IncIndent
yield `methodResults: ABIResult[]`
yield `simulateResponse: modelsv2.SimulateResponse`
yield DecIndentAndCloseBlock

yield `export type ${name}ComposerResults<TReturns extends [...any[]]> = {`
yield IncIndent
yield `returns: TReturns`
Expand Down
8 changes: 8 additions & 0 deletions src/client/call-composer.ts
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,14 @@ export function* composeMethod(ctx: GeneratorContext): DocumentParts {
yield DecIndent
yield '},'

yield `async simulate() {`
yield IncIndent
yield `await promiseChain`
yield `const result = await atc.simulate(client.algod)`
yield `return result`
yield DecIndent
yield '},'

yield `async execute() {`
yield IncIndent
yield `await promiseChain`
Expand Down
2 changes: 1 addition & 1 deletion src/client/imports.ts
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,6 @@ import type {
} from '@algorandfoundation/algokit-utils/types/app-client'
import type { AppSpec } from '@algorandfoundation/algokit-utils/types/app-spec'
import type { SendTransactionResult, TransactionToSign, SendTransactionFrom } from '@algorandfoundation/algokit-utils/types/transaction'
import type { TransactionWithSigner } from 'algosdk'
import type { ABIResult, TransactionWithSigner, modelsv2 } from 'algosdk'
import { Algodv2, OnApplicationComplete, Transaction, AtomicTransactionComposer } from 'algosdk'`
}

0 comments on commit 460b11f

Please sign in to comment.