Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Generate types from a given ABI #87

Draft
wants to merge 3 commits into
base: dev
Choose a base branch
from

Conversation

janniksam
Copy link

@janniksam janniksam commented Feb 3, 2024

Generate Types from a given smart contract interface description (abi)

Overview

This pull request introduces a typescript generator aimed at enhancing development workflows by automating the generation of typescript proxy types from a smart contract abi file.

The generated typescript file includes builders for all included endpoints and types, providing encoders and decoders for all of them. It makes the interaction with the smart contracts very straightforward.

One significant advantage of utilizing this typescript generator, and this is why I built this, is the inherent type safety it provides for all interactions with a smart contracts.

Usage

To use the generator, use the following command:

xsuite generateproxy --from-abi /path/foobar.abi.json --output=/path/foobar.ts

Usage of the generated file

Given that we can't find the following endpoint description in our ABI file:

{
    "name": "getOptionalMultiValue3",
    "mutability": "readonly",
    "inputs": [
        {
            "name": "_id",
            "type": "u64"
        },
        {
            "name": "value",
            "type": "optional<multi<u32,BigUint,u8>>",
            "multi_arg": true
        }
    ],
    "outputs": [
        {
            "type": "optional<multi<u32,BigUint,u8>>",
            "multi_result": true
        }
    ]
},

We can use the generated file like this:

// Import the endpoint builder from the generated typescript file
import { getOptionalMultiValue3Builder } from 'foobar';

// getOptionalMultiValue3 is the method we want to call.
// the convention is methodName + Builder
const builder = getFooBuilder();

// encode the inputs
const funcArgs = builder.encodeInput(77n, [1, 30n, 2]);

/// query the endpoint
const response = await world.query({
  callee: myContract,
  funcName: builder.functionName,
  funcArgs,
});

// decode the response
const decodedData = builder.decodeOutput(response.returnData);
expect(decodedData).toBe([1, 30n, 2]);

###Limitation:

The following things aren't supported yet:

  • The type BigFloat
  • Complex Rust Enums
  • Endpoints with more than one MultiValue

@janniksam janniksam force-pushed the feature/abiTypeGenerator branch 9 times, most recently from 4dfa15d to 33bdb63 Compare February 3, 2024 20:55
@janniksam janniksam force-pushed the feature/abiTypeGenerator branch from 33bdb63 to e80a157 Compare February 4, 2024 11:35
@janniksam janniksam force-pushed the feature/abiTypeGenerator branch from 42983be to 56ab34f Compare February 11, 2024 14:13
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

1 participant