How do I create a transaction with IOTA's TypeScript SDK?
how do I create a transaction with iota's typescript sdk to send some iota from one address to another address? Use testnet btw
I'll help you create a transaction with IOTA's TypeScript SDK on testnet. Let me explore the repository structure first to find the relevant documentation and code examples.
Good! I found the TypeScript SDK documentation. Let me look at some specific examples for transferring IOTA.
Good! Let me get the rest of that file and look for example code:
Perfect! Now let me check if there are example files that show a complete transfer example:
Let me look at a simpler example file to understand the pattern:
Let me look at the stardust example which shows a complete transfer:
Perfect! Now I have a good understanding. Let me also check the hello-iota example which should be simpler:
Great! Now let me search for a complete example of a transfer transaction:
Perfect! Now I have enough information. Let me provide a comprehensive answer with code examples:
Based on the IOTA TypeScript SDK documentation and examples I found in the repository, here's how to create a transaction to send IOTA from one address to another on testnet:
##Basic IOTA Transfer Example
import { getFullnodeUrl, IotaClient } from '@iota/iota-sdk/client';
import { Ed25519Keypair } from '@iota/iota-sdk/keypairs/ed25519';
import { Transaction } from '@iota/iota-sdk/transactions';
// Setup
const iotaClient = new IotaClient({ url: getFullnodeUrl('testnet') });
// Create or import a keypair for signing
// Option 1: Create from a mnemonic phrase
const keypair = Ed25519Keypair.deriveKeypair(
'your mnemonic phrase here...'
);
// Option 2: Create from private key
const keypair = Ed25519Keypair.fromSecretKey(Buffer.from('your_private_key_hex', 'hex'));
const senderAddress = keypair.toIotaAddress();
const recipientAddress = '0xRecipientAddress'; // Replace with actual recipient address
const amountInNanos = 1_000_000_000; // 1 IOTA in nanos
// Create a transaction
const tx = new Transaction();
// Split a coin from gas to send
const [coinToSend] = tx.splitCoins(tx.gas, [amountInNanos]);
// Transfer the coin to the recipient
tx.transferObjects([coinToSend], recipientAddress);
// Sign and execute the transaction
const result = await iotaClient.signAndExecuteTransaction({
signer: keypair,
transaction: tx,
options: {
showObjectChanges: true,
showBalanceChanges: true,
},
});
console.log('Transaction digest:', result.digest);
##Key Components Explained
###1. Client Setup
const iotaClient = new IotaClient({ url: getFullnodeUrl('testnet') });
- Uses testnet RPC endpoint (use
'devnet','mainnet', or'localnet'for other networks)
###2. Create/Import Keypair
const keypair = Ed25519Keypair.deriveKeypair('your mnemonic');
- Generate a keypair to sign transactions
- Other options:
Secp256k1Keypair,Secp256r1Keypairfor different signature schemes
###3. Build Transaction
const tx = new Transaction();
const [coinToSend] = tx.splitCoins(tx.gas, [amountInNanos]);
tx.transferObjects([coinToSend], recipientAddress);
- Create transaction object
- Split IOTA from your gas coin
- Transfer to recipient
###4. Sign and Execute
const result = await iotaClient.signAndExecuteTransaction({
signer: keypair,
transaction: tx,
});
##Complete Example with Multiple Transfers
If you want to send IOTA to multiple addresses:
const transfers = [
{ to: '0xAddress1', amount: 100_000_000 },
{ to: '0xAddress2', amount: 200_000_000 },
{ to: '0xAddress3', amount: 150_000_000 },
];
const tx = new Transaction();
// Split coins for each transfer
const coins = tx.splitCoins(
tx.gas,
transfers.map(t => t.amount)
);
// Transfer each coin to its recipient
transfers.forEach((transfer, index) => {
tx.transferObjects([coins[index]], transfer.to);
});
const result = await iotaClient.signAndExecuteTransaction({
signer: keypair,
transaction: tx,
});
##Getting Test IOTA on Testnet
If you need testnet IOTA:
import { getFaucetHost, requestIotaFromFaucetV1 } from '@iota/iota-sdk/faucet';
await requestIotaFromFaucetV1({
host: getFaucetHost('testnet'),
recipient: senderAddress,
});
##Key Notes
- Gas: Every transaction requires IOTA to pay for gas. The SDK automatically handles gas coin selection
- Amounts: IOTA amounts are in nanos (1 IOTA = 1,000,000,000 nanos)
- Options: You can add
showObjectChanges,showBalanceChangesto see transaction effects immediately - Waiting: Use
client.waitForTransaction({ digest })to ensure the transaction is indexed before querying balances
That covers the basics! The IOTA TypeScript SDK handles a lot of complexity automatically (like coin merging and gas budget calculation).
END OF POST
Claude Haiku 4.5