getters.gno
3.26 Kb · 87 lines
1package ucs03_zkgm
2
3import (
4 types "gno.land/p/onbloc/ibc/union/types"
5 tb "gno.land/p/onbloc/ibc/union/zkgm/tokenbucket"
6 u256 "gno.land/p/onbloc/math/uint256"
7 zkgm "gno.land/r/onbloc/ibc/union/apps/ucs03_zkgm"
8)
9
10// getters.gno implements the read-only IAppGetter surface. Results read the
11// injected proxy store and return snapshots so callers do not receive mutable
12// proxy-owned pointers.
13
14// GetTokenBucket returns the rate-limit bucket configured for denom.
15func (v *ucs03ZkgmV1) GetTokenBucket(denom string) (*tb.TokenBucket, error) {
16 bucket, ok := v.store.GetTokenBucket(denom)
17 if !ok || bucket == nil {
18 return nil, makeError(zkgm.ErrTokenBucketNotFound, denom)
19 }
20
21 return cloneTokenBucket(bucket), nil
22}
23
24// GetChannelBalanceV2 returns the escrow balance for the v2 channel-balance key.
25func (v *ucs03ZkgmV1) GetChannelBalanceV2(channelId types.ChannelId, path *u256.Uint, baseToken string, quoteToken []byte) (*u256.Uint, error) {
26 balance, ok := v.store.GetChannelBalanceV2(ChannelBalanceKeyV2(channelId, path, []byte(baseToken), quoteToken))
27 if !ok || balance == nil {
28 return nil, makeError(zkgm.ErrChannelBalanceV2NotFound, channelId.String())
29 }
30
31 return balance.Clone(), nil
32}
33
34// GetLockedTokenAmount returns the terminally locked base-token amount for denom.
35func (v *ucs03ZkgmV1) GetLockedTokenAmount(denom string) (*u256.Uint, error) {
36 total, ok := v.store.GetLockedTokenAmount(denom)
37 if !ok || total == nil {
38 return u256.Zero(), nil
39 }
40
41 return total.Clone(), nil
42}
43
44// GetVoucherList returns a page of up to count wrapped voucher tokens starting
45// at offset, ordered by denom. An empty page (e.g. offset past the end, or no
46// vouchers registered yet) is not an error condition, so this returns an empty
47// slice rather than failing; the error return exists only to match the
48// calling convention shared by every other IAppGetter method and is always nil.
49func (v *ucs03ZkgmV1) GetVoucherList(offset, count int) ([]zkgm.VoucherInfo, error) {
50 return v.store.GetVoucherList(offset, count), nil
51}
52
53// GetVoucherSize returns the total number of registered vouchers. Like
54// GetVoucherList, the error return exists only to match the calling
55// convention shared by every other IAppGetter method and is always nil.
56func (v *ucs03ZkgmV1) GetVoucherSize() (int, error) {
57 return v.store.GetVoucherSize(), nil
58}
59
60// GetVoucherInfo returns a snapshot of the single wrapped voucher registered
61// for ibcDenom. Unlike GetVoucherList, the caller already holds the key, so
62// this reads Store.GetVoucher directly and converts via the voucher's public
63// (capability-free) accessors instead of adding a Store-level method.
64func (v *ucs03ZkgmV1) GetVoucherInfo(ibcDenom string) (zkgm.VoucherInfo, error) {
65 vou, ok := v.store.GetVoucher(ibcDenom)
66 if !ok || vou == nil {
67 return zkgm.NewVoucherInfo("", "", "", 0, 0, 0), makeError(errVoucherNotFound, ibcDenom)
68 }
69
70 return zkgm.NewVoucherInfo(
71 ibcDenom,
72 vou.Token().GetName(),
73 vou.Token().GetSymbol(),
74 vou.OriginDecimals(),
75 uint8(vou.Token().GetDecimals()),
76 vou.Token().TotalSupply(),
77 ), nil
78}
79
80func cloneTokenBucket(bucket *tb.TokenBucket) *tb.TokenBucket {
81 return &tb.TokenBucket{
82 Capacity: bucket.Capacity.Clone(),
83 Available: bucket.Available.Clone(),
84 RefillRate: bucket.RefillRate.Clone(),
85 LastRefill: bucket.LastRefill.Clone(),
86 }
87}