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

ratelimitdemo/v0 package

Overview

Package ratelimitdemo is a small on-chain demo of the token-bucket rate limiter provided by the p/moul/x/daily/ratelimit(/p/moul/x/daily/ratelimit/v0) library.

It holds a single package-level ratelimit.Limiter and wires it to the chain: the tick is runtime.ChainHeight() and each caller is keyed by its address (unsafe.PreviousRealm().Address()). It contains no limiter logic of its own — refill/consume math all lives in the library.

Functions

Allow

func Allow(cur realm) bool

Allow consumes one token for the calling realm/user at the current block height and reports whether the request is permitted. Returns false (and consumes nothing) when the caller's bucket is empty.

Command

# WARNING: This command is running in an INSECURE mode.
# It is strongly recommended to use a hardware device for signing
# and avoid trusting any computer connected to the internet,
# as your private keys could be exposed.

gnokey maketx call -pkgpath "gno.land/r/moul/x/daily/ratelimitdemo/v0" -func "Allow" -gas-fee 1000000ugnot -gas-wanted 1_000_000_000 -send "" -chainid "gnoland-1" -remote "https://rpc.gno.land" ADDRESSgnokey query -remote "https://rpc.gno.land" auth/accounts/ADDRESS
gnokey maketx call -pkgpath "gno.land/r/moul/x/daily/ratelimitdemo/v0" -func "Allow" -gas-fee 1000000ugnot -gas-wanted 1_000_000_000 -send "" -broadcast=false ADDRESS > call.tx
gnokey sign -tx-path call.tx -chainid "gnoland-1" -account-number ACCOUNTNUMBER -account-sequence SEQUENCENUMBER ADDRESS
gnokey broadcast -remote "https://rpc.gno.land" call.tx
  

Render

func Render(path string) string

Render shows the limiter config and a table of known callers with their current tokens and last-seen height. Path "/addr/<address>" focuses one caller.

Param

Command

gnokey query vm/qeval -remote "https://rpc.gno.land" -data "gno.land/r/moul/x/daily/ratelimitdemo/v0.Render()"

Result

SetConfig

func SetConfig(cur realm, rate, burst float64)

SetConfig updates the shared rate (tokens per block) and burst (capacity). rate is clamped to >= 0, burst to >= 1.

Params

Command

# WARNING: This command is running in an INSECURE mode.
# It is strongly recommended to use a hardware device for signing
# and avoid trusting any computer connected to the internet,
# as your private keys could be exposed.

gnokey maketx call -pkgpath "gno.land/r/moul/x/daily/ratelimitdemo/v0" -func "SetConfig" -args $'' -args $'' -gas-fee 1000000ugnot -gas-wanted 1_000_000_000 -send "" -chainid "gnoland-1" -remote "https://rpc.gno.land" ADDRESSgnokey query -remote "https://rpc.gno.land" auth/accounts/ADDRESS
gnokey maketx call -pkgpath "gno.land/r/moul/x/daily/ratelimitdemo/v0" -func "SetConfig" -args $'' -args $'' -gas-fee 1000000ugnot -gas-wanted 1_000_000_000 -send "" -broadcast=false ADDRESS > call.tx
gnokey sign -tx-path call.tx -chainid "gnoland-1" -account-number ACCOUNTNUMBER -account-sequence SEQUENCENUMBER ADDRESS
gnokey broadcast -remote "https://rpc.gno.land" call.tx
  

Tokens

func Tokens(addr address) int

Tokens reports how many whole tokens `addr` has available at the current block height, without mutating any state.

Param

Command

gnokey query vm/qeval -remote "https://rpc.gno.land" -data "gno.land/r/moul/x/daily/ratelimitdemo/v0.Tokens()"

Result