04 / 04Advanced
Code walkthrough
The project consists of two main files:
-
index.ts: Entry point- Handles environment configuration, blockchain connections, tool setup, and interaction with the user.
- Shows how to run blockchain operations securely, with MetaMask signing transactions and the GOAT SDK handling blockchain management.
-
tools.ts: Helper functions- Holds reusable tools, such as fetching the live CHR price and configuring how the assistant behaves.
Key components
1. Environment and network setup
You can point the project at testnet or mainnet. The CHROMIA_CONFIG object supplies predefined constants for whichever network you pick:
const chromiaNetwork = "testnet"; // Switch to "mainnet" for mainnet operationsconsole.log(chalk.green("✔ Using Chromia network:"), chromiaNetwork);const config = CHROMIA_CONFIG[chromiaNetwork];Key points:
- Predefined configurations:
CHROMIA_CONFIGincludes ready-made constants for both testnet and mainnet:NODE_URL_POOL: The node URLs used to communicate with the blockchain.ECONOMY_CHAIN_BRID: The identifier of the Economy Chain.CHR_ASSET_ID: The identifier of the CHR token.
- Dynamic targeting: Pass
"testnet"or"mainnet"to load the matching values fromCHROMIA_CONFIG. - Customization:
CHROMIA_CONFIGis only a convenience. For advanced cases, skip it and provide your own values.
2. Connect to Chromia and enable transactions
Next, the project opens a connection to the Chromia blockchain and sets up transactions. You can adjust this setup to work with other chains and tokens.
const chromiaClient = await createClient({ nodeUrlPool: config.NODE_URL_POOL, blockchainRid: config.ECONOMY_CHAIN_BRID, // Specifies the target chain});const connection = createConnection(chromiaClient);const evmKeyStore = await createWeb3ProviderEvmKeyStore(window.ethereum);const keystoreInteractor = createKeyStoreInteractor(chromiaClient, evmKeyStore);const baseTools = await getOnChainTools({ wallet: chromia({ client: chromiaClient, accountAddress, keystoreInteractor, assetId: config.CHR_ASSET_ID, // Specifies the token for transfer connection, }), plugins: [sendCHR()], // Adds token transfer functionality});Key points:
- Network targeting:
NODE_URL_POOLdetermines which network you connect to. For a current list of testnet nodes, see the Chromia Explorer. - Custom configuration:
- Chains: To connect to a particular chain or dapp, swap out
blockchainRid. You can look up a chain’sbridin the Chromia Explorer. - Tokens: To use a different token, set
assetIdto that token’s unique identifier. For instance, theassetIdof tCHR on the Economy Chain is listed under 'Assets' in the Economy Chain Explorer.
- Chains: To connect to a particular chain or dapp, swap out
- MetaMask integration: MetaMask takes care of signing transactions securely, and the Chromia tools handle communication with the blockchain.
3. Extend tools for real-time price fetching
The getLiveTokenPrice helper retrieves live price data from CoinGecko. index.ts then wraps it so the assistant can call it.
-
Helper function in
tools.ts:typescript export const getLiveTokenPrice = async () => { const response = await fetch("https://api.coingecko.com/api/v3/simple/price?vs_currencies=usd&ids=chromaway&include_market_cap=true&include_24hr_vol=true&include_24hr_change=true"); return await response.json();}; -
Integration in
index.ts:typescript const enhancedTools = { ...baseTools, getLiveTokenPrice: { name: "getLiveTokenPrice", description: "Fetches the current CHR price in USD.", parameters: z.object({}), execute: async () => formatPriceData(await getLiveTokenPrice()), },};
Key points:
- The helper function retrieves price data and is written to be reused.
- The extended tool exposes it to the assistant at runtime so users can ask about prices.
4. Setting up the AI assistant and configuring its behavior
The assistant answers user questions and carries out blockchain actions. You configure it through environment variables and shape its behavior with MASTER_PROMPT in tools.ts.
-
Assistant setup in
index.ts:typescript const openai = createOpenAI({ baseURL: import.meta.env.VITE_AI_BASE_URL, apiKey: import.meta.env.VITE_AI_API_KEY,}); -
Behavior configuration in
tools.ts:typescript export const MASTER_PROMPT = (priceData: string) => `You are CHRA, a helpful assistant for CHR tokens...`;
Key points:
- Flexibility: Any AI service compatible with the OpenAI client standard works with the assistant.
- Customization: Edit
MASTER_PROMPTto give the assistant the style and tone your project needs.
The Chromia GOAT demo combines blockchain functionality and an AI assistant in a single application. Its modular design separates the blockchain tools from the configurable assistant, so you can reuse either part. Use it as a reference when you need secure transaction signing, blockchain asset handling, or a conversational AI interface in your own project.