APTOS
Aptos Cross-Chain SDK
The Aptos blockchain uses the Move programming language and provides a powerful environment for smart contracts and decentralized applications. The Demos Network SDK provides seamless integration with Aptos through our XM (Cross-chain Messaging) architecture.
Key Concepts
Demos Network Architecture: All operations go through Demos Network nodes by default, ensuring network participation and cross-chain compatibility. This relay-first approach provides enhanced security and enables true cross-chain functionality.
Move Language: Aptos uses Move, a resource-oriented programming language where smart contracts are organized as modules deployed at specific addresses.
Type Arguments: Many Move functions require explicit type arguments, especially when working with generic types like coins.
Installation
npm install @kynesyslabs/demosdk @aptos-labs/ts-sdk
Quick Start
import { APTOS } from '@kynesyslabs/demosdk/multichain/core'
import { Network } from '@aptos-labs/ts-sdk'
// Initialize with network
const aptos = new APTOS("", Network.DEVNET)
await aptos.connect()
// Connect wallet
const privateKey = "0x..." // Your private key
await aptos.connectWallet(privateKey)
Balance Queries
Balance queries go through Demos Network by default, providing network-wide visibility and cross-chain compatibility.
// Get APT balance (recommended - goes through Demos Network)
const balance = await aptos.getAPTBalance(address)
// Returns XM operation object for network execution
// Get specific coin balance (recommended - goes through Demos Network)
const coinBalance = await aptos.getCoinBalance("0x1::aptos_coin::AptosCoin", address)
// Returns XM operation object for network execution
Direct Balance Access
For specialized use cases requiring direct RPC access:
// Direct APT balance query (bypasses Demos Network)
const balanceDirect = await aptos.getAPTBalanceDirect(address)
// Returns balance string directly from Aptos RPC
// Direct coin balance query (bypasses Demos Network)
const coinBalanceDirect = await aptos.getCoinBalanceDirect("0x1::aptos_coin::AptosCoin", address)
// Returns balance string directly from Aptos RPC
Smart Contract Interactions
Reading from Contracts
Contract reads go through Demos Network by default, enabling cross-chain contract queries and enhanced monitoring.
// Read from contract (recommended - goes through Demos Network)
const result = await aptos.readFromContract(
"0x1", // Module address
"coin", // Module name
"name", // Function name
[], // Arguments
["0x1::aptos_coin::AptosCoin"] // Type arguments
)
// Returns XM operation object for network execution
Direct Contract Reads
For specialized scenarios requiring direct RPC access:
// Direct contract read (bypasses Demos Network)
const resultDirect = await aptos.readFromContractDirect(
"0x1", // Module address
"coin", // Module name
"name", // Function name
[], // Arguments
["0x1::aptos_coin::AptosCoin"] // Type arguments
)
// Returns actual result directly from Aptos RPC
Writing to Contracts
All contract writes must go through Demos Network to ensure proper validation, security, and cross-chain functionality.
// Prepare contract write transaction
const signedTx = await aptos.writeToContract(
"0x123...", // Module address
"my_module", // Module name
"transfer_coins", // Function name
[recipient, amount], // Arguments
["0x1::aptos_coin::AptosCoin"] // Type arguments
)
// Returns signed transaction bytes for Demos Network relay
Token Transfers
APT transfers are prepared as signed transactions for Demos Network execution.
// Prepare APT transfer
const signedTx = await aptos.preparePay(
"0x742d35Cc6634C0532925a3b844Bc454e4438f44e", // Recipient
"100000000" // Amount in Octas (1 APT = 100,000,000 Octas)
)
// Returns signed transaction bytes for Demos Network relay
// Prepare multiple transfers
const transfers = [
{ address: "0x742d35Cc6634C0532925a3b844Bc454e4438f44e", amount: "100000000" },
{ address: "0x456...", amount: "200000000" }
]
const signedTxs = await aptos.preparePays(transfers)
// Returns array of signed transaction bytes
Utility Methods
// Get wallet address
const address = aptos.getAddress()
// Validate Aptos address
const isValid = aptos.isAddress("0x1")
// Sign message
const signature = await aptos.signMessage("Hello Demos Network")
// Verify message signature
const isVerified = await aptos.verifyMessage(message, signature, publicKey)
Network Configuration
import { Network } from '@aptos-labs/ts-sdk'
// Mainnet
const mainnet = new APTOS("", Network.MAINNET)
// Testnet
const testnet = new APTOS("", Network.TESTNET)
// Devnet (recommended for development)
const devnet = new APTOS("", Network.DEVNET)
// Custom RPC endpoint
const custom = new APTOS("https://custom-aptos-rpc.com", Network.DEVNET)
Move-Specific Considerations
Amounts in Octas: APT amounts are specified in Octas (1 APT = 100,000,000 Octas).
Type Arguments: Most Move functions require explicit type arguments for generic parameters.
Resource Management: Move uses resources instead of traditional contract storage, requiring specific interaction patterns.
Entry vs View Functions: Distinguish between entry functions (state-modifying) and view functions (read-only).
Best Practices
Use XM Operations: Always prefer the default methods that go through Demos Network for enhanced security and cross-chain functionality.
Direct Methods: Only use
*Direct()
methods when you specifically need to bypass Demos Network for specialized use cases.Error Handling: Implement comprehensive error handling for network operations and Move-specific errors.
Testing: Use Devnet for development and Testnet for staging before mainnet deployment.
Advanced Features
// Wait for transaction confirmation
const txResponse = await aptos.waitForTransaction(transactionHash)
// Get empty transaction template
const emptyTx = await aptos.getEmptyTransaction()
// Set custom network
aptos.setNetwork(Network.TESTNET)
Last updated