letmejustputthishere / ic-pos

Accept Bitcoin payments (ckBTC) on the Internet Computer using this simple Point of Sale (POS) application.

Home Page:https://hngac-6aaaa-aaaal-qb6tq-cai.icp0.io/

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

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.

The Internet Computer integrates directly with the Bitcoin network. 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.

For deeper understanding of the ICP < > BTC integration, see the IC wiki article on Bitcoin integration.

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.
  • 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 feature of the Internet Computer.
  • Transaction history: Users can view a list of transactions made to the store.

Try it!

IC-POS is deployed on the Internet Computer. You can try it out here:

https://hngac-6aaaa-aaaal-qb6tq-cai.icp0.io/

Video.description.mp4

Architecture

Backend

The backend is written in Motoko and consist of one canister, icpos. It exposes four public methods:

  • getMerchant - returns the store configuration for a given principal.
  • updateMerchant - updates the store configuration for a given principal.
  • setCourierApiKey - sets the Courier API key used to send email and SMS notifications. Only the canister controller can call this method.
  • setLedgerId - sets the ledger id to monitor for transactions. Only the canister controller can call this method.
  • getLogs - The canister maintains a debug log that can be fetched using this method.

In addition to the public methods, the canister uses a timer to monitor ledger transactions. When a new transaction is found that matches a configured store – depending on the store settings – the canister will send a notification either by email or SMS.

Frontend

The frontend is written in Typescript/Vite/React/TailwindCSS. Users authenticate using the Internet Identity to access their store. The first time a user logs in, a store is created for them.

The frontend interacts with the following IC canisters:

  • icpos - to fetch and update store configuration.
  • ckbtc ledger - to send ckBTC to other users.
  • ckbtc index - to fetch transaction history.
  • internet identity - to authenticate users.

Local deployment

Prerequisites

Step 1: Start a local instance of the Internet Computer

dfx start --background

Step 2: Deploy the Internet Identity canister

Integration with the Internet Identity allows store owners to securely setup and manage their store. The Internet Identity canister is already deployed on the IC mainnet. For local development, you need to deploy it to your local instance of the IC.

dfx deploy --network local internet_identity

Step 3: Save current principal as a variable

The principal will be used when deploying the ledger canister.

export OWNER=$(dfx identity get-principal)

Step 3: Deploy the ckBTC ledger canister

The responsibilities of the ledger canister is to keep track of token balances and handle token transfers.

The ckBTC ledger canister is already deployed on the IC mainnet. ckBTC implements the ICRC-1 token standard. For local development, we deploy the ledger for an ICRC-1 token mimicking the mainnet setup.

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:

  • 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.
dfx deploy --network local 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; }; };
      metadata = vec {};
      transfer_fee = 10;
      archive_options = record {
        trigger_threshold = 2000;
        num_blocks_to_archive = 1000;
        controller_id = principal "'${OWNER}'";
      }
    }
  })
'

Step 4: Save the ledger principal as a variable

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

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

Step 5: Deploy the index canister

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

dfx deploy --network local icrc1-index --argument '
  record {
   ledger_id = (principal "'${LEDGER_PRINCIPAL}'");
  }
'

Step 6: Deploy the icpos canister

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

The --argument '(0)' argument is used to initialize the canister with startBlock set to 0. This is used to tell the canister to start monitoring the ledger from block 0. When deploying to the IC mainnet, this should be set to the current block height to prevent the canister from processing old transactions.

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.

dfx canister --network local call icpos setLedgerId "${LEDGER_PRINCIPAL}"

ic-pos uses Courier 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:

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.

cp src/.env.template src/.env

Step 9: Build and run the frontend

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

yarn
yarn 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.

Step 10: 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

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.

dfx canister --network local call icrc1-ledger icrc1_transfer '
  (record {
    to=(record {
      owner=(principal "[STORE PRINCIPAL 1 HERE]")
    });
    amount=100_000
  })
'

10.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.

Now, go back to the first browser/store, navigate to the Send page and transfer some tokens to the second store.

If everything is working, you should see a notification in the second store.

🎉

Possible Improvements

  • Login state is not persisted. Reloading the app will log the user out. This should be done using localStorage or sessionStorage.
  • Show more information about transactions. A transaction detail page.
  • Show a confirmation dialog after user clicks on Send button.

Known issues

Contributing

Contributions are welcome! Please open an issue or submit a pull request.

Author

License

MIT

About

Accept Bitcoin payments (ckBTC) on the Internet Computer using this simple Point of Sale (POS) application.

https://hngac-6aaaa-aaaal-qb6tq-cai.icp0.io/

License:MIT License


Languages

Language:TypeScript 63.4%Language:JavaScript 24.5%Language:Motoko 10.4%Language:CSS 1.5%Language:HTML 0.3%