Functions
Every function in the Return market contract: what it does, who may call it, and when.
All amounts are in raw token units (for USDG, 1 USDG = 1000000). Every settlement function takes the funded Loan struct and recomputes its ID, so the exact terms from the Opened event must be passed back unchanged.
Data types
enum Side { Lend, Borrow } // 0: the maker is the lender. 1: the maker is the borrower.
struct Order {
Side side;
address maker;
address taker; // Zero: anyone may accept.
address collateral;
uint128 collateralAmount;
address loanToken;
uint96 principal; // Maximum across all fills.
uint96 repayment; // Must exceed principal.
uint32 duration; // Seconds from funding to maturity.
uint64 validUntil; // Last second the order can be filled.
uint256 counter; // Must equal counters(maker).
uint256 salt; // Separates otherwise identical orders.
uint96 minFill; // Equal to principal: all or nothing.
}
struct Loan {
bytes32 orderId;
address borrower;
address collateral;
uint128 collateralAmount; // This fill's share of the collateral.
address loanToken;
uint96 principal; // This fill's amount.
uint96 repayment; // This fill's payback.
uint96 offset; // Principal filled before this loan.
uint32 duration;
}Constants
| Constant | Value | Meaning |
|---|---|---|
FEE_BPS | 30 | Fee: 0.3% of each fill's principal, rounded up to one raw unit, deducted from the borrower's proceeds. |
GRACE_PERIOD | 86400 | Seconds after maturity during which repayment is still accepted. |
MAX_REPAYMENT_EXTENSION | 604800 | Most that incident extensions can add to one loan's deadline, in seconds. |
REPAYMENT_GAS | 500000 | Gas repay forwards to the direct payment to the note holder. |
ORDER_TYPEHASH | 0xf8495076cbe37e8342a7fcb35987eae47fba15b5e745b974cb0bcf38fdf507a9 | EIP-712 type hash of Order. See Sign and fill orders. |
Opening loans
| Function | Caller | What it does |
|---|---|---|
fill(Order order, uint96 amount, bytes signature) → bytes32 id | Taker | Opens one loan for amount of the order. Pulls collateral from the borrower, sends amount − fee to the borrower and fee to the treasury from the lender, mints the note to the lender, emits Opened. Returns the loan ID. |
post(Order order, bytes signature) | Maker only | Publishes a public order: checks terms, state and signature, then emits Posted. Stores nothing. Reverts if taker is set, so wallet-restricted orders cannot be listed. |
previewFill(Order order, uint96 amount, address taker) → (Loan loan, uint256 fee) | Anyone (view) | Runs every check fill runs except the signature, balances and approvals. Returns the loan that would open, and its fee. |
loanTerms(Order order, uint96 amount, uint96 offset, address taker) → Loan | Anyone (view) | Reconstructs the Loan for a past or hypothetical fill at a given offset. |
Who is who in a fill:
order.side | Lender (pays, gets the note) | Borrower (locks collateral) |
|---|---|---|
Lend (0) | order.maker | the caller of fill |
Borrow (1) | the caller of fill | order.maker |
Cancelling
| Function | Caller | What it does |
|---|---|---|
cancel(Order order) | Maker only | Cancels the order's unfilled remainder. Existing loans are unaffected. Emits OrderCancelled. |
incrementCounter() | Any maker | Raises counters(msg.sender) by one, invalidating every order signed with any other counter. Emits CounterIncremented. |
Settling loans
| Function | Caller | When | What it does |
|---|---|---|---|
repay(Loan loan) | Anyone | Active and not past repaymentDeadline(loan) | Transfers loan.repayment from the caller directly to the note holder (the caller must approve the market), burning the note. If the caller is the borrower, also returns the collateral. The direct payment runs with exactly REPAYMENT_GAS; if it fails, the payment is pulled into the contract and held for the note holder, and the note stays. Emits Repaid. |
reclaimCollateral(Loan loan) | Borrower only | Paid back by someone else, collateral not yet released | Sends the collateral to the borrower. Emits CollateralReclaimed. |
collectRepayment(Loan loan) | Anyone | Paid back, payment held, note still exists | Sends the held payment to the current note holder and burns the note. Emits RepaymentCollected. |
foreclose(Loan loan) | Anyone | Unpaid, strictly after repaymentDeadline(loan) | Sends the collateral to the note holder and burns the note. Emits Foreclosed. |
Reading state
| Function | Returns |
|---|---|
orderDigest(Order) → bytes32 | The order ID: its EIP-712 hash. |
loanDigest(Loan) → bytes32 | The loan ID, which is also the note's token ID: keccak256(abi.encode(loan)). |
orderState(bytes32 orderId) → (uint96 filled, bool cancelled) | Principal filled so far, and whether the order was cancelled. |
loanState(bytes32 id) → (uint40 maturity, bool repaid, bool collateralReleased) | Maturity is zero for an unknown ID. |
repaymentDeadline(Loan) → uint256 | The current last second to repay, including any incident extension. Zero for unknown terms. |
heldRepayment(Loan) → uint256 | A repayment held for the note holder after a failed push. |
counters(address maker) → uint256 | The counter new orders must use. |
protocolFee(uint96 principal) → uint256 | ceil(principal × 30 / 10000). |
assetAllowed(address) → bool | Whether a token is admitted for new loans. |
newLoansPaused() → bool | Whether new fills are paused. |
repaymentWindow(address token) → uint40 | The incident window for a token. Address zero is the global window. |
repaymentWindowSetAt(address token) → uint40 | When that window was last declared. It covers only loans funded before this time. |
reserved(address token) → uint256 | Collateral and held repayments owed in that token. Never withdrawable as surplus. |
assets(uint256 index) → address | Every token ever admitted, in admission order. Positions never change. |
art() → address | The NoteArt contract that renders note metadata. It holds no funds and no permissions. |
eip712Domain() | The signing domain, per ERC-5267. |
treasury() → address, owner() → address | Fee recipient, and the address we control the contract from. |
ownershipHandoverExpiresAt(address) → uint256 | When a pending ownership handover to that address expires. |
Notes (ERC721)
The contract is a standard ERC721 collection: balanceOf, ownerOf, approve, getApproved, setApprovalForAll, isApprovedForAll, transferFrom, safeTransferFrom, tokenURI, name (Return ERC20 Note), symbol (RNOTE20) and supportsInterface.
- The token ID is
uint256(loanId). A note buyer can check its terms withloanDigest(loan) == bytes32(tokenId). - Minting calls no receiver hook, so any contract can be a lender.
- Notes cannot be transferred while a settlement is running, and never to the market contract itself.
- An approved operator can move a note but has no claim of its own.
tokenURIreturns on-chain JSON with an SVG image. Its status reads Active, Overdue (past maturity) or Claimable (payment held for the holder). Overdue does not mean claimable:repaymentDeadlinedecides that.
Owner functions
Only we can call these, from the address owner() returns.
| Function | Effect | Limits |
|---|---|---|
setAsset(address token, bool allowed) | Admits or removes a token for new fills. | When admitting, rejects addresses without code and contracts that report the ERC721 interface. Never blocks settlement. |
setNewLoansPaused(bool paused) | Pauses or resumes new fills. | Never blocks settlement. |
extendRepaymentWindow(address token, uint40 until) | Extends repayment for loans funded before this call that use token as collateral or loan token, or for all such loans if token is zero. | until must be in the future, later than the current window, and at most 7 days away. Each loan is capped at its normal deadline + 7 days. Loans funded later keep their signed deadline. |
setTreasury(address) | Changes the recipient of future fees and surplus. | Loan claims never go to the treasury. |
withdrawSurplus(address token) | Sends the token balance above reserved(token) (or all ETH, for address zero) to the treasury. | Reverts if the balance is below reserved. |
requestOwnershipHandover, completeOwnershipHandover, cancelOwnershipHandover | Two-step ownership transfer. | transferOwnership and renounceOwnership always revert. |
transferRepayment is an internal helper exposed for a try call inside repay. It reverts unless called by the contract itself.
Events and errors are on Events and errors.
Last updated