hts/associateTokens

How to call the API from an app

Hedera Wallet Snap connects to your currently connected Metamask account by default. To learn how apps can connect to Hedera Wallet Snap using a non-metamask(external) account, refer to this documentation.

Then, depending on whether you're trying to connect to a metamask account or a non-metamask account, you can call the snap API in the following way:

const snapId = `npm:@hashgraph/hedera-wallet-snap`

const associateTokensAPI = async () => {
  const externalAccountParams = {
    externalAccount: {
      accountIdOrEvmAddress: '0.0.12345',
      curve: 'ED25519'
    }
  }

  await window.ethereum.request({
    method: 'wallet_invokeSnap',
    params: {
      snapId,
      request: {
        method: 'hts/associateTokens',
        params: {
          network: 'testnet',
          tokenIds: ['0.0.4280233'],
          /* 
            Uncomment the below line if you want to connect 
            to a non-metamask account
          */
          // ...externalAccountParams
        }
      }
    }
  })
}

Note that you can also pass in multiple token Ids to associate multiple tokens to an account.

What the API does

  1. Retrieves the currently connected account the user has selected on Metamask. If it's the first time, a new snap account is created and the account info is saved in snap state.

  2. Parses the arguments that were passed

  3. Calls the Hedera SDK Associate Tokens API to associate the account with the token Ids that are passed in parameters. This works for associating both fungible and non-fungible tokens.

  4. Returns the transaction receipt as response.

Some example responses:

{
    "currentAccount": {
        "metamaskEvmAddress": "0x0b3628d1b838993b5fceec8b2a26502e7a8e5241",
        "externalEvmAddress": "",
        "hederaAccountId": "0.0.3581604",
        "hederaEvmAddress": "0xca53f9c93d30e0b7212d67901e5a24fb090d542b",
        "publicKey": "0x0206022cea4c6dd6d2e7263b8802253971de922f5380661d97cba82dee66f57ad6",
        "balance": {
            "hbars": 97.34885218,
            "timestamp": "Thu, 25 Apr 2024 19:22:28 GMT",
            "tokens": {
                "0.0.4279119": {
                    "balance": 50,
                    "decimals": 1,
                    "tokenId": "0.0.4279119",
                    "name": "Tuum",
                    "symbol": "TUUM",
                    "tokenType": "FUNGIBLE_COMMON",
                    "supplyType": "INFINITE",
                    "totalSupply": "50",
                    "maxSupply": "0"
                }
            }
        },
        "network": "testnet",
        "mirrorNodeUrl": "https://testnet.mirrornode.hedera.com"
    },
    "receipt": {
        "status": "SUCCESS",
        "accountId": "",
        "fileId": "",
        "contractId": "",
        "topicId": "",
        "tokenId": "",
        "scheduleId": "",
        "exchangeRate": {
            "hbars": 30000,
            "cents": 358543,
            "expirationTime": "Thu, 25 Apr 2024 20:00:00 GMT",
            "exchangeRateInCents": 11.951433333333334
        },
        "topicSequenceNumber": "0",
        "topicRunningHash": "",
        "totalSupply": "0",
        "scheduledTransactionId": "",
        "serials": [],
        "duplicates": [],
        "children": []
    }
}

Live Demo on CodePen

Some things to keep in mind while interacting with the above demo
  • If you're getting any errors with the live demo, make sure you go through the FAQs section to learn about what you may be missing. You need to install Metamask in your browser for the live demo to work

To ease the integration of Hedera Wallet Snap on an application, we have created a template web application that you can run locally and check out the code in its entirety to learn how you can integrate and interact with various APIs exposed by Hedera Wallet Snap. Check out the full source code at template application github repository.

You can also check out the API reference to learn how each API works.

Last updated