Skip to content

Commit

Permalink
Sync dfinity/examples with original repo
Browse files Browse the repository at this point in the history
  • Loading branch information
kristoferlund committed Oct 2, 2023
1 parent 28d45a4 commit afa3ca6
Show file tree
Hide file tree
Showing 40 changed files with 7,185 additions and 5,056 deletions.
3 changes: 3 additions & 0 deletions motoko/ic-pos/.eslintrc.cjs
Original file line number Diff line number Diff line change
Expand Up @@ -11,4 +11,7 @@ module.exports = {
rules: {
'react-refresh/only-export-components': 'warn',
},
globals: {
process: "readonly"
},
}
Binary file removed motoko/ic-pos/.yarn/install-state.gz
Binary file not shown.
73 changes: 31 additions & 42 deletions motoko/ic-pos/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@

# IC-POS

IC-POS is an experimental app to demonstrate a real world use case for ckBTC on the Internet Computer. It is a simple Point of Sale app that allows users to accept ckBTC payments.
IC-POS is an experimental app to demonstrate a real world use case for [ckBTC](https://internetcomputer.org/ckbtc/) on the Internet Computer. It is a simple Point of Sale app that allows users to accept ckBTC payments.

The Internet Computer [integrates directly with the Bitcoin network](https://internetcomputer.org/docs/current/developer-docs/integrations/bitcoin/). This allows canisters on the Internet Computer to receive, hold, and send Bitcoin, all directly with transactions on the Bitcoin network. Chain-key Bitcoin (ckBTC) is an ICRC-1-compliant token that is backed 1:1 by Bitcoin held 100% on the IC mainnet.

Expand All @@ -11,7 +11,7 @@ For deeper understanding of the ICP < > BTC integration, see the IC wiki article
## Features

- **Create store**: Users logs in with their Internet Identity and configure the store with a name and other settings.
- **Charge customer**: Users can charge a customer by entering an amount. This will generate and display a QR code for the customer to scan and pay. QR code follows the ICR-22 standard.
- **Charge customer**: Users can charge a customer by entering an amount. This will generate and display a QR code for the customer to scan and pay. QR code follows the [ICRC-22](https://github.com/dfinity/ICRC/issues/22) standard.
- **Send tokens**: Users can send ckBTC tokens to other users.
- **Receive notifications**: Users can choose to receive notifications by email or SMS when a payment is received. This uses the [HTTP Outcall](https://internetcomputer.org/docs/current/developer-docs/integrations/https-outcalls/) feature of the Internet Computer.
- **Transaction history**: Users can view a list of transactions made to the store.
Expand Down Expand Up @@ -54,11 +54,12 @@ The frontend interacts with the following IC canisters:
## Prerequisites

- [x] Install the [IC SDK](https://internetcomputer.org/docs/current/developer-docs/setup/install/index.mdx).
- [x] Install [Node.js](https://nodejs.org/en/).

### Step 1: Start a local instance of the Internet Computer

```bash
dfx start --background
dfx start --clean --background
```

### Step 2: Deploy the Internet Identity canister
Expand All @@ -85,19 +86,29 @@ The ckBTC ledger canister is already deployed on the IC mainnet. ckBTC implement

Take a moment to read the details of the call we are making below. Not only are we deploying the ledger canister, we are also:

- Deploying the canister to the same canister ID as the mainnet ledger canister. This is to make it easier to switch between local and mainnet deployments.
- Naming the token `Local ckBTC / LCKBTC`
- Setting the owner principal to the principal we saved in the previous step.
- Minting 100_000_000_000 tokens to the owner principal.
- Setting the transfer fee to 10 LCKBTC.

```bash
dfx deploy --network local icrc1-ledger --argument '
dfx deploy --network local --specified-id mxzaz-hqaaa-aaaar-qaada-cai icrc1_ledger --argument '
(variant {
Init = record {
token_name = "Local ckBTC";
token_symbol = "LCKBTC";
minting_account = record { owner = principal "'${OWNER}'";};
initial_balances = vec { record { record { owner = principal "'${OWNER}'";}; 100_000_000_000; }; };
minting_account = record {
owner = principal "'${OWNER}'";
};
initial_balances = vec {
record {
record {
owner = principal "'${OWNER}'";
};
100_000_000_000;
};
};
metadata = vec {};
transfer_fee = 10;
archive_options = record {
Expand All @@ -110,27 +121,19 @@ dfx deploy --network local icrc1-ledger --argument '
'
```

### Step 4: Save the ledger principal as a variable

We need this information in the next step, when deploying the index canister.

```bash
export LEDGER_PRINCIPAL=$(dfx canister --network local id icrc1-ledger)
```

### Step 5: Deploy the index canister
### Step 4: Deploy the index canister

The index canister syncs the ledger transactions and indexes them by account.

```bash
dfx deploy --network local icrc1-index --argument '
dfx deploy --network local icrc1_index --argument '
record {
ledger_id = (principal "'${LEDGER_PRINCIPAL}'");
ledger_id = (principal "mxzaz-hqaaa-aaaar-qaada-cai");
}
'
```

### Step 6: Deploy the icpos canister
### Step 5: Deploy the icpos canister

The icpos canister manages the store configuration and sends notifications when a payment is received.

Expand All @@ -140,53 +143,39 @@ The `--argument '(0)'` argument is used to initialize the canister with `startBl
dfx deploy --network local icpos --argument '(0)'
```

### Step 7: Configure the icpos canister

The icpos canister needs to be configured with the ledger id to be able to monitor for new transactions and send notifications.

```bash
dfx canister --network local call icpos setLedgerId "${LEDGER_PRINCIPAL}"
```
### Step 6: Configure the icpos canister

ic-pos uses [Courier](https://courier.com/) to send email and SMS notifications. If you want to enable notifications, you need to sign up for a Courier account and and create and API key. Then issue the following command:

```bash
dfx canister --network local call icpos setCourierApiKey "pk_prod_..."
```

### Step 8 - Setup `.env` for the frontend environment

The frontend needs information about how to access the canisters. This is done by setting environment variables. Copy the `.env.template` file and rename it to `.env`. Then update the values to match your local setup.

```bash
cp src/.env.template src/.env
```

### Step 9: Build and run the frontend
### Step 7: Build and run the frontend

Run yarn to install dependencies and start the frontend. The frontend will be available at http://localhost:5173.
Run npm to install dependencies and start the frontend. The frontend will be available at http://localhost:5173.

```bash
yarn
yarn dev
npm install
npm run dev
```

Why don't we deploy the frontend as a local canister? Vite uses lazy loading of modules. This does not work when deploying to a local canister. When deploying to the IC mainnet, this is not an issue. Also, running using `yarn dev` allows for hot reloading of the frontend code when making changes.
Why don't we deploy the frontend as a local canister? Vite uses lazy loading of modules. This does not work when deploying to a local canister. When deploying to the IC mainnet, this is not an issue. Also, running using `npm run dev` allows for hot reloading of the frontend code when making changes.

### Step 10: Make a transfer!
### Step 8: Make a transfer!

Now that everything is up and running, you can make a transfer to your newly created store.

Transfers made from the owner principal will not trigger notifications in the UI since they are regarded as `mint` transactions. To test notifications, you need to make a transfer from another principal.

The easiest way to do this is to create two stores using two different Internet Identity accounts, using two different web browsers. Then, transfer some tokens from one store to the other.

#### 10.1: Create the first store and supply it with some tokens
#### 8.1: Create the first store and supply it with some tokens

Log in to the frontend using the Internet Identity. Configure the store and navigate to the `Receive` page. Click on the principal pill to copy the address to your clipboard. Then, using the `dfx` command, mint some tokens from your owner principal to the store principal.

```bash
dfx canister --network local call icrc1-ledger icrc1_transfer '
dfx canister --network local call icrc1_ledger icrc1_transfer '
(record {
to=(record {
owner=(principal "[STORE PRINCIPAL 1 HERE]")
Expand All @@ -196,7 +185,7 @@ dfx canister --network local call icrc1-ledger icrc1_transfer '
'
```

#### 10.2: Create the second store
#### 8.2: Create the second store

Log in to the frontend using **a new Internet Identity on another web browser**. Configure the store and navigate to the `Receive` page. Click on the principal pill to copy the address to your clipboard.

Expand Down
20 changes: 7 additions & 13 deletions motoko/ic-pos/dfx.json
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,8 @@
"canisters": {
"icpos": {
"main": "src/icpos/main.mo",
"type": "motoko"
"type": "motoko",
"dependencies": ["icrc1_ledger"]
},
"icpos_frontend": {
"dependencies": ["icpos"],
Expand All @@ -11,20 +12,19 @@
},
"source": ["dist"],
"type": "assets",
"build": ["yarn run build"]
"build": ["npm run build"]
},
"internet_identity": {
"type": "custom",
"candid": "https://github.com/dfinity/internet-identity/releases/latest/download/internet_identity.did",
"wasm": "https://github.com/dfinity/internet-identity/releases/latest/download/internet_identity_dev.wasm.gz",
"remote": {
"id": {
"ic": "be2us-64aaa-aaaaa-qaabq-cai"
"ic": "rdmx6-jaaaa-aaaaa-aaadq-cai"
}
},
"frontend": {}
}
},
"icrc1-ledger": {
"icrc1_ledger": {
"type": "custom",
"candid": "https://raw.githubusercontent.com/dfinity/ic/95787355499f3be929f2ab302ed44cdad7c64061/rs/rosetta-api/icrc1/ledger/ledger.did",
"wasm": "https://download.dfinity.systems/ic/95787355499f3be929f2ab302ed44cdad7c64061/canisters/ic-icrc1-ledger.wasm.gz",
Expand All @@ -34,7 +34,7 @@
}
}
},
"icrc1-index": {
"icrc1_index": {
"type": "custom",
"candid": "https://raw.githubusercontent.com/dfinity/ic/95787355499f3be929f2ab302ed44cdad7c64061/rs/rosetta-api/icrc1/index/index.did",
"wasm": "https://download.dfinity.systems/ic/95787355499f3be929f2ab302ed44cdad7c64061/canisters/ic-icrc1-index.wasm.gz",
Expand All @@ -51,12 +51,6 @@
"packtool": ""
}
},
"networks": {
"local": {
"bind": "127.0.0.1:8000",
"type": "ephemeral"
}
},
"output_env_file": ".env",
"version": 1
}
Loading

0 comments on commit afa3ca6

Please sign in to comment.