Gathering detailed insights and metrics for abi-wan-kanabi
Gathering detailed insights and metrics for abi-wan-kanabi
Gathering detailed insights and metrics for abi-wan-kanabi
Gathering detailed insights and metrics for abi-wan-kanabi
Abi parser for Cairo smart contracts, based on wagmi abitype
npm install abi-wan-kanabi
Typescript
Module System
Node Version
NPM Version
87.5
Supply Chain
99.5
Quality
90.6
Maintenance
100
Vulnerability
99.6
License
TypeScript (86.64%)
Cairo (9.69%)
JavaScript (3.67%)
Total Downloads
1,710,474
Last Day
4,461
Last Week
35,707
Last Month
222,645
Last Year
1,495,691
59 Stars
153 Commits
19 Forks
5 Watching
4 Branches
12 Contributors
Latest Version
2.2.4
Package Id
abi-wan-kanabi@2.2.4
Unpacked Size
54.21 kB
Size
14.15 kB
File Count
19
NPM Version
10.8.2
Node Version
20.18.1
Publised On
13 Dec 2024
Cumulative downloads
Total Downloads
Last day
-62.4%
4,461
Compared to previous day
Last week
-42%
35,707
Compared to previous week
Last month
10.5%
222,645
Compared to previous month
Last year
596.9%
1,495,691
Compared to previous year
4
Abiwan is an UNLICENSE 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 the call on-chain, and thus enhancing the overall Dapp development experience.
Abiwan will support multiple Cairo compiler versions, but not in parallel - different package versions will support different Cairo versions.
Abiwan | Cairo compiler |
---|---|
1.0.3 | Cairo v1.0.0 Cairo v1.1.0 |
2.1.1 | Cairo v2.3.0 |
2.2.3 | Cairo v2.4.4 |
Abiwan dependence only on typescript version 4.9.5 or higher.
Also, it makes use of BigInt, so the tsconfig.json
should target at least ES2020
:
1// tsconfig.json 2{ 3 "compilerOptions": { 4 "target": "ES2020", 5 "lib": ["ES2020", "ESNext"] 6 } 7}
To use Abiwan, you must first export your ABI as const in a typescript file
1export const ABI = [ 2 //Your ABI here 3] as const;
If you have a json file containing your contract class, you can use the CLI to generate the typescript file for you:
1npx abi-wan-kanabi --input /path/to/contract_class.json --output /path/to/abi.ts
You can then import it in any script and you are set to go:
1import ABI from "./path/to/abi"; 2import { call } from "abi-wan-kanabi"; 3// You'll notice the editor is able to infer the types of the contract's functions 4// It'll give you autocompletion and typechecking 5const balance = call(ABI, "your_function_name", ["your", "function", "args"]);
If you think that we should be able to import the ABI directly from the json files, we think so too! See this typescript issue and thumb it up!
starknet.js
Let's say you want to interact with the Ekubo: Core contract using starknet.js
You need to first get the ABI of the contract and export it in a typescript file, you can do so using one command combining both starkli
(tested with version 0.2.3) and npx abi-wan-kanabi
, the command will also print a helpful snippet that you can use to get started
1starkli class-at "0x00000005dd3d2f4429af886cd1a3b08289dbcea99a294197e9eb43b0e0325b4b" --network mainnet | npx abi-wan-kanabi --input /dev/stdin --output abi.ts
1import { Contract, RpcProvider, constants } from "starknet"; 2import { ABI } from "./abi"; 3 4async function main() { 5 const address = 6 "0x00000005dd3d2f4429af886cd1a3b08289dbcea99a294197e9eb43b0e0325b4b"; 7 const provider = new RpcProvider({ nodeUrl: constants.NetworkName.SN_MAIN }); 8 const contract = new Contract(ABI, address, provider).typedv2(ABI); 9 10 const version = await contract.getVersion(); 11 console.log("version", version); 12 13 // Abiwan is now successfully installed, just start writing your contract 14 // function calls (`const ret = contract.your_function()`) and you'll get 15 // helpful editor autocompletion, linting errors ... 16 const primary_inteface_id = contract.get_primary_interface_id(); 17 const protocol_fees_collected = contract.get_protocol_fees_collected("0x1"); 18} 19main().catch(console.error);
Abiwan's types are customizable using declaration merging. Just extend the Config
interface and override the types you want to change, see how starknet.js
is doing it here
1declare module "abi-wan-kanabi" { 2 interface Config { 3 FeltType: string; 4 IntType: number; 5 // ... 6 } 7}
Check config.ts
for all the available options and the their default values.
Abiwan supports all of Cairo types, here's the mapping between Cairo types and Typescript types
Cairo | TypeScript |
---|---|
felt252 | string | number | bigint |
u8 - u32 | number | bigint |
u64 - u256 | number | bigint | U256 |
ContractAddress | string |
EthAddress | string |
ClassHash | string |
bytes31 | string |
ByteArray | string |
bool | boolean |
() | void |
Cairo | TypeScript |
---|---|
Option<T> | T | undefined |
Array<T> | T[] |
Span<T> | T[] |
tuple (T1, T2, ..., Tn) | [T1, T2, ..., Tn] |
struct | an object where keys are struct member names |
enum | a union of objects, each enum variant is an object |
Cairo:
1struct TestStruct { 2 int128: u128, 3 felt: felt252, 4 tuple: (u32, u32) 5}
Typescript:
1{ 2 int128: number | bigint | Uint256; 3 felt: string | number | bigint; 4 tuple: [number | bigint, number | bigint]; 5}
Cairo:
1enum TestEnum { 2 int128: u128, 3 felt: felt252, 4 tuple: (u32, u32), 5}
Typescript:
1{ int128: number | bigint | Uint256 } | 2{ felt: string | number | bigint } | 3{ tuple: [number | bigint, number | bigint]}
1npm run typecheck
test/example.ts
1# First build the example project with `scarb` 2cd test/example 3scarb build 4# Then generate test/example.ts 5cd ../.. 6npm run generate -- --input test/example/target/dev/example_example_contract.contract_class.json --output test/example.ts
Contributions on Abiwan are most welcome! If you are willing to contribute, please get in touch with one of the project leads or via the repositories Discussions
For a full list of all authors and contributors, see the contributors page.
Big thanks and shoutout to Francesco! :clap: who is at the origin of the project!
Also thanks to the awesome Haroune (@haroune-mohammedi) and Thomas (@thomas-quadratic) from Quadratic!
Abiwan is greatly influenced by the similar project for EVM-compatible contracts wagmi/abitype.
No vulnerabilities found.
No security vulnerabilities found.