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

rate_limit.gno

1.03 Kb · 30 lines
 1package ucs03_zkgm
 2
 3import (
 4	"time"
 5
 6	u256 "gno.land/p/onbloc/math/uint256"
 7)
 8
 9// errTokenBucketAbsent rejects a rate-limited denom that has no configured bucket,
10// matching union's ContractError::TokenBucketIsAbsent.
11
12// rateLimit charges `amount` from denom's token bucket when rate limiting is
13// enabled. The bucket is anchored to block time, so its RefillRate is tokens per
14// second. When rate limiting is enabled, a denom with no configured bucket is
15// rejected (union TokenBucketIsAbsent) rather than passing unlimited.
16// reference: https://github.com/unionlabs/union/blob/d91c5e94354e15801bd5f82dc658eae3b79f2dad/cosmwasm/app/ucs03-zkgm/src/contract.rs#L3218-L3237
17func (v *ucs03ZkgmV1) rateLimit(_ int, rlm realm, denom string, amount *u256.Uint) error {
18	if v.store.RateLimitDisabled() || amount == nil {
19		return nil
20	}
21
22	bucket, ok := v.store.GetTokenBucket(denom)
23	if !ok || bucket == nil {
24		return makeError(errTokenBucketAbsent, denom)
25	}
26
27	now := time.Now().Unix()
28
29	return bucket.RateLimit(amount, u256.NewUint(uint64(now)))
30}