Example: Create Atom from String
This example demonstrates creating an atom from a plain text string, including setup, error handling, and querying the result.
SDK reads default to the mainnet GraphQL API, so the example explicitly selects the testnet API to match its write chain. createAtomFromString fetches and forwards the required atom base cost; additionalDeposit is extra tTRUST signal, not the base cost.
Complete Codeβ
import {
configureSdk,
intuitionTestnet,
getMultiVaultAddressFromChainId,
createAtomFromString,
getAtomDetails,
wait,
} from '@0xintuition/sdk'
import { API_URL_DEV } from '@0xintuition/graphql'
import { createPublicClient, createWalletClient, http, parseEther, formatEther } from 'viem'
import { privateKeyToAccount } from 'viem/accounts'
async function main() {
// 1. Setup account and clients
const account = privateKeyToAccount(process.env.PRIVATE_KEY as `0x${string}`)
// SDK reads default to mainnet; keep reads paired with the testnet write chain.
configureSdk({ apiUrl: API_URL_DEV })
const publicClient = createPublicClient({
chain: intuitionTestnet,
transport: http(),
})
const walletClient = createWalletClient({
chain: intuitionTestnet,
transport: http(),
account,
})
const address = getMultiVaultAddressFromChainId(intuitionTestnet.id)
console.log('Connected to Intuition Testnet')
console.log('Account:', account.address)
// 2. Check balance
const balance = await publicClient.getBalance({ address: account.address })
console.log('Balance:', formatEther(balance), 'tTRUST')
if (balance < parseEther('0.1')) {
throw new Error('Insufficient balance. Get testnet tokens from faucet.')
}
// 3. Create atom
const atomData = 'TypeScript'
const additionalDeposit = parseEther('0.01')
console.log(`\nCreating atom: "${atomData}"`)
console.log('Additional signal:', formatEther(additionalDeposit), 'tTRUST')
const atom = await createAtomFromString(
{ walletClient, publicClient, address },
atomData,
additionalDeposit
)
console.log('\nβ Atom created successfully!')
console.log(' Atom ID:', atom.state.termId)
console.log(' Creator:', atom.state.creator)
console.log(' Vault:', atom.state.atomWallet)
console.log(' Transaction:', atom.transactionHash)
// 4. Wait for indexing
console.log('\nWaiting for indexing...')
await wait(atom.transactionHash, {
pollingInterval: 1000,
timeout: 30000,
})
// 5. Query atom details
console.log('Fetching atom details...')
const details = await getAtomDetails(atom.state.termId)
if (!details) throw new Error('Created atom was not found on the testnet API')
const vault = details.term?.vaults[0]
console.log('\nβ Atom Details:')
console.log(' Label:', details.label)
console.log(' Creator:', details.creator?.label ?? details.creator_id)
console.log(' Total Shares:', vault?.total_shares)
console.log(' Share Price:', vault?.current_share_price)
console.log(' Positions:', vault?.position_count)
console.log('\nSuccess!')
}
main()
.then(() => process.exit(0))
.catch((error) => {
console.error('Error:', error)
process.exit(1)
})
Run the Exampleβ
# Set your private key
export PRIVATE_KEY=0xYOUR_PRIVATE_KEY
# Run the script
npx tsx create-atom-example.ts
Expected Outputβ
Connected to Intuition Testnet
Account: 0xYourAddress
Balance: 10.5 tTRUST
Creating atom: "TypeScript"
Additional signal: 0.01 tTRUST
β Atom created successfully!
Atom ID: 0x1234567890abcdef...
Creator: 0xYourAddress
Vault: 0xVaultAddress
Transaction: 0xTransactionHash
Waiting for indexing...
Fetching atom details...
β Atom Details:
Label: TypeScript
Creator: 0xYourAddress
Total Shares: 1000000
Share Price: 0.01
Positions: 1
Success!