Skip to content

Commit c22b156

Browse files
Merge pull request #1178 from dfinity/jessiemongeon1-patch-9
add: Deploy to ICP Ninja + Standardize README
2 parents 88e480f + 7ac4c4c commit c22b156

File tree

1 file changed

+10
-126
lines changed

1 file changed

+10
-126
lines changed

motoko/hello_cycles/README.md

Lines changed: 10 additions & 126 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
# Hello, cycles!
22

3-
The `hello_cycle`s sample project provides a simple example to illustrate how you might add functions to receive cycles, transfer cycles, and check your cycle balance with a simple Motoko actor (canister).
3+
The `hello_cycles` sample project provides a simple example to illustrate how you might add functions to receive cycles, transfer cycles, and check your cycle balance with a simple Motoko actor (canister).
44

55
This sample project assumes that you are using the default cycles wallet canister that is created for you.
66

@@ -16,141 +16,25 @@ This example consists of the following functions (see `src/hello_cycles/main.mo`
1616
The wallet's `wallet_receive` return type differs from hello_cycle's `wallet_receive`.
1717
:::
1818

19-
This is a Motoko example that does not currently have a Rust variant.
19+
## Deploying from ICP Ninja
2020

21-
## Prerequisites
22-
This example requires an installation of:
21+
[![](https://icp.ninja/assets/open.svg)](https://icp.ninja/editor?g=https://github.com/dfinity/examples/tree/master/motoko/hello_cycles)
2322

24-
- [x] Install the [IC SDK](https://internetcomputer.org/docs/current/developer-docs/setup/install/index.mdx).
25-
- [x] Clone the example dapp project: `git clone https://github.com/dfinity/examples`
23+
## Build and deploy from the command-line
2624

27-
Begin by opening a terminal window.
25+
### 1. [Download and install the IC SDK.](https://internetcomputer.org/docs/building-apps/getting-started/install)
2826

29-
## Step 1: Setup the project environment
27+
### 2. Download your project from ICP Ninja using the 'Download files' button on the upper left corner, or [clone the GitHub examples repository.](https://github.com/dfinity/examples/)
3028

31-
Navigate into the folder containing the project's files and start a local instance of the Internet Computer with the commands:
29+
### 3. Navigate into the project's directory.
3230

31+
### 4. Deploy the project to your local environment:
3332

34-
```bash
35-
cd examples/motoko/hello_cycles
36-
dfx start --background
3733
```
38-
39-
## Step 2: Deploy the canister
40-
41-
```bash
42-
dfx deploy
43-
```
44-
45-
## Step 3: Check that the current cycles balance of the `hello_cycles` canister
46-
47-
```bash
48-
dfx canister call hello_cycles wallet_balance
49-
```
50-
51-
The output should resemble the following:
52-
53-
```bash
54-
(3_091_662_816_985 : nat)
55-
```
56-
57-
You can also see the cycles balance of `hello_cycles` (or any canister you control) by calling:
58-
59-
```bash
60-
dfx canister status hello_cycles
61-
```
62-
63-
The output of this command will be similar to:
64-
65-
```bash
66-
Canister status call result for hello_cycles.
67-
Status: Running
68-
Controllers: 2vxsx-fae b77ix-eeaaa-aaaaa-qaada-cai
69-
Memory allocation: 0
70-
Compute allocation: 0
71-
Freezing threshold: 2_592_000
72-
Memory Size: Nat(2371252)
73-
Balance: 3_092_278_099_590 Cycles
74-
Module hash: 0x09198be65e161bdb5c75c705dfec4b707a8091ac5d1a095dd45c025142a1fc43
75-
```
76-
77-
## Step 4: Display the default wallet and hello_cycles canister principals
78-
79-
```bash
80-
dfx identity get-wallet
81-
dfx canister id hello_cycles
82-
```
83-
84-
The output should resemble the following:
85-
86-
```bash
87-
b77ix-eeaaa-aaaaa-qaada-cai
88-
dzh22-nuaaa-aaaaa-qaaoa-cai
89-
```
90-
91-
Below, we'll frequently use `$(dfx identity get-wallet)` and `$(dfx canister id hello_cycles)` to splice canister principals into longer bash commands.
92-
93-
## Step 5: Attempt to send 2 trillion cycles from the default wallet to the hello_cycles canister
94-
95-
```bash
96-
dfx canister call $(dfx identity get-wallet) wallet_send "(record { canister = principal \"$(dfx canister id hello_cycles)\"; amount = (2000000000000:nat64); } )"
97-
```
98-
99-
The wallet's `wallet_send` function transfers the amount to the argument canister's `wallet_receive` function (see above) and returns a result signaling success or failure.
100-
101-
If successful, the output will look similar to:
102-
103-
```bash
104-
(variant { 17_724 })
34+
dfx start --background --clean && dfx deploy
10535
```
10636

107-
## Step 6: Verify that the cycles balance for the hello_cycles canister has increased by 10_000_000
108-
109-
```bash
110-
dfx canister call hello_cycles wallet_balance
111-
```
112-
113-
Output:
114-
115-
```bash
116-
(5_091_662_569_379 : nat)
117-
```
118-
119-
The amount is only increased by 10_000_000 because the implementation of `wallet_receive` is coded to accept at most 10_000_000 cycles, even when more cycles were transferred with the call. The unaccepted cycles are not lost but implicitly refunded to the caller (in this case, the wallet).
120-
121-
## Step 7: Send some cycles from the hello_cycles canister back to the wallet
122-
123-
```bash
124-
dfx canister call hello_cycles transfer "(func \"$(dfx identity get-wallet)\".\"wallet_receive\", 5000000)"
125-
```
126-
127-
Output:
128-
129-
```bash
130-
(record { refunded = 0 : nat })
131-
```
132-
133-
## Step 8: Verify that the cycles balance of the `hello_cycles` canister has decreased
134-
135-
```bash
136-
dfx canister call hello_cycles wallet_balance
137-
```
138-
139-
Output:
140-
141-
```bash
142-
(5_091_657_208_987 : nat)
143-
```
144-
145-
In this step, we are passing our own wallet's `wallet_receive` function as the first argument, followed by the amount. We, or a third party, could also pass any other function of the same signature, belonging to any other canister or wallet.
146-
147-
Without some additional access control checks (omitted here), a malicious client could abuse our naive transfer function to drain the canister of all of its cycles.
148-
149-
15037
## Security considerations and best practices
15138

152-
If you base your application on this example, we recommend you familiarize yourself with and adhere to the [security best practices](https://internetcomputer.org/docs/current/references/security/) for developing on the Internet Computer. This example may not implement all the best practices.
153-
154-
For example, the following aspect is particularly relevant for this app:
155-
* [Protect against draining the cycles balance](https://internetcomputer.org/docs/current/developer-docs/security/security-best-practices/overview), since this canister consumes cycles that are being transferred to other canisters.
39+
If you base your application on this example, it is recommended that you familiarize yourself with and adhere to the [security best practices](https://internetcomputer.org/docs/building-apps/security/overview) for developing on ICP. This example may not implement all the best practices.
15640

0 commit comments

Comments
 (0)