Error trying to get USDC (FiatToken) balance

Hi all… very excited about the USDC announcement and post from @andrea and I’m trying to start leveraging it.

It seemed like the simplest thing to start with would be to follow the setup instructions and then simply try to get a balance but I’m getting an error.

I have a Circle sandbox wallet setup with a Flow, have successfully funded the account via faucet. Also have updated my dev environment to latest @onflow/fcl and flow-cli just to be sure.

Any help would greatly appreciated!

Here’s the script - the code is identical to the get_balance.cdc in the flow-usdc Github (flow-usdc / scripts / vault / get_balance.cdc)

import {send, decode, script, args, arg, cdc} from "@onflow/fcl"
import {Address} from "@onflow/types"

const CODE = cdc`
  import FungibleToken from 0xFungibleToken
  import FiatToken from 0xFiatToken

  pub fun main(account: Address): UFix64 {
    let acct = getAccount(account)
    let vaultRef = acct.getCapability(FiatToken.VaultBalancePubPath)
        .borrow<&FiatToken.Vault{FungibleToken.Balance}>()
        ?? panic("Could not borrow Balance reference to the Vault")

    return vaultRef.balance
  }
`

export function fetchUSDCBalance(address) {
    if (address == null) return Promise.resolve(0)

    // prettier-ignore
    return send([
        script(CODE),
        args([
            arg(address, Address)
        ])
    ]).then(decode)
}

Browser stack trace is as follows:

send-execute-script.js:49 Uncaught SyntaxError: Unexpected end of JSON input
at JSON.parse (<anonymous>)
at o (send-execute-script.js:49:1)

|o|@|send-execute-script.js:49|
|---|---|---|
|Promise.catch (async)|||
|putIntoCache|@|recoil.js:4426|
|getFromCacheOrEvaluate|@|recoil.js:4484|
|(anonymous)|@|recoil.js:4615|
|detectCircularDependencies|@|recoil.js:4570|
|myGet|@|recoil.js:4615|
|getNodeLoadable|@|recoil.js:424|
|getRecoilValue|@|recoil.js:4498|
|evaluateSelectorFunction|@|recoil.js:4516|
|getFromCacheOrEvaluate|@|recoil.js:4478|
|(anonymous)|@|recoil.js:4615|
|detectCircularDependencies|@|recoil.js:4570|
|myGet|@|recoil.js:4615|
|getNodeLoadable|@|recoil.js:424|
|getRecoilValueAsLoadable|@|recoil.js:690|
|useRecoilValueLoadable_LEGACY|@|recoil.js:2519|
|useRecoilValueLoadable|@|recoil.js:2533|
|useRecoilValue|@|recoil.js:2551|
|useRecoilState|@|recoil.js:2603|
1 Like

Hi @codington please check this sandbox containing a sample implementation on how to get the balance of a USDC vault using FCL.

1 Like

Hi @bruno - thanks for this. I’m sure it will work, but the account I am trying to get a balance for is one that was setup by Circle, following the instructions from @andrea’s post. Perhaps I misunderstood the way things are supposed to work for this integration with Circle.

The post about USDC describes creating a Flow address within a Circle wallet and then appears to use the same Flow address in subsequent transfer calls.

Based on the fact that your sample code creates a vault within an existing Blocto wallet, I suppose perhaps the on ramp for USDC looks something like this:

  1. Create Circle wallet with new Flow address
  2. Create FiatToken vault within user’s existing Blocto wallet (under a different Flow address)
  3. Fund the Circle wallet Flow address (through whatever means)
  4. Transfer funds from Circle wallet Flow address to user’s Blocto wallet
  5. Blocto wallet now has USDC to spend

Is that correct?

1 Like

Actually it’s even simpler than that. The Circle account is only required if you want to on-ramp and off-ramp fiat, or transfer USDC from another chain (e.g. Ethereum USDC). If you try @bruno 's sandbox again and initialize the FiatToken vault on any testnet Flow address (wait for the sealed message to appear), then try to fund your account via usdcfaucet.com (again, wait a bit for the funds to go through), you will see you can successfully query the balance and get 1 USD :slight_smile:

So to summarize, the updated steps are:

  1. Create FiatToken vault on any Flow address
  2. Fund via any means necessary (on testnet this will usually be through usdcfaucet.com)
  3. Account now has USDC to spend
2 Likes

Thanks, @andrea - all clear now. The on-/off-ramp aspect is indeed what I am interested in, but your summary was helpful.

Final note: the Flow account created by Circle still causes an error if passed to the balance script. I assume this is expected behaviour, but wanted to make sure you were aware.

Thanks again to you and @bruno for the help!

No problem! Glad it was helpful. BTW, on your last point, when a blockchain transaction is sent to a Circle managed blockchain address, Circle sweeps it into a Circle omnibus account. This results in a balance held on the associated Circle account’s wallet and not on the destination blockchain address.

tldr: anything involving Circle should go through the API :slight_smile:

1 Like