rooms source realm
Package rooms is the on-chain trade window.
Package rooms is the on-chain trade window.
Two parties share a room. Each one puts assets on their own side: native coins (GNOT and any other bank denom), GRC20 tokens from the registry at gno.land/r/nt/grc20reg/v0, and GRC721 NFTs from collections registered with this realm (see RegisterCollection). The realm holds the deposits.
Every change to the room raises its version. A party locks the room by naming the exact version they reviewed, so a lock can never apply to contents the party has not seen. When both sides hold a lock on the current version, the second lock swaps everything in the same transaction: the maker's side goes to the taker and the taker's side goes to the maker.
Until that moment each party can withdraw their own deposits, and either party can cancel the room, which returns every deposit to its owner. An expired room can no longer settle; its deposits stay withdrawable, and anyone may cancel it to send them home.
A maker can also turn a room into an offer (see Offer): their side plus an exact list of what they want in return. The first account allowed to take the other seat that puts in exactly that list completes the trade in one transaction (see Fill).
On settlement the realm keeps a service fee on coins and tokens (see fee.gno); NFTs, withdrawals and cancellations pay nothing.
4
const StatusOpen, StatusSettled, StatusCancelled, statusFilling, KindCoin, KindToken, KindNFT, MaxAssetsPerSide, DefaultTTLMinutes, MinTTLMinutes, MaxTTLMinutes
1const (
2 StatusOpen = "open"
3 StatusSettled = "settled"
4 StatusCancelled = "cancelled"
5
6 // statusFilling holds a room for the length of one Fill, so nothing an
7 // asset's own code runs during the transfers can change it.
8 statusFilling = "filling"
9
10 KindCoin = "coin"
11 KindToken = "token"
12 KindNFT = "nft"
13
14 // MaxAssetsPerSide bounds the work a settlement or refund can do.
15 MaxAssetsPerSide = 8
16
17 DefaultTTLMinutes = 24 * 60
18 MinTTLMinutes = 10
19 MaxTTLMinutes = 7 * 24 * 60
20)const MaxTokenIDLen
MaxTokenIDLen bounds what a room stores per NFT.
const VerifyDelay
VerifyDelay is how long a new verification waits before it counts.
40
func AcceptAdmin
crossing ActionAcceptAdmin completes a handover started with ProposeAdmin.
func Address
ActionAddress returns this realm's address: the spender to approve before DepositToken, and the account that holds deposits.
func Admin
ActionAdmin returns the current admin address, or "" when there is none.
func BalancesJSON
ActionBalancesJSON lists the registered GRC20 tokens that owner holds, with the balance and the allowance to this realm: a wallet's inventory in one query. A page covers limit registry entries (at most 50); read on while "more" is true.
func Cancel
crossing ActionCancel closes an open room and returns every deposit to its owner. Either party may cancel at any time before settlement; once the room has expired, anyone may cancel it.
func CollectionsJSON
ActionCollectionsJSON lists the GRC721 collections registered with rooms.
func DepositCoins
crossing ActionDepositCoins adds the coins sent with this call to the caller's side. It must be called directly from the depositing account (maketx call or a wallet MsgCall), because only then are the sent coins guaranteed to have reached this realm.
func DepositNFT
crossing ActionDepositNFT moves one NFT of a registered collection from the caller to this realm and adds it to the caller's side. The caller must first approve this realm's address (see Address) for that token id, in the collection's realm.
func DepositToken
crossing ActionDepositToken moves amount of a registered GRC20 token from the caller to this realm and adds it to the caller's side. The caller must first approve this realm's address (see Address) for at least amount.
func FeeBps
ActionFeeBps returns the rate a room opened now would get.
func Fill
crossing ActionFill completes an offer: the caller takes the other seat, puts in exactly what the offer wants, and the room settles in this call. version is the version the caller reviewed. Coins in the offer must be attached to this call, from the caller's own account.
func IsPaused
ActionIsPaused reports whether new activity is stopped.
func IsVerified
ActionIsVerified reports whether an asset counts as verified now. denom is a coin denom, a registry key, or a collection key (with or without "#token id").
func Join
crossing ActionJoin takes the open taker seat of a room.
func LatestJSON
ActionLatestJSON lists all rooms, newest first.
func Lock
crossing ActionLock records that the caller agrees to swap the room exactly as it is at version. It fails if the room has changed since. When the other side already holds a lock on the same version, this call settles the room and returns "settled"; otherwise it returns "locked".
func NFTJSON
ActionNFTJSON describes one NFT of a registered collection: who owns it, and whether this realm is approved to move it. null when the collection is not registered or the token does not exist.
func Offer
crossing Actionfunc Open
crossing ActionOpen creates a room with the caller as maker and returns its id. The room takes only verified assets (see Verify). counterparty is the taker's address, or "" to leave the seat open for the first account that calls Join. ttlMinutes is how long the room can settle; 0 means 24 hours, and the allowed range is 10 minutes to 7 days.
func OpenMarket
crossing ActionOpenMarket is Open for a room that takes any registered asset, verified or not. Both parties trade unverified assets at their own risk; the app says so in every view of such a room.
func OwnedJSON
ActionOwnedJSON lists the NFTs owner holds in registered collections that can be listed (those registered with their enumerable view), at most 20 per collection, and whether this realm is approved for each. A page covers limit collections; read on while "more" is true.
func ProposeAdmin
crossing ActionProposeAdmin names the next admin; the change happens when that address calls AcceptAdmin. Pass "" to withdraw a proposal.
func RegisterCollection
crossing ActionRegisterCollection makes a GRC721 collection tradable in rooms and returns its key. Only the collection's own realm can register it, once; list is its enumerable view, or nil. A collection's realm also lets owners approve this realm (see Address) for one token id, as its own Approve does; DepositNFT spends that approval.
Example
1key := rooms.RegisterCollection(cross(cur), Token, List)
func Render
Render shows the realm on gnoweb: the home page lists recent rooms, and room/<id> shows one room. Every value that comes from a caller goes through sanitize before it reaches the markdown.
func RoomJSON
ActionRoomJSON returns one room, or null when it does not exist.
func RoomsOfJSON
ActionRoomsOfJSON lists the rooms where addr is maker or taker, newest first.
func SetFee
crossing ActionSetFee sets the rate for rooms opened from now on, in basis points (50 is 0.5%), at most MaxFeeBps. Rooms already open keep their rate.
func SetPaused
crossing ActionSetPaused stops or resumes new activity.
func SetTreasury
crossing ActionSetTreasury names the account that receives fees from now on.
func StatsJSON
ActionStatsJSON returns counters and the settled volume per asset.
func TokenJSON
ActionTokenJSON describes one registered token and, when owner is given, the owner's balance and current allowance to this realm.
func TokensJSON
ActionTokensJSON lists GRC20 tokens from the registry that rooms accept.
func Treasury
ActionTreasury returns the account that receives fees, or "" when fees are off.
func Unlock
crossing ActionUnlock withdraws the caller's lock on the current version.
func Unverify
crossing ActionUnverify removes a verification at once. Ordinary rooms stop accepting the asset, and those that hold it can no longer settle; deposits stay withdrawable. reason is public.
func VerifiedJSON
ActionVerifiedJSON lists verifications, counted or still waiting, with their public reasons.
func Verify
crossing ActionVerify makes a coin denom, a registered GRC20 token or a registered NFT collection acceptable in ordinary rooms from VerifyDelay from now. kind is coin, token or nft; key is the denom, the registry key or the collection key. reason is public: say why this asset is safe to trade.
func WithdrawCoins
crossing ActionWithdrawCoins returns amount of denom from the caller's side to the caller. It works while the room is open, also after it has expired or while the realm is paused.
func WithdrawNFT
crossing ActionWithdrawNFT returns one NFT from the caller's side to the caller. Same rules as WithdrawCoins: it works while the room is open, also after it has expired or while the realm is paused.
func WithdrawToken
crossing ActionWithdrawToken returns amount of a GRC20 token from the caller's side to the caller. Same rules as WithdrawCoins.