Skip to content

Commit 5866d89

Browse files
docs: update abiwan guides (#922)
* docs: update abiwan guide * docs: add a section about abiwan in the connect contract guide * Update www/docs/guides/connect_contract.md --------- Co-authored-by: Ivan Pavičić <ivan.pavicic@live.com>
1 parent c0b4e67 commit 5866d89

File tree

2 files changed

+38
-18
lines changed

2 files changed

+38
-18
lines changed

www/docs/guides/automatic_cairo_ABI_parsing.md

Lines changed: 31 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,7 @@ First, you need to wrap your ABI in a array and export it as a `const`.
1919
Example:
2020

2121
```js
22-
export const tAbi = [
22+
export const ABI = [
2323
{
2424
type: 'function',
2525
name: 'increase_balance',
@@ -35,35 +35,48 @@ export const tAbi = [
3535
] as const;
3636
```
3737

38-
Later on, to use it in our code, we have 2 options.
39-
40-
### Option 1
38+
Later on, to use it in our code:
4139

4240
```js
43-
import { tAbi } from '../__mocks__/hello';
44-
import { TypedContract } from '../src';
41+
import { Contract, RpcProvider, constants } from 'starknet';
42+
43+
const address = "YOUR_ADDRESS_HERE";
44+
const provider = new RpcProvider({ nodeUrl: `${yourNodeUrl}` });
45+
const contract = new Contract(ABI, address, provider).typedv2(ABI);
4546

46-
let cairo1Contract: TypedContract<typeof tAbi>; // tAbi is your Cairo contract ABI
47+
// Notice the autocompletion and typechecking in your editor
48+
const result = await contract.increase_balance(100);
4749
```
4850

49-
After that, you can use `cairo1Contract` in your code as you would before, but with autocomplete and type checking!
51+
After that, you can use `contract` in your code as you would before, but with autocompletion and typechecking!
5052

51-
For example:
53+
### Generate `abi.ts` from the contract class
5254

53-
```js
54-
const tx = await cairo1Contract.increase_balance(100);
55+
If you have your contract class in a Json file, you can use the abiwan CLI to generate the `abi.ts` typescript file
56+
57+
```bash
58+
npx abi-wan-kanabi --input /path/to/contract_class.json --output /path/to/abi.ts
5559
```
5660

57-
### Option 2
61+
### Usage for deployed contracts
5862

59-
```js
60-
import { tAbi } from '../__mocks__/hello';
63+
Let's say you want to interact with the [Ekubo: Core](https://starkscan.co/contract/0x00000005dd3d2f4429af886cd1a3b08289dbcea99a294197e9eb43b0e0325b4b) contract
64+
65+
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`](https://github.com/xJonathanLEI/starkli) (tested with version 0.2.3) and `npx abi-wan-kanabi`:
6166

62-
// ...
67+
```bash
68+
starkli class-at "0x00000005dd3d2f4429af886cd1a3b08289dbcea99a294197e9eb43b0e0325b4b" --network mainnet | npx abi-wan-kanabi --input /dev/stdin --output abi.ts
69+
```
6370

64-
let cairo1Contract = new Contract(compiledHelloSierra.abi, dd.deploy.contract_address, account);
71+
```typescript
72+
import { Contract, RpcProvider, constants } from "starknet";
73+
import { ABI } from "./abi";
6574

66-
let cairo1ContractTyped = cairo1Contract.typed(tAbi);
75+
const address = "0x00000005dd3d2f4429af886cd1a3b08289dbcea99a294197e9eb43b0e0325b4b";
76+
const provider = new RpcProvider({ nodeUrl: constants.NetworkName.SN_MAIN });
77+
const contract = new Contract(ABI, address, provider).typedv2(ABI);
6778

68-
cairo1ContractTyped.test_bool();
79+
// Notice the types inferred for the parameter and the returned value
80+
const primary_inteface_id = contract.get_primary_interface_id()
81+
const protocol_fees_collected = contract.get_protocol_fees_collected('0x1')
6982
```

www/docs/guides/connect_contract.md

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -42,10 +42,17 @@ const compiledContract = json.parse(fs.readFileSync("./compiledContracts/test.js
4242
// initialize provider
4343
const provider = new RpcProvider({ nodeUrl: `${myNodeUrl}` });
4444

45+
4546
// initialize deployed contract
4647
const testAddress = "0x7667469b8e93faa642573078b6bf8c790d3a6184b2a1bb39c5c923a732862e1";
4748
const compiledTest = json.parse(fs.readFileSync("./compiledContracts/test.json").toString("ascii"));
4849

4950
// connect the contract
5051
const myTestContract = new Contract(compiledTest.abi, testAddress, provider);
5152
```
53+
54+
## Typechecking and autocompletion
55+
56+
If you want to have typechecking and autocompletion for your contracts functions calls and catch typing errors early, you can use Abiwan!
57+
58+
See [this guide](./automatic_cairo_ABI_parsing.md) for more details.

0 commit comments

Comments
 (0)