Skip to content

Commit

Permalink
feat(docs): Add Typescript SDK examples (#3566)
Browse files Browse the repository at this point in the history
* improve readability of print statements

* Add address-unlock-condition typescript sdk example

* format comments

* Add alias-migration typescript sdk example

* refactor print statements conform guideline

* Add alias-output-claim typescript sdk example

* Add basic-output-claim typescript sdk example

* Add check-basic-output-unlock-conditions typescript sdk example

* Add foundry-output-claim typescript sdk example

* Add iota-self-sponsor typescript sdk example

* Add nft-migration typescript sdk example

* Add nft-output-claim typescript sdk example

* feat(docs): Add typescript sdk example snippets in docs

* chore(docs): Add missing copyright header to utils.ts

* fix(docs): Add iota binary exists check

* chore(docs): Swap typescript and rust tabs

* chore(docs): Synchronize comments between rust and typescript examples

* chore(docs): Adjust line numbers of example code snippets
  • Loading branch information
nonast authored Nov 8, 2024
1 parent 3c64aa4 commit f59220d
Show file tree
Hide file tree
Showing 27 changed files with 1,907 additions and 104 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -22,55 +22,62 @@ For this example, we use an `AliasOutput` to extract an `Alias` object that owns
1. The first step is to fetch the `AliasOutput` object that is needed to claim the `NftOutput`.

<Tabs groupId="language">
<TabItem value="typescript" label="TypeScript">

```typescript file=<rootDir>/docs/examples/typescript/src/stardust/address-unlock-condition.ts#L38-L44
```
</TabItem>
<TabItem value="rust" label="Rust">

```rust file=<rootDir>/docs/examples/rust/stardust/address-unlock-condition.rs#L74-L92
```

</TabItem>
<TabItem value="typescript" label="TypeScript">

</TabItem>
</Tabs>

2. Use the dynamic field function with the "alias" dynamic field key filter to gather the `Alias` object itself.

<Tabs groupId="language">
<TabItem value="typescript" label="TypeScript">
```typescript file=<rootDir>/docs/examples/typescript/src/stardust/address-unlock-condition.ts#L46-L64
```


</TabItem>
<TabItem value="rust" label="Rust">

```rust file=<rootDir>/docs/examples/rust/stardust/address-unlock-condition.rs#L94-L107
```

</TabItem>
<TabItem value="typescript" label="TypeScript">

</TabItem>
</Tabs>

3. Some objects are owned by the `Alias` object. In this case, filter them by type using the `NftOutput` type tag. Apply the filter to get `NftOutput`s owned by the `Alias`.

<Tabs groupId="language">
<TabItem value="typescript" label="TypeScript">
```typescript file=<rootDir>/docs/examples/typescript/src/stardust/address-unlock-condition.ts#L66-L84
```
</TabItem>
<TabItem value="rust" label="Rust">

```rust file=<rootDir>/docs/examples/rust/stardust/address-unlock-condition.rs#L109-L132
```

</TabItem>
<TabItem value="typescript" label="TypeScript">

</TabItem>
</Tabs>

4. Create a PTB that first extracts the assets from the `AliasOutput`, and then uses the extracted `Alias` to "address unlock" the `NftOutput` using the function `unlock_alias_address_owned_nft`.

<Tabs groupId="language">
<TabItem value="typescript" label="TypeScript">
```typescript file=<rootDir>/docs/examples/typescript/src/stardust/address-unlock-condition.ts#L86-L166
```
</TabItem>
<TabItem value="rust" label="Rust">

```rust file=<rootDir>/docs/examples/rust/stardust/address-unlock-condition.rs#L134-L243
```

</TabItem>
<TabItem value="typescript" label="TypeScript">

</TabItem>
</Tabs>
32 changes: 18 additions & 14 deletions docs/content/developer/stardust/claiming/alias.mdx
Original file line number Diff line number Diff line change
Expand Up @@ -23,15 +23,16 @@ A Governor address can claim the `AliasOutput` assets at any time:
The first step is to fetch the `AliasOutput` object that needs to be claimed.

<Tabs groupId="language">
<TabItem value="rust" label="Rust">
<TabItem value="typescript" label="TypeScript">

```rust file=<rootDir>/docs/examples/rust/stardust/alias-output-claim.rs#L56-L81
```typescript file=<rootDir>/docs/examples/typescript/src/stardust/alias-output-claim.ts#L24-L38
```

</TabItem>
<TabItem value="typescript" label="TypeScript">
<TabItem value="rust" label="Rust">

Coming soon
```rust file=<rootDir>/docs/examples/rust/stardust/alias-output-claim.rs#L56-L81
```

</TabItem>
</Tabs>
Expand All @@ -42,15 +43,16 @@ Next, check the native tokens that might be held by this output. A [`Bag`](../..


<Tabs groupId="language">
<TabItem value="rust" label="Rust">
<TabItem value="typescript" label="TypeScript">

```rust file=<rootDir>/docs/examples/rust/stardust/alias-output-claim.rs#L83-L110
```typescript file=<rootDir>/docs/examples/typescript/src/stardust/alias-output-claim.ts#L43-L62
```

</TabItem>
<TabItem value="typescript" label="TypeScript">
<TabItem value="rust" label="Rust">

Coming soon
```rust file=<rootDir>/docs/examples/rust/stardust/alias-output-claim.rs#L83-L110
```

</TabItem>
</Tabs>
Expand All @@ -63,14 +65,15 @@ An `AliasOutput` differs from an `NftOutput` or a `BasicOutput` because it conta
The main purpose of claiming is to extract the `Alias` object from the `AliasOutput`.

<Tabs groupId="language">
<TabItem value="typescript" label="TypeScript">
```typescript file=<rootDir>/docs/examples/typescript/src/stardust/alias-output-claim.ts#L64-L117
```
</TabItem>
<TabItem value="rust" label="Rust">

```rust file=<rootDir>/docs/examples/rust/stardust/alias-output-claim.rs#L112-L180
```

</TabItem>
<TabItem value="typescript" label="TypeScript">

</TabItem>
</Tabs>

Expand Down Expand Up @@ -104,13 +107,14 @@ Also, the `nft.move` module was extended with the following function:
Once the package is prepared, you can extract and use a Stardust `Alias` in a single transaction to create a `CollectionControllerCap`. This capability is then used in later transactions to manage new collections.

<Tabs groupId="language">
<TabItem value="typescript" label="TypeScript">
```typescript file=<rootDir>/docs/examples/typescript/src/stardust/alias-migration.ts#L72-L168
```
</TabItem>
<TabItem value="rust" label="Rust">

```rust file=<rootDir>/docs/examples/rust/stardust/alias-migration.rs#L119-L244
```

</TabItem>
<TabItem value="typescript" label="TypeScript">

</TabItem>
</Tabs>
36 changes: 20 additions & 16 deletions docs/content/developer/stardust/claiming/basic.mdx
Original file line number Diff line number Diff line change
Expand Up @@ -21,15 +21,16 @@ As a user, you may own `BasicOutput` objects that need to be unlocked before you
To begin, you need to determine if your `BasicOutput` can be unlocked. You can achieve this by performing specific off-chain queries that will check the unlock conditions for the `BasicOutput`.

<Tabs groupId="language">
<TabItem value="rust" label="Rust">
<TabItem value="typescript" label="TypeScript">

```rust file=<rootDir>/docs/examples/rust/stardust/check-basic-output-unlock-conditions.rs#L20-L56
```typescript file=<rootDir>/docs/examples/typescript/src/stardust/check-basic-output-unlock-conditions.ts#L17-L49
```

</TabItem>
<TabItem value="typescript" label="TypeScript">
<TabItem value="rust" label="Rust">

Coming soon
```rust file=<rootDir>/docs/examples/rust/stardust/check-basic-output-unlock-conditions.rs#L20-L56
```

</TabItem>
</Tabs>
Expand All @@ -43,15 +44,16 @@ Once you've confirmed that the `BasicOutput` can be unlocked, you can start the
The first step is to fetch the `BasicOutput` object that you intend to claim.

<Tabs groupId="language">
<TabItem value="rust" label="Rust">
<TabItem value="typescript" label="TypeScript">

```rust file=<rootDir>/docs/examples/rust/stardust/basic-output-claim.rs#L56-L81
```typescript file=<rootDir>/docs/examples/typescript/src/stardust/basic-output-claim.ts#L25-L42
```

</TabItem>
<TabItem value="typescript" label="TypeScript">
<TabItem value="rust" label="Rust">

Coming soon
```rust file=<rootDir>/docs/examples/rust/stardust/basic-output-claim.rs#L56-L81
```

</TabItem>
</Tabs>
Expand All @@ -66,15 +68,16 @@ these keys are strings representing the [`OTW`](../../iota-101/move-overview/one


<Tabs groupId="language">
<TabItem value="rust" label="Rust">
<TabItem value="typescript" label="TypeScript">

```rust file=<rootDir>/docs/examples/rust/stardust/basic-output-claim.rs#L83-L110
```typescript file=<rootDir>/docs/examples/typescript/src/stardust/basic-output-claim.ts#L47-L66
```

</TabItem>
<TabItem value="typescript" label="TypeScript">
<TabItem value="rust" label="Rust">

Coming soon
```rust file=<rootDir>/docs/examples/rust/stardust/basic-output-claim.rs#L83-L110
```

</TabItem>
</Tabs>
Expand All @@ -85,15 +88,16 @@ Finally, you can create a Programmable Transaction Block (PTB) using the `basic_
along with the `Bag` keys to iterate over the extracted native tokens.

<Tabs groupId="language">
<TabItem value="rust" label="Rust">
<TabItem value="typescript" label="TypeScript">

```rust file=<rootDir>/docs/examples/rust/stardust/basic-output-claim.rs#L113-L177
```typescript file=<rootDir>/docs/examples/typescript/src/stardust/basic-output-claim.ts#L68-L118
```

</TabItem>
<TabItem value="typescript" label="TypeScript">
<TabItem value="rust" label="Rust">

Coming soon
```rust file=<rootDir>/docs/examples/rust/stardust/basic-output-claim.rs#L113-L177
```

</TabItem>
</Tabs>
46 changes: 25 additions & 21 deletions docs/content/developer/stardust/claiming/foundry.mdx
Original file line number Diff line number Diff line change
Expand Up @@ -17,15 +17,16 @@ Instead, claiming a Foundry Output involves extracting a [`CoinManagerTreasuryCa
The first step is to retrieve the `AliasOutput` object that you intend to claim.

<Tabs groupId="language">
<TabItem value="rust" label="Rust">
<TabItem value="typescript" label="TypeScript">

```rust file=<rootDir>/docs/examples/rust/stardust/foundry-output-claim.rs#L60-L74
```typescript file=<rootDir>/docs/examples/typescript/src/stardust/foundry-output-claim.ts#L33-L39
```

</TabItem>
<TabItem value="typescript" label="TypeScript">
<TabItem value="rust" label="Rust">

Coming soon
```rust file=<rootDir>/docs/examples/rust/stardust/foundry-output-claim.rs#L60-L74
```

</TabItem>
</Tabs>
Expand All @@ -35,15 +36,16 @@ Coming soon
Next, use the dynamic field function with the "alias" dynamic field key filter to obtain the `Alias` object itself.

<Tabs groupId="language">
<TabItem value="rust" label="Rust">
<TabItem value="typescript" label="TypeScript">

```rust file=<rootDir>/docs/examples/rust/stardust/foundry-output-claim.rs#L76-L89
```typescript file=<rootDir>/docs/examples/typescript/src/stardust/foundry-output-claim.ts#L41-L59
```

</TabItem>
<TabItem value="typescript" label="TypeScript">
<TabItem value="rust" label="Rust">

Coming soon
```rust file=<rootDir>/docs/examples/rust/stardust/foundry-output-claim.rs#L76-L89
```

</TabItem>
</Tabs>
Expand All @@ -53,15 +55,16 @@ Coming soon
The `Alias` object may own various other objects (for more details, refer to the [Output Unlockable by an Alias/Nft Address](address-unlock-condition.mdx) page). In this step, you filter these objects by type, specifically looking for the `CoinManagerTreasuryCap` type.

<Tabs groupId="language">
<TabItem value="rust" label="Rust">
<TabItem value="typescript" label="TypeScript">

```rust file=<rootDir>/docs/examples/rust/stardust/foundry-output-claim.rs#L91-L131
```typescript file=<rootDir>/docs/examples/typescript/src/stardust/foundry-output-claim.ts#L61-L86
```

</TabItem>
<TabItem value="typescript" label="TypeScript">
<TabItem value="rust" label="Rust">

Coming soon
```rust file=<rootDir>/docs/examples/rust/stardust/foundry-output-claim.rs#L91-L131
```

</TabItem>
</Tabs>
Expand All @@ -71,15 +74,16 @@ Coming soon
Since each native token is tied to its own package, a Foundry's native token has a specific [`OTW`](../../iota-101/move-overview/one-time-witness.mdx). Here, you will extract the `OTW` from the `CoinManagerTreasuryCap` object.

<Tabs groupId="language">
<TabItem value="rust" label="Rust">
<TabItem value="typescript" label="TypeScript">

```rust file=<rootDir>/docs/examples/rust/stardust/foundry-output-claim.rs#L133-L142
```typescript file=<rootDir>/docs/examples/typescript/src/stardust/foundry-output-claim.ts#L88-L91
```

</TabItem>
<TabItem value="typescript" label="TypeScript">
<TabItem value="rust" label="Rust">

Coming soon
```rust file=<rootDir>/docs/examples/rust/stardust/foundry-output-claim.rs#L133-L142
```

</TabItem>
</Tabs>
Expand All @@ -89,15 +93,15 @@ Coming soon
Finally, you should create a Programmable Transaction Block (PTB) that claims the `CoinManagerTreasuryCap` associated with the Foundry Output from the `AliasOutput` using the `unlock_alias_address_owned_coinmanager_treasury` function.

<Tabs groupId="language">
<TabItem value="rust" label="Rust">
<TabItem value="typescript" label="TypeScript">

```rust file=<rootDir>/docs/examples/rust/stardust/foundry-output-claim.rs#L144-L215
```typescript file=<rootDir>/docs/examples/typescript/src/stardust/foundry-output-claim.ts#L93-L143
```

</TabItem>
<TabItem value="typescript" label="TypeScript">
<TabItem value="rust" label="Rust">

Coming soon
```rust file=<rootDir>/docs/examples/rust/stardust/foundry-output-claim.rs#L144-L215
```

</TabItem>
</Tabs>
Expand Down
Loading

0 comments on commit f59220d

Please sign in to comment.