Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

adding wallet get assets usage on appkit send #3827

Open
wants to merge 11 commits into
base: main
Choose a base branch
from

Conversation

KannuSingh
Copy link
Collaborator

@KannuSingh KannuSingh commented Feb 10, 2025

Description

Enhance Send Flow with wallet_getAssets for CA Balance Display

This PR integrates the wallet_getAssets method into the Send Flow to display the user's Chain Abstraction (CA) balance. Since the embedded wallet supports CA for USDC and USDT and implements the wallet_getAssets RPC (indicated by the assetDiscovery capability), the AppKit Send Flow now:

Checks the connected chain's namespace and the wallet's capabilities.
If assetDiscovery is supported, it calls wallet_getAssets to fetch and display token balances.
Otherwise, it falls back to blockchainApi for retrieving account balances.

Type of change

  • Chore (non-breaking change that addresses non-functional tasks, maintenance, or code quality improvements)
  • Bug fix (non-breaking change which fixes an issue)
  • New feature (non-breaking change which adds functionality)
  • Breaking change (fix or feature that would cause existing functionality to not work as expected)

Associated Issues

For Linear issues: Closes APKT-xxx
For GH issues: closes #...

Showcase (Optional)

Appkit.CA.Send.mp4

Checklist

  • Code in this PR is covered by automated tests (Unit tests, E2E tests)
  • My changes generate no new warnings
  • I have reviewed my own code
  • I have filled out all required sections
  • I have tested my changes on the preview link
  • Approver of this PR confirms that the changes are tested on the preview link

Copy link

changeset-bot bot commented Feb 10, 2025

⚠️ No Changeset found

Latest commit: 6fa173c

Merging this PR will not cause a version bump for any packages. If these changes should not result in a new version, you're good to go. If these changes should result in a version bump, you need to add a changeset.

This PR includes no changesets

When changesets are added to this PR, you'll see the packages that this PR includes changesets for and the associated semver types

Click here to learn what changesets are, and how to add one.

Click here if you're a maintainer who wants to add a changeset to this PR

Copy link

vercel bot commented Feb 10, 2025

The latest updates on your projects. Learn more about Vercel for Git ↗︎

Name Status Preview Comments Updated (UTC)
appkit-demo ✅ Ready (Inspect) Visit Preview 💬 Add feedback Feb 12, 2025 9:31am
appkit-ethers5-bera ✅ Ready (Inspect) Visit Preview 💬 Add feedback Feb 12, 2025 9:31am
appkit-laboratory ✅ Ready (Inspect) Visit Preview 💬 Add feedback Feb 12, 2025 9:31am
next-wagmi-solana-bitcoin-example ✅ Ready (Inspect) Visit Preview 💬 Add feedback Feb 12, 2025 9:31am
9 Skipped Deployments
Name Status Preview Comments Updated (UTC)
appkit-basic-ep ⬜️ Ignored (Inspect) Visit Preview Feb 12, 2025 9:31am
appkit-basic-ep-example ⬜️ Ignored (Inspect) Feb 12, 2025 9:31am
appkit-basic-example ⬜️ Ignored (Inspect) Visit Preview Feb 12, 2025 9:31am
appkit-basic-sign-client-example ⬜️ Ignored (Inspect) Visit Preview Feb 12, 2025 9:31am
appkit-basic-up-example ⬜️ Ignored (Inspect) Visit Preview Feb 12, 2025 9:31am
appkit-vue-solana ⬜️ Ignored (Inspect) Feb 12, 2025 9:31am
appkit-wagmi-cdn-example ⬜️ Ignored (Inspect) Feb 12, 2025 9:31am
ethereum-provider-wagmi-example ⬜️ Ignored (Inspect) Visit Preview Feb 12, 2025 9:31am
vue-wagmi-example ⬜️ Ignored (Inspect) Feb 12, 2025 9:31am

Copy link
Contributor

github-actions bot commented Feb 10, 2025

Warnings
⚠️ File packages/adapters/bitcoin/src/adapter.ts contains a KEY or SECRET
⚠️ File packages/adapters/ethers/src/client.ts contains a KEY or SECRET
⚠️ File packages/adapters/ethers5/src/client.ts contains a KEY or SECRET
⚠️ File packages/adapters/solana/src/client.ts contains a KEY or SECRET
⚠️ File packages/adapters/wagmi/src/client.ts contains a KEY or SECRET
⚠️ File packages/appkit/src/client.ts contains a KEY or SECRET
⚠️ File packages/appkit/src/universal-adapter/client.ts contains a KEY or SECRET
⚠️ File packages/core/src/controllers/ConnectionController.ts contains a KEY or SECRET
⚠️ File packages/core/src/controllers/SendController.ts contains a KEY or SECRET
⚠️ File packages/core/tests/controllers/ChainController.test.ts contains a KEY or SECRET

Generated by 🚫 dangerJS against 6fa173c

Copy link
Contributor

github-actions bot commented Feb 10, 2025

Coverage Report

Status Category Percentage Covered / Total
🔵 Lines 68.77% 20259 / 29456
🔵 Statements 68.77% 20259 / 29456
🔵 Functions 60.8% 1795 / 2952
🔵 Branches 80.83% 3771 / 4665
File Coverage
File Stmts Branches Functions Lines Uncovered Lines
Changed Files
packages/adapters/bitcoin/src/adapter.ts 98.26% 100% 89.65% 98.26% 260-263
packages/adapters/ethers/src/client.ts 47.05% 55.73% 61.76% 47.05% 42-105, 121-122, 153-154, 167-190, 193-203, 216-253, 259-260, 267-284, 289-296, 299-321, 324-329, 340-341, 348-353, 367-368, 377-378, 394-404, 407-434, 438-439, 452-453, 455, 477-478, 482, 496, 513-515, 522-526, 566-567, 571-576, 585-603, 609-610, 614-619, 628-629, 640-641, 647-659
packages/adapters/ethers5/src/client.ts 46.19% 56.14% 61.76% 46.19% 43-106, 122-123, 154-155, 168-191, 194-204, 217-253, 259-260, 267-284, 289-296, 299-321, 324-329, 339-340, 347-352, 366-367, 376-377, 393-420, 423-433, 437-438, 451-452, 454, 479-480, 484, 498, 515-517, 524-528, 566-579, 587-605, 611-612, 616-621, 630-631, 642-643, 649-661
packages/adapters/solana/src/client.ts 51.14% 75% 33.33% 51.14% 49-56, 60-61, 88-91, 94-97, 100-101, 104-105, 108-109, 112-115, 118-126, 133-134, 144-164, 167-200, 203-204, 207-208, 216-217, 255-256, 278-280, 283-287, 293, 308-314, 317-324, 339-340, 346-350, 353-359
packages/adapters/wagmi/src/client.ts 72.88% 62.5% 72.22% 72.88% 123-149, 174, 235-239, 245-248, 271-272, 297, 299, 328-329, 381-384, 389-390, 400-401, 440-441, 469, 485-500, 504-524, 534-535, 539-540, 557-568, 607, 611-612, 633-634, 642-643, 648-649, 653-658, 665-666, 674-675, 680-681, 690-691, 699-700, 705-706, 712-734, 737-744
packages/adapters/wagmi/src/tests/mocks/AppKit.ts 92.92% 100% 100% 92.92% 111-118
packages/appkit/src/client.ts 60.05% 83.54% 76.41% 60.05% 182-183, 215-216, 236-237, 240-241, 256-259, 262-265, 285-286, 289-290, 306-340, 343-352, 367-368, 618-619, 622-625, 628-629, 632-633, 636-637, 640-641, 648-652, 717-727, 736-761, 768-771, 798-799, 813-814, 823-824, 827-828, 833-834, 858-859, 886-892, 918-929, 932-968, 971-976, 992-996, 1009-1020, 1023-1042, 1045-1051, 1054-1064, 1067-1079, 1082-1084, 1087-1089, 1092-1094, 1097-1099, 1102-1108, 1111-1114, 1119-1177, 1185-1197, 1208-1222, 1225-1232, 1235-1246, 1249-1255, 1260, 1264, 1279, 1292-1293, 1301-1304, 1307-1310, 1313-1319, 1329-1330, 1362-1378, 1390-1393, 1397, 1413-1423, 1432-1433, 1444-1462, 1468-1475, 1479-1494, 1512-1522, 1525-1605, 1608-1628, 1646, 1662-1663, 1669, 1671-1687, 1692, 1704, 1742-1749, 1751-1762, 1765-1773, 1811-1814, 1821-1827, 1834-1835, 1840-1841, 1881-1882, 1895-1896, 1901-1902, 1904-1905, 1910-1913, 1924-1925, 1964-1968, 1993-1994, 2018-2019, 2049-2050, 2054-2055, 2153-2154, 2183-2188, 2190-2200, 2202, 2204-2216, 2218-2232, 308-344
packages/appkit/src/adapters/ChainAdapterBlueprint.ts 89.07% 93.75% 75% 89.07% 136-137, 160-164, 170-173, 228-229
packages/appkit/src/universal-adapter/client.ts 42.32% 75% 24.32% 42.32% 29-38, 75-108, 111-144, 153-156, 159-163, 166-169, 172-175, 177-180, 183-186, 189-190, 193-194, 197-198, 201-202, 205-206, 209-216, 229, 233-235, 251-252, 260-30, 76-173
packages/core/src/controllers/ConnectionController.ts 77.5% 79.48% 41.5% 77.5% 111-131, 157-158, 173, 181, 185, 189, 193, 197, 201, 205, 209, 213, 249, 253, 263-264, 91-168
packages/core/src/controllers/SendController.ts 54.1% 70.83% 56.09% 54.1% 64, 104, 114-116, 118, 143-166, 209-223, 231-232, 243-244, 256-307, 326, 328-343, 345-346, 350-374, 134-304
packages/core/src/utils/ERC7811Util.ts 100% 90% 100% 100%
packages/core/src/utils/SendApiUtil.ts 12.5% 100% 0% 12.5% 16-59, 63-77, 17
packages/scaffold-ui/src/views/w3m-wallet-send-select-token-view/index.ts 92.5% 100% 77.77% 92.5% 53, 69, 105, 138, 145-146
packages/scaffold-ui/src/views/w3m-wallet-send-view/index.ts 96.96% 90.9% 90% 96.96% 105, 161-162
Generated in workflow #9514 for commit 6fa173c by the Vitest Coverage Report Action

@lukaisailovic
Copy link
Collaborator

lukaisailovic commented Feb 12, 2025

@KannuSingh Can we add the Showcase section to the PR? Might be easier to understand what you did. Also the description is not very useful. It explains what you did but not why.


if (!balances) {
const networkTokenBalances = SendApiUtil.mapBalancesToSwapTokens(state.tokenBalances)
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

why do you need this?

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Previously, the implementation retrieved balances using:

const balances = await SwapApiUtil.getMyTokensWithBalance();

Inside SwapApiUtil.getMyTokensWithBalance(), it first fetched balances from BlockchainApi, then mapped them using mapBalancesToSwapTokens before returning the result.

In the current implementation, since SendController already maintains the token balance state, I’m now directly using it to call mapBalancesToSwapTokens, avoiding the API call.

Comment on lines +169 to +207
async fetchTokenBalance(onError?: (error: unknown) => void): Promise<Balance[]> {
state.loading = true
const chainId = ChainController.state.activeCaipNetwork?.caipNetworkId
const chain = ChainController.state.activeCaipNetwork?.chainNamespace
const caipAddress = ChainController.state.activeCaipAddress
const address = caipAddress ? CoreHelperUtil.getPlainAddress(caipAddress) : undefined
if (
state.lastRetry &&
!CoreHelperUtil.isAllowedRetry(state.lastRetry, 30 * ConstantsUtil.ONE_SEC_MS)
) {
state.loading = false

return []
}

try {
if (address && chainId && chain) {
const balances = await SendApiUtil.getMyTokensWithBalance()
state.tokenBalances = balances
state.lastRetry = undefined

return balances
}
} catch (error) {
state.lastRetry = Date.now()

onError?.(error)
SnackController.showError('Token Balance Unavailable')
} finally {
state.loading = false
}

return []
},

fetchNetworkBalance() {
if (state.tokenBalances.length === 0) {
return
}
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

don't we already have this ?

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

We have a similar method in AccountController, but currently, we're maintaining separate token balance lists—one for CA balances (wallet_getAssets) in SendController and another for active chain balances in AccountController.

This method is called only when Send UI is opened.

Comment on lines +36 to +37
SendController.subscribe(val => {
this.tokenBalances = val.tokenBalances
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

hm. why move the state to SendController? I mean the tokens would also be required on portfolio in some cases so I'd think it's better to keep them in the account state

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

why move the state to SendController?

added this state to SendController bc this hold the tokens balance retrieved via wallet_getAssets rpc (CA balances). The AccountController holds the token balances from connected chain only.

I mean the tokens would also be required on portfolio in some cases

yes, but right now we don't have design to show both the balances nicely

Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@lukaisailovic food for thought but aren't portolio api and the CA api related? are we using diff providers?
feels kind of weird having to mantain 2 pieces of code / reconciliate, seems like they are doing basically the same?

Copy link
Collaborator

@lukaisailovic lukaisailovic Feb 12, 2025

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think we can utilize 7811 in portfolio if we wanted. I think in this case, we need to keep the portfolio call separate (inside the secure site) in order to have 1 place where we have the real user balance (no overrides). I think we can utilize 7811 appkit wide probably but requires more effort

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants