Search Apps Documentation Source Content File Folder Download Copy Actions Download State String Boolean Number Struct Map Slice Pointer Function Closure Reference Nil Package Type Interface Unknown

gnft source realm

Package gnft implements GRC721-compliant NFTs for GnoSwap LP positions.

Readme View source

GNFT

GRC721-compliant NFT contract for GnoSwap LP positions.

Overview

GNFT represents each liquidity position as a unique NFT with dynamically generated SVG artwork. Each NFT features a gradient background with parameters stored efficiently to minimize gas costs.

Core Features

GRC721 Standard Compliance

  • Full implementation of GRC721 interface
  • Transfer, approval, and operator management
  • Token enumeration and metadata

Dynamic SVG Generation

  • Unique gradient backgrounds for each NFT
  • Parameters: x1, y1, x2, y2, color1, color2
  • On-demand SVG rendering from stored parameters
  • Base64-encoded data URI for direct browser display

Gas Optimization

  • Compact parameter storage (CSV string format)
  • Template-based SVG generation
  • Lazy rendering (generate on read, not on mint)
  • Minimal storage footprint per token

Integration

  • Position contract mints NFTs for new positions
  • Staker contract locks NFTs during staking
  • GRC721 owner/approval checks for transfers and operator management

Key Functions

Mint

Mints new NFT for LP position.

Parameters:

  • cur realm: Current realm context
  • to address: Recipient address
  • tid grc721.TokenID: Token ID to mint

Returns: grc721.TokenID

Burn

Burns NFT when position is closed.

Parameters:

  • cur realm: Current realm context
  • tid grc721.TokenID: Token ID to burn

TransferFrom

Transfers NFT ownership.

Parameters:

  • cur realm: Current realm context
  • from address: Current owner
  • to address: New owner
  • tid grc721.TokenID: Token ID to transfer

TokenURI

Returns the token URI, rendering stored SVG parameters as a base64 image data URI.

Parameters:

  • tid grc721.TokenID: Token ID

Returns: Token URI string, using a base64-encoded SVG data URI for generated GNFT images

Approve

Approves address to manage specific token.

Parameters:

  • cur realm: Current realm context
  • to address: Address to approve
  • tid grc721.TokenID: Token ID

SetApprovalForAll

Approves operator to manage all tokens.

Parameters:

  • cur realm: Current realm context
  • operator address: Operator address
  • approved bool: Approval status

SVG Generation

Parameter Format

Token URI stores compact parameters:

"x1,y1,x2,y2,#COLOR1,#COLOR2"
Example: "10,12,125,123,#FF5733,#33B5FF"

Parameter Ranges

  • x1: 7-13
  • y1: 7-13
  • x2: 121-126
  • y2: 121-126
  • colors: 6-digit hex (#RRGGBB)

SVG Structure

1<svg width="135" height="135">
2  <circle cx="67.5" cy="67.5" r="67.5" fill="url(#gradient)"/>
3  <!-- GnoSwap logo paths -->
4  <linearGradient id="gradient" x1="X1" y1="Y1" x2="X2" y2="Y2">
5    <stop stop-color="#COLOR1"/>
6    <stop offset="1" stop-color="#COLOR2"/>
7  </linearGradient>
8</svg>

Rendering Process

  1. Mint: Generate random parameters → Store as CSV string
  2. TokenURI: Parse CSV → Generate SVG → Encode base64 → Return data URI
  3. Display: Browser decodes data URI → Renders SVG

Usage

 1// Position contract mints NFT when creating position
 2import "gno.land/r/gnoswap/gnft"
 3
 4// Mint NFT for new position
 5tokenId := gnft.Mint(cross(cur), ownerAddress, positionId)
 6
 7// Get token URI with SVG
 8imageURI, err := gnft.TokenURI(tokenId)
 9// Returns: "data:image/svg+xml;base64,..." for generated GNFT images
10
11// Transfer NFT
12gnft.TransferFrom(cross(cur), fromAddress, toAddress, tokenId)
13
14// Burn NFT when closing position
15gnft.Burn(cross(cur), tokenId)

Security

  • Position contract mints NFTs for new positions
  • Transfers and approvals require the caller to be the owner or approved for the token
  • Tokens held by the staker contract can only be moved by the staker
  • Validated parameter ranges
  • Secure random generation

Architecture

Dependencies

  • gno.land/p/demo/tokens/grc721: GRC721 implementation
  • gno.land/r/gnoswap/rbac: Access control
  • gno.land/r/gnoswap/access: Position role mirror

State Variables

  • nft: GRC721 token instance
  • Token URIs are stored by the GRC721 token as compact SVG parameter strings

Access Control

  • owner.AssertOwnedByPrevious(): Checks that the caller owns the GNFT before owner-only operations
  • checkErr(): Panic on errors

Overview

Package gnft implements GRC721-compliant NFTs for GnoSwap LP positions.

Each LP position in GnoSwap is represented as a unique NFT with dynamically generated SVG artwork. The contract manages ownership, transfers, and visual representation of positions while minimizing storage costs.

Functions 15

func Approve

crossing Action
1func Approve(cur realm, approved address, tid grc721.TokenID) error
source

Approve grants permission to transfer a specific token ID to another address.

Parameters:

  • approved: address to approve
  • tid: token ID to approve for transfer

Returns error if approval fails.

func BalanceOf

Action
1func BalanceOf(owner address) (int64, error)
source

BalanceOf returns the number of NFTs owned by the specified address.

func Burn

crossing Action
1func Burn(cur realm, tid grc721.TokenID)
source

Burn removes a specific token ID.

Parameters:

  • tid: token ID to burn

Only callable by position.

func IsApprovedForAll

Action
1func IsApprovedForAll(owner, operator address) bool
source

IsApprovedForAll checks if operator can manage all owner's tokens.

Parameters:

  • owner: token owner address
  • operator: operator address to check

Returns true if operator is approved for all owner's tokens.

func Mint

crossing Action
1func Mint(cur realm, to address, tid grc721.TokenID) grc721.TokenID
source

Mint creates new NFT and transfers to address.

Parameters:

  • to: recipient address
  • tid: token ID

Returns minted token ID. Only callable by position contract.

func Render

1func Render(path string) string
source

Render returns the HTML representation of the NFT.

func SafeTransferFrom

crossing Action
1func SafeTransferFrom(cur realm, from, to address, tid grc721.TokenID) error
source

SafeTransferFrom transfers token ownership with receiver validation.

Parameters:

  • from: current owner address
  • to: recipient address
  • tid: token ID to transfer

Returns error if transfer fails.

Permission model:

  • Tokens held by the staker contract (i.e. currently staked) can only be moved by the staker itself; the underlying staked LP position is non-transferable.
  • Otherwise, ownership and approval are enforced by the GRC721 layer (owner / approved-for-token / approved-for-all).

func SetApprovalForAll

crossing Action
1func SetApprovalForAll(cur realm, operator address, approved bool) error
source

SetApprovalForAll enables/disables operator approval for all tokens.

Parameters:

  • operator: address to set approval for
  • approved: true to approve, false to revoke

Returns error if operation fails.

func SetTokenURI

crossing Action
1func SetTokenURI(cur realm, tid grc721.TokenID, tURI grc721.TokenURI) (bool, error)
source

SetTokenURI sets the metadata URI for the specified token.

Parameters:

  • tid: token ID
  • tURI: token URI

Only callable by position contract.

func TokenURI

Action
1func TokenURI(tid grc721.TokenID) (string, error)
source

TokenURI returns the metadata URI for the specified token ID. If stored value is in parameter format (x1,y1,x2,y2,color1,color2), it converts to full base64-encoded SVG image URI on read.

func TransferFrom

crossing Action
1func TransferFrom(cur realm, from, to address, tid grc721.TokenID) error
source

TransferFrom transfers a token from one address to another.

Parameters:

  • from: current owner address
  • to: recipient address
  • tid: token ID

Returns error if transfer fails.

Permission model:

  • Tokens held by the staker contract (i.e. currently staked) can only be moved by the staker itself; the underlying staked LP position is non-transferable.
  • Otherwise, ownership and approval are enforced by the GRC721 layer (owner / approved-for-token / approved-for-all).

Types 1

type ImageParams

struct
1type ImageParams struct {
2	x1, y1, x2, y2 int
3	color1, color2 string
4}
source

ImageParams holds parsed and validated image parameters.

Imports 12

Source Files 8