The useExchangeAccounts hook provides functionality for managing exchange accounts and performing exchange-related operations. It allows users to retrieve their exchange accounts, view transaction history, and execute transfers between exchange accounts.

This hook integrates with the Dynamic SDK to handle exchange operations across different blockchain networks and exchange platforms.

This hook supports the Fund from Exchange feature, allowing users to transfer funds directly from their exchange accounts to their wallets.

Available Methods

Methods

exchangeTransfer

Summary: Executes a transfer between exchange accounts. The method automatically finds the appropriate account based on the currency being transferred and creates the transfer request.

Inputs:

ParameterTypeDescription
exchangeExchangeKeyEnumThe exchange platform identifier (e.g., Coinbase, Binance, etc.)
transferRequestCreateExchangeTransferRequestThe transfer request object containing transfer details

CreateExchangeTransferRequest Fields:

FieldTypeDescription
tostringThe recipient address (e.g., ‘0xRecipientAddr’)
amountnumberThe amount to transfer (e.g., 0.25)
currencystringThe currency code (e.g., ‘ETH’, ‘USDC’)
networkstringThe blockchain network (e.g., ‘ethereum’)
descriptionstringOptional description of the transfer
mfaCodestringOptional MFA verification code

Outputs:

FieldTypeDescription
ReturnsPromise<ExchangeTransferResponse>A promise that resolves to the transfer response containing transfer status and details

ExchangeTransferResponse Fields:

FieldTypeDescription
idstringUnique transfer identifier (e.g., ‘tx-1’)
exchangeAccountIdstringID of the exchange account used for transfer
statusstringTransfer status (e.g., ‘pending’, ‘completed’, ‘failed’)
amountnumberThe transferred amount
currencystringThe currency that was transferred
createdAtDateTimestamp when the transfer was created

Example Response:

{
  id: 'tx-1',
  exchangeAccountId: '123',
  status: 'pending',
  amount: 1,
  currency: 'USDC',
  createdAt: new Date(),
}

Example:

const { exchangeTransfer } = useExchangeAccounts();

const result = await exchangeTransfer({
  exchange: ExchangeKeyEnum.Coinbase,
  transferRequest: {
    to: '0xRecipient',
    amount: 1,
    currency: 'USDC',
    network: 'ethereum',
  },
});

getExchangeTransactions

Summary: Retrieves transaction history for exchange accounts. Can fetch transactions for a specific account or all accounts if no accountId is provided.

Inputs:

ParameterTypeDescription
exchangeExchangeKeyEnumThe exchange platform identifier
accountIdstring | undefinedOptional account ID to filter transactions for a specific account

Outputs:

FieldTypeDescription
ReturnsPromise<ExchangeTransaction[]>A promise that resolves to an array of exchange transactions

ExchangeTransaction Fields:

FieldTypeDescription
transactionIdstringUnique transaction identifier (e.g., ‘tx-123’)
transactionHashstringBlockchain transaction hash (e.g., ‘0xabc’)
statusstringTransaction status (e.g., ‘pending’, ‘completed’, ‘failed’)
amountnumberTransaction amount
currencystringCurrency involved in the transaction
createdAtDateTimestamp when the transaction was created

Example Response:

[
  {
    transactionId: 'tx-123',
    transactionHash: '0xabc',
    status: 'pending',
    amount: 0.5,
    currency: 'ETH',
    createdAt: new Date('2025-06-25T12:00:00Z'),
  },
];

Example:

const { getExchangeTransactions } = useExchangeAccounts();

// Get all transactions for an exchange
const allTransactions = await getExchangeTransactions({
  exchange: ExchangeKeyEnum.Coinbase,
});

// Get transactions for a specific account
const accountTransactions = await getExchangeTransactions({
  exchange: ExchangeKeyEnum.Coinbase,
  accountId: 'acc-1',
});

getExchangeUserAccounts

Summary: Retrieves all exchange accounts associated with the current user for a specific exchange platform. Returns accounts with their balances and currency information.

Inputs:

ParameterTypeDescription
exchangeExchangeKeyEnumThe exchange platform identifier

Outputs:

FieldTypeDescription
ReturnsPromise<Account[]>A promise that resolves to an array of user accounts with balance information

Account Fields:

FieldTypeDescription
idstringUnique account identifier (e.g., ‘acc-1’, ‘acc-2’)
exchangeExchangeKeyEnumThe exchange platform this account belongs to
balancesArray<Balance>Array of balance objects for different currencies

Balance Fields:

FieldTypeDescription
currencystringCurrency code (e.g., ‘USDC’, ‘BTC’, ‘ETH’)
balancenumberAvailable balance amount

Example Response:

[
  {
    id: 'acc-1',
    exchange: ExchangeKeyEnum.Coinbase,
    balances: [
      {
        currency: 'USDC',
        balance: 1,
      },
    ],
  },
  {
    id: 'acc-2',
    exchange: ExchangeKeyEnum.Coinbase,
    balances: [
      {
        currency: 'BTC',
        balance: 1,
      },
    ],
  },
];

Example:

const { getExchangeUserAccounts } = useExchangeAccounts();

const accounts = await getExchangeUserAccounts({
  exchange: ExchangeKeyEnum.Coinbase,
});

// Each account contains:
// - id: string (e.g., 'acc-1', 'acc-2')
// - exchange: ExchangeKeyEnum
// - balances: Array of balance objects with currency and balance
//   Example: [{ currency: 'USDC', balance: 1 }, { currency: 'BTC', balance: 1 }]

Usage Example

import { useExchangeAccounts } from '@dynamic-labs/sdk-react-core';
import { ExchangeKeyEnum } from '@dynamic-labs/sdk-api-core';

function ExchangeComponent() {
  const { exchangeTransfer, getExchangeTransactions, getExchangeUserAccounts } =
    useExchangeAccounts();

  const handleTransfer = async () => {
    try {
      const result = await exchangeTransfer({
        exchange: ExchangeKeyEnum.Coinbase,
        transferRequest: {
          to: '0xRecipient',
          amount: 1,
          currency: 'USDC',
          network: 'ethereum',
        },
      });
      console.log('Transfer successful:', result);
    } catch (error) {
      console.error('Transfer failed:', error);
    }
  };

  const loadAccounts = async () => {
    const accounts = await getExchangeUserAccounts({
      exchange: ExchangeKeyEnum.Coinbase,
    });
    console.log('User accounts:', accounts);
  };

  const loadTransactions = async () => {
    const transactions = await getExchangeTransactions({
      exchange: ExchangeKeyEnum.Coinbase,
    });
    console.log('Transactions:', transactions);
  };

  return (
    <div>
      <button onClick={handleTransfer}>Transfer</button>
      <button onClick={loadAccounts}>Load Accounts</button>
      <button onClick={loadTransactions}>Load Transactions</button>
    </div>
  );
}

Error Handling

The hook throws DynamicError with specific error codes when operations fail:

  • UnprocessableEntityErrorCode.InvalidTransferCurrency: Thrown when no account is found for the specified currency during transfer operations.

Dependencies

This hook depends on:

  • useInternalDynamicContext for accessing wallet and network information
  • Dynamic SDK data layer for API calls
  • Environment configuration for API endpoints