Skip to main content

Method Signature

align.blockchain.contracts.write(
  params: ContractTransaction
): Promise<TransactionResponse>

Parameters

contractAddress
string
required
Contract address
abi
array
required
Contract ABI
method
string
required
Function name to call
args
array
Function arguments
wallet
Wallet
required
Wallet for signing
network
string
required
Target network

Returns

Returns an ethers.js TransactionResponse that can be awaited for confirmation.

Examples

import Align from "@tolbel/align";

const align = new Align({
  apiKey: process.env.ALIGN_API_KEY!,
  environment: "sandbox",
});

const wallet = await align.blockchain.wallets.createFromPrivateKey(
  process.env.PRIVATE_KEY!
);

// Approve USDC spending
const ERC20_ABI = [
  "function approve(address spender, uint256 amount) returns (bool)",
];

const tx = await align.blockchain.contracts.write({
  contractAddress: "0x2791Bca1f2de4661ED88A30C99A7a9449Aa84174",
  abi: ERC20_ABI,
  method: "approve",
  args: ["0xSpenderAddress...", "1000000000"], // 1000 USDC
  wallet,
  network: "polygon",
});

console.log(`TX Hash: ${tx.hash}`);

// Wait for confirmation
const receipt = await tx.wait();
console.log(`Confirmed in block ${receipt?.blockNumber}`);

ERC-20 Transfer

const tx = await align.blockchain.contracts.write({
  contractAddress: USDC_ADDRESS,
  abi: ["function transfer(address to, uint256 amount) returns (bool)"],
  method: "transfer",
  args: [recipientAddress, "50000000"], // 50 USDC
  wallet,
  network: "polygon",
});

const receipt = await tx.wait();
console.log(`Transferred in block ${receipt?.blockNumber}`);
Write operations require native tokens for gas. Ensure the wallet has sufficient balance.