Skip to content

Send Token (daccSendToken)

A function to send ERC20 tokens

The daccSendToken function allows you to send ERC20 tokens from a Dacc wallet to any destination address. It handles the transaction signing and broadcasting using the encrypted wallet data.

Import

import { daccSendToken } from 'dacc-js';

Usage

import { daccSendToken } from 'dacc-js';

import { optimismSepolia } from 'viem/chains'; // used `viem` - npm i viem
 
const tx = await daccSendToken({
  // account: "0xPrivatekey...", // Can call with `allowDaccWallet` function
  daccPublickey: 'daccPublickey_0x123_XxX..',
  // address: '0x123address...', // Only the address created is set to `publicEncryption: true`
  passwordSecretkey: 'my+Password#123..',
  network: optimismSepolia,
  tokenAddress: '0xTokenContract...',
  to: '0xRecipient...',
  amount: 0.0002
  // decimals: 6
});
 
console.log(tx); // {txHash, chainId, from, to, tokenAddress, amount, decimals}
console.log(tx?.txHash); // 0xTransactionHash...

Arguments

ParameterTypeDescription
accountAccount or 0x${string}Conditional: The account to use for signing (Account object is private key).
daccPublickeystringConditional: The encrypted wallet data from createDaccWallet.
address0x${string}Conditional: wallet address from a created with createDaccWallet in publicEncryption: true mode (can use instead of daccPublickey).
passwordSecretkeystringConditional: The user's password used to decrypt the wallet.
networkChainThe blockchain network object to send the transaction on.
tokenAddress0x${string}The ERC20 token contract address.
to0x${string}The recipient's wallet address.
amountnumberThe amount of tokens to send.
decimalsnumberOptional: The token decimals (default: 18).

Return Value

The response result is an object containing {txHash, chainId, from, to, tokenAddress, amount, decimals}

Parameters

account (conditional)

  • Type: Account | 0x${string}

The user's account to use for signing the transaction.

const tx = await daccSendToken({
  account: "0xPrivatekey...", // Can call with `allowDaccWallet` function
//   daccPublickey: 'daccPublickey_0x123_XxX..', //
//   address: '0x123address...', //
//   passwordSecretkey: 'my+Password#123...', //
  network: optimismSepolia,
  tokenAddress: '0xTokenContract...',
  to: '0xRecipient...',
  amount: 100
});

daccPublickey (conditional)

  • Type: string

The user's encrypted public key to address.

daccPublickey is the most reliable addressing solution and supports all types of data storage using daccPublickey.

const tx = await daccSendToken({
//   account: "0xPrivatekey...", //
  daccPublickey: 'daccPublickey_0x123_XxX..', 
  passwordSecretkey: 'my+Password#123...', 
  network: optimismSepolia,
  tokenAddress: '0xTokenContract...',
  to: '0xRecipient...',
  amount: 100
});

address (optional)

  • Type: 0x${string}

The encrypted wallet data returned from createDaccWallet.

const tx = await daccSendToken({
//   account: "0xPrivatekey...", //
//   daccPublickey: 'daccPublickey_0x123_XxX..', // Match with address
  address: '0x123address...', // Only the address created is set to `publicEncryption: true`
  passwordSecretkey: 'my+Password#123...', 
  network: optimismSepolia,
  tokenAddress: '0xTokenContract...',
  to: '0xRecipient...',
  amount: 100
});

passwordSecretkey (conditional)

  • Type: string

The same password used when creating the wallet.

const tx = await daccSendToken({
  daccPublickey: 'daccPublickey_0x123_XxX..',
  passwordSecretkey: 'my+Password#123...', 
  network: optimismSepolia,
  tokenAddress: '0xTokenContract...',
  to: '0xRecipient...',
  amount: 100,
});

network

  • Type: Chain

The blockchain network object to send the transaction on.

const tx = await daccSendToken({
  daccPublickey: 'daccPublickey_0x123_XxX..',
  passwordSecretkey: 'my+Password#123...',
  network: optimismSepolia, // used `viem`
//   network: myCustomChain, // For - Custom Chain Network
  tokenAddress: '0xTokenContract...',
  to: '0xRecipient...',
  amount: 100
});

tokenAddress

  • Type: 0x${string}

The ERC20 token contract address.

const tx = await daccSendToken({
  address: '0x123address...',
  daccPublickey: 'daccPublickey_0x123_XxX..',
  passwordSecretkey: 'my+Password#123...',
  network: optimismSepolia,
  tokenAddress: '0xTokenContract...', 
  to: '0xRecipient...',
  amount: 100
});

to

  • Type: 0x${string}

The recipient's wallet address.

const tx = await daccSendToken({
  address: '0x123address...',
  daccPublickey: 'daccPublickey_0x123_XxX..',
  passwordSecretkey: 'my+Password#123...',
  network: optimismSepolia,
  tokenAddress: '0xTokenContract...',
  to: '0xRecipient...', 
  amount: 100
});

amount

  • Type: number

The amount of tokens to send.

const tx = await daccSendToken({
  address: '0x123address...',
  daccPublickey: 'daccPublickey_0x123_XxX..',
  passwordSecretkey: 'my+Password#123...',
  network: optimismSepolia,
  tokenAddress: '0xTokenContract...',
  to: '0xRecipient...',
  amount: 100
});

decimals (optional)

  • Type: number
  • Default: 18

The token decimals. Most ERC20 tokens use 18 decimals, but some tokens like USDC use 6 decimals.

const tx = await daccSendToken({
  address: '0x123address...',
  daccPublickey: 'daccPublickey_0x123_XxX..',
  passwordSecretkey: 'my+Password#123...',
  network: optimismSepolia,
  tokenAddress: '0xUSDCContract...',
  to: '0xRecipient...',
  amount: 100,
  decimals: 6 // Optional: For tokens like USDC
});

Examples

Send USDC tokens (6 decimals)

const tx = await daccSendToken({
  daccPublickey: 'daccPublickey_0x123_XxX..',
  address: '0x123address...',
  passwordSecretkey: 'my+Password#123...',
  network: optimismSepolia,
  tokenAddress: '0xUSDCContractAddress...',
  to: '0xRecipientAddress...',
  amount: 100, // 100 USDC
  decimals: 6
});
 
console.log(`Transaction hash: ${tx.txHash}`);
console.log(`Sent ${tx.amount} USDC to ${tx.to}`);

Send custom ERC20 token with private key

const tx = await daccSendToken({
  account: "0xYourPrivateKey...",
  network: optimismSepolia,
  tokenAddress: '0xCustomTokenAddress...',
  to: '0xRecipientAddress...',
  amount: 1000,
  decimals: 18
});
 
console.log(`Transaction successful: ${tx.txHash}`);