You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
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).
4
4
5
5
This sample project assumes that you are using the default cycles wallet canister that is created for you.
6
6
@@ -16,141 +16,25 @@ This example consists of the following functions (see `src/hello_cycles/main.mo`
16
16
The wallet's `wallet_receive` return type differs from hello_cycle's `wallet_receive`.
17
17
:::
18
18
19
-
This is a Motoko example that does not currently have a Rust variant.
-[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
26
24
27
-
Begin by opening a terminal window.
25
+
### 1. [Download and install the IC SDK.](https://internetcomputer.org/docs/building-apps/getting-started/install)
28
26
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/)
30
28
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.
32
30
31
+
### 4. Deploy the project to your local environment:
33
32
34
-
```bash
35
-
cd examples/motoko/hello_cycles
36
-
dfx start --background
37
33
```
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:
## 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
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
105
35
```
106
36
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
-
150
37
## Security considerations and best practices
151
38
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.
0 commit comments