Write Contract (daccWriteContract)
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
| Parameter | Type | Description |
|---|---|---|
account | Account or 0x${string} | Conditional: The account to use for signing (Account object is private key). |
daccPublickey | string | Conditional: The encrypted wallet data from createDaccWallet. |
address | 0x${string} | Conditional: wallet address from a created with createDaccWallet in publicEncryption: true mode (can use instead of daccPublickey). |
passwordSecretkey | string | Conditional: The user's password used to decrypt the wallet. |
network | Chain | The blockchain network object to execute the transaction on. |
contractAddress | 0x${string} | The smart contract address to interact with. |
abi | Abi | The contract ABI for function calls. |
functionName | string | The contract function to execute. |
args | any[] | Optional: Array of function arguments. |
value | number | Optional: 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}`);