Quick Start
This guide is designed to allow users with basic notions of Typescript to interact with the AgoraStableSwap contract. The complete examples are available on our GitHub repository.
For this guide, we will be using the AUSD/CTK trading pair on the Avalanche Fuji Testnet, where AUSD(AgoraDollar) is a proxy for our mainnet contract, and CTK (ConstantToken) serves as a representative example token that could be substituted with any other token in a mainnet pair.
Prerequisites
-
Node.js ≥ 18 and npm (comes with Node).
-
TypeScript toolchain (optional but recommended):
npm install -D typescript ts-node @types/node
npx tsc --init
Package Installation
viem: viem is a TypeScript interface for Ethereum that provides low-level stateless primitives for interacting with evm chains.dotenv:dotenvis a Node.js module that loads environment variables from a.envfile, commonly used to manage configuration settings.
You can install the packages by running:
npm install viem@^1 dotenv@^16
Wallet Setup
Never use production (mainnet) private keys or wallets when experimenting on testnets. Create a fresh, disposable wallet for Fuji (or any other test network) so that mistakes or leaked keys can't jeopardize real funds.
For sake of this example's simplicity, we recommend using MetaMask because it makes switching between networks and exporting a private key straightforward. In production, you may want to use an MCP provider which provides additional safety guarantees.
-
Install MetaMask from the official site.
-
Create a new wallet (or add an additional account).
-
In MetaMask, switch the network to "Avalanche Fuji Testnet."
If you don't see it, click Add Network → Testnets and enable Fuji.
In order to sign transactions programmatically, you'll need to export your private key into a .env file. for that:
- In MetaMask, click the three-dot menu next to your Fuji account → Account Details → Export Private Key.
- Copy the hex string that begins with
0x.... - Paste it into a
.envfile and make sure.envis part of your.gitignore
Fund your Test Wallet
You'll need a small amount of AVAX-Fuji for gas. Claim it from the official faucet.
Paste your wallet address, complete the captcha, and you'll receive testnet AVAX within seconds. With your disposable wallet, environment variables, and testnet gas ready, you're all set to create viem clients and run the code examples that follow.
Setting up a viem wallet client
For the rest of the guides we'll use a single wallet client that can:
- Sign & send transactions (write calls)
- Perform public read calls (so we don't need a separate public-only client)
- Automatically track nonces when you fire several transactions in quick succession
The pattern below satisfies all three goals.
We'll use avalancheFuji Testnet client for the following examples:
import { http, createWalletClient, publicActions, nonceManager } from "viem";
import { avalancheFuji } from "viem/chains";
import { privateKeyToAccount } from "viem/accounts";
import dotenv from "dotenv";
dotenv.config();
if (!process.env.TESTNET_HOTWALLET_PK)
throw new Error('TESTNET_HOTWALLET_PK not set in .env');
// Build the Account object from the test-only private key
const account = privateKeyToAccount(
process.env.TESTNET_HOTWALLET_PK as `0x${string}`,
{ nonceManager }, // keeps nonces in sync across runs
);
// Create a wallet client and extend it with public-read helpers
const client = createWalletClient({
chain: avalancheFuji,
transport: http(
process.env.FUJI_RPC_URL || 'https://avalanche-fuji-c-chain-rpc.publicnode.com',
),
account,
}).extend(publicActions);
With this wallet client in place you can copy-paste every example that follows without extra setup code.