Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add examples submodules #2417

Closed
wants to merge 4 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 3 additions & 0 deletions .gitmodules
Original file line number Diff line number Diff line change
Expand Up @@ -15,3 +15,6 @@
path = submodules/interface-spec
url = https://github.com/dfinity/interface-spec.git
branch = public
[submodule "submodules/examples"]
path = submodules/examples
url = https://github.com/dfinity/examples.git
6 changes: 3 additions & 3 deletions docs/developer-docs/setup/first-canister.mdx
Original file line number Diff line number Diff line change
Expand Up @@ -223,13 +223,13 @@ By default, the `dfx.json` file will contain automatically generated configurati

## Reviewing the default program code

The backend canister's code will be located in the `src/hello_world_backend` subdirectory.
The backend canister's code will be located in the `src/hello_backend` subdirectory.

<Tabs groupId="language">
<TabItem value="motoko" label="Motoko" default>

```motoko
///src/hello_world_backend/main.mo
///src/hello_backend/main.mo
actor {
public query func greet(name : Text) : async Text {
return "Hello, " # name # "!";
Expand All @@ -241,7 +241,7 @@ public query func greet(name : Text) : async Text {
<TabItem value="rust" label="Rust">

```rust
///src/hello_world_backend/src/lib.rs
///src/hello_backend/src/lib.rs
#[ic_cdk::query]
fn greet(name: String) -> String {
format!("Hello, {}!", name)
Expand Down
254 changes: 254 additions & 0 deletions docs/developer-docs/setup/hello-world.mdx
Original file line number Diff line number Diff line change
@@ -0,0 +1,254 @@
# Hello, world!

This guide shows how to get started developing on ICP quickly by deploying your first 'Hello, world!' smart contract.

## Install dependencies

### Install the IC SDK (v0.15.2 and newer)

```bash
sh -ci "$(curl -fsSL https://internetcomputer.org/install.sh)"
```

### Install Node.js

```bash
curl -o- https://raw.githubusercontent.com/nvm-sh/nvm/v0.39.3/install.sh | bash
nvm install 18
```

## Start the local replica

```bash
dfx start
```

## Create a new project

<Tabs groupId="language">
<TabItem value="motoko" label="Motoko" default>

```bash
dfx new hello
cd hello
npm install
```

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

```bash
dfx new hello --type=rust
cd hello
npm install
```

</TabItem>

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

```bash
npx azle new azle_hello
cd hello
npm install
```

</TabItem>

<TabItem value="python" label="Python">

Create the project directory and file structure:

```bash
mkdir kybra_hello
cd kybra_hello

mkdir src

touch src/main.py
touch dfx.json
```

Create a virtual environment:

```bash
~/.pyenv/versions/3.10.7/bin/python -m venv venv
source venv/bin/activate
```

Install Kybra:

```bash
pip install kybra
```

</TabItem>
</Tabs>

For information about project structure and default configuration, [learn more about deploying your first canister](first-canister.mdx).

## Smart contract code

<Tabs groupId="language">
<TabItem value="motoko" label="Motoko" default>

```motoko
// src/hello_backend/main.mo
actor {
public query func greet(name : Text) : async Text {
return "Hello, " # name # "!";
};
};
```

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

```rust
// src/hello_world_backend/src/lib.rs
#[ic_cdk::query]
fn greet(name: String) -> String {
format!("Hello, {}!", name)
}
```

</TabItem>

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

```typescript
// azle_hello/src/index.ts
import { Canister, query, text, update, Void } from 'azle';

// This is a global variable that is stored on the heap
let message = '';

export default Canister({
// Query calls complete quickly because they do not go through consensus
getMessage: query([], text, () => {
return message;
}),
// Update calls take a few seconds to complete
// This is because they persist state changes and go through consensus
setMessage: update([text], Void, (newMessage) => {
message = newMessage; // This change will be persisted
})
});
```

</TabItem>

<TabItem value="python" label="Python">

Create the project directory and file structure:

```python
# kybra_hello/src/main.py

from kybra import query, update, void

# This is a global variable that is stored on the heap
message: str = ''

# Query calls complete quickly because they do not go through consensus
@query
def get_message() -> str:
return message

# Update calls take a few seconds to complete
# This is because they persist state changes and go through consensus
@update
def set_message(new_message: str) -> void:
global message
message = new_message # This change will be persisted
```

</TabItem>
</Tabs>


## Deploy locally

Deploy the project's canisters locally:

```
dfx deploy
```

## Call the canister

<Tabs groupId="language">
<TabItem value="motoko" label="Motoko" default>

```bash
dfx canister call hello_backend greet world
```

Output:

```
("Hello, world!")
```

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

```bash
dfx canister call hello_backend greet world
```

Output:

```
("Hello, world!")
```

</TabItem>

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

```bash
dfx canister call azle_hello setMessage '("Hello, world!")'
```

Then, call the `getMessage` function:

```
dfx canister call azle_hello getMessage
```

Output:

```
("Hello, world!")
```

</TabItem>

<TabItem value="python" label="Python">

```bash
dfx canister call kybra_hello set_message '("Hello, world!")'
```

Then, call the `getMessage` function:

```
dfx canister call kybra_hello getMessage
```

Output:

```
("Hello, world!")
```

</TabItem>
</Tabs>


## Next steps

- [Acquire cycles](cycles/cycles-faucet.md).

- [Deploy to the mainnet](deploy-mainnet.md).
5 changes: 5 additions & 0 deletions sidebars.js
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,11 @@ const sidebars = {
label: "What is the Internet Computer?",
id: "concepts/what-is-ic",
},
{
type: "doc",
label: "Hello, world!",
id: "developer-docs/setup/hello-world",
},
{
type: "doc",
label: "Installing the IC SDK",
Expand Down
1 change: 1 addition & 0 deletions submodules/examples
Submodule examples added at d6234d
Loading