Skip to content

Commit

Permalink
feat(app): Added deployApp implementation, extracted out algod types
Browse files Browse the repository at this point in the history
  • Loading branch information
robdmoore committed Mar 12, 2023
1 parent dadfcdd commit 68d5fdd
Show file tree
Hide file tree
Showing 7 changed files with 554 additions and 98 deletions.
128 changes: 128 additions & 0 deletions src/algod-type.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,128 @@
import { EncodedSignedTransaction } from 'algosdk'

/** The response from the pending transaction API @see https://developer.algorand.org/docs/rest-apis/algod/v2/#get-v2transactionspendingtxid */
export interface PendingTransactionResponse {
/**
* The application index if the transaction was found and it created an
* application.
*/
'application-index'?: number
/**
* The number of the asset's unit that were transferred to the close-to address.
*/
'asset-closing-amount'?: number
/**
* The asset index if the transaction was found and it created an asset.
*/
'asset-index'?: number
/**
* Rewards in microalgos applied to the close remainder to account.
*/
'close-rewards'?: number
/**
* Closing amount for the transaction.
*/
'closing-amount'?: number
/**
* The round where this transaction was confirmed, if present.
*/
'confirmed-round'?: number
/**
* (gd) Global state key/value changes for the application being executed by this
* transaction.
*/
'global-state-delta'?: Record<string, EvalDelta>[]
/**
* Inner transactions produced by application execution.
*/
'inner-txns'?: PendingTransactionResponse[]
/**
* (ld) Local state key/value changes for the application being executed by this
* transaction.
*/
'local-state-delta'?: Record<string, EvalDelta>[]
/**
* (lg) Logs for the application being executed by this transaction.
*/
logs?: Uint8Array[]
/** Indicates that the transaction was kicked out of this node's transaction pool (and specifies why that happened).
* An empty string indicates the transaction wasn't kicked out of this node's txpool due to an error. */
'pool-error': string
/**
* Rewards in µALGOs applied to the receiver account.
*/
'receiver-rewards'?: number
/**
* Rewards in µALGOs applied to the sender account.
*/
'sender-rewards'?: number
/**
* The raw signed transaction.
*/
txn: EncodedSignedTransaction
}

/** Represents a TEAL value delta @see https://developer.algorand.org/docs/rest-apis/algod/v2/#evaldelta */
interface EvalDelta {
action: number
bytes: string
uint: number
}

/** The response from the application API @see https://developer.algorand.org/docs/rest-apis/algod/v2/#get-v2applicationsapplication-id */
export interface ApplicationResponse {
id: number
params: ApplicationParams
}

/** Stores the global information associated with an application @see https://developer.algorand.org/docs/rest-apis/algod/v2/#applicationparams */
interface ApplicationParams {
/** Address of the account that created the app */
creator: string
/** Base64 encoded TEAL approval program */
'approval-program': string
/** Base64 encoded TEAL clear state program */
'clear-state-program': string
/** The amount of extra program pages available to this app. */
'extra-program-pages'?: number
/** Current global state values */
'global-state'?: { key: string; value: TealValue }[]
/** Global state schema */
'global-state-schema'?: ApplicationStateSchema
/** Local state schema */
'local-state-schema'?: ApplicationStateSchema
}

/**
* Represents a TEAL value @see https://developer.algorand.org/docs/rest-apis/algod/v2/#tealvalue
*/
type TealValue =
| {
/**
* (tt) value type. Value `1` refers to **bytes**, value `2` refers to **uint**
*/
type: 1
/**
* (tb) bytes value.
*/
bytes: string
}
| {
/**
* (tt) value type. Value `1` refers to **bytes**, value `2` refers to **uint**
*/
type: 2

/**
* (ui) uint value.
*/
uint: number | bigint
}

/** Specifies maximums on the number of each type that may be stored @see https://developer.algorand.org/docs/rest-apis/algod/v2/#applicationstateschema */
export interface ApplicationStateSchema {
/** [nbs] num of byte slices */
'num-byte-slice': number
/** [nui] num of uints */
'num-uint': number
}
12 changes: 12 additions & 0 deletions src/app.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
import algosdk, { Algodv2, SuggestedParams, Transaction } from 'algosdk'
import { Buffer } from 'buffer'
import { ApplicationResponse } from './algod-type'
import { AlgoKitConfig } from './config'
import {
encodeTransactionNote,
Expand Down Expand Up @@ -254,6 +255,17 @@ export function getAppArgsForTransaction(args?: AppCallArgs) {
}
}

/**
* Gets the current data for the given app from algod.
*
* @param appIndex The index of the app
* @param client An algod client
* @returns The data about the app
*/
export async function getAppByIndex(appIndex: number, client: Algodv2) {
return (await client.getApplicationByID(appIndex).do()) as ApplicationResponse
}

/**
* Compiles the given TEAL using algod and returns the result.
*
Expand Down
3 changes: 2 additions & 1 deletion src/application-client.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import algosdk, { Algodv2, SuggestedParams } from 'algosdk'
/*import algosdk, { Algodv2, SuggestedParams } from 'algosdk'
import { createApp } from './app'
import { SendTransactionFrom } from './transaction'
Expand Down Expand Up @@ -64,3 +64,4 @@ export class ApplicationClient {
)
}
}
*/
20 changes: 12 additions & 8 deletions src/deploy-app.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,14 +2,8 @@ import { describe, test } from '@jest/globals'
import { readFile } from 'fs/promises'
import path from 'path'
import { localNetFixture } from '../tests/fixtures/localnet-fixture'
import { callApp, createApp, updateApp } from './app'
import {
AppDeployMetadata,
APP_DEPLOY_NOTE_PREFIX,
getCreatorAppsByName,
getStorageSchemaFromAppSpec,
replaceDeployTimeControlParams,
} from './deploy-app'
import { AppStorageSchema, callApp, createApp, updateApp } from './app'
import { AppDeployMetadata, APP_DEPLOY_NOTE_PREFIX, getCreatorAppsByName, replaceDeployTimeControlParams } from './deploy-app'
import { SendTransactionFrom } from './transaction'

describe('deploy-app', () => {
Expand Down Expand Up @@ -88,3 +82,13 @@ describe('deploy-app', () => {
expect(app3Data.deleted).toBe(true)
})
})

// eslint-disable-next-line @typescript-eslint/no-explicit-any
function getStorageSchemaFromAppSpec(appSpec: any): AppStorageSchema {
return {
globalByteSlices: appSpec.state.global.num_byte_slices,
globalInts: appSpec.state.global.num_uints,
localByteSlices: appSpec.state.local.num_byte_slices,
localInts: appSpec.state.local.num_byte_slices,
}
}
Loading

0 comments on commit 68d5fdd

Please sign in to comment.