Transaction
Utility class for managing Ethereum transactions.
Constructor
The Transaction class is automatically instantiated and available as dsa.transaction.
Methods
send
Send a transaction and get the transaction hash.
await dsa.transaction.send(
transactionConfig: TransactionConfig,
callbacks?: TransactionCallbacks
): Promise<string>
Parameters:
transactionConfig
TransactionConfig
required
Web3 transaction configuration object
Optional callbacks for transaction events
TransactionConfig:
interface TransactionConfig {
from: string; // Sender address
to: string; // Recipient address
data?: string; // Transaction data
value?: string; // ETH value in wei
gas?: string | number; // Gas limit
gasPrice?: string | number; // Gas price (legacy)
maxFeePerGas?: string | number; // EIP-1559
maxPriorityFeePerGas?: string | number; // EIP-1559
nonce?: number; // Transaction nonce
}
TransactionCallbacks:
interface TransactionCallbacks {
onReceipt?: (receipt: TransactionReceipt) => void;
onConfirmation?: (
confirmationNumber: number,
receipt: TransactionReceipt,
latestBlockHash?: string
) => void;
}
Returns: Transaction hash as a string
Example:
const txConfig = {
from: "0xYourAddress...",
to: dsa.instance.address,
data: "0x...",
value: "0",
gas: "500000"
};
const txHash = await dsa.transaction.send(txConfig, {
onReceipt: (receipt) => {
console.log("Transaction mined:", receipt);
},
onConfirmation: (confirmationNumber, receipt) => {
console.log(`Confirmation ${confirmationNumber}`);
}
});
console.log("Transaction hash:", txHash);
In node mode, transactions are automatically signed using the configured private key. In browser mode, the wallet provider handles signing.
cancel
Cancel a pending transaction by sending a zero-value transaction with the same nonce and higher gas price.
await dsa.transaction.cancel(params: {
nonce: number | string;
gasPrice: string | number;
}): Promise<string>
Parameters:
Nonce of the transaction to cancel
Gas price higher than the original transaction
Returns: Transaction hash of the cancellation transaction
Example:
// Cancel transaction with nonce 42
const cancelTxHash = await dsa.transaction.cancel({
nonce: 42,
gasPrice: "50000000000" // 50 Gwei
});
console.log("Cancellation transaction:", cancelTxHash);
The gas price must be higher than the original transaction for the cancellation to succeed. The transaction sends 0 ETH to your own address with gas limit of 27500.
speedUp
Speed up a pending transaction by resubmitting it with a higher gas price.
await dsa.transaction.speedUp(
dsa: DSA,
params: {
transactionHash: string;
gasPrice: string | number;
}
): Promise<string>
Parameters:
Hash of the transaction to speed up
New gas price (must be higher than the original)
Returns: Transaction hash of the new transaction
Example:
// Speed up a pending transaction
const newTxHash = await dsa.transaction.speedUp(dsa, {
transactionHash: "0xOriginalTxHash...",
gasPrice: "60000000000" // 60 Gwei
});
console.log("New transaction hash:", newTxHash);
The method fetches the original transaction details and resubmits it with the same parameters but a higher gas price. The sender address must match the original transaction.
getNonce
Get the nonce of a specific transaction.
await dsa.transaction.getNonce(transactionHash: string): Promise<number>
Parameters:
Transaction hash to query
Returns: Nonce of the transaction
Example:
const nonce = await dsa.transaction.getNonce("0xTxHash...");
console.log("Transaction nonce:", nonce);
getTransactionCount
Get the total number of transactions sent from an address (current nonce).
await dsa.transaction.getTransactionCount(address: string): Promise<number>
Parameters:
Returns: Transaction count for the address
Example:
const count = await dsa.transaction.getTransactionCount("0xAddress...");
console.log("Total transactions:", count);
Alias
The send method is also aliased as dsa.sendTransaction() for convenience:
// These are equivalent
await dsa.transaction.send(txConfig);
await dsa.sendTransaction(txConfig);
Transaction Flow
Browser Mode
In browser mode, Web3 sends transactions through the connected wallet provider (MetaMask, WalletConnect, etc.):
const dsa = new DSA(web3);
// Wallet will prompt for signature
const txHash = await dsa.sendTransaction({
from: "0xYourAddress...",
to: dsa.instance.address,
data: "0x..."
});
Node Mode
In node mode, transactions are automatically signed using the configured private key:
const dsa = new DSA({
web3: web3,
mode: "node",
privateKey: "0xYourPrivateKey..."
});
// Automatically signed and sent
const txHash = await dsa.sendTransaction({
from: "0xYourAddress...",
to: dsa.instance.address,
data: "0x...",
gasPrice: "50000000000", // Required in node mode
nonce: 42 // Required in node mode
});
In node mode, gasPrice (or maxFeePerGas + maxPriorityFeePerGas) and nonce are required parameters.
Types
TransactionConfig
import { TransactionConfig } from 'web3-core';
interface TransactionConfig {
from: string;
to: string;
data?: string;
value?: string | number;
gas?: string | number;
gasPrice?: string | number;
maxFeePerGas?: string | number;
maxPriorityFeePerGas?: string | number;
nonce?: number;
}
TransactionCallbacks
import { TransactionReceipt } from 'web3-core';
interface TransactionCallbacks {
onReceipt?: (receipt: TransactionReceipt) => void;
onConfirmation?: (
confirmationNumber: number,
receipt: TransactionReceipt,
latestBlockHash?: string
) => void;
}