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:

  1. Create an account — One account per user. You need an account before you can open any loan.

  2. 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:

Step
Action
Description

1

Create account

TSAccount.prepare.createAccountTSAccount.write.createAccount. Uses a unique nonce (4 bytes).

2a

Create loan (empty)

TSLoan.prepare.createLoanTSLoan.write.createLoan. Opens a loan with a name and loan type; no deposit yet.

2b

Initiate loan with deposit

TSLoan.prepare.initiateLoanAccountWithDepositTSLoan.write.initiateLoanAccountWithDeposit. Creates the loan and funds it in one transaction.

Loan types (choose one when creating the loan):

Loan type
Constant
Use case

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-sdk

or

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.MAINNET

  • Chain: Use TS_CHAIN_ID (e.g. TS_CHAIN_ID.MONAD for mainnet Monad). The signer’s chain must match the chain you intend to transact on.

  • Signer: Any viem WalletClient with chain and account set. 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:

Loan type
Constant
Description

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