Learn everything about Circle and USDC
Section Summary
Reviewing User-Controlled Wallets
Welcome. In this video, we will review what you've learned in the last section of the course, focusing on user-controlled wallets.
Project Overview
The project used in this section is where the provided code originates. We begin by examining the package.json file, which includes commands like:
- checkWalletStatus
- checkWalletBalance
- initiateTransaction
Before running these, ensure you execute npm install to install all necessary dependencies.
Environment Configuration
Next, we look at the .env file, where we store credentials like the API key, App ID, and User ID. These were created in the previous section where we built user-controlled wallets. The goal now is to acquire a new session token, encryption key, and challenge ID, which are needed to initiate new challenges, similar to earlier steps.
Checking Wallet Status
We use an API call to check the wallet status by fetching variables from the .env file (User ID and API key). This is a GET method request. The response is stringified to JSON so we can visualize the output, which includes:
- Wallet ID
- Wallet Address
- Additional relevant data
This data will be used in subsequent actions.
Retrieving Wallet Balance
We then retrieve the wallet balance using another API call, this time requiring the Wallet ID obtained earlier. Run npm run checkWalletStatus to get the necessary details and add them to your .env file:
- WALLET_ID
- ADDRESS
Once set, you can execute npm run checkWalletBalance to see the current balance. The response typically includes:
- A token balances array
- Native token (e.g., MATIC on Mumbai testnet)
- USDC token (non-native)
Each token includes:
- Amount
- Decimals
- Whether it’s native
- Token ID (critical for identifying tokens during transactions)
Add the USDC Token ID to your .env file for later use.
Understanding Native Tokens and USDC
Native tokens are essential for paying gas fees. USDC, although not native, is also stored and transferred using the same wallet. Each token adheres to the ERC-20 standard as Mumbai is an Ethereum Layer-2 chain. This means all smart contracts and transactions are compatible with EVM.
Preparing for Transactions
Before initiating a transaction, you need test tokens from:
- The Mumbai Faucet (for native tokens)
- Circle’s USDC Faucet (for USDC)
Assuming you have these, you can proceed to initiate a transaction using the same approach as before—first by acquiring a session token and encryption key using a helper function.
These values are not stored in .env because they are temporary. You’ll also need to generate a challenge ID, which is used to present the transaction to the user through Circle’s Web SDK.
Initiating a Transaction
The transaction logic resides in index.js. We:
- Use the .env values and helper functions to retrieve necessary credentials.
- Generate a potency key (a unique identifier to ensure the transaction is processed once).
- Use a POST request to initiate the transaction.
The request body includes:
- User token
- Destination address (same or different wallet)
- Amount (e.g., 1 USDC)
- Fee level (high for now)
- Token ID
- Wallet ID
We log this data to verify that the transaction has all necessary components.
Validating Challenges and Completing Transactions
Running npm run initiateTransaction will generate:
- User token
- Encryption key
- Potency key
- Challenge ID
To complete the transaction, the Web SDK needs:
- App ID
- User ID
- Encryption key
- Session token
- Challenge ID
Once these are submitted, the user completes a simple challenge (e.g., entering a PIN). This finalizes the transaction, as the user controls the wallet.
Final Thoughts and Next Steps
You’ve accomplished a lot:
- Created user-controlled wallets
- Checked wallet status and balance
- Sent transactions
- Verified them through challenges
This replicates many functions of modern financial applications. In the next section, we’ll explore gas stations—a novel solution to manage gas fees.
Comments
You need to enroll in the course to be able to comment!