Execute ERC20 approve and transferFrom transactions on Hedera testnet.
This script performs a two-step token transfer process:
- Approve: The
fromwallet approves thespenderwallet to spend a specified amount of tokens - TransferFrom: The
spenderwallet transfers tokens fromfromtoto
- Node.js >= 16.0.0
- npm or yarn
cd hedera-transfer-from
npm installCreate a transfer-config.json file based on the example:
cp transfer-config.example.json transfer-config.jsonEdit transfer-config.json with your actual values:
{
"token": "0x...",
"from": {
"address": "0x...",
"privateKey": "0x..."
},
"spender": {
"address": "0x...",
"privateKey": "0x..."
},
"to": "0x...",
"amount": "1000000",
"rpcUrl": "https://testnet.hashio.io/api",
"chainId": 296
}| Field | Description |
|---|---|
token |
ERC20 token contract address |
from.address |
Address that owns the tokens |
from.privateKey |
Private key to sign the approve transaction |
spender.address |
Address authorized to transfer tokens |
spender.privateKey |
Private key to sign the transferFrom transaction |
to |
Recipient address |
amount |
Amount in smallest units (wei) |
rpcUrl |
RPC endpoint (optional, defaults to Hedera testnet) |
chainId |
Chain ID (optional, defaults to 296 for Hedera testnet) |
# Using npm script
npm run transfer ./transfer-config.json
# Or directly with node
node executeTransfer.js --config ./transfer-config.jsonnpm run example
# or
node executeTransfer.js --examplenpm run help
# or
node executeTransfer.js --helpThe script provides detailed logging:
-
Pre-transaction State
- Token balances (from, spender, to)
- ETH/HBAR balances (for gas)
- Current allowance
-
Transaction Execution
- Approve transaction hash and confirmation
- TransferFrom transaction hash and confirmation
-
Post-transaction State
- Final token balances
- Final allowance
- Balance changes summary
================================================================================
EXECUTE APPROVE AND TRANSFERFROM
================================================================================
📡 Network: unknown (Chain ID: 296)
RPC URL: https://testnet.hashio.io/api
📄 Token Contract: 0xdb7a07aff9a9c1e73b10cf50c8659a847149612d
Name: Example Token
Symbol: EXT
Decimals: 18
💸 Transfer Parameters:
From (owner): 0x1b39b2d0f810feb53fdf5d1324d73334132667b8
Spender: 0xb8c5ac6f6945d905bac2649b7369fba36a69d41f
To (recipient): 0x1ccf61d63616278db8078711b80ebd1fc06bfee5
Amount: 0.000000000001 EXT (1000000 wei)
--------------------------------------------------------------------------------
PRE-TRANSACTION STATE
--------------------------------------------------------------------------------
💰 Token Balances:
From: 100.0 EXT
Spender: 0.0 EXT
To: 50.0 EXT
🔐 Current Allowance:
0x1b39... -> 0xb8c5...: 0.0 EXT
--------------------------------------------------------------------------------
STEP 1: APPROVE TRANSACTION
--------------------------------------------------------------------------------
⏳ Waiting for approve transaction...
Transaction hash: 0x...
✅ Transaction confirmed in block 12345678
--------------------------------------------------------------------------------
STEP 2: TRANSFERFROM TRANSACTION
--------------------------------------------------------------------------------
⏳ Waiting for transferFrom transaction...
Transaction hash: 0x...
✅ Transaction confirmed in block 12345679
================================================================================
SUMMARY
================================================================================
✅ TRANSFER COMPLETED SUCCESSFULLY
Approve TX: 0x...
TransferFrom TX: 0x...
Transferred: 0.000000000001 EXT
From: 0x1b39b2d0f810feb53fdf5d1324d73334132667b8
To: 0x1ccf61d63616278db8078711b80ebd1fc06bfee5
transfer-config.json file with real private keys to version control!
- The
transfer-config.jsonfile should be added to.gitignore - Only use testnet accounts for testing
- Keep private keys secure and never share them
- RPC URL:
https://testnet.hashio.io/api - Chain ID:
296
- RPC URL:
https://mainnet.hashio.io/api - Chain ID:
295
MIT