SDK Guide
The TownSquare SDK (@townsq/mm-sdk) provides developers with tools to interact seamlessly with the protocol — creating accounts, managing loans, depositing, borrowing, repaying, and withdrawing assets — without directly calling smart contracts.
Flow of transacting
To use the protocol you follow this order:
Create an account — One account per user. You need an account before you can open any loan.
Create a loan (or initiate loan with deposit) — Each loan has a loan type that defines which assets you can supply and borrow. Choose the type that matches your strategy:
1
Create account
TSAccount.prepare.createAccount → TSAccount.write.createAccount. Uses a unique nonce (4 bytes).
2a
Create loan (empty)
TSLoan.prepare.createLoan → TSLoan.write.createLoan. Opens a loan with a name and loan type; no deposit yet.
2b
Initiate loan with deposit
TSLoan.prepare.initiateLoanAccountWithDeposit → TSLoan.write.initiateLoanAccountWithDeposit. Creates the loan and funds it in one transaction.
Loan types (choose one when creating the loan):
General
MAINNET_LOAN_TYPE_ID.GENERAL
Accepts all supported tokens as collateral and for borrowing. Maximum flexibility.
Stable Efficiency
MAINNET_LOAN_TYPE_ID.STABLE_EFFICIENCY
Restricted to stablecoin-related assets (e.g. USDC, USDT, AUSD, USD1). Optimized for stablecoin strategies.
Deposit (LPs)
MAINNET_LOAN_TYPE_ID.DEPOSIT
Deposit-only: you can lend assets to pools but cannot borrow. Suited for LPs.
MON Efficiency
MAINNET_LOAN_TYPE_ID.MON_EFFICIENCY
Restricted to MON-related assets (e.g. MON, wMON, sMON, aprMON). Optimized for MON-focused strategies.
After the account and loan exist, you use Deposit, Borrow, Repay, Withdraw, as needed. The loan type does not change after creation.
Installation
npm install @townsq/mm-sdkor
Peer dependency
viem— used for chain config, wallet client, and RPC.
Initialization
Before using the SDK, you need to configure the core instance and signer.
Network:
NetworkType.MAINNETChain: Use
TS_CHAIN_ID(e.g.TS_CHAIN_ID.MONADfor mainnet Monad). The signer’schainmust match the chain you intend to transact on.Signer: Any viem
WalletClientwithchainandaccountset. The SDK uses it for signing and for resolving the current address.
Reading Loan Information
The SDK provides utilities to fetch and compute detailed information about a loan, including collateral value, debt value, and health factors. This is useful for dashboards, liquidation bots, or analytics.
Creating Account
Accounts are created on the chain selected at initialization. You need a nonce (unique 4-byte value) to avoid collisions.
The account will be created on the chain defined during initialization.
Creating a Loan
TownSquare supports multiple loan types:
Deposit-only
MAINNET_LOAN_TYPE_ID.DEPOSIT
No borrowing (1)
General
MAINNET_LOAN_TYPE_ID.GENERAL
All supported tokens (2)
MON Efficiency
MAINNET_LOAN_TYPE_ID.MON_EFFICIENCY
MON-related assets only (3)
Stable Efficiency
MAINNET_LOAN_TYPE_ID.STABLE_EFFICIENCY
Stablecoin-related (4)
Use the loan type that matches the assets you want to deposit/borrow
Create loan and deposit in one transaction
You can create a new loan and fund it in a single call using initiateLoanAccountWithDeposit. This is useful for “open account and deposit” flows.
Deposit
Deposit tokens into an existing loan. Use the correct pool ID (token) and loan type for the loan.
Example (USDC on Monad):
Borrow
Borrow from a loan. Specify the debt token (pool), amount, and (for variable-rate) max stable rate. The borrowed asset is sent on the chosen receiver chain (TS_CHAIN_ID).
Repay
Repay debt in a given token. You pass the borrow amount (position) and the repayment amount (how much you are paying back).
Withdraw
Withdraw collateral from a loan. You specify the token (pool), amount, whether the amount is in underlying or “t” (wrapped) units, and the receiver chain (EVM chain ID).
Last updated