Skip to content

Write Contract (daccWriteContract)

A function to execute smart contract write operations

The daccWriteContract function allows you to execute write transactions on smart contracts from a Dacc Wallet. It handles the transaction signing and broadcasting using the encrypted wallet.

Import

import { daccWriteContract } from 'dacc-js';

Usage

import { daccWriteContract } from 'dacc-js';

import { sepolia } from 'viem/chains'; // used `viem` - npm i viem
 
const tx = await daccWriteContract({
  // account: "0xPrivatekey...", // Can call with `allowDaccWallet` function
  daccPublickey: 'daccPublickey_0x123_XxX..',
  // address: '0x123...', // Only the address created is set to `publicEncryption: true`
  passwordSecretkey: 'my+Password#123..',
  network: sepolia,
  contractAddress: '0xcontract...',
  abi: contractAbi,
  functionName: 'transfer',
  args: ['0xrecipient...', 1000000000000000000n]
  // value: 0.1 // Optional: ETH Native value to send
});
 
console.log(tx); // {txHash, chainId, from, contractAddress, functionName, args}
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 execute the transaction on.
contractAddress0x${string}The smart contract address to interact with.
abiAbiThe contract ABI for function calls.
functionNamestringThe contract function to execute.
argsany[]Optional: Array of function arguments.
valuenumberOptional: ETH Native value to send with the transaction (in ETH units).

Return Value

The response result is an object containing {txHash, chainId, from, contractAddress, functionName, args}

Parameters

account (conditional)

  • Type: Account | 0x${string}

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

const tx = await daccWriteContract({
  account: "0xPrivatekey...", // Can call with `allowDaccWallet` function
//   address: '0x123...', //
//   daccPublickey: 'daccPublickey_0x123_XxX..', //
//   passwordSecretkey: 'my+Password#123..', //
  network: sepolia,
  contractAddress: '0xcontract...',
  abi: contractAbi,
  functionName: 'transfer',
  args: ['0xrecipient...', 1000000000000000000n]
});

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 daccWriteContract({
//   account: "0xPrivatekey...", //
  daccPublickey: 'daccPublickey_0x123_XxX..', 
  passwordSecretkey: 'my+Password#123..', 
  network: sepolia,
  contractAddress: '0xcontract...',
  abi: contractAbi,
  functionName: 'transfer',
  args: ['0xrecipient...', 1000000000000000000n]
});

address (optional)

  • Type: 0x${string}

The encrypted wallet data returned from createDaccWallet.

const tx = await daccWriteContract({
//   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: sepolia,
  contractAddress: '0xcontract...',
  abi: contractAbi,
  functionName: 'transfer',
  args: ['0xrecipient...', 1000000000000000000n]
});

passwordSecretkey (conditional)

  • Type: string

The same password used when creating the wallet.

const tx = await daccWriteContract({
  daccPublickey: 'daccPublickey_0x123_XxX..',
  passwordSecretkey: 'my+Password#123..', 
  network: sepolia,
  contractAddress: '0xcontract...',
  abi: contractAbi,
  functionName: 'transfer',
  args: ['0xrecipient...', 1000000000000000000n]
});

network

  • Type: Chain

The blockchain network object to execute the transaction on.

const tx = await daccWriteContract({
  address: '0x123...',
  daccPublickey: 'daccPublickey_0x123_XxX..',
  passwordSecretkey: 'my+Password#123..',
  network: sepolia, 
//   network: myCustomChain, // For - Custom Chain Network
  contractAddress: '0xcontract...',
  abi: contractAbi,
  functionName: 'transfer',
  args: ['0xrecipient...', 1000000000000000000n]
});

contractAddress

  • Type: 0x${string}

The smart contract address to interact with.

const tx = await daccWriteContract({
  address: '0x123...',
  daccPublickey: 'daccPublickey_0x123_XxX..',
  passwordSecretkey: 'my+Password#123..',
  network: sepolia,
  contractAddress: '0xcontract...', 
  abi: contractAbi,
  functionName: 'transfer',
  args: ['0xrecipient...', 1000000000000000000n]
});

abi

  • Type: Abi

The contract ABI (Application Binary Interface) for function calls.

const contractAbi = [
  {
    "inputs": [
      {"name": "to", "type": "address"},
      {"name": "amount", "type": "uint256"}
    ],
    "name": "transfer",
    "outputs": [{"name": "", "type": "bool"}],
    "type": "function"
  }
] as const;
 
const tx = await daccWriteContract({
  address: '0x123...',
  daccPublickey: 'daccPublickey_0x123_XxX..',
  passwordSecretkey: 'my+Password#123..',
  network: sepolia,
  contractAddress: '0xcontract...',
  abi: contractAbi, 
  functionName: 'transfer',
  args: ['0xrecipient...', 1000000000000000000n]
});

functionName

  • Type: string

The contract function to execute.

const tx = await daccWriteContract({
  address: '0x123...',
  daccPublickey: 'daccPublickey_0x123_XxX..',
  passwordSecretkey: 'my+Password#123..',
  network: sepolia,
  contractAddress: '0xcontract...',
  abi: contractAbi,
  functionName: 'transfer', 
  args: ['0xrecipient...', 1000000000000000000n]
});

args (optional)

  • Type: any[]
  • Default: []

Array of function arguments.

const tx = await daccWriteContract({
  address: '0x123...',
  daccPublickey: 'daccPublickey_0x123_XxX..',
  passwordSecretkey: 'my+Password#123..',
  network: sepolia,
  contractAddress: '0xcontract...',
  abi: contractAbi,
  functionName: 'transfer',
  args: ['0xrecipient...', 1000000000000000000n] 
});

value (optional)

  • Type: number
  • Default: undefined

ETH value to send with the transaction (in ETH units).

const tx = await daccWriteContract({
  address: '0x123...',
  daccPublickey: 'daccPublickey_0x123_XxX..',
  passwordSecretkey: 'my+Password#123..',
  network: sepolia,
  contractAddress: '0xcontract...',
  abi: contractAbi,
  functionName: 'deposit',
  args: [],
  value: 0.1 // Send 0.1 ETH Native with the transaction
});

Examples

Transfer ERC20 tokens

const erc20Abi = [
  {
    "inputs": [
      {"name": "to", "type": "address"},
      {"name": "amount", "type": "uint256"}
    ],
    "name": "transfer",
    "outputs": [{"name": "", "type": "bool"}],
    "type": "function"
  }
] as const;
 
const tx = await daccWriteContract({
  daccPublickey: 'daccPublickey_0x123_XxX..',
  address: '0x123...',
  passwordSecretkey: 'my+Password#123..',
  network: sepolia,
  contractAddress: '0xTokenContract...',
  abi: erc20Abi,
  functionName: 'transfer',
  args: ['0xrecipient...', BigInt('1000000000000000000')] // 1 token with 18 decimals
});
 
console.log(`Transaction hash: ${tx.txHash}`);
console.log(`Called function: ${tx.functionName}`);

Mint NFT with ETH payment

const nftAbi = [
  {
    "inputs": [{"name": "to", "type": "address"}],
    "name": "mint",
    "outputs": [],
    "payable": true,
    "type": "function"
  }
] as const;
 
const tx = await daccWriteContract({
  account: "0xYourPrivateKey...",
  network: sepolia,
  contractAddress: '0xNFTContract...',
  abi: nftAbi,
  functionName: 'mint',
  args: ['0xminterAddress...'],
  value: 0.05 // Pay 0.05 ETH to mint
});
 
console.log(`NFT minted! Transaction: ${tx.txHash}`);

Approve token spending

const erc20Abi = [
  {
    "inputs": [
      {"name": "spender", "type": "address"},
      {"name": "amount", "type": "uint256"}
    ],
    "name": "approve",
    "outputs": [{"name": "", "type": "bool"}],
    "type": "function"
  }
] as const;
 
const tx = await daccWriteContract({
  daccPublickey: 'daccPublickey_0x123_XxX..',
  address: '0x123...',
  passwordSecretkey: 'my+Password#123..',
  network: sepolia,
  contractAddress: '0xUSDCContract...',
  abi: erc20Abi,
  functionName: 'approve',
  args: ['0xSpenderContract...', BigInt('1000000000')] // Approve 1000 USDC (6 decimals)
});
 
console.log(`Approval successful: ${tx.txHash}`);

Call function with no arguments

const contractAbi = [
  {
    "inputs": [],
    "name": "pause",
    "outputs": [],
    "type": "function"
  }
] as const;
 
const tx = await daccWriteContract({
  daccPublickey: 'daccPublickey_0x123_XxX..',
  address: '0x123...',
  passwordSecretkey: 'my+Password#123..',
  network: sepolia,
  contractAddress: '0xContract...',
  abi: contractAbi,
  functionName: 'pause'
  // args: [] is optional when no arguments needed
});
 
console.log(`Contract paused: ${tx.txHash}`);