Skip to content

Commit

Permalink
Merge pull request #44 from haroune-mohammedi/feat/support-v2
Browse files Browse the repository at this point in the history
[Feat]: support cairo v2.3.0-rc0
  • Loading branch information
ivpavici authored Oct 27, 2023
2 parents f7cb9f5 + 89bbc5c commit bdfd48d
Show file tree
Hide file tree
Showing 10 changed files with 413 additions and 2,164 deletions.
33 changes: 19 additions & 14 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -48,25 +48,26 @@ abiwan will support multiple Cairo compiler versions, but not in parallel - diff

Currently supported:

| Abi-Wan npm | Cairo compiler |
| ------------- | ------------- |
| [1.0.3](https://www.npmjs.com/package/abi-wan-kanabi/v/1.0.3) | [Cairo v1.0.0](https://github.com/starkware-libs/cairo/releases/tag/v1.0.0) <br> [Cairo v1.1.0](https://github.com/starkware-libs/cairo/releases/tag/v1.1.0) |
| Abi-Wan npm | Cairo compiler |
| ------------------------------------------------------------- | ------------------------------------------------------------------------------------------------------------------------------------------------------------ |
| [1.0.3](https://www.npmjs.com/package/abi-wan-kanabi/v/1.0.3) | [Cairo v1.0.0](https://github.com/starkware-libs/cairo/releases/tag/v1.0.0) <br> [Cairo v1.1.0](https://github.com/starkware-libs/cairo/releases/tag/v1.1.0) |
| [2.0.0](https://www.npmjs.com/package/abi-wan-kanabi/v/2.0.0) | [Cairo v2.3.0-rc0](https://github.com/starkware-libs/cairo/releases/tag/v2.3.0-rc0) |

### Build

To use abiwan, you must first generate types from your contracts' ABI json files, for example using the helper script (deprecated):
To use abiwan, you must first generate types from your contracts' ABI json files, you can use the helper script:

```bash
./scripts/extract_abi.sh <path>/<to>/<abi>.json <path>/<to>/<other_abi>.json ./
npm run generate -- --input /path/to/abi.json --output /path/to/abi.ts
```

This will create a declaration file (.d.ts) for the abi.
This will create a typescript file for the abi.
You can then import it in any script and you are set to go:

```typescript
import abi from "./abi_demo.ts";
import { call } from "./kannabi.ts";
call(abi, "balance_of", 5n);
import abi from "./path/to/abi";
import { call } from "./kanabi";
const balance = call(abi, "balance_of", 5n);
```

> If you think that we should be able to import types directly from the json files, we think so too!
Expand All @@ -78,14 +79,17 @@ call(abi, "balance_of", 5n);
npm run typecheck
```

### Generate `test/example.ts` from `test/example.json`
### Generate `test/example.ts`

```bash
npm run generate -- --input test/example.json --output test/example.ts
# First build the example project with `scarb`
cd test/example
scarb build
# Then generate test/example.ts
cd ../..
npm run generate -- --input test/example/target/dev/example_example_contract.contract_class.json --output test/example.ts
```

Note: `test/example.json` was generated by `starknet-compile test/example.cairo test/example.json`

### Demo

https://drive.google.com/file/d/1OpIgKlk-okvwJn-dkR2Pq2FvOVwlXTUJ/view?usp=sharing
Expand All @@ -96,7 +100,7 @@ Abiwan is still very young and has not yet been subject to an official release.

##  Supported Cairo Types

Abi-wan-kanabi supports all of Cairo 1 types, here's the mapping between Cairo types and Typescript types
Abi-wan-kanabi supports all of Cairo types, here's the mapping between Cairo types and Typescript types

### Primitive Types

Expand All @@ -106,6 +110,7 @@ Abi-wan-kanabi supports all of Cairo 1 types, here's the mapping between Cairo t
| `u8 - u32` | `number \| bigint`  |
| `u64 - u256` | `number \| bigint \| Uint256`  |
| `ContractAddress`  | `string` |
| `ClassHash`  | `string` |
| `bool` | `boolean`  |
| `()`  | `void` |

Expand Down
59 changes: 54 additions & 5 deletions kanabi.ts
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@ export type CairoInt = `${'core::integer::u'}${MBits}`
export type CairoBigInt = `${'core::integer::u'}${BigMBits}`
export type CairoU256 = 'core::integer::u256'
export type CairoAddress = 'core::starknet::contract_address::ContractAddress'
export type CairoClassHash = 'core::starknet::class_hash::ClassHash'
export type CairoFunction = 'function'
export type CairoVoid = '()'
export type CairoBool = 'core::bool'
Expand Down Expand Up @@ -41,6 +42,7 @@ type AbiType =
| CairoBigInt
| CairoU256
| CairoAddress
| CairoClassHash
| CairoBool
| CairoVoid

Expand All @@ -66,6 +68,24 @@ type AbiOutput = {

type AbiStateMutability = 'view' | 'external'

type AbiImpl = {
type: 'impl'
name: string
interface_name: string
}

type AbiInterface = {
type: 'interface'
name: string
items: readonly AbiFunction[]
}

type AbiConstructor = {
type: 'constructor'
name: 'constructor'
inputs: readonly AbiParameter[]
}

type AbiFunction = {
type: 'function'
name: string
Expand All @@ -74,12 +94,22 @@ type AbiFunction = {
state_mutability: AbiStateMutability
}

type AbiEvent = {
type AbiEventStruct = {
type: 'event'
name: string
inputs: readonly AbiParameter[]
kind: 'struct'
members: readonly AbiParameter[]
}

type AbiEventEnum = {
type: 'event'
name: string
kind: 'enum'
variants: readonly AbiParameter[]
}

type AbiEvent = AbiEventStruct | AbiEventEnum

type AbiMember = {
name: string
type: string
Expand All @@ -97,7 +127,15 @@ type AbiEnum = {
variants: readonly AbiParameter[]
}

export type Abi = readonly (AbiFunction | AbiStruct | AbiEvent | AbiEnum)[]
export type Abi = readonly (
| AbiImpl
| AbiInterface
| AbiConstructor
| AbiFunction
| AbiStruct
| AbiEnum
| AbiEvent
)[]

/// Implement
type _BuildArgs<
Expand Down Expand Up @@ -135,11 +173,20 @@ export type FunctionRet<
ExtractAbiFunction<TAbi, TFunctionName>['outputs'][0]['type']
>

export type ExtractAbiFunctions<TAbi extends Abi> = Extract<
export type ExtractAbiImpls<TAbi extends Abi> = Extract<
TAbi[number],
{ type: 'function' }
{ type: 'impl' }
>

export type ExtractAbiInterfaces<TAbi extends Abi> = Extract<
TAbi[number],
{ type: 'interface' }
>

export type ExtractAbiFunctions<TAbi extends Abi> =
| Extract<ExtractAbiInterfaces<TAbi>['items'][number], { type: 'function' }>
| Extract<TAbi[number], { type: 'function' }>

export type ExtractAbiFunctionNames<TAbi extends Abi> =
ExtractAbiFunctions<TAbi>['name']

Expand Down Expand Up @@ -187,6 +234,8 @@ type PrimitiveTypeLookup<TAbi extends Abi> = {
[_ in CairoBigInt]: number | bigint
} & {
[_ in CairoAddress]: string
} & {
[_ in CairoClassHash]: string
} & {
[_ in CairoVoid]: void
} & {
Expand Down
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "abi-wan-kanabi",
"version": "1.0.4",
"version": "2.0.0",
"description": "Abi parser for Cairo smart contracts, based on wagmi abitype",
"main": "index.js",
"bin": {
Expand Down
89 changes: 0 additions & 89 deletions test/example.cairo

This file was deleted.

Loading

0 comments on commit bdfd48d

Please sign in to comment.