Quick Start
This guide will help you get started with the Artha Chain SDK quickly. We'll cover the basic operations you can perform with the SDK.
Prerequisites
- An Artha Chain API key
- Node.js 16+ (for JavaScript/TypeScript)
- Python 3.8+ (for Python)
- Go 1.18+ (for Go)
- Rust 1.56+ (for Rust)
Basic Operations
1. Initialize the Client
import { ArthaChain } from '@arthachain/sdk';
const client = new ArthaChain({
apiKey: 'your-api-key',
network: 'testnet', // Start with testnet
});
2. Create a Wallet
// Generate a new wallet
const wallet = await client.wallet.create();
// Or import from private key
const wallet = await client.wallet.import('your-private-key');
3. Get Account Balance
const balance = await client.account.getBalance(wallet.address);
console.log(`Balance: ${balance} ARTHA`);
4. Send a Transaction
const tx = await client.transaction.send({
from: wallet.address,
to: 'recipient-address',
amount: '1.0',
currency: 'ARTHA',
});
console.log(`Transaction hash: ${tx.hash}`);
5. Deploy a Smart Contract
const contract = await client.contract.deploy({
from: wallet.address,
bytecode: 'contract-bytecode',
abi: contractABI,
});
console.log(`Contract address: ${contract.address}`);
Complete Example
Here's a complete example that demonstrates the main features:
import { ArthaChain } from '@arthachain/sdk';
async function main() {
// Initialize client
const client = new ArthaChain({
apiKey: 'your-api-key',
network: 'testnet',
});
// Create wallet
const wallet = await client.wallet.create();
console.log(`Wallet address: ${wallet.address}`);
// Get balance
const balance = await client.account.getBalance(wallet.address);
console.log(`Initial balance: ${balance} ARTHA`);
// Send transaction
const tx = await client.transaction.send({
from: wallet.address,
to: 'recipient-address',
amount: '1.0',
currency: 'ARTHA',
});
console.log(`Transaction sent: ${tx.hash}`);
// Wait for confirmation
const receipt = await tx.wait();
console.log(`Transaction confirmed in block ${receipt.blockNumber}`);
// Deploy contract
const contract = await client.contract.deploy({
from: wallet.address,
bytecode: 'contract-bytecode',
abi: contractABI,
});
console.log(`Contract deployed at ${contract.address}`);
// Interact with contract
const result = await contract.methods.someFunction().call();
console.log(`Contract call result: ${result}`);
}
main().catch(console.error);
Error Handling
The SDK provides comprehensive error handling:
try {
const tx = await client.transaction.send({
from: wallet.address,
to: 'recipient-address',
amount: '1.0',
currency: 'ARTHA',
});
} catch (error) {
if (error.code === 'INSUFFICIENT_FUNDS') {
console.error('Not enough balance to send transaction');
} else if (error.code === 'INVALID_ADDRESS') {
console.error('Invalid recipient address');
} else {
console.error('Unexpected error:', error);
}
}