Skip to content

Latest commit

 

History

History
69 lines (47 loc) · 1.67 KB

automatic_cairo_ABI_parsing.md

File metadata and controls

69 lines (47 loc) · 1.67 KB
sidebar_position
18

Automatic TypeScript parsing of Cairo ABI-s

Starknet.js has integrated Abi-Wan-Kanabi, the standalone TypeScript parser for Cairo smart contracts.

It enables on-the-fly typechecking and autocompletion for contract calls directly in TypeScript. Developers can now catch typing mistakes early, prior to executing a call on-chain, thus enhancing the overall DAPP development experience.

Supported Cairo ABI-s

Please take a look on the Abi-Wan documentation for a list of supported Cairo ABI-s.

Usage

First, you need to wrap your ABI in a array and export it as a const.

Example:

export const tAbi = [
  {
    type: 'function',
    name: 'increase_balance',
    inputs: [
      {
        name: 'amount',
        type: 'core::felt252',
      },
    ],
    outputs: [],
    state_mutability: 'external',
  },
] as const;

Later on, to use it in our code, we have 2 options.

Option 1

import { tAbi } from '../__mocks__/hello';
import { TypedContract } from '../src';

let cairo1Contract: TypedContract<typeof tAbi>; // tAbi is your Cairo contract ABI

After that, you can use cairo1Contract in your code as you would before, but with autocomplete and type checking!

For example:

const tx = await cairo1Contract.increase_balance(100);

Option 2

import { tAbi } from '../__mocks__/hello';

// ...

let cairo1Contract = new Contract(compiledHelloSierra.abi, dd.deploy.contract_address, account);

let cairo1ContractTyped = cairo1Contract.typed(tAbi);

cairo1ContractTyped.test_bool();