Git Product home page Git Product logo

algokit-client-generator-ts's Introduction

AlgoKit TypeScript client generator (algokit-client-generator-ts)

This project generates a type-safe smart contract client in TypeScript for the Algorand Blockchain that wraps the application client in AlgoKit Utils and tailors it to a specific smart contract. It does this by reading an ARC-0032 application spec file and generating a client which exposes methods for each ABI method in the target smart contract, along with helpers to create, update, and delete the application.

Usage

Prerequisites

To be able to consume the generated file you need to include it in a TypeScript project that has (at least) the following package installed:

npm install @algorandfoundation/algokit-utils

Use

The cli can be used to generate a client via the following command.

npx --yes  @algorandfoundation/algokit-client-generator generate -a ./application.json -o ./client.generated.ts

Alternatively, a client can be generated from code by invoking the generate function paired with either writeDocumentPartsToString or writeDocumentPartsToStream depending on your needs. We also expose helpers to optionally load and validate an application.json file.

import fs from 'fs'
import {
  writeDocumentPartsToStream,
  writeDocumentPartsToString,
  generate,
  loadApplicationJson,
  validateApplicationJson,
} from '@algorandfoundation/algokit-client-generator'
import appJson from './application.json'

const appJsonFromFile = loadApplicationJson('./application.json')
const appJsonFromObject = validateApplicationJson(appJson)

const fileStream = fs.createWriteStream('./client.ts', {
  flags: 'w',
})
writeDocumentPartsToStream(generate(appJsonFromFile, fileStream))

const clientAsString = writeDocumentPartsToString(appJsonFromObject)

For details on how to use the generated client see the more detailed usage docs

Examples

There are a range of examples that you can look at to see a source smart contract ({contract.py}), the generated client (client.ts) and some tests that demonstrate how you can use the client (client.spec.ts).

Contributing

If you want to contribute to this project the following information will be helpful.

Initial setup

  1. Clone this repository locally

  2. Install pre-requisites:

    • Install AlgoKit - Link: Ensure you can execute algokit --version.
    • Bootstrap your local environment; run algokit bootstrap all within this folder, which will:
      • Install Poetry - Link: The minimum required version is 1.2. Ensure you can execute poetry -V and get 1.2+
      • Run poetry install in the root directory, which will set up a .venv folder with a Python virtual environment and also install all Python dependencies
      • Run npm install
  3. Open the project and start debugging / developing via:

    • VS Code
      1. Open the repository root in VS Code
      2. Install recommended extensions
      3. Run tests via test explorer
    • IDEA (e.g. PyCharm)
      1. Open the repository root in the IDE
      2. It should automatically detect it's a Poetry project and set up a Python interpreter and virtual environment.
      3. Run tests
    • Other
      1. Open the repository root in your text editor of choice
      2. Run npm run test

Subsequently

  1. If you update to the latest source code and there are new dependencies you will need to run algokit bootstrap all again
  2. Follow step 3 above

Building examples

In the examples folder there is a series of example contracts along with their generated client. These contracts are built using Beaker.

If you want to make changes to any of the smart contract examples and re-generate the ARC-0032 application.json files then change the corresponding examples/{contract}/{contract}.py file and then run:

poetry run python -m examples

Or in Visual Studio Code you can use the default build task (Ctrl+Shift+B).

To regenerate the generated clients run npm run update-approvals.

Continuous Integration / Continuous Deployment (CI/CD)

This project uses GitHub Actions to define CI/CD workflows, which are located in the .github/workflows folder.

Approval tests

Making any changes to the generated code will result in the approval tests failing. The approval tests work by generating a version of client and outputting it to ./examples/APP_NAME/client.generated.ts then comparing to the approved version ./examples/APP_NAME/client.ts. If you make a change and break the approval tests, you will need to update the approved version by overwriting it with the generated version. You can run npm run update-approvals to update all approved clients in one go.

algokit-client-generator-ts's People

Contributors

aorumbayev avatar daniel-makerx avatar dependabot[bot] avatar github-actions[bot] avatar joe-p avatar loedn avatar neilcampbell avatar patrickdinh avatar phearzero avatar robdmoore avatar tristanmenzel avatar

Stargazers

 avatar  avatar  avatar  avatar

Watchers

 avatar  avatar  avatar  avatar

algokit-client-generator-ts's Issues

Enable the ability to allow empty signatures when calling simulate

Reported by @CiottiGiorgio:

I wrote a pure function in a contract which is the perfect candidate to be simulated in my frontend.

Unfortunately that still requires and triggers a signature from the user. At least using the composer and then .simulate().
I found trying to set allowEmptySignatures anywhere difficult and ultimately gave up because:

  • .simulate() doesn't take arguments to be passed to the underlying atc
  • I didn't want to manually edit the appclient since that is going to be overwritten when the client is generated

Type error in generated client

I used algokit generate client application.json -o client.generated.ts and the generated client has a type error in the schema for global reserved state:

Actual
"descr"

        "reserved_global_bytes_value": {
          "type": "bytes",
          "max_keys": 30,
          "descr": "Reserved global state bytes value with 30 possible keys"
        },
        "reserved_global_uint_value": {
          "type": "uint64",
          "max_keys": 29,
          "descr": "Reserved global state uint value with 29 possible keys"
        }
      }

Expected
"desc"

These are the types from algokit-utils:

/** Reserved schema value specification */
export interface ReservedSchemaValueSpec {
    /** The type of value */
    type: AVMType;
    /** The description of the reserved storage space */
    desc: string;
    /** The maximum number of slots to reserve */
    max_keys: number;
}

Incorrect return type for create and update calls

@iskysun96 reported an error in the return type for create (and I confirmed the same is true for update) calls. The appId and appAddress fields are not in the returned type even though they are in the return value from the underlying AlgoKit Utils call.

Capitalization of method names is not preserved

With version 2.2.4, the capitalization of method names is not preserved.

If there is an ABI method defined with multiple capital letters in a row, such as getRegisteredASA, the generated typed client will have a method getRegisteredAsa. One would exepct capitalization to be preserved in the typed client.

uint64[] args result in incorrect types

This ARC-32

      {
        "name": "vaultOptIn",
        "args": [
          {
            "name": "assets",
            "type": "uint64[]"
          }
        ],
        "returns": {
          "type": "void"
        }
      }

Generates this

    & Record<'vaultOptIn(uint64[])void' | 'vaultOptIn', {
      argsObj: {
        assets: bigint | number[]
      }
      argsTuple: [assets: bigint | number[]]
      returns: void
    }>

It should be:

    & Record<'vaultOptIn(uint64[])void' | 'vaultOptIn', {
      argsObj: {
        assets: bigint[]
      }
      argsTuple: [assets: bigint[]]
      returns: void
    }>

Map bigint to number for uint<64

Reported in discord:

The algokit generated clients define explicit types for things like uint16 -> number, etc. on calls and returns - however - the actual runtime return values of these types are always bigints.
bigint certainly corresponds properly to uint64 but this means I have to explicitly coerce the types on my side as part of the hand-created wrappers I have to create for structured types.

Account arguments are typed as bigint | number

In the generated client, any method that has an Account argument expects a type of bigint | number. While this is technically correct, the expectation for the client is that it would take in an address much like the ATC or underlying appclient, especially since there doesn't seem to be a way to manual specify the accounts array.

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.