Skip to content

Commit

Permalink
Merge pull request #594 from ulan/ulan/qrcode
Browse files Browse the repository at this point in the history
Add a QR code generator example
  • Loading branch information
jessiemongeon1 authored Jul 24, 2023
2 parents e977780 + 9ae92ed commit f85b099
Show file tree
Hide file tree
Showing 19 changed files with 8,630 additions and 0 deletions.
24 changes: 24 additions & 0 deletions rust/qrcode/.gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
# Various IDEs and Editors
.vscode/
.idea/
**/*~

# Mac OSX temporary files
.DS_Store
**/.DS_Store

# dfx temporary files
.dfx/

# generated files
src/declarations/

# rust
target/

# frontend code
node_modules/
dist/

# environment variables
.env
4 changes: 4 additions & 0 deletions rust/qrcode/Cargo.toml
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
[workspace]
members = [
"src/qrcode_backend",
]
66 changes: 66 additions & 0 deletions rust/qrcode/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,66 @@
# QR code generator on the Internet Computer

## Overview

This example shows that an Internet Computer dapp can perform a long-running computation, like image processing, in a single message execution.
This is possible due to a unique feature called Deterministic Time Slicing (DTS), which automatically divides long computations into smaller slices executed across multiple blocks.
Developers can write long-running code as usual and don't require anything special to take advantage of DTS, as demonstrated in this example.

You try the live version of the dapp running on the Internet Computer mainnet here: [https://khpe2-4qaaa-aaaao-a2fnq-cai.icp0.io/](https://khpe2-4qaaa-aaaao-a2fnq-cai.icp0.io/).

## Prerequisites
This example requires an installation of:

- [x] Install the [IC SDK](https://internetcomputer.org/docs/current/developer-docs/setup/install/index.mdx).
- [x] Install `node.js` to build the web frontend.
- [x] Clone this project to a local directory.

## Running locally

Start a local replica of the Internet Computer by running:

```
dfx start --background
```

You can omit the `--background` argument if you want to see log messages of the dapp.

Now you can build and deploy the dapp with a single command
```
dfx deploy
```

If you see any error, it might be worthwhile to consult the [developer forum](https://forum.dfinity.org/).
In case of successful deployment, you will see an output with local URLs:

```
Deployed canisters.
URLs:
Frontend canister via browser
qrcode_frontend: ...
Backend canister via Candid interface:
qrcode_backend: ...
```

Navigate to the frontend URL in your browser and you'll be able to interact with the dapp.

![Screenshot of the frontend UI](screenshot.png)


## How it works

The initial code of the dapp was autogenerated by `dfx` using the standard frontend/backend template.

The frontend consists of an HTML page with a form where users can enter text for the QR code and choose various options.
When the user clicks the "Generate!" button, a JavaScript handler initiates a call to the backend canister.
The heavy-lifting of this call is managed by `candid`, `js-agent`, and `dfx`, which automatically generates a JavaScript object from the backend's Candid interface.
That object contains `async` functions for each of the backend's endpoints, and the button handler uses them to make the calls.

The backend, written in Rust, uses the `qrcode-generator`` and `image`` crates to create a QR code from user text.
It also performs some image processing, to add the Internet Computer logo and a color gradient to the final result.
Note the amount of computational work may be significant for large images.

For educational purposes the backend offers two public endpoint for QR code generation: one for updates and another for queries.
Currently, DTS is supported for updates, but not for queries.
As a result, the update endpoint has larger instruction limit compared to the query endpoint and thus can handle larger images.

30 changes: 30 additions & 0 deletions rust/qrcode/dfx.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
{
"canisters": {
"qrcode_backend": {
"candid": "src/qrcode_backend/qrcode_backend.did",
"package": "qrcode_backend",
"type": "rust"
},
"qrcode_frontend": {
"dependencies": [
"qrcode_backend"
],
"frontend": {
"entrypoint": "src/qrcode_frontend/src/index.html"
},
"source": [
"src/qrcode_frontend/assets",
"dist/qrcode_frontend/"
],
"type": "assets"
}
},
"defaults": {
"build": {
"args": "",
"packtool": ""
}
},
"output_env_file": ".env",
"version": 1
}
Loading

0 comments on commit f85b099

Please sign in to comment.